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>
This commit is contained in:
Mohit Agarwal 2016-04-22 00:47:04 +05:30 committed by Brad Fitzpatrick
parent ab52ad894f
commit 9f1ccd647f
2 changed files with 9 additions and 3 deletions

View File

@ -573,8 +573,12 @@ func parseHost(host string) (string, error) {
}
return host1 + host2 + host3, nil
}
} else if i := strings.LastIndex(host, ":"); i > 0 {
colonPort := host[i:]
if !validOptionalPort(colonPort) {
return "", fmt.Errorf("invalid port %q after host", colonPort)
}
}
var err error
if host, err = unescape(host, encodeHost); err != nil {
return "", err

View File

@ -418,10 +418,10 @@ var urltests = []URLTest{
},
// worst case host, still round trips
{
"scheme://!$&'()*+,;=hello!:port/path",
"scheme://!$&'()*+,;=hello!:8080/path",
&URL{
Scheme: "scheme",
Host: "!$&'()*+,;=hello!:port",
Host: "!$&'()*+,;=hello!:8080",
Path: "/path",
},
"",
@ -636,8 +636,10 @@ var parseRequestURLTests = []struct {
{"*", true},
{"http://192.168.0.1/", true},
{"http://192.168.0.1:8080/", true},
{"http://192.168.0.1:foo/", false},
{"http://[fe80::1]/", true},
{"http://[fe80::1]:8080/", true},
{"http://[fe80::1]:foo/", false},
// Tests exercising RFC 6874 compliance:
{"http://[fe80::1%25en0]/", true}, // with alphanum zone identifier