Skip to content

Commit 9a237c7

Browse files
Reject invalid hosts with multiple port delimiters
1 parent 0ad54a4 commit 9a237c7

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
@@ -398,6 +398,9 @@ func parseHost(host []byte) ([]byte, error) {
398398
return append(host1, append(host2, host3...)...), nil
399399
}
400400
} else if i := bytes.LastIndexByte(host, ':'); i != -1 {
401+
if bytes.IndexByte(host[:i], ':') != -1 {
402+
return nil, fmt.Errorf("invalid host %q with multiple port delimiters", host)
403+
}
401404
colonPort := host[i:]
402405
if !validOptionalPort(colonPort) {
403406
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
@@ -104,6 +104,27 @@ func testURIPathEscape(t *testing.T, path, expectedRequestURI string) {
104104
}
105105
}
106106

107+
func TestURIRejectMultiplePorts(t *testing.T) {
108+
t.Parallel()
109+
110+
testcases := []string{
111+
"http://192.168.1.1:1111:2222/",
112+
"http://example.com:80:8080/",
113+
}
114+
115+
for _, raw := range testcases {
116+
var u URI
117+
if err := u.Parse(nil, []byte(raw)); err == nil {
118+
t.Fatalf("expected Parse to fail for %q", raw)
119+
}
120+
}
121+
122+
var valid URI
123+
if err := valid.Parse(nil, []byte("http://192.168.1.1:1111/")); err != nil {
124+
t.Fatalf("unexpected error for valid uri: %v", err)
125+
}
126+
}
127+
107128
func TestURIUpdate(t *testing.T) {
108129
t.Parallel()
109130

0 commit comments

Comments
 (0)