Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node: Fix url verification bug where <ip>:<port> is not supported #3719

Merged
merged 9 commits into from
Jan 18, 2024
2 changes: 1 addition & 1 deletion node/cmd/guardiand/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func init() {
moonbeamRPC = node.RegisterFlagWithValidationOrFail(NodeCmd, "moonbeamRPC", "Moonbeam RPC URL", "ws://eth-devnet:8545", []string{"ws", "wss"})
moonbeamContract = NodeCmd.Flags().String("moonbeamContract", "", "Moonbeam contract address")

neonRPC = node.RegisterFlagWithValidationOrFail(NodeCmd, "neonRPC", "Neon RPC URL", "ws://eth-devnet:8545", []string{"ws", "wss"})
neonRPC = node.RegisterFlagWithValidationOrFail(NodeCmd, "neonRPC", "Neon RPC URL", "http://eth-devnet:8545", []string{"http", "https"})
neonContract = NodeCmd.Flags().String("neonContract", "", "Neon contract address")

terraWS = node.RegisterFlagWithValidationOrFail(NodeCmd, "terraWS", "Path to terrad root for websocket connection", "ws://terra-terrad:26657/websocket", []string{"ws", "wss"})
Expand Down
11 changes: 6 additions & 5 deletions node/pkg/node/url_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ func hasKnownSchemePrefix(urlStr string) bool {
}

func validateURL(urlStr string, validSchemes []string) bool {
parsedURL, err := url.Parse(urlStr)
if err != nil {
return false
}

// If no scheme is required, validate host:port format
if len(validSchemes) == 1 && validSchemes[0] == "" {
host, port, err := net.SplitHostPort(urlStr)
return err == nil && host != "" && port != "" && !hasKnownSchemePrefix(urlStr)
}

// url.Parse() has to come later because it will fail if the scheme is empty
parsedURL, err := url.Parse(urlStr)
if err != nil {
return false
}

for _, scheme := range validSchemes {
if parsedURL.Scheme == scheme {
return true
Expand Down
1 change: 1 addition & 0 deletions node/pkg/node/url_verification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestValidateURL(t *testing.T) {
{[]string{""}, "example.com:8080", true},
{[]string{""}, "http://invalid-scheme:8080", false},
{[]string{""}, "ws://invalid-scheme:8080", false},
{[]string{""}, "170.0.0.1:8080", true},
}

for _, test := range tests {
Expand Down
Loading