Skip to content

Commit 606f56a

Browse files
fix(node): enhance public address validation for hostnames with stricter rules
1 parent 11207ea commit 606f56a

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

pkg/node/node.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import (
8181
ma "github.com/multiformats/go-multiaddr"
8282
"github.com/prometheus/client_golang/prometheus"
8383
"golang.org/x/crypto/sha3"
84+
"golang.org/x/net/idna"
8485
"golang.org/x/sync/errgroup"
8586
)
8687

@@ -1498,5 +1499,15 @@ func validatePublicAddress(addr string) error {
14981499
return nil
14991500
}
15001501

1502+
idnaValidator := idna.New(
1503+
idna.ValidateLabels(true),
1504+
idna.VerifyDNSLength(true),
1505+
idna.StrictDomainName(true),
1506+
idna.CheckHyphens(true),
1507+
)
1508+
if _, err := idnaValidator.ToASCII(host); err != nil {
1509+
return fmt.Errorf("invalid hostname: %w", err)
1510+
}
1511+
15011512
return nil
15021513
}

pkg/node/node_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,13 @@ func TestValidatePublicAddress(t *testing.T) {
6969
expErr: false,
7070
},
7171
{
72-
name: "hostname format valid",
73-
addr: "not-an-ip:8080",
72+
name: "valid hostname",
73+
addr: "example.com:8080",
74+
expErr: false,
75+
},
76+
{
77+
name: "valid hostname with hyphen",
78+
addr: "test-example.com:8080",
7479
expErr: false,
7580
},
7681
{
@@ -79,13 +84,13 @@ func TestValidatePublicAddress(t *testing.T) {
7984
expErr: true,
8085
},
8186
{
82-
name: "hostname",
83-
addr: "example.com:8080",
84-
expErr: false,
87+
name: "invalid hostname format",
88+
addr: "invalid..hostname:8080",
89+
expErr: true,
8590
},
8691
{
87-
name: "hostname without port",
88-
addr: "example.com",
92+
name: "hostname starts with hyphen",
93+
addr: "-test.com:8080",
8994
expErr: true,
9095
},
9196
}

0 commit comments

Comments
 (0)