core: reject overlong ports in ParseNetworkAddress (#8277)
parent
a72377a10d
commit
61e9b223da
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue