Skip to content

Commit 3dd6592

Browse files
Reject bad ipv6 hostnames
1 parent f18eb9e commit 3dd6592

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

uri.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -445,13 +445,20 @@ func parseHost(host []byte) ([]byte, error) {
445445
}
446446
return append(host1, append(host2, host3...)...), nil
447447
}
448-
} else if i := bytes.LastIndexByte(host, ':'); i != -1 {
449-
if bytes.IndexByte(host[:i], ':') != -1 {
450-
return nil, fmt.Errorf("invalid host %q with multiple port delimiters", host)
448+
} else {
449+
if bytes.IndexByte(host, '[') != -1 || bytes.IndexByte(host, ']') != -1 {
450+
return nil, fmt.Errorf("invalid host %q", host)
451451
}
452-
colonPort := host[i:]
453-
if !validOptionalPort(colonPort) {
454-
return nil, fmt.Errorf("invalid port %q after host", colonPort)
452+
453+
if i := bytes.LastIndexByte(host, ':'); i != -1 {
454+
if bytes.IndexByte(host[:i], ':') != -1 {
455+
return nil, fmt.Errorf("invalid host %q with multiple port delimiters", host)
456+
}
457+
458+
colonPort := host[i:]
459+
if !validOptionalPort(colonPort) {
460+
return nil, fmt.Errorf("invalid port %q after host", colonPort)
461+
}
455462
}
456463
}
457464

uri_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,22 @@ func TestURIUpdate(t *testing.T) {
203203
testURIUpdate(t, "http://example.net/", "//example.com:8080/", "http://example.com:8080/")
204204
}
205205

206+
func TestURIRejectsMixedBracketHost(t *testing.T) {
207+
t.Parallel()
208+
209+
tests := []string{
210+
"http://127.0.0.1[192.168.0.1]/",
211+
"http://example.com[fd00::1]/",
212+
}
213+
214+
for _, raw := range tests {
215+
var u URI
216+
if err := u.Parse(nil, []byte(raw)); err == nil {
217+
t.Fatalf("expected error for %q", raw)
218+
}
219+
}
220+
}
221+
206222
func testURIUpdate(t *testing.T, base, update, result string) {
207223
var u URI
208224
u.Parse(nil, []byte(base)) //nolint:errcheck

0 commit comments

Comments
 (0)