Skip to content

Commit 9f1ccd6

Browse files
0xmohitbradfitz
authored andcommitted
net/url: validate ports in IPv4 addresses
Fixes #14860 Change-Id: Id55ad942d45a104d560a879d6e8e1aa09671789b Reviewed-on: https://go-review.googlesource.com/22351 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
1 parent ab52ad8 commit 9f1ccd6

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/net/url/url.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,12 @@ func parseHost(host string) (string, error) {
573573
}
574574
return host1 + host2 + host3, nil
575575
}
576+
} else if i := strings.LastIndex(host, ":"); i > 0 {
577+
colonPort := host[i:]
578+
if !validOptionalPort(colonPort) {
579+
return "", fmt.Errorf("invalid port %q after host", colonPort)
580+
}
576581
}
577-
578582
var err error
579583
if host, err = unescape(host, encodeHost); err != nil {
580584
return "", err

src/net/url/url_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,10 @@ var urltests = []URLTest{
418418
},
419419
// worst case host, still round trips
420420
{
421-
"scheme://!$&'()*+,;=hello!:port/path",
421+
"scheme://!$&'()*+,;=hello!:8080/path",
422422
&URL{
423423
Scheme: "scheme",
424-
Host: "!$&'()*+,;=hello!:port",
424+
Host: "!$&'()*+,;=hello!:8080",
425425
Path: "/path",
426426
},
427427
"",
@@ -636,8 +636,10 @@ var parseRequestURLTests = []struct {
636636
{"*", true},
637637
{"http://192.168.0.1/", true},
638638
{"http://192.168.0.1:8080/", true},
639+
{"http://192.168.0.1:foo/", false},
639640
{"http://[fe80::1]/", true},
640641
{"http://[fe80::1]:8080/", true},
642+
{"http://[fe80::1]:foo/", false},
641643

642644
// Tests exercising RFC 6874 compliance:
643645
{"http://[fe80::1%25en0]/", true}, // with alphanum zone identifier

0 commit comments

Comments
 (0)