Skip to content
Merged
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
19 changes: 13 additions & 6 deletions uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,13 +445,20 @@ func parseHost(host []byte) ([]byte, error) {
}
return append(host1, append(host2, host3...)...), nil
}
} else if i := bytes.LastIndexByte(host, ':'); i != -1 {
if bytes.IndexByte(host[:i], ':') != -1 {
return nil, fmt.Errorf("invalid host %q with multiple port delimiters", host)
} else {
if bytes.ContainsAny(host, "[]") {
return nil, fmt.Errorf("invalid host %q", host)
}
colonPort := host[i:]
if !validOptionalPort(colonPort) {
return nil, fmt.Errorf("invalid port %q after host", colonPort)

if i := bytes.LastIndexByte(host, ':'); i != -1 {
if bytes.IndexByte(host[:i], ':') != -1 {
return nil, fmt.Errorf("invalid host %q with multiple port delimiters", host)
}

colonPort := host[i:]
if !validOptionalPort(colonPort) {
return nil, fmt.Errorf("invalid port %q after host", colonPort)
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,22 @@ func TestURIUpdate(t *testing.T) {
testURIUpdate(t, "http://example.net/", "//example.com:8080/", "http://example.com:8080/")
}

func TestURIRejectsMixedBracketHost(t *testing.T) {
t.Parallel()

tests := []string{
"http://127.0.0.1[192.168.0.1]/",
"http://example.com[fd00::1]/",
}

for _, raw := range tests {
var u URI
if err := u.Parse(nil, []byte(raw)); err == nil {
t.Fatalf("expected error for %q", raw)
}
}
}

func testURIUpdate(t *testing.T, base, update, result string) {
var u URI
u.Parse(nil, []byte(base)) //nolint:errcheck
Expand Down