Skip to content

Commit

Permalink
fix: allowing hostnames in gateway/rpc addr field (#1378)
Browse files Browse the repository at this point in the history
Closes #1374
  • Loading branch information
Ryan authored Nov 21, 2022
1 parent f8bef40 commit 93b4474
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 25 deletions.
40 changes: 40 additions & 0 deletions libs/utils/address.go
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
}
49 changes: 49 additions & 0 deletions libs/utils/address_test.go
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)
})
}
}
20 changes: 3 additions & 17 deletions nodebuilder/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package core
import (
"fmt"
"strconv"
"strings"

"github.com/celestiaorg/celestia-node/libs/utils"
)

// Config combines all configuration fields for managing the relationship with a Core node.
Expand All @@ -25,7 +26,7 @@ func DefaultConfig() Config {

// Validate performs basic validation of the config.
func (cfg *Config) Validate() error {
ip, err := sanitizeIP(cfg.IP)
ip, err := utils.ValidateAddr(cfg.IP)
if err != nil {
return err
}
Expand All @@ -40,18 +41,3 @@ func (cfg *Config) Validate() error {
}
return nil
}

// sanitizeIP trims leading protocol scheme and port from the given
// IP address if present.
func sanitizeIP(ip string) (string, error) {
original := ip
ip = strings.TrimPrefix(ip, "http://")
ip = strings.TrimPrefix(ip, "https://")
ip = strings.TrimPrefix(ip, "tcp://")
ip = strings.TrimSuffix(ip, "/")
ip = strings.Split(ip, ":")[0]
if ip == "" {
return "", fmt.Errorf("nodebuilder/core: invalid IP addr given: %s", original)
}
return ip, nil
}
12 changes: 8 additions & 4 deletions nodebuilder/gateway/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package gateway

import (
"fmt"
"net"
"strconv"

"github.com/celestiaorg/celestia-node/libs/utils"
)

type Config struct {
Expand All @@ -22,10 +23,13 @@ func DefaultConfig() Config {
}

func (cfg *Config) Validate() error {
if ip := net.ParseIP(cfg.Address); ip == nil {
return fmt.Errorf("gateway: invalid listen address format: %s", cfg.Address)
sanitizedAddress, err := utils.ValidateAddr(cfg.Address)
if err != nil {
return fmt.Errorf("gateway: invalid address: %w", err)
}
_, err := strconv.Atoi(cfg.Port)
cfg.Address = sanitizedAddress

_, err = strconv.Atoi(cfg.Port)
if err != nil {
return fmt.Errorf("gateway: invalid port: %s", err.Error())
}
Expand Down
12 changes: 8 additions & 4 deletions nodebuilder/rpc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package rpc

import (
"fmt"
"net"
"strconv"

"github.com/celestiaorg/celestia-node/libs/utils"
)

type Config struct {
Expand All @@ -20,10 +21,13 @@ func DefaultConfig() Config {
}

func (cfg *Config) Validate() error {
if ip := net.ParseIP(cfg.Address); ip == nil {
return fmt.Errorf("service/rpc: invalid listen address format: %s", cfg.Address)
sanitizedAddress, err := utils.ValidateAddr(cfg.Address)
if err != nil {
return fmt.Errorf("service/rpc: invalid address: %w", err)
}
_, err := strconv.Atoi(cfg.Port)
cfg.Address = sanitizedAddress

_, err = strconv.Atoi(cfg.Port)
if err != nil {
return fmt.Errorf("service/rpc: invalid port: %s", err.Error())
}
Expand Down

0 comments on commit 93b4474

Please sign in to comment.