Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions config/tomlloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"fmt"
"maps"
"net"
"net/netip"
"regexp"
"runtime"
"slices"
"strings"

"github.com/BurntSushi/toml"
"github.com/c-robinson/iplib"
"github.com/knqyf263/go-cpe/naming"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -193,27 +193,32 @@ func enumerateHosts(host string) ([]string, error) {
return []string{host}, nil
}

ipAddr, ipNet, err := net.ParseCIDR(host)
prefix, err := netip.ParsePrefix(host)
if err != nil {
return nil, xerrors.Errorf("Failed to parse CIDR. err: %w", err)
}
maskLen, _ := ipNet.Mask.Size()

addrs := []string{}
if net.ParseIP(ipAddr.String()).To4() != nil {
n := iplib.NewNet4(ipAddr, int(maskLen))
for _, addr := range n.Enumerate(int(n.Count()), 0) {
addrs = append(addrs, addr.String())
}
} else if net.ParseIP(ipAddr.String()).To16() != nil {
n := iplib.NewNet6(ipAddr, int(maskLen), 0)
if !n.Count().IsInt64() {
return nil, xerrors.Errorf("Failed to enumerate IP address. err: mask bitsize too big")
}
for _, addr := range n.Enumerate(int(n.Count().Int64()), 0) {
addrs = append(addrs, addr.String())
}
hostBits := prefix.Addr().BitLen() - prefix.Bits()

// Cap at 2^16 = 65536 addresses (a /16 for IPv4, /112 for IPv6) to prevent OOM.
const maxHostBits = 16
if hostBits > maxHostBits {
return nil, xerrors.Errorf("Failed to enumerate IP address: prefix /%d too large (max host bits %d)", prefix.Bits(), maxHostBits)
}

count := int(1) << hostBits
addrs := make([]string, 0, count)
addr := prefix.Masked().Addr()
for range count {
addrs = append(addrs, addr.String())
addr = addr.Next()
}
Comment on lines +201 to +215
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This enumerates 1<<hostBits addresses and also preallocates the slice to that size. For inputs like IPv4 /0 (hostBits=32) or IPv6 /65 (hostBits=63), this will attempt to allocate/enumerate billions to quintillions of entries and will reliably OOM/hang. Consider adding a hard upper bound on the number of hosts that can be expanded (or at least reject prefixes where count exceeds a reasonable limit / math.MaxInt) to avoid config-triggered resource exhaustion.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c35f046. Tightened the upper bound from 2^63 to 2^16 host bits (maxHostBits = 16), so the maximum enumerable range is now a /16 for IPv4 (65,536 addresses) or /112 for IPv6. Wider prefixes like /0 or /8 now return an error immediately instead of attempting a huge allocation. Added test cases for 10.0.0.0/0 and 192.168.0.0/8 to verify.


// IPv4 with prefix < 31: strip network (first) and broadcast (last) addresses
if prefix.Addr().Is4() && prefix.Bits() < 31 && len(addrs) > 2 {
addrs = addrs[1 : len(addrs)-1]
}

return addrs, nil
}

Expand Down
32 changes: 32 additions & 0 deletions config/tomlloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,38 @@ func TestHosts(t *testing.T) {
}
}

func TestEnumerateHosts(t *testing.T) {
t.Parallel()
tests := []struct {
in string
expected []string
err bool
}{
{in: "192.168.1.1", expected: []string{"192.168.1.1"}},
{in: "ssh/host", expected: []string{"ssh/host"}},
{in: "192.168.1.0/30", expected: []string{"192.168.1.1", "192.168.1.2"}},
{in: "192.168.1.0/31", expected: []string{"192.168.1.0", "192.168.1.1"}},
{in: "192.168.1.1/32", expected: []string{"192.168.1.1"}},
{in: "2001:db8::1/126", expected: []string{"2001:db8::", "2001:db8::1", "2001:db8::2", "2001:db8::3"}},
{in: "2001:db8::1/127", expected: []string{"2001:db8::", "2001:db8::1"}},
{in: "2001:db8::1/128", expected: []string{"2001:db8::1"}},
{in: "2001:db8::1/32", err: true},
{in: "10.0.0.0/0", err: true},
{in: "192.168.0.0/8", err: true},
}
for i, tt := range tests {
actual, err := enumerateHosts(tt.in)
if err != nil && !tt.err {
t.Errorf("[%d] unexpected error: %v", i, err)
} else if err == nil && tt.err {
t.Errorf("[%d] expected error but got none", i)
}
if !reflect.DeepEqual(actual, tt.expected) {
t.Errorf("[%d] in: %s, got: %q, want: %q", i, tt.in, actual, tt.expected)
}
}
}

func TestToCpeURI(t *testing.T) {
var tests = []struct {
in string
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ require (
github.com/aws/aws-sdk-go-v2/credentials v1.19.10
github.com/aws/aws-sdk-go-v2/service/s3 v1.96.2
github.com/aws/aws-sdk-go-v2/service/sts v1.41.7
github.com/c-robinson/iplib v1.0.8
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/d4l3k/messagediff v1.2.2-0.20190829033028-7e0a312ae40b
github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,6 @@ github.com/bshuster-repo/logrus-logstash-hook v1.0.0 h1:e+C0SB5R1pu//O4MQ3f9cFuP
github.com/bshuster-repo/logrus-logstash-hook v1.0.0/go.mod h1:zsTqEiSzDgAa/8GZR7E1qaXrhYNDKBYy5/dWPTIflbk=
github.com/bytecodealliance/wasmtime-go/v39 v39.0.1 h1:RibaT47yiyCRxMOj/l2cvL8cWiWBSqDXHyqsa9sGcCE=
github.com/bytecodealliance/wasmtime-go/v39 v39.0.1/go.mod h1:miR4NYIEBXeDNamZIzpskhJ0z/p8al+lwMWylQ/ZJb4=
github.com/c-robinson/iplib v1.0.8 h1:exDRViDyL9UBLcfmlxxkY5odWX5092nPsQIykHXhIn4=
github.com/c-robinson/iplib v1.0.8/go.mod h1:i3LuuFL1hRT5gFpBRnEydzw8R6yhGkF4szNDIbF8pgo=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
Expand Down
Loading