Skip to content

Commit e3be2d1

Browse files
seankhliaoneild
authored andcommitted
net/url: disallow raw IPv6 addresses in host
RFC 3986 requires square brackets around IPv6 addresses. Parse's acceptance of raw IPv6 addresses is non compliant, and complicates splitting out a port. Fixes golang#31024 Fixes golang#75223 Change-Id: I477dc420a7441cb33156627dbd5e46d88c677f1e Reviewed-on: https://go-review.googlesource.com/c/go/+/710176 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent aced4c7 commit e3be2d1

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

src/net/url/url.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,9 @@ func parseHost(host string) (string, error) {
698698
return "", errors.New("invalid IP-literal")
699699
}
700700
return "[" + unescapedHostname + "]" + unescapedColonPort, nil
701-
} else if i := strings.LastIndex(host, ":"); i != -1 {
701+
} else if i := strings.Index(host, ":"); i != -1 {
702+
// IPv4address / reg-name
703+
// E.g. 1.2.3.4, 1.2.3.4:80, example.com, example.com:80
702704
colonPort := host[i:]
703705
if !validOptionalPort(colonPort) {
704706
return "", fmt.Errorf("invalid port %q after host", colonPort)

src/net/url/url_test.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -506,26 +506,6 @@ var urltests = []URLTest{
506506
},
507507
"",
508508
},
509-
{
510-
// Malformed IPv6 but still accepted.
511-
"http://2b01:e34:ef40:7730:8e70:5aff:fefe:edac:8080/foo",
512-
&URL{
513-
Scheme: "http",
514-
Host: "2b01:e34:ef40:7730:8e70:5aff:fefe:edac:8080",
515-
Path: "/foo",
516-
},
517-
"",
518-
},
519-
{
520-
// Malformed IPv6 but still accepted.
521-
"http://2b01:e34:ef40:7730:8e70:5aff:fefe:edac:/foo",
522-
&URL{
523-
Scheme: "http",
524-
Host: "2b01:e34:ef40:7730:8e70:5aff:fefe:edac:",
525-
Path: "/foo",
526-
},
527-
"",
528-
},
529509
{
530510
"http://[2b01:e34:ef40:7730:8e70:5aff:fefe:edac]:8080/foo",
531511
&URL{
@@ -735,6 +715,9 @@ var parseRequestURLTests = []struct {
735715
{"https://[0:0::test.com]:80", false},
736716
{"https://[2001:db8::test.com]", false},
737717
{"https://[test.com]", false},
718+
{"https://1:2:3:4:5:6:7:8", false},
719+
{"https://1:2:3:4:5:6:7:8:80", false},
720+
{"https://example.com:80:", false},
738721
}
739722

740723
func TestParseRequestURI(t *testing.T) {

0 commit comments

Comments
 (0)