Skip to content

Commit 9eeed53

Browse files
Reject invalid hosts with multiple port delimiters
1 parent d3fc682 commit 9eeed53

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

uri.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,9 @@ func parseHost(host []byte) ([]byte, error) {
446446
return append(host1, append(host2, host3...)...), nil
447447
}
448448
} 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)
451+
}
449452
colonPort := host[i:]
450453
if !validOptionalPort(colonPort) {
451454
return nil, fmt.Errorf("invalid port %q after host", colonPort)

uri_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,27 @@ func TestURIRejectInvalidScheme(t *testing.T) {
145145
}
146146
}
147147

148+
func TestURIRejectMultiplePorts(t *testing.T) {
149+
t.Parallel()
150+
151+
testcases := []string{
152+
"http://192.168.1.1:1111:2222/",
153+
"http://example.com:80:8080/",
154+
}
155+
156+
for _, raw := range testcases {
157+
var u URI
158+
if err := u.Parse(nil, []byte(raw)); err == nil {
159+
t.Fatalf("expected Parse to fail for %q", raw)
160+
}
161+
}
162+
163+
var valid URI
164+
if err := valid.Parse(nil, []byte("http://192.168.1.1:1111/")); err != nil {
165+
t.Fatalf("unexpected error for valid uri: %v", err)
166+
}
167+
}
168+
148169
func TestURIUpdate(t *testing.T) {
149170
t.Parallel()
150171

0 commit comments

Comments
 (0)