Reject invalid hosts with multiple port delimiters (#2077)

This commit is contained in:
Erik Dubbelboer
2025-10-05 08:46:46 +08:00
committed by GitHub
parent d3fc682391
commit bed90bcf09
2 changed files with 24 additions and 0 deletions
+3
View File
@@ -446,6 +446,9 @@ 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)
}
colonPort := host[i:]
if !validOptionalPort(colonPort) {
return nil, fmt.Errorf("invalid port %q after host", colonPort)
+21
View File
@@ -145,6 +145,27 @@ func TestURIRejectInvalidScheme(t *testing.T) {
}
}
func TestURIRejectMultiplePorts(t *testing.T) {
t.Parallel()
testcases := []string{
"http://192.168.1.1:1111:2222/",
"http://example.com:80:8080/",
}
for _, raw := range testcases {
var u URI
if err := u.Parse(nil, []byte(raw)); err == nil {
t.Fatalf("expected Parse to fail for %q", raw)
}
}
var valid URI
if err := valid.Parse(nil, []byte("http://192.168.1.1:1111/")); err != nil {
t.Fatalf("unexpected error for valid uri: %v", err)
}
}
func TestURIUpdate(t *testing.T) {
t.Parallel()