carlos / platform Public

apppolicy, instancectl: the box accepts egress prefixes exactly (#1357)

Validate reads each entry with ParseEntry, checks it with AllowedPrefix,
and requires the stored form (EntryString) in ComparePrefix's order.
Root's desired writes each entry's own prefix, never widened or masked,
and refuses one overlapping any of the box's addresses. egressaddr
exports ComparePrefix and EntryString so the order and stored form are
defined once. Witnesses: the desired table, a prefix written and read
back through the fake systemctl, the old-box fail-closed path in
provision, and a registry prefix column through root's real source.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 66284ac13df9ffb1a8952233bfbb03da7de41d5c parent 8ced880
9 files changed, +317 −40
  • internal/apppolicy/apppolicy.go +13 −8
  • internal/apppolicy/apppolicy_test.go +14 −0
  • internal/egressaddr/egressaddr.go +20 −11
  • internal/egressaddr/egressaddr_test.go +42 −0
  • internal/host/egress_source_test.go +58 −0
  • internal/instancectl/egress.go +25 −21
  • internal/instancectl/egress_test.go +104 −0
  • internal/instancectl/localaddrs.go +11 −0
  • internal/provision/apppolicy_reconcile_test.go +30 −0
diff --git a/internal/apppolicy/apppolicy.go b/internal/apppolicy/apppolicy.go
index 97df1976a..2984b1f0d 100644
--- a/internal/apppolicy/apppolicy.go
+++ b/internal/apppolicy/apppolicy.go
@@ -73,10 +73,12 @@ type SizeOverride struct {
}
// Validate checks the policy's shape: the version, known size names, host
-// keys, and an egress allow already in its stored form (canonical text,
-// strictly ascending, at most egressaddr.MaxAllow). It does not decide
-// whether an address may be granted (the door does, with
-// egressaddr.Normalize) and does not look at the signing fields.
+// keys, and an egress allow already in its stored form (each entry a single
+// address or a prefix in egressaddr.EntryString's text, one
+// egressaddr.AllowedPrefix accepts, strictly ascending in
+// egressaddr.ComparePrefix's order, at most egressaddr.MaxAllow). It does
+// not look at the signing fields. A box whose Validate predates prefixes
+// refuses a policy holding one (#1357), and its reader then fails closed.
func (p Policy) Validate() error {
if p.Demo != nil || p.EgressMode != "" {
if p.Demo == nil || p.EgressMode != "deny-all" || p.DefaultSize != "small" || len(p.EgressAllow) != 0 || len(p.Sizes) != 0 {
@@ -109,13 +111,16 @@ func (p Policy) Validate() error {
if len(p.EgressAllow) > egressaddr.MaxAllow {
return fmt.Errorf("%w: %d egress allows, at most %d", ErrInvalid, len(p.EgressAllow), egressaddr.MaxAllow)
}
- var prev netip.Addr
+ var prev netip.Prefix
for i, e := range p.EgressAllow {
- a, err := egressaddr.Parse(e)
- if err != nil || a.String() != e {
+ a, err := egressaddr.ParseEntry(e)
+ if err != nil || egressaddr.EntryString(a) != e {
return fmt.Errorf("%w: egress allow %q", ErrInvalid, e)
}
- if i > 0 && prev.Compare(a) >= 0 {
+ if err := egressaddr.AllowedPrefix(a); err != nil {
+ return fmt.Errorf("%w: egress allow %v", ErrInvalid, err)
+ }
+ if i > 0 && egressaddr.ComparePrefix(prev, a) >= 0 {
return fmt.Errorf("%w: egress allow %q out of order", ErrInvalid, e)
}
prev = a
diff --git a/internal/apppolicy/apppolicy_test.go b/internal/apppolicy/apppolicy_test.go
index eb4e734ad..1a2ce8b9d 100644
--- a/internal/apppolicy/apppolicy_test.go
+++ b/internal/apppolicy/apppolicy_test.go
@@ -76,6 +76,9 @@ func TestValidate(t *testing.T) {
{V: 1},
{V: 1, AppID: "x", EgressAllow: []string{"10.0.5.20", "fd00:5::20"}, DefaultSize: "small",
Sizes: map[string]SizeOverride{"shop-a.example.com": {"xlarge", t0}}},
+ // Prefixes in their stored form and decision 6's order: address,
+ // then prefix length, so a prefix sorts before its own first address.
+ {V: 1, EgressAllow: []string{"10.0.5.20", "10.0.64.0/20", "10.0.64.0", "fd00:5::/64", "fd00:5::20"}},
} {
if err := p.Validate(); err != nil {
t.Errorf("Validate(%+v) = %v", p, err)
@@ -99,6 +102,17 @@ func TestValidate(t *testing.T) {
"duplicate allows": {V: 1, EgressAllow: []string{"10.0.5.20", "10.0.5.20"}},
"allow with a port": {V: 1, EgressAllow: []string{"10.0.5.20:80"}},
"allow not in canon form": {V: 1, EgressAllow: []string{"::ffff:10.0.5.20"}},
+ "single stored as /32": {V: 1, EgressAllow: []string{"10.0.5.20/32"}},
+ "single stored as /128": {V: 1, EgressAllow: []string{"fd00:5::20/128"}},
+ "prefix with host bits": {V: 1, EgressAllow: []string{"10.0.64.5/20"}},
+ "prefix not canonical": {V: 1, EgressAllow: []string{"fd00:5:0::/64"}},
+ "prefix with space": {V: 1, EgressAllow: []string{" 10.0.64.0/20"}},
+ "prefix after its first": {V: 1, EgressAllow: []string{"10.0.64.0", "10.0.64.0/20"}},
+ "duplicate prefixes": {V: 1, EgressAllow: []string{"10.0.64.0/20", "10.0.64.0/20"}},
+ "prefix too wide": {V: 1, EgressAllow: []string{"10.0.0.0/8"}},
+ "prefix public": {V: 1, EgressAllow: []string{"8.8.8.0/24"}},
+ "prefix holds metadata": {V: 1, EgressAllow: []string{"fd00:ec2::/48"}},
+ "single public": {V: 1, EgressAllow: []string{"8.8.8.8"}},
} {
if err := p.Validate(); !errors.Is(err, ErrInvalid) {
t.Errorf("%s: Validate = %v, want ErrInvalid", name, err)
diff --git a/internal/egressaddr/egressaddr.go b/internal/egressaddr/egressaddr.go
index 2061903d4..02a8d3dcb 100644
--- a/internal/egressaddr/egressaddr.go
+++ b/internal/egressaddr/egressaddr.go
@@ -293,19 +293,28 @@ func Normalize(in []string) ([]string, error) {
if len(entries) == 0 {
return nil, nil
}
- slices.SortFunc(entries, func(a, b netip.Prefix) int {
- if c := a.Addr().Compare(b.Addr()); c != 0 {
- return c
- }
- return a.Bits() - b.Bits()
- })
+ slices.SortFunc(entries, ComparePrefix)
out := make([]string, len(entries))
for i, p := range entries {
- if p.IsSingleIP() {
- out[i] = p.Addr().String()
- } else {
- out[i] = p.String()
- }
+ out[i] = EntryString(p)
}
return out, nil
}
+
+// ComparePrefix is the stored order: by address (IPv4 before IPv6), then
+// by prefix length, so 10.0.64.0/20 comes before 10.0.64.0.
+func ComparePrefix(a, b netip.Prefix) int {
+ if c := a.Addr().Compare(b.Addr()); c != 0 {
+ return c
+ }
+ return a.Bits() - b.Bits()
+}
+
+// EntryString is p's stored form: a full-length prefix as the bare
+// address, any other as its canonical prefix text.
+func EntryString(p netip.Prefix) string {
+ if p.IsSingleIP() {
+ return p.Addr().String()
+ }
+ return p.String()
+}
diff --git a/internal/egressaddr/egressaddr_test.go b/internal/egressaddr/egressaddr_test.go
index 5073d0e87..afba92c31 100644
--- a/internal/egressaddr/egressaddr_test.go
+++ b/internal/egressaddr/egressaddr_test.go
@@ -4,6 +4,7 @@ import (
"fmt"
"net/netip"
"reflect"
+ "slices"
"strings"
"testing"
)
@@ -268,3 +269,44 @@ func TestAcceptedPrefixEdgesPassAllowed(t *testing.T) {
}
}
}
+
+// Parse is the single-address reader an old box's code shape still uses:
+// it refuses every prefix form, a full-length one included, so an old
+// box's Validate fails on a prefix and never reads it as an address.
+func TestParseRefusesEveryPrefixForm(t *testing.T) {
+ for _, in := range []string{
+ "10.0.64.0/20", "10.0.5.20/32", "10.0.64.5/20", " 10.0.64.0/20 ", "10.0.0.0/8",
+ "fd00:5::/64", "fd00:5::20/128", "::ffff:10.0.5.20/128", "10.0.64.0/", "/20",
+ } {
+ if a, err := Parse(in); err == nil {
+ t.Errorf("Parse(%q) = %s, want a refusal", in, a)
+ }
+ }
+}
+
+// ComparePrefix is decision 6's order, and EntryString the stored form:
+// the one definition Normalize, apppolicy.Validate and root share.
+func TestComparePrefixAndEntryString(t *testing.T) {
+ in := []string{"fd00:5::/64", "10.0.64.0", "fd00:5::", "10.0.64.0/20", "10.0.5.20", "10.0.64.0/24"}
+ var ps []netip.Prefix
+ for _, s := range in {
+ p, err := ParseEntry(s)
+ if err != nil {
+ t.Fatal(err)
+ }
+ ps = append(ps, p)
+ }
+ slices.SortFunc(ps, ComparePrefix)
+ var got []string
+ for _, p := range ps {
+ got = append(got, EntryString(p))
+ }
+ want := []string{"10.0.5.20", "10.0.64.0/20", "10.0.64.0/24", "10.0.64.0", "fd00:5::/64", "fd00:5::"}
+ if !reflect.DeepEqual(got, want) {
+ t.Errorf("sorted = %q, want %q", got, want)
+ }
+ norm, err := Normalize(in)
+ if err != nil || !reflect.DeepEqual(norm, want) {
+ t.Errorf("Normalize = %q, %v; want %q", norm, err, want)
+ }
+}
diff --git a/internal/host/egress_source_test.go b/internal/host/egress_source_test.go
index 905bb1a93..fbe033924 100644
--- a/internal/host/egress_source_test.go
+++ b/internal/host/egress_source_test.go
@@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"io/fs"
+ "net/netip"
"os"
"path/filepath"
"reflect"
@@ -939,3 +940,60 @@ func TestRegistryEgressSourceRefusesFormerDemo(t *testing.T) {
}
}
}
+
+// TestRegistryPrefixColumnReachesTheSliceExactly is #1357's production
+// wiring: a registry egress column holding a prefix and a single goes
+// through root's real source (registryEgressSource) into root's asserter
+// (newEgressAsserter), which writes exactly those two prefixes, with only
+// systemctl, the box's addresses and the cgroup tree faked.
+func TestRegistryPrefixColumnReachesTheSliceExactly(t *testing.T) {
+ t.Parallel()
+ const host = "shop-a.example.com"
+ reg, dd := egressRegistry(t)
+ clock := &testClock{now: time.Date(2026, 11, 22, 9, 0, 0, 0, time.UTC)}
+ upsertRows(t, reg, nestedRow(dd, host))
+ setAllow(t, reg, host, "10.0.64.0/20 10.0.5.20", unixText(clock.Now().Add(time.Hour)))
+
+ var mu sync.Mutex
+ var sets []string
+ held := ""
+ slice := instancectl.SliceName(host)
+ e := newEgressAsserter(testEgressSource(t, dd, clock.Now), nil)
+ e.Systemctl = func(_ context.Context, args ...string) (string, error) {
+ mu.Lock()
+ defer mu.Unlock()
+ switch {
+ case len(args) == 5 && args[0] == "show" && args[4] == slice:
+ return held + "\n", nil
+ case len(args) == 5 && args[0] == "show":
+ return "\n", nil // the parent holds nothing
+ case len(args) >= 4 && args[0] == "set-property" && args[2] == slice:
+ sets = append(sets, strings.Join(args, " "))
+ held = strings.TrimPrefix(args[len(args)-1], "IPAddressAllow=")
+ return "", nil
+ }
+ return "", errors.New("unexpected call: " + strings.Join(args, " "))
+ }
+ e.LocalAddrs = func() ([]netip.Prefix, error) { return []netip.Prefix{netip.MustParsePrefix("127.0.0.0/8")}, nil }
+ e.CgroupRoot = t.TempDir()
+ e.Logf = func(string, ...any) {}
+ e.DueDir = ""
+
+ if err := e.Assert(context.Background(), host); err != nil {
+ t.Fatal(err)
+ }
+ want := []string{"set-property --runtime " + slice + " IPAddressAllow= IPAddressAllow=10.0.5.20/32 10.0.64.0/20"}
+ if !slices.Equal(sets, want) {
+ t.Fatalf("writes = %q, want %q", sets, want)
+ }
+ if rep := e.Reports()[host]; rep.State != "" || !slices.Equal(rep.Allowed, []string{"10.0.5.20", "10.0.64.0/20"}) {
+ t.Fatalf("report = %+v, want both in force", rep)
+ }
+ // Asserted again: in force as read back, nothing written.
+ if err := e.Assert(context.Background(), host); err != nil {
+ t.Fatal(err)
+ }
+ if len(sets) != 1 {
+ t.Fatalf("writes = %q, want the one write", sets)
+ }
+}
diff --git a/internal/instancectl/egress.go b/internal/instancectl/egress.go
index a8f15b2d9..15d1516ae 100644
--- a/internal/instancectl/egress.go
+++ b/internal/instancectl/egress.go
@@ -630,33 +630,36 @@ func (e *EgressAsserter) Reports() map[string]EgressReport {
return out
}
-// desired validates a policy's entries in order: egressaddr.Parse, then
-// egressaddr.Allowed, then not one of the box's own addresses. It returns
-// the full-length prefixes to write, sorted, and a refusal per entry
-// refused. More than egressaddr.MaxAllow accepted closes them all, and
-// tooMany says so. When the box's addresses cannot be read, no entry is
-// accepted: an allow that might name the box itself is never written.
+// desired validates a policy's entries in order: egressaddr.ParseEntry,
+// then egressaddr.AllowedPrefix, then overlapping none of the box's own
+// addresses (#1357: an entry may be a prefix, and IPAddressAllow beats
+// IPAddressDeny, so a prefix holding one cannot be carved out). It returns
+// each entry's own prefix to write, never widened or masked, sorted, and a
+// refusal per entry refused. More than egressaddr.MaxAllow accepted closes
+// them all, and tooMany says so. When the box's addresses cannot be read,
+// no entry is accepted: an allow that might name the box itself is never
+// written.
func (e *EgressAsserter) desired(host string, allow []string) (want []netip.Prefix, refused []string, tooMany string) {
local, lerr := e.localAddrs()
if lerr != nil {
e.logf("egress: %s: this box's addresses: %v; every entry is closed", host, lerr)
}
- seen := map[netip.Addr]bool{}
+ seen := map[netip.Prefix]bool{}
for _, s := range allow {
s = strings.TrimSpace(s)
if s == "" {
continue
}
- a, err := egressaddr.Parse(s)
- if err == nil && seen[a] {
+ p, err := egressaddr.ParseEntry(s)
+ if err == nil && seen[p] {
continue
}
if err == nil {
- seen[a] = true
- err = egressaddr.Allowed(a)
+ seen[p] = true
+ err = egressaddr.AllowedPrefix(p)
}
if err != nil {
- // egressaddr's errors already name the address.
+ // egressaddr's errors already name the entry.
refused = append(refused, err.Error())
continue
}
@@ -664,11 +667,15 @@ func (e *EgressAsserter) desired(host string, allow []string) (want []netip.Pref
refused = append(refused, s+": this box's addresses could not be read")
continue
}
- if lp, ok := isLocal(a, local); ok {
- refused = append(refused, fmt.Sprintf("%s: an address of this box (%s)", s, lp))
+ if lp, ok := overlapsLocal(p, local); ok {
+ if p.IsSingleIP() {
+ refused = append(refused, fmt.Sprintf("%s: an address of this box (%s)", s, lp))
+ } else {
+ refused = append(refused, fmt.Sprintf("%s: holds an address of this box (%s)", s, lp))
+ }
continue
}
- want = append(want, netip.PrefixFrom(a, a.BitLen()))
+ want = append(want, p)
}
if len(want) > egressaddr.MaxAllow {
return nil, refused, fmt.Sprintf("the policy has more than %d addresses (%d); every one is closed", egressaddr.MaxAllow, len(want))
@@ -784,12 +791,9 @@ func prefixesText(ps []netip.Prefix) string {
return strings.Join(s, " ")
}
-func comparePrefix(a, b netip.Prefix) int {
- if c := a.Addr().Compare(b.Addr()); c != 0 {
- return c
- }
- return a.Bits() - b.Bits()
-}
+// comparePrefix is the stored order (egressaddr.ComparePrefix), so root
+// writes and reports an allow in the order the policy holds it.
+func comparePrefix(a, b netip.Prefix) int { return egressaddr.ComparePrefix(a, b) }
func (e *EgressAsserter) readSource(ctx context.Context, host string) (EgressPolicy, error) {
if e.Source == nil {
diff --git a/internal/instancectl/egress_test.go b/internal/instancectl/egress_test.go
index 78d666162..36b821499 100644
--- a/internal/instancectl/egress_test.go
+++ b/internal/instancectl/egress_test.go
@@ -228,6 +228,110 @@ func TestEgressDropsTheBoxsOwnAddress(t *testing.T) {
}
}
+// desired takes a prefix exactly (#1357 decision 7): written as its own
+// prefix, never widened and never masked, refused when it overlaps any of
+// the box's own addresses, a single address as before.
+func TestEgressDesiredTakesPrefixesExactly(t *testing.T) {
+ local := []netip.Prefix{
+ netip.MustParsePrefix("127.0.0.0/8"),
+ netip.MustParsePrefix("10.0.5.20/32"),
+ netip.MustParsePrefix("10.0.80.7/32"),
+ netip.MustParsePrefix("10.1.0.0/24"),
+ netip.MustParsePrefix("fd00:6::9/128"),
+ }
+ pfx := func(ss ...string) []netip.Prefix {
+ var out []netip.Prefix
+ for _, s := range ss {
+ out = append(out, netip.MustParsePrefix(s))
+ }
+ return out
+ }
+ var nine []string
+ for i := 1; i <= 9; i++ {
+ nine = append(nine, fmt.Sprintf("10.%d.0.0/16", 100+i))
+ }
+ for _, c := range []struct {
+ name string
+ allow []string
+ want []netip.Prefix
+ refused []string
+ tooMany bool
+ }{
+ {name: "a prefix is written as is", allow: []string{"10.0.64.0/20", "10.0.5.21"},
+ want: pfx("10.0.5.21/32", "10.0.64.0/20")},
+ {name: "a prefix and its own first address are two entries", allow: []string{"10.0.64.0", "10.0.64.0/20", "fd00:5::/64"},
+ want: pfx("10.0.64.0/20", "10.0.64.0/32", "fd00:5::/64")},
+ {name: "duplicates collapse by prefix", allow: []string{"10.0.64.0/20", " 10.0.64.0/20", "10.0.5.21", "10.0.5.21/32"},
+ want: pfx("10.0.5.21/32", "10.0.64.0/20")},
+ {name: "a prefix holding a local /32 is refused", allow: []string{"10.0.80.0/20", "10.0.64.0/20"},
+ want: pfx("10.0.64.0/20"),
+ refused: []string{"10.0.80.0/20: holds an address of this box (10.0.80.7/32)"}},
+ {name: "a prefix overlapping a local range is refused", allow: []string{"10.1.0.0/16", "fd00:6::/64"},
+ refused: []string{
+ "10.1.0.0/16: holds an address of this box (10.1.0.0/24)",
+ "fd00:6::/64: holds an address of this box (fd00:6::9/128)",
+ }},
+ {name: "a single local address keeps its text", allow: []string{"10.0.5.20", "10.1.0.9"},
+ refused: []string{
+ "10.0.5.20: an address of this box (10.0.5.20/32)",
+ "10.1.0.9: an address of this box (10.1.0.0/24)",
+ }},
+ {name: "a host-bits entry is refused, never masked", allow: []string{"10.0.64.5/20"},
+ refused: []string{`"10.0.64.5/20" has host bits set. write it as 10.0.64.0/20`}},
+ {name: "a wide prefix is refused", allow: []string{"10.0.0.0/8"},
+ refused: []string{"10.0.0.0/8 is wider than /16. allow at most a /16"}},
+ {name: "nine prefixes close every one", allow: nine, tooMany: true},
+ {name: "eight prefixes are written", allow: nine[:8],
+ want: pfx("10.101.0.0/16", "10.102.0.0/16", "10.103.0.0/16", "10.104.0.0/16",
+ "10.105.0.0/16", "10.106.0.0/16", "10.107.0.0/16", "10.108.0.0/16")},
+ } {
+ e := newEgressAsserter(newEgressCtl(), nil)
+ e.LocalAddrs = func() ([]netip.Prefix, error) { return local, nil }
+ want, refused, tooMany := e.desired(egressHost, c.allow)
+ if !slices.Equal(want, c.want) {
+ t.Errorf("%s: want = %v, want %v", c.name, want, c.want)
+ }
+ if !slices.Equal(refused, c.refused) {
+ t.Errorf("%s: refused = %q, want %q", c.name, refused, c.refused)
+ }
+ if (tooMany != "") != c.tooMany {
+ t.Errorf("%s: tooMany = %q, want set %v", c.name, tooMany, c.tooMany)
+ }
+ }
+}
+
+// Assert writes a prefix through systemctl as is, and systemd's read-back
+// of it counts as in force: nothing is written again and nothing fails.
+func TestEgressWritesAPrefixAndReadsItBack(t *testing.T) {
+ ctl := newEgressCtl()
+ e := newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.64.0/20", "10.0.5.20", "fd00:5::/64"}}, nil))
+ rep := mustAssert(t, e, egressHost)
+ wantCalls(t, ctl.takeCalls(),
+ showCall(egressHost),
+ setCall(egressHost, "10.0.5.20/32", "10.0.64.0/20", "fd00:5::/64"),
+ showCall(egressHost))
+ if want := (EgressReport{Allowed: []string{"10.0.5.20", "10.0.64.0/20", "fd00:5::/64"}}); !reflect.DeepEqual(rep, want) {
+ t.Fatalf("report = %+v, want %+v", rep, want)
+ }
+
+ // systemd's own form of the same allow, in its order: in force.
+ ctl = newEgressCtl()
+ ctl.setHeld(SliceName(egressHost), "10.0.64.0/20 fd00:5::/64 10.0.5.20/32")
+ e = newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.5.20", "10.0.64.0/20", "fd00:5::/64"}}, nil))
+ rep = mustAssert(t, e, egressHost)
+ wantCalls(t, ctl.takeCalls(), showCall(egressHost))
+ if rep.State != "" || !reflect.DeepEqual(rep.Allowed, []string{"10.0.5.20", "10.0.64.0/20", "fd00:5::/64"}) {
+ t.Fatalf("report = %+v, want in force with nothing written", rep)
+ }
+
+ // A held prefix that differs from the wanted one is not in force.
+ ctl = newEgressCtl()
+ ctl.setHeld(SliceName(egressHost), "10.0.0.0/16")
+ e = newEgressAsserter(ctl, staticSource(EgressPolicy{Allow: []string{"10.0.64.0/20"}}, nil))
+ mustAssert(t, e, egressHost)
+ wantCalls(t, ctl.sets(), setCall(egressHost, "10.0.64.0/20"))
+}
+
func TestEgressClosesWhenTheBoxsAddressesCannotBeRead(t *testing.T) {
ctl := newEgressCtl()
ctl.setHeld(SliceName(egressHost), "10.0.5.20/32")
diff --git a/internal/instancectl/localaddrs.go b/internal/instancectl/localaddrs.go
index 4be41e2ec..f0d0ba517 100644
--- a/internal/instancectl/localaddrs.go
+++ b/internal/instancectl/localaddrs.go
@@ -126,6 +126,17 @@ func isLocal(a netip.Addr, local []netip.Prefix) (netip.Prefix, bool) {
return netip.Prefix{}, false
}
+// overlapsLocal names the local prefix p overlaps, if any: for a
+// full-length p, the one holding its address, as isLocal.
+func overlapsLocal(p netip.Prefix, local []netip.Prefix) (netip.Prefix, bool) {
+ for _, l := range local {
+ if l.Overlaps(p) {
+ return l, true
+ }
+ }
+ return netip.Prefix{}, false
+}
+
// LocalAddresses is everything that routes to this box, read from
// procfs as the resolver allow reads it: the egress asserter's
// LocalAddrs, and the agent's list of the box's addresses.
diff --git a/internal/provision/apppolicy_reconcile_test.go b/internal/provision/apppolicy_reconcile_test.go
index a43c3c75a..7e49be561 100644
--- a/internal/provision/apppolicy_reconcile_test.go
+++ b/internal/provision/apppolicy_reconcile_test.go
@@ -4,6 +4,7 @@ import (
"context"
"crypto/ed25519"
"database/sql"
+ "encoding/base64"
"errors"
"fmt"
"os"
@@ -215,6 +216,16 @@ func TestReconcileVerifiesBeforeConverging(t *testing.T) {
other.App = "other"
stale := good()
stale.SignedAt = fx.now.Add(-apppolicy.Lifetime - time.Minute)
+ // A record the release key did sign, holding an entry this box's
+ // Validate refuses: the old-box path (#1357), where a box whose
+ // Validate predates prefixes reads "10.0.64.0/20" the way this one
+ // reads a host-bits or too-wide prefix. The whole allow closes, the
+ // good single beside it included, and nothing wider is ever written.
+ badEntry := func(entry string) []byte {
+ p := good()
+ p.EgressAllow = []string{"10.0.5.20", entry}
+ return signUnchecked(t, fx.priv, p)
+ }
for _, c := range []struct {
name string
@@ -225,6 +236,9 @@ func TestReconcileVerifiesBeforeConverging(t *testing.T) {
{"another key", sign(otherPriv, grant), "policy unverifiable"},
{"another app's record", sign(fx.priv, other), "policy unverifiable"},
{"expired", sign(fx.priv, stale), "policy expired"},
+ {"a host-bits prefix", badEntry("10.0.64.5/20"), "policy unverifiable"},
+ {"a too-wide prefix", badEntry("10.0.0.0/8"), "policy unverifiable"},
+ {"a single stored as /32", badEntry("10.0.5.21/32"), "policy unverifiable"},
} {
converge()
fx.putRaw(c.body)
@@ -236,6 +250,13 @@ func TestReconcileVerifiesBeforeConverging(t *testing.T) {
fx.wantState(c.state)
}
+ // signUnchecked's control: the same signing over a policy this box
+ // accepts converges, so the refusals above are the entry's.
+ fx.putRaw(badEntry("10.0.64.0/20"))
+ fx.run()
+ wantCols(t, fx.row(policyHost), "10.0.5.20 10.0.64.0/20", "large", fx.now.Add(apppolicy.Lifetime).Unix())
+ fx.wantState("")
+
// No release key on the box: a valid record cannot be verified.
converge()
fx.cfg.ReleasePub = nil
@@ -610,3 +631,12 @@ func mintOrder(t *testing.T, reg *registry.Registry) []string {
}
return hosts
}
+
+// signUnchecked signs p the way apppolicy.Sign does, without Sign's
+// Validate: a record the release key signed whose shape a box refuses.
+func signUnchecked(t *testing.T, priv ed25519.PrivateKey, p apppolicy.Policy) []byte {
+ t.Helper()
+ payload := base64.StdEncoding.EncodeToString(mustJSON(t, p))
+ return mustJSON(t, map[string]string{"payload": payload,
+ "sig": base64.StdEncoding.EncodeToString(ed25519.Sign(priv, []byte("carlos app-policy v1\n"+payload)))})
+}