Skip to content

Commit 854023c

Browse files
ethanalee-workrickystewart
authored andcommitted
[release-branch.go1.24] 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 Fixes golang#75712 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-internal-review.googlesource.com/c/go/+/2968 Reviewed-by: Nicholas Husin <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/709838 TryBot-Bypass: Michael Pratt <[email protected]> Reviewed-by: Carlos Amedee <[email protected]> Auto-Submit: Michael Pratt <[email protected]>
1 parent 70682f3 commit 854023c

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed

src/net/url/url.go

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package url
1313
import (
1414
"errors"
1515
"fmt"
16+
"net/netip"
1617
"path"
1718
"slices"
1819
"strconv"
@@ -622,40 +623,61 @@ func parseAuthority(authority string) (user *Userinfo, host string, err error) {
622623
// parseHost parses host as an authority without user
623624
// information. That is, as host[:port].
624625
func parseHost(host string) (string, error) {
625-
if strings.HasPrefix(host, "[") {
626+
if openBracketIdx := strings.LastIndex(host, "["); openBracketIdx != -1 {
626627
// Parse an IP-Literal in RFC 3986 and RFC 6874.
627628
// E.g., "[fe80::1]", "[fe80::1%25en0]", "[fe80::1]:80".
628-
i := strings.LastIndex(host, "]")
629-
if i < 0 {
629+
closeBracketIdx := strings.LastIndex(host, "]")
630+
if closeBracketIdx < 0 {
630631
return "", errors.New("missing ']' in host")
631632
}
632-
colonPort := host[i+1:]
633+
634+
colonPort := host[closeBracketIdx+1:]
633635
if !validOptionalPort(colonPort) {
634636
return "", fmt.Errorf("invalid port %q after host", colonPort)
635637
}
638+
unescapedColonPort, err := unescape(colonPort, encodeHost)
639+
if err != nil {
640+
return "", err
641+
}
636642

643+
hostname := host[openBracketIdx+1 : closeBracketIdx]
644+
var unescapedHostname string
637645
// RFC 6874 defines that %25 (%-encoded percent) introduces
638646
// the zone identifier, and the zone identifier can use basically
639647
// any %-encoding it likes. That's different from the host, which
640648
// can only %-encode non-ASCII bytes.
641649
// We do impose some restrictions on the zone, to avoid stupidity
642650
// like newlines.
643-
zone := strings.Index(host[:i], "%25")
644-
if zone >= 0 {
645-
host1, err := unescape(host[:zone], encodeHost)
651+
zoneIdx := strings.Index(hostname, "%25")
652+
if zoneIdx >= 0 {
653+
hostPart, err := unescape(hostname[:zoneIdx], encodeHost)
646654
if err != nil {
647655
return "", err
648656
}
649-
host2, err := unescape(host[zone:i], encodeZone)
657+
zonePart, err := unescape(hostname[zoneIdx:], encodeZone)
650658
if err != nil {
651659
return "", err
652660
}
653-
host3, err := unescape(host[i:], encodeHost)
661+
unescapedHostname = hostPart + zonePart
662+
} else {
663+
var err error
664+
unescapedHostname, err = unescape(hostname, encodeHost)
654665
if err != nil {
655666
return "", err
656667
}
657-
return host1 + host2 + host3, nil
658668
}
669+
670+
// Per RFC 3986, only a host identified by a valid
671+
// IPv6 address can be enclosed by square brackets.
672+
// This excludes any IPv4 or IPv4-mapped addresses.
673+
addr, err := netip.ParseAddr(unescapedHostname)
674+
if err != nil {
675+
return "", fmt.Errorf("invalid host: %w", err)
676+
}
677+
if addr.Is4() || addr.Is4In6() {
678+
return "", errors.New("invalid IPv6 host")
679+
}
680+
return "[" + unescapedHostname + "]" + unescapedColonPort, nil
659681
} else if i := strings.LastIndex(host, ":"); i != -1 {
660682
colonPort := host[i:]
661683
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)