Skip to content
Merged
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
70 changes: 70 additions & 0 deletions go-native/ressrf/denied_prefixes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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)
}
}

// 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
// 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)
}
}
14 changes: 14 additions & 0 deletions go-native/ressrf/internal/engine/cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions go-native/ressrf/internal/engine/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,35 @@ 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 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
// 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
}
// 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 {
np := c.NativePrefix()
if _, dup := seen[np]; dup {
continue
}
seen[np] = struct{}{}
out = append(out, np)
}
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.
Expand Down
16 changes: 16 additions & 0 deletions go-native/ressrf/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading