Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/url: return an error if port is out of range #69444

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/net/url/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,20 +789,18 @@ func (u *URL) EscapedFragment() string {
}

// validOptionalPort reports whether port is either an empty string
// or matches /^:\d*$/
// or matches /^:\d*$/ and fits in an unsigned 16-bit integer
func validOptionalPort(port string) bool {
if port == "" {
if port == "" || port == ":" {
return true
}
if port[0] != ':' {
return false
}
for _, b := range port[1:] {
if b < '0' || b > '9' {
return false
}
}
return true

p, err := strconv.Atoi(port[1:])

return err == nil && p < 65536
}

// String reassembles the [URL] into a valid URL string.
Expand Down
3 changes: 3 additions & 0 deletions src/net/url/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,9 @@ var parseRequestURLTests = []struct {
// RFC 6874.
{"http://[fe80::1%en0]/", false},
{"http://[fe80::1%en0]:8080/", false},

// Tests exercising WHATWG URL Living Standard compliance:
{"https://example.org:70000", false}, // port out of range
}

func TestParseRequestURI(t *testing.T) {
Expand Down