File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ require (
4747 go.uber.org/atomic v1.11.0
4848 go.uber.org/goleak v1.3.0
4949 golang.org/x/crypto v0.45.0
50+ golang.org/x/net v0.47.0
5051 golang.org/x/sync v0.18.0
5152 golang.org/x/sys v0.38.0
5253 golang.org/x/term v0.37.0
@@ -169,7 +170,6 @@ require (
169170 go.uber.org/zap v1.27.0 // indirect
170171 golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
171172 golang.org/x/mod v0.29.0 // indirect
172- golang.org/x/net v0.47.0 // indirect
173173 golang.org/x/text v0.31.0 // indirect
174174 golang.org/x/tools v0.38.0 // indirect
175175 google.golang.org/protobuf v1.36.1 // indirect
Original file line number Diff line number Diff 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
@@ -1486,15 +1487,26 @@ func validatePublicAddress(addr string) error {
14861487 if host == "localhost" {
14871488 return errors .New ("localhost is not a valid address" )
14881489 }
1490+
14891491 ip := net .ParseIP (host )
1490- if ip == nil {
1491- return errors .New ("not a valid IP address" )
1492- }
1493- if ip .IsLoopback () {
1494- return errors .New ("loopback address is not a valid address" )
1492+ if ip != nil {
1493+ if ip .IsLoopback () {
1494+ return errors .New ("loopback address is not a valid address" )
1495+ }
1496+ if ip .IsPrivate () {
1497+ return errors .New ("private address is not a valid address" )
1498+ }
1499+ return nil
14951500 }
1496- if ip .IsPrivate () {
1497- return errors .New ("private address is not a valid address" )
1501+
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 )
14981510 }
14991511
15001512 return nil
Original file line number Diff line number Diff line change @@ -69,18 +69,28 @@ func TestValidatePublicAddress(t *testing.T) {
6969 expErr : false ,
7070 },
7171 {
72- name : "invalid IP" ,
73- addr : "not-an-ip:8080" ,
74- expErr : true ,
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" ,
79+ expErr : false ,
7580 },
7681 {
7782 name : "private IP" ,
7883 addr : "192.168.1.1:8080" ,
7984 expErr : true ,
8085 },
8186 {
82- name : "hostname" ,
83- addr : "example.com:8080" ,
87+ name : "invalid hostname format" ,
88+ addr : "invalid..hostname:8080" ,
89+ expErr : true ,
90+ },
91+ {
92+ name : "hostname starts with hyphen" ,
93+ addr : "-test.com:8080" ,
8494 expErr : true ,
8595 },
8696 }
You can’t perform that action at this time.
0 commit comments