core: reject overlong ports in ParseNetworkAddress (#8277)

pull/8315/head
jasper 2026-10-31 18:57:28 -07:00 committed by GitHub
parent a72377a10d
commit 61e9b223da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 42 additions and 0 deletions

View File

@ -348,6 +348,9 @@ func ParseNetworkAddressWithDefaults(addr, defaultNetwork string, defaultPort ui
start = uint64(defaultPort)
end = uint64(defaultPort)
} else {
if len(port) > maxPortLen {
return NetworkAddress{}, fmt.Errorf("invalid port: longer than %d characters", maxPortLen)
}
before, after, found := strings.Cut(port, "-")
if !found {
after = before
@ -776,3 +779,6 @@ type PacketConnWrapper interface {
var listenerPool = NewUsagePool()
const maxPortSpan = 65535
// maxPortLen is the length of the longest valid port range.
const maxPortLen = len("65535-65535")

View File

@ -19,6 +19,7 @@ import (
"crypto/tls"
"net"
"reflect"
"strings"
"testing"
"github.com/caddyserver/caddy/v2/internal"
@ -343,6 +344,21 @@ func TestParseNetworkAddress(t *testing.T) {
defaultNetwork: "tcp",
expectErr: true,
},
{
input: "localhost:65535-65535",
defaultNetwork: "tcp",
expectAddr: NetworkAddress{
Network: "tcp",
Host: "localhost",
StartPort: 65535,
EndPort: 65535,
},
},
{
input: "localhost:000000000080",
defaultNetwork: "tcp",
expectErr: true,
},
} {
actualAddr, err := ParseNetworkAddressWithDefaults(tc.input, tc.defaultNetwork, tc.defaultPort)
if tc.expectErr && err == nil {
@ -361,6 +377,26 @@ func TestParseNetworkAddress(t *testing.T) {
}
}
// A very long port should fail with a short error (issue #8229).
func TestParseNetworkAddressLongPort(t *testing.T) {
junk := strings.Repeat("\x7f", 1<<20)
for i, input := range []string{
":" + junk,
":80-" + junk,
":" + junk + "-80",
"localhost:" + strings.Repeat("9", 1<<20),
} {
_, err := ParseNetworkAddress(input)
if err == nil {
t.Errorf("Test %d: expected error but got none", i)
continue
}
if len(err.Error()) > 100 {
t.Errorf("Test %d: error message is %d bytes, expected at most 100", i, len(err.Error()))
}
}
}
func TestParseNetworkAddressWithDefaults(t *testing.T) {
for i, tc := range []struct {
input string