From e25ee6dfac35b6026dc13c6f2b75d6975a93549f Mon Sep 17 00:00:00 2001 From: Gonzalo Serrano Date: Wed, 10 Jun 2026 12:32:03 +0200 Subject: [PATCH 1/2] feat(ressrf): expose the deny CIDR set for callers to mirror the gate Add Policy.DeniedIPPrefixes() returning the network-layer deny ranges (PresetExternalOnly default set plus WithCloudProviderDenies and WithDeniedCIDRs entries; nil for PresetNone/InternalOnly), and CIDR.NativePrefix() to return IPv4 ranges in native 4-byte form rather than the IPv4-mapped form the matcher stores internally. A consumer that must mirror the gate at another layer (e.g. a Kubernetes NetworkPolicy egress except-list) can derive it from the policy instead of hand-maintaining a parallel CIDR list that drifts. denied_prefixes_test.go pins the invariant that the exported set equals what IsNetworkAllowed enforces. --- go-native/ressrf/denied_prefixes_test.go | 59 ++++++++++++++++++++++ go-native/ressrf/internal/engine/cidr.go | 14 +++++ go-native/ressrf/internal/engine/policy.go | 20 ++++++++ go-native/ressrf/policy.go | 16 ++++++ 4 files changed, 109 insertions(+) create mode 100644 go-native/ressrf/denied_prefixes_test.go diff --git a/go-native/ressrf/denied_prefixes_test.go b/go-native/ressrf/denied_prefixes_test.go new file mode 100644 index 0000000..90bf2a6 --- /dev/null +++ b/go-native/ressrf/denied_prefixes_test.go @@ -0,0 +1,59 @@ +package ressrf + +import ( + "context" + "slices" + "testing" +) + +func TestDeniedIPPrefixes(t *testing.T) { + policy, err := NewPolicy(PresetExternalOnly, WithCloudProviderDenies(CloudAWS)) + if err != nil { + t.Fatalf("NewPolicy: %v", err) + } + + prefixes := policy.DeniedIPPrefixes() + if len(prefixes) == 0 { + t.Fatal("expected a non-empty deny set for PresetExternalOnly") + } + + // Well-known ranges are present in canonical, family-native form. + got := make([]string, len(prefixes)) + for i, p := range prefixes { + got[i] = p.String() + } + for _, want := range []string{ + "10.0.0.0/8", // RFC1918 + "169.254.0.0/16", // link-local + "169.254.169.254/32", // cloud IMDS (via CloudAWS) + "fc00::/7", // IPv6 ULA + "::1/128", // IPv6 loopback + } { + if !slices.Contains(got, want) { + t.Errorf("deny set missing %s; got %v", want, got) + } + } + + // The exported list must agree with what the gate actually enforces: the + // network address of every returned range is denied by IsNetworkAllowed, and + // a public address is allowed. This invariant is what lets a consumer mirror + // the gate (e.g. a NetworkPolicy except-list) without the two drifting apart. + for _, p := range prefixes { + ip := p.Addr().String() + if err := policy.IsNetworkAllowed(context.Background(), []string{ip}); err == nil { + t.Errorf("IsNetworkAllowed permitted %s, but it is in the exported deny set", ip) + } + } + if err := policy.IsNetworkAllowed(context.Background(), []string{"1.1.1.1"}); err != nil { + t.Errorf("expected public IP 1.1.1.1 to be allowed, got %v", err) + } + + // PresetNone has no network-layer deny set. + none, err := NewPolicy(PresetNone) + if err != nil { + t.Fatalf("NewPolicy(PresetNone): %v", err) + } + if got := none.DeniedIPPrefixes(); got != nil { + t.Errorf("PresetNone should have no deny prefixes, got %v", got) + } +} diff --git a/go-native/ressrf/internal/engine/cidr.go b/go-native/ressrf/internal/engine/cidr.go index 7cea44f..31fac00 100644 --- a/go-native/ressrf/internal/engine/cidr.go +++ b/go-native/ressrf/internal/engine/cidr.go @@ -83,6 +83,20 @@ func (c CIDR) Contains(addr netip.Addr) bool { return c.Prefix.Contains(netip.AddrFrom16(addr.As16())) } +// NativePrefix returns the prefix in native form: an IPv4 range is returned as +// a 4-byte (Is4) prefix rather than the IPv4-mapped IPv6 form the matcher +// stores internally (a.b.c.d/n instead of ::ffff:a.b.c.d/(n+96)). IPv6 ranges +// are returned unchanged. Use this when handing the range to code that expects +// human / family-native CIDRs, e.g. rendering a Kubernetes NetworkPolicy +// except-list. +func (c CIDR) NativePrefix() netip.Prefix { + addr := c.Prefix.Addr() + if addr.Is4In6() { + return netip.PrefixFrom(addr.Unmap(), c.Prefix.Bits()-96) + } + return c.Prefix +} + // CIDRSet is a flat collection of CIDRs scanned in insertion order. The Rust // core uses the same linear scan; for ~30 entries it outperforms tree // structures because of cache locality. diff --git a/go-native/ressrf/internal/engine/policy.go b/go-native/ressrf/internal/engine/policy.go index f383140..29653f5 100644 --- a/go-native/ressrf/internal/engine/policy.go +++ b/go-native/ressrf/internal/engine/policy.go @@ -200,6 +200,26 @@ func (b *PolicyBuilder) Build() (*Policy, error) { // Preset returns the preset this policy was built with. func (p *Policy) Preset() Preset { return p.preset } +// DenyPrefixes returns the IP CIDR ranges this policy blocks at the network +// layer, in native form (IPv4 ranges as Is4 prefixes). For PresetExternalOnly +// this is the built-in default deny set followed by any cloud-provider and +// user-supplied deny CIDRs, in insertion order. PresetNone and +// PresetInternalOnly have no network-layer deny set and return nil. +// +// The allow set is NOT subtracted: allow overrides deny at match time (see +// isNetworkAllowedIPs), so a caller that added allow CIDRs and needs the +// effective blocked set must account for them itself. +func (p *Policy) DenyPrefixes() []netip.Prefix { + if p.denySet.IsEmpty() { + return nil + } + out := make([]netip.Prefix, 0, p.denySet.Len()) + for _, c := range p.denySet.Ranges { + out = append(out, c.NativePrefix()) + } + return out +} + // IsNetworkAllowed validates that every IP in the list is permitted. Returns // nil on success or a DenyReason (which implements error) on rejection. // Behaviour matches crates/ressrf-core/src/policy.rs::is_network_allowed. diff --git a/go-native/ressrf/policy.go b/go-native/ressrf/policy.go index 687809e..f8d2a15 100644 --- a/go-native/ressrf/policy.go +++ b/go-native/ressrf/policy.go @@ -358,6 +358,22 @@ func (p *Policy) IsAllowed(_ context.Context, url string) error { return nil } +// DeniedIPPrefixes returns the IP CIDR ranges this policy blocks at the network +// layer, in native form (IPv4 prefixes are Is4, not IPv4-mapped). For +// PresetExternalOnly this is the built-in default deny set plus any +// WithCloudProviderDenies and WithDeniedCIDRs entries; PresetNone and +// PresetInternalOnly return nil (they have no network-layer deny set). +// +// This exposes the same ranges IsNetworkAllowed enforces, so a caller that must +// mirror the gate at another layer — e.g. rendering a Kubernetes NetworkPolicy +// egress except-list — can derive it from the policy instead of hand-maintaining +// a parallel CIDR list that silently drifts. Bucket by family with +// Addr().Is4(); String() yields the canonical CIDR text (e.g. "10.0.0.0/8", +// "fc00::/7"). The allow set is not subtracted; see WithAllowedCIDRs. +func (p *Policy) DeniedIPPrefixes() []netip.Prefix { + return p.core.DenyPrefixes() +} + // IsNetworkAllowed checks whether a set of IPs is permitted by the policy. // This is the lower-level IP check (no URL parsing/scheme validation). func (p *Policy) IsNetworkAllowed(_ context.Context, ips []string) error { From c1b0fccba00c24cf8f7a249adc87de8a68f730b6 Mon Sep 17 00:00:00 2001 From: Gonzalo Serrano Date: Wed, 10 Jun 2026 15:27:07 +0200 Subject: [PATCH 2/2] fix(ressrf): de-duplicate DenyPrefixes so derived block lists stay minimal A metadata IP that lives in both the built-in deny set and a cloud-provider module (169.254.169.254/32, 169.254.170.2/32, fd00:ec2::254/128 under PresetExternalOnly + WithCloudProviderDenies(CloudAWS)) was returned twice, so a NetworkPolicy except-list derived from DeniedIPPrefixes() carried duplicate entries that some API servers reject. De-duplicate in first-seen order; add a no-duplicate assertion to TestDeniedIPPrefixes. --- go-native/ressrf/denied_prefixes_test.go | 11 +++++++++++ go-native/ressrf/internal/engine/policy.go | 15 ++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/go-native/ressrf/denied_prefixes_test.go b/go-native/ressrf/denied_prefixes_test.go index 90bf2a6..b1460a4 100644 --- a/go-native/ressrf/denied_prefixes_test.go +++ b/go-native/ressrf/denied_prefixes_test.go @@ -34,6 +34,17 @@ func TestDeniedIPPrefixes(t *testing.T) { } } + // No duplicates: a metadata IP present in both the built-in deny set and a + // cloud-provider module must appear once, so a derived NetworkPolicy + // except-list stays minimal. + seen := make(map[string]bool, len(got)) + for _, s := range got { + if seen[s] { + t.Errorf("duplicate prefix %s in deny set: %v", s, got) + } + seen[s] = true + } + // The exported list must agree with what the gate actually enforces: the // network address of every returned range is denied by IsNetworkAllowed, and // a public address is allowed. This invariant is what lets a consumer mirror diff --git a/go-native/ressrf/internal/engine/policy.go b/go-native/ressrf/internal/engine/policy.go index 29653f5..01f7c9a 100644 --- a/go-native/ressrf/internal/engine/policy.go +++ b/go-native/ressrf/internal/engine/policy.go @@ -202,8 +202,8 @@ func (p *Policy) Preset() Preset { return p.preset } // DenyPrefixes returns the IP CIDR ranges this policy blocks at the network // layer, in native form (IPv4 ranges as Is4 prefixes). For PresetExternalOnly -// this is the built-in default deny set followed by any cloud-provider and -// user-supplied deny CIDRs, in insertion order. PresetNone and +// this is the built-in default deny set plus any cloud-provider and +// user-supplied deny CIDRs, de-duplicated in first-seen order. PresetNone and // PresetInternalOnly have no network-layer deny set and return nil. // // The allow set is NOT subtracted: allow overrides deny at match time (see @@ -213,9 +213,18 @@ func (p *Policy) DenyPrefixes() []netip.Prefix { if p.denySet.IsEmpty() { return nil } + // A metadata IP can appear in both the built-in deny set and a + // cloud-provider module; de-duplicate so a derived network-layer block + // list (e.g. a Kubernetes NetworkPolicy except-list) stays minimal. out := make([]netip.Prefix, 0, p.denySet.Len()) + seen := make(map[netip.Prefix]struct{}, p.denySet.Len()) for _, c := range p.denySet.Ranges { - out = append(out, c.NativePrefix()) + np := c.NativePrefix() + if _, dup := seen[np]; dup { + continue + } + seen[np] = struct{}{} + out = append(out, np) } return out }