Skip to content

Commit f6f4e8b

Browse files
ethanalee-workgopherbot
authored andcommitted
net/url: enforce stricter parsing of bracketed IPv6 hostnames
- Previously, url.Parse did not enforce validation of hostnames within square brackets. - RFC 3986 stipulates that only IPv6 hostnames can be embedded within square brackets in a URL. - Now, the parsing logic should strictly enforce that only IPv6 hostnames can be resolved when in square brackets. IPv4, IPv4-mapped addresses and other input will be rejected. - Update url_test to add test cases that cover the above scenarios. Thanks to Enze Wang, Jingcheng Yang and Zehui Miao of Tsinghua University for reporting this issue. Fixes CVE-2025-47912 Fixes golang#75678 Change-Id: Iaa41432bf0ee86de95a39a03adae5729e4deb46c Reviewed-on: https://go-internal-review.googlesource.com/c/go/+/2680 Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/709857 TryBot-Bypass: Michael Pratt <[email protected]> Reviewed-by: Carlos Amedee <[email protected]> Auto-Submit: Michael Pratt <[email protected]>
1 parent 7dd54e1 commit f6f4e8b

File tree

3 files changed

+77
-14
lines changed

3 files changed

+77
-14
lines changed

src/go/build/deps_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ var depsRules = `
237237
internal/types/errors,
238238
mime/quotedprintable,
239239
net/internal/socktest,
240-
net/url,
241240
runtime/trace,
242241
text/scanner,
243242
text/tabwriter;
@@ -300,6 +299,12 @@ var depsRules = `
300299
FMT
301300
< text/template/parse;
302301
302+
internal/bytealg, internal/itoa, math/bits, slices, strconv, unique
303+
< net/netip;
304+
305+
FMT, net/netip
306+
< net/url;
307+
303308
net/url, text/template/parse
304309
< text/template
305310
< internal/lazytemplate;
@@ -414,9 +419,6 @@ var depsRules = `
414419
< golang.org/x/net/dns/dnsmessage,
415420
golang.org/x/net/lif;
416421
417-
internal/bytealg, internal/itoa, math/bits, slices, strconv, unique
418-
< net/netip;
419-
420422
os, net/netip
421423
< internal/routebsd;
422424

src/net/url/url.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"errors"
1717
"fmt"
1818
"maps"
19+
"net/netip"
1920
"path"
2021
"slices"
2122
"strconv"
@@ -642,40 +643,61 @@ func parseAuthority(authority string) (user *Userinfo, host string, err error) {
642643
// parseHost parses host as an authority without user
643644
// information. That is, as host[:port].
644645
func parseHost(host string) (string, error) {
645-
if strings.HasPrefix(host, "[") {
646+
if openBracketIdx := strings.LastIndex(host, "["); openBracketIdx != -1 {
646647
// Parse an IP-Literal in RFC 3986 and RFC 6874.
647648
// E.g., "[fe80::1]", "[fe80::1%25en0]", "[fe80::1]:80".
648-
i := strings.LastIndex(host, "]")
649-
if i < 0 {
649+
closeBracketIdx := strings.LastIndex(host, "]")
650+
if closeBracketIdx < 0 {
650651
return "", errors.New("missing ']' in host")
651652
}
652-
colonPort := host[i+1:]
653+
654+
colonPort := host[closeBracketIdx+1:]
653655
if !validOptionalPort(colonPort) {
654656
return "", fmt.Errorf("invalid port %q after host", colonPort)
655657
}
658+
unescapedColonPort, err := unescape(colonPort, encodeHost)
659+
if err != nil {
660+
return "", err
661+
}
656662

663+
hostname := host[openBracketIdx+1 : closeBracketIdx]
664+
var unescapedHostname string
657665
// RFC 6874 defines that %25 (%-encoded percent) introduces
658666
// the zone identifier, and the zone identifier can use basically
659667
// any %-encoding it likes. That's different from the host, which
660668
// can only %-encode non-ASCII bytes.
661669
// We do impose some restrictions on the zone, to avoid stupidity
662670
// like newlines.
663-
zone := strings.Index(host[:i], "%25")
664-
if zone >= 0 {
665-
host1, err := unescape(host[:zone], encodeHost)
671+
zoneIdx := strings.Index(hostname, "%25")
672+
if zoneIdx >= 0 {
673+
hostPart, err := unescape(hostname[:zoneIdx], encodeHost)
666674
if err != nil {
667675
return "", err
668676
}
669-
host2, err := unescape(host[zone:i], encodeZone)
677+
zonePart, err := unescape(hostname[zoneIdx:], encodeZone)
670678
if err != nil {
671679
return "", err
672680
}
673-
host3, err := unescape(host[i:], encodeHost)
681+
unescapedHostname = hostPart + zonePart
682+
} else {
683+
var err error
684+
unescapedHostname, err = unescape(hostname, encodeHost)
674685
if err != nil {
675686
return "", err
676687
}
677-
return host1 + host2 + host3, nil
678688
}
689+
690+
// Per RFC 3986, only a host identified by a valid
691+
// IPv6 address can be enclosed by square brackets.
692+
// This excludes any IPv4 or IPv4-mapped addresses.
693+
addr, err := netip.ParseAddr(unescapedHostname)
694+
if err != nil {
695+
return "", fmt.Errorf("invalid host: %w", err)
696+
}
697+
if addr.Is4() || addr.Is4In6() {
698+
return "", errors.New("invalid IPv6 host")
699+
}
700+
return "[" + unescapedHostname + "]" + unescapedColonPort, nil
679701
} else if i := strings.LastIndex(host, ":"); i != -1 {
680702
colonPort := host[i:]
681703
if !validOptionalPort(colonPort) {

src/net/url/url_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,16 @@ var urltests = []URLTest{
383383
},
384384
"",
385385
},
386+
// valid IPv6 host with port and path
387+
{
388+
"https://[2001:db8::1]:8443/test/path",
389+
&URL{
390+
Scheme: "https",
391+
Host: "[2001:db8::1]:8443",
392+
Path: "/test/path",
393+
},
394+
"",
395+
},
386396
// host subcomponent; IPv6 address with zone identifier in RFC 6874
387397
{
388398
"http://[fe80::1%25en0]/", // alphanum zone identifier
@@ -707,6 +717,24 @@ var parseRequestURLTests = []struct {
707717
// RFC 6874.
708718
{"http://[fe80::1%en0]/", false},
709719
{"http://[fe80::1%en0]:8080/", false},
720+
721+
// Tests exercising RFC 3986 compliance
722+
{"https://[1:2:3:4:5:6:7:8]", true}, // full IPv6 address
723+
{"https://[2001:db8::a:b:c:d]", true}, // compressed IPv6 address
724+
{"https://[fe80::1%25eth0]", true}, // link-local address with zone ID (interface name)
725+
{"https://[fe80::abc:def%254]", true}, // link-local address with zone ID (interface index)
726+
{"https://[2001:db8::1]/path", true}, // compressed IPv6 address with path
727+
{"https://[fe80::1%25eth0]/path?query=1", true}, // link-local with zone, path, and query
728+
729+
{"https://[::ffff:192.0.2.1]", false},
730+
{"https://[:1] ", false},
731+
{"https://[1:2:3:4:5:6:7:8:9]", false},
732+
{"https://[1::1::1]", false},
733+
{"https://[1:2:3:]", false},
734+
{"https://[ffff::127.0.0.4000]", false},
735+
{"https://[0:0::test.com]:80", false},
736+
{"https://[2001:db8::test.com]", false},
737+
{"https://[test.com]", false},
710738
}
711739

712740
func TestParseRequestURI(t *testing.T) {
@@ -1643,6 +1671,17 @@ func TestParseErrors(t *testing.T) {
16431671
{"cache_object:foo", true},
16441672
{"cache_object:foo/bar", true},
16451673
{"cache_object/:foo/bar", false},
1674+
1675+
{"http://[192.168.0.1]/", true}, // IPv4 in brackets
1676+
{"http://[192.168.0.1]:8080/", true}, // IPv4 in brackets with port
1677+
{"http://[::ffff:192.168.0.1]/", true}, // IPv4-mapped IPv6 in brackets
1678+
{"http://[::ffff:192.168.0.1]:8080/", true}, // IPv4-mapped IPv6 in brackets with port
1679+
{"http://[::ffff:c0a8:1]/", true}, // IPv4-mapped IPv6 in brackets (hex)
1680+
{"http://[not-an-ip]/", true}, // invalid IP string in brackets
1681+
{"http://[fe80::1%foo]/", true}, // invalid zone format in brackets
1682+
{"http://[fe80::1", true}, // missing closing bracket
1683+
{"http://fe80::1]/", true}, // missing opening bracket
1684+
{"http://[test.com]/", true}, // domain name in brackets
16461685
}
16471686
for _, tt := range tests {
16481687
u, err := Parse(tt.in)

0 commit comments

Comments
 (0)