Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ require (
go.uber.org/atomic v1.11.0
go.uber.org/goleak v1.3.0
golang.org/x/crypto v0.45.0
golang.org/x/net v0.47.0
golang.org/x/sync v0.18.0
golang.org/x/sys v0.38.0
golang.org/x/term v0.37.0
Expand Down Expand Up @@ -169,7 +170,6 @@ require (
go.uber.org/zap v1.27.0 // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
golang.org/x/mod v0.29.0 // indirect
golang.org/x/net v0.47.0 // indirect
golang.org/x/text v0.31.0 // indirect
golang.org/x/tools v0.38.0 // indirect
google.golang.org/protobuf v1.36.1 // indirect
Expand Down
26 changes: 19 additions & 7 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import (
ma "github.com/multiformats/go-multiaddr"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/crypto/sha3"
"golang.org/x/net/idna"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -1486,15 +1487,26 @@ func validatePublicAddress(addr string) error {
if host == "localhost" {
return errors.New("localhost is not a valid address")
}

ip := net.ParseIP(host)
if ip == nil {
return errors.New("not a valid IP address")
}
if ip.IsLoopback() {
return errors.New("loopback address is not a valid address")
if ip != nil {
if ip.IsLoopback() {
return errors.New("loopback address is not a valid address")
}
if ip.IsPrivate() {
return errors.New("private address is not a valid address")
}
return nil
}
if ip.IsPrivate() {
return errors.New("private address is not a valid address")

idnaValidator := idna.New(
idna.ValidateLabels(true),
idna.VerifyDNSLength(true),
idna.StrictDomainName(true),
idna.CheckHyphens(true),
)
if _, err := idnaValidator.ToASCII(host); err != nil {
return fmt.Errorf("invalid hostname: %w", err)
}

return nil
Expand Down
20 changes: 15 additions & 5 deletions pkg/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,28 @@ func TestValidatePublicAddress(t *testing.T) {
expErr: false,
},
{
name: "invalid IP",
addr: "not-an-ip:8080",
expErr: true,
name: "valid hostname",
addr: "example.com:8080",
expErr: false,
},
{
name: "valid hostname with hyphen",
addr: "test-example.com:8080",
expErr: false,
},
{
name: "private IP",
addr: "192.168.1.1:8080",
expErr: true,
},
{
name: "hostname",
addr: "example.com:8080",
name: "invalid hostname format",
addr: "invalid..hostname:8080",
expErr: true,
},
{
name: "hostname starts with hyphen",
addr: "-test.com:8080",
expErr: true,
},
Comment thread
akrem-chabchoub marked this conversation as resolved.
}
Expand Down
Loading