-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allowing hostnames in gateway/rpc addr field (#1378)
Closes #1374
- Loading branch information
Ryan
authored
Nov 21, 2022
1 parent
f8bef40
commit 93b4474
Showing
5 changed files
with
108 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strings" | ||
) | ||
|
||
// SanitizeAddr trims leading protocol scheme and port from the given | ||
// IP address or hostname if present. | ||
func SanitizeAddr(addr string) (string, error) { | ||
original := addr | ||
addr = strings.TrimPrefix(addr, "http://") | ||
addr = strings.TrimPrefix(addr, "https://") | ||
addr = strings.TrimPrefix(addr, "tcp://") | ||
addr = strings.TrimSuffix(addr, "/") | ||
addr = strings.Split(addr, ":")[0] | ||
if addr == "" { | ||
return "", fmt.Errorf("invalid IP address or hostname given: %s", original) | ||
} | ||
return addr, nil | ||
} | ||
|
||
// ValidateAddr sanitizes the given address and verifies that it is a valid IP or hostname. The | ||
// sanitized address is returned. | ||
func ValidateAddr(addr string) (string, error) { | ||
addr, err := SanitizeAddr(addr) | ||
if err != nil { | ||
return addr, err | ||
} | ||
|
||
if ip := net.ParseIP(addr); ip == nil { | ||
_, err = net.LookupHost(addr) | ||
if err != nil { | ||
return addr, fmt.Errorf("could not resolve hostname or ip: %w", err) | ||
} | ||
} | ||
|
||
return addr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSanitizeAddr(t *testing.T) { | ||
var tests = []struct { | ||
addr string | ||
want string | ||
}{ | ||
// Testcase: trims protocol prefix | ||
{addr: "http://celestia.org", want: "celestia.org"}, | ||
// Testcase: trims protocol prefix, and trims port and trailing slash suffix | ||
{addr: "tcp://192.168.42.42:5050/", want: "192.168.42.42"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.addr, func(t *testing.T) { | ||
got, err := SanitizeAddr(tt.addr) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} | ||
|
||
func TestValidateAddr(t *testing.T) { | ||
var tests = []struct { | ||
addr string | ||
want string | ||
}{ | ||
// Testcase: ip is valid | ||
{addr: "192.168.42.42:5050", want: "192.168.42.42"}, | ||
// Testcase: hostname is valid | ||
{addr: "https://celestia.org", want: "celestia.org"}, | ||
// Testcase: resolves localhost | ||
{addr: "http://localhost:8080/", want: "localhost"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.addr, func(t *testing.T) { | ||
got, err := ValidateAddr(tt.addr) | ||
require.NoError(t, err) | ||
require.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters