File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -1770,11 +1770,22 @@ func (c *DefaultCtx) Subdomains(offset ...int) []string {
17701770 // Normalize host according to RFC 3986
17711771 // Trim any trailing dot from a fully qualified domain name
17721772 host := strings .TrimSuffix (c .Hostname (), "." )
1773- // Decode punycode labels to Unicode form
1774- if u , err := idna .Lookup .ToUnicode (host ); err == nil {
1775- host = utils .ToLower (u )
1776- } else {
1777- host = utils .ToLower (host )
1773+ host = utils .ToLower (host )
1774+
1775+ // Decode punycode labels only when necessary
1776+ if strings .Contains (host , "xn--" ) {
1777+ if u , err := idna .Lookup .ToUnicode (host ); err == nil {
1778+ host = utils .ToLower (u )
1779+ }
1780+ }
1781+
1782+ // Return nothing for IP addresses
1783+ ip := host
1784+ if strings .HasPrefix (ip , "[" ) && strings .HasSuffix (ip , "]" ) {
1785+ ip = strings .Trim (ip , "[]" )
1786+ }
1787+ if utils .IsIPv4 (ip ) || utils .IsIPv6 (ip ) {
1788+ return []string {}
17781789 }
17791790
17801791 parts := strings .Split (host , "." )
Original file line number Diff line number Diff line change @@ -3098,11 +3098,29 @@ func Test_Ctx_Subdomains(t *testing.T) {
30983098 want : []string {"bücher" },
30993099 },
31003100 {
3101- name : "invalid domain keeps original lowercased" ,
3101+ name : "punycode decode failure uses fallback" ,
3102+ host : "xn--bcher--.example.com" ,
3103+ offset : nil ,
3104+ want : []string {"xn--bcher--" },
3105+ },
3106+ {
3107+ name : "invalid host keeps original lowercased" ,
31023108 host : "Foo Bar" ,
31033109 offset : []int {0 },
31043110 want : []string {"foo bar" },
31053111 },
3112+ {
3113+ name : "IPv4 host returns empty" ,
3114+ host : "192.168.0.1" ,
3115+ offset : nil ,
3116+ want : []string {},
3117+ },
3118+ {
3119+ name : "IPv6 host returns empty" ,
3120+ host : "[2001:db8::1]" ,
3121+ offset : nil ,
3122+ want : []string {},
3123+ },
31063124 }
31073125
31083126 for _ , tc := range cases {
You can’t perform that action at this time.
0 commit comments