carlos / platform Public

edge: one cache namespace per front; catch-alls keep off it (#186)

Task 10 fix round 1 (Astra P1-1, P1-2; Opus Important and minor). A front
shadowed here by a catch-all let two tenancies share one LRU namespace.

caseThreeCacheable(t, host), true only when nothing here answers host
(lookupIn misses), now decides both sides: ServeHTTP looks up and stores
a case 3 response (up.front set) only then, and purgeOwnsIn and
resolvePurgeOwnersIn take the front branch only then. otherAccountFront
denies a catch-all purge authority over a name another account's claim
path-routes here: explicit Carlos-Purge-Host (refused whole), "*", and
"all"'s expansion (instanceHostsIn, now over the routeTable). Same-account
catch-alls keep today's behaviour. unservedFrontTarget is pinned in
selectionEntrySites. edge.go and ServeHTTP do not grow.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 1c9df4700d0348de828be1084c675386e8165a9a parent c389788
5 files changed, +245 −27
  • internal/edge/edge.go +2 −2
  • internal/edge/linkpin_test.go +8 −2
  • internal/edge/pagecache.go +21 −14
  • internal/edge/purge_across_test.go +158 −0
  • internal/edge/purge_header.go +56 −9
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index 59bd61717..3c7280cd3 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -2983,10 +2983,10 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// (page cache design §5.3). This is the one place that rule is checked.
//
// A linked request is never cached, looked up or stored (ServeLinked),
- // as a forward row is not on the hub.
+ // as a forward row is not on the hub; nor a case 3 response under a front a catch-all shadows (caseThreeCacheable).
cacheable := e.cache != nil && (r.Method == http.MethodGet || r.Method == http.MethodHead) &&
up.route.Kind != "status" && up.route.Kind != "static" && up.route.ImportGate == "" &&
- !up.route.IsForward() && !markedRequest(r) && !e.pagesOptedIn(up) && !pinned
+ !up.route.IsForward() && !markedRequest(r) && !e.pagesOptedIn(up) && !pinned && (up.front == nil || caseThreeCacheable(e.table(), host))
var key CacheKey
if cacheable {
key = CacheKey{Host: host, Method: r.Method, Path: r.URL.Path, RawQuery: r.URL.RawQuery}
diff --git a/internal/edge/linkpin_test.go b/internal/edge/linkpin_test.go
index cc95bd49d..f5b7d9d08 100644
--- a/internal/edge/linkpin_test.go
+++ b/internal/edge/linkpin_test.go
@@ -52,6 +52,10 @@ var selectionEntrySites = map[string]map[string]string{
"selectForUnservedFront": {
"selectAcross": "case 3, after the door and door-hop checks, with the remote map selectAcross read",
},
+ "unservedFrontTarget": {
+ "selectForUnservedFront": "case 3's eligibility of a target served here",
+ "frontPathOwns": "purge owner check; never dispatches: a route purges a front's pages exactly while case 3 would select it",
+ },
}
// The other ways to read the routing map by host, and where each may be
@@ -68,6 +72,7 @@ var mapReadSites = map[string]map[string]string{
"pageHostsFor": "the pages a purge declaration clears, from the response's own route",
"resolvePurgeOwnersIn": "validates the hosts a Carlos-Purge-Host names against the serving route's tenancy",
"purgeOwnsIn": "the owner check of a purge target host",
+ "caseThreeCacheable": "whether anything here answers a front's name, so a case 3 response may use the LRU under it and its target purge there; never picks an upstream",
},
// lookupDemoAware(m, host): the demo zone's exact-only lookup.
"lookupDemoAware": {
@@ -88,6 +93,7 @@ var mapReadSites = map[string]map[string]string{
"Reload": "the one publisher of the table (TestReloadIsTheOnlyPublisher) sets it beside hosts",
"purgeOwnsIn": "the LRU owner check of a purge for a front this box does not serve (spec §7.2); it can only refuse or allow an eviction, never pick an upstream",
"resolvePurgeOwnersIn": "accepts a Carlos-Purge-Host naming a front this box does not serve whose prefix selects the serving route (spec §7.2); never picks an upstream",
+ "otherAccountFront": "denies a catch-all purge authority over a name another account's claim path-routes here (spec §7.2); can only refuse",
},
}
@@ -163,7 +169,7 @@ func TestSelectUpstreamIsTheOnlySite(t *testing.T) {
callers[fn.Name.Name] = append(callers[fn.Name.Name], fset.Position(ident.Pos()).String())
case "lookupIn", "lookupDemoAware":
note(ident.Name, fn.Name.Name, ident.Pos())
- case "pathMatch", "selectAcross", "selectFromServedFront", "selectForUnservedFront":
+ case "pathMatch", "selectAcross", "selectFromServedFront", "selectForUnservedFront", "unservedFrontTarget":
if ident == n {
entries[ident.Name] = appendSite(entries[ident.Name], fn.Name.Name, fset.Position(ident.Pos()).String())
}
@@ -179,7 +185,7 @@ func TestSelectUpstreamIsTheOnlySite(t *testing.T) {
if id, ok := n.(*ast.Ident); ok {
switch id.Name {
case "lookupUpstream", "lookupUpstreamPath", "lookupIn", "lookupDemoAware",
- "pathMatch", "selectAcross", "selectFromServedFront", "selectForUnservedFront":
+ "pathMatch", "selectAcross", "selectFromServedFront", "selectForUnservedFront", "unservedFrontTarget":
t.Errorf("%s: %s referenced outside a function", fset.Position(id.Pos()), id.Name)
}
}
diff --git a/internal/edge/pagecache.go b/internal/edge/pagecache.go
index 5c8c64359..e981c616f 100644
--- a/internal/edge/pagecache.go
+++ b/internal/edge/pagecache.go
@@ -158,10 +158,10 @@ func (e *Edge) clearPages(owner registry.Route, host string, c pageindex.Clear)
x.Clear(routeScope(owner), c)
}
-// instanceHosts is instanceHostsIn over one snapshot of the live table
-// (purgeBridge).
+// instanceHosts is instanceHostsIn over one snapshot of the live table, its
+// remote path map included (purgeBridge).
func (e *Edge) instanceHosts(owner registry.Route, cached []string) []string {
- return instanceHostsIn(e.current(), owner, cached)
+ return instanceHostsIn(e.purgeTable(), owner, cached)
}
// ownsHost is purgeOwnsIn over one snapshot of the live table, its remote
@@ -170,18 +170,25 @@ func (e *Edge) ownsHost(owner registry.Route, host string) bool {
return e.purgeOwnsIn(e.purgeTable(), owner, host)
}
-// instanceHostsIn is every host m serves as owner's instance, sorted: each
-// key whose entry is that instance (the primary and every alias), and each
-// host in cached that m resolves (lookupIn) to that instance. The second
-// half finds the names served through a wildcard alias, which the LRU keys
-// by the name asked for and m cannot list; lookupIn lets an exact key win
-// over the wildcard, so a name another instance's route takes is never
-// among them. A wildcard key itself is a routing pattern, not a name
-// anything is cached under, and is left out. Nil when owner has no host.
-func instanceHostsIn(m map[string]upstream, owner registry.Route, cached []string) []string {
- if owner.Host == "" {
+// instanceHostsIn is every host t serves as owner's instance, sorted: each
+// key of t.hosts whose entry is that instance (the primary and every alias),
+// and each host in cached that t.hosts resolves (lookupIn) to that instance.
+// The second half finds the names served through a wildcard alias, which
+// the LRU keys by the name asked for and t.hosts cannot list; lookupIn lets
+// an exact key win over the wildcard, so a name another instance's route
+// takes is never among them. A wildcard key itself is a routing pattern, not
+// a name anything is cached under, and is left out. Nil when owner has no
+// host.
+//
+// A name another account's claim path-routes here (otherAccountFront) is
+// never among them either, though a catch-all of owner's resolves it: that
+// account's case 3 target has served it here (spec §7.2), and the catch-all
+// holds no authority over its pages.
+func instanceHostsIn(t *routeTable, owner registry.Route, cached []string) []string {
+ if owner.Host == "" || t == nil {
return nil
}
+ m := t.hosts
set := map[string]bool{}
for h, up := range m {
if !strings.HasPrefix(h, "*") && sameInstance(up.route, owner) {
@@ -189,7 +196,7 @@ func instanceHostsIn(m map[string]upstream, owner registry.Route, cached []strin
}
}
for _, h := range cached {
- if up, ok := lookupIn(m, h); ok && sameInstance(up.route, owner) {
+ if up, ok := lookupIn(m, h); ok && sameInstance(up.route, owner) && !otherAccountFront(t, h, owner) {
set[h] = true
}
}
diff --git a/internal/edge/purge_across_test.go b/internal/edge/purge_across_test.go
index bb615ab94..84e8c89d6 100644
--- a/internal/edge/purge_across_test.go
+++ b/internal/edge/purge_across_test.go
@@ -12,6 +12,8 @@ package edge
import (
"net/http"
"net/http/httptest"
+ "slices"
+ "sync/atomic"
"testing"
"github.com/carlosframework/platform/internal/registry"
@@ -292,3 +294,159 @@ func TestPurgeAcrossOwnsHostFromOneTable(t *testing.T) {
t.Fatal("ownsHost still grants the front after its prefix moved")
}
}
+
+// ---- a front shadowed here by a catch-all (fix round 1) --------------------
+//
+// The catch-all *.example.com of the route wild.example.net also answers
+// hub.example.com here, while hub.example.com's claim (account bae) sends
+// /acme to tenant.example.com here (case 3). Both tenancies would share one
+// LRU namespace, hub.example.com. The rule: a case 3 response uses the LRU
+// under a front only while nothing here answers that front
+// (caseThreeCacheable), and a catch-all never purges a name another
+// account's claim path-routes here (otherAccountFront).
+
+const shadowWild = "wild.example.net"
+
+// shadowBackend answers a GET with a cacheable body, counting them, and a
+// POST with the purge declaration set on it.
+type shadowBackend struct {
+ gets atomic.Int64
+ purgeHost, purge string
+}
+
+func (b *shadowBackend) ServeHTTP(w http.ResponseWriter, r *http.Request) {
+ if r.Method == http.MethodGet {
+ b.gets.Add(1)
+ w.Header().Set("Cache-Control", "public, max-age=600")
+ w.Write([]byte(r.Host + r.URL.Path))
+ return
+ }
+ if b.purgeHost != "" {
+ w.Header().Set("Carlos-Purge-Host", b.purgeHost)
+ }
+ w.Header().Set("Carlos-Purge", b.purge)
+ w.WriteHeader(http.StatusOK)
+}
+
+// shadowScene is purgeAcrossScene's box plus the catch-all route
+// wild.example.net of wildAccount (app "wild"), aliased as *.example.com.
+func shadowScene(t *testing.T, wildAccount string) (s *selScene, e *Edge, tenant, wild *shadowBackend) {
+ t.Helper()
+ tenant, wild = &shadowBackend{}, &shadowBackend{}
+ tsrv, wsrv := httptest.NewServer(tenant), httptest.NewServer(wild)
+ t.Cleanup(tsrv.Close)
+ t.Cleanup(wsrv.Close)
+ s = newSelScene(t)
+ s.instance(selTarget, selAcct, "tenant", func(r *registry.Route) { r.Addr = tsrv.Listener.Addr().String() })
+ s.instance(shadowWild, wildAccount, "wild", func(r *registry.Route) { r.Addr = wsrv.Listener.Addr().String() })
+ if err := s.reg.UpsertAlias(registry.RouteAlias{Alias: "*.example.com", Host: shadowWild, Account: wildAccount, App: "wild"}); err != nil {
+ t.Fatal(err)
+ }
+ s.path(selFront, "acme", selTarget, selAcct, "hub")
+ s.publish("betty", selFront, frontRow(selAcct, "hub"))
+ e = s.build()
+ e.EnableCache(NewCache(DefaultCacheMaxEntries, DefaultCacheMaxBodyBytes))
+ got := s.sel(selFront, "/acme/page")
+ wantLocal(t, got, selTarget, "acme", selTarget)
+ if got.Up.front == nil {
+ t.Fatalf("fixture: %s is not a case 3 selection", describeSel(got))
+ }
+ if !s.sel(selFront, "/other").OK || s.sel(selFront, "/other").Up.route.Host != shadowWild {
+ t.Fatalf("fixture: the catch-all does not answer %s", selFront)
+ }
+ return s, e, tenant, wild
+}
+
+func getAcross(t *testing.T, e *Edge, host, urlPath string) {
+ t.Helper()
+ rec := httptest.NewRecorder()
+ e.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "http://"+host+urlPath, nil))
+ if rec.Code != http.StatusOK {
+ t.Fatalf("GET %s%s = %d, want 200; body %q", host, urlPath, rec.Code, rec.Body.String())
+ }
+}
+
+// A case 3 response under a shadowed front is neither stored nor answered
+// from the LRU, and its target's purge of that front is a no-op: the
+// catch-all's entry under the same name stays. Once the catch-all is gone,
+// case 3 responses cache and purge again.
+func TestPurgeAcrossShadowedFrontNotStored(t *testing.T) {
+ s, e, tenant, wild := shadowScene(t, selOther)
+
+ getAcross(t, e, selFront, "/acme/page")
+ getAcross(t, e, selFront, "/acme/page")
+ if n := tenant.gets.Load(); n != 2 {
+ t.Fatalf("the target saw %d GETs for a shadowed front, want 2 (never answered from the LRU)", n)
+ }
+ wantCached(t, e, selFront, "/acme/page", "", false, "a case 3 response was stored under a front a catch-all shadows")
+
+ getAcross(t, e, selFront, "/other")
+ getAcross(t, e, selFront, "/other")
+ if n := wild.gets.Load(); n != 1 {
+ t.Fatalf("control: the catch-all saw %d GETs, want 1 (its own answer is cached)", n)
+ }
+ wantCached(t, e, selFront, "/other", "", true, "control: the catch-all's answer is cached under the front")
+
+ tenant.purgeHost, tenant.purge = selFront, "*"
+ postAcross(t, e, selFront, "/acme/admin")
+ wantCached(t, e, selFront, "/other", "", true, "the case 3 target purged a shadowed front, the catch-all's entry included")
+
+ if err := s.reg.DeleteAlias("*.example.com"); err != nil {
+ t.Fatal(err)
+ }
+ if err := e.Reload(); err != nil {
+ t.Fatal(err)
+ }
+ getAcross(t, e, selFront, "/acme/page")
+ getAcross(t, e, selFront, "/acme/page")
+ if n := tenant.gets.Load(); n != 3 {
+ t.Fatalf("after the catch-all left, the target saw %d GETs, want 3 (the second answered from the LRU)", n)
+ }
+ wantCached(t, e, selFront, "/acme/page", "", true, "an unshadowed case 3 response was not stored")
+ postAcross(t, e, selFront, "/acme/admin")
+ wantCached(t, e, selFront, "/acme/page", "", false, "the target could not purge its page under an unshadowed front")
+}
+
+// A catch-all never purges a name another account's claim path-routes here:
+// not by Carlos-Purge-Host (a path, "*" or "all") and not by "all"'s
+// expansion over the names it answers, on the response header or the
+// control channel. A catch-all of the claim's own account keeps today's
+// authority. The claim's entries are seeded: one stored before the
+// catch-all appeared (a shadowed front stores none, as above).
+func TestPurgeAcrossCatchAllCannotPurgeAnotherAccountsFront(t *testing.T) {
+ for _, tc := range []struct {
+ name, purgeHost, purge string
+ }{
+ {"named path", selFront, "/acme/events/42"},
+ {"named *", selFront, "*"},
+ {"named all", selFront, "all"},
+ {"own all", "", "all"},
+ } {
+ for _, account := range []string{selOther, selAcct} {
+ t.Run(tc.name+"/"+account, func(t *testing.T) {
+ _, e, _, wild := shadowScene(t, account)
+ seed(t, e.cache, selFront, "/acme/events/42", "", "the claim's page")
+ wild.purgeHost, wild.purge = tc.purgeHost, tc.purge
+
+ postAcross(t, e, shadowWild, "/write")
+
+ same := account == selAcct
+ wantCached(t, e, selFront, "/acme/events/42", "", !same, "a catch-all's purge authority over a path-routed name follows the claim's account")
+ })
+ }
+ }
+ for _, account := range []string{selOther, selAcct} {
+ t.Run("control channel/"+account, func(t *testing.T) {
+ _, e, _, _ := shadowScene(t, account)
+ seed(t, e.cache, selFront, "/acme/events/42", "", "the claim's page")
+ wild := e.current()[shadowWild].route
+ got := slices.Contains(e.instanceHosts(wild, e.cache.cachedHosts()), selFront)
+ if same := account == selAcct; got != same {
+ t.Fatalf("instanceHosts lists %s = %v, want %v", selFront, got, same)
+ }
+ if got := e.ownsHost(wild, selFront); got != (account == selAcct) {
+ t.Fatalf("ownsHost(catch-all, %s) = %v", selFront, got)
+ }
+ })
+ }
+}
diff --git a/internal/edge/purge_header.go b/internal/edge/purge_header.go
index cdef5df0a..165ba87b4 100644
--- a/internal/edge/purge_header.go
+++ b/internal/edge/purge_header.go
@@ -528,8 +528,7 @@ func (e *Edge) resolvePurgeOwnersIn(t *routeTable, rt registry.Route, public str
// the name listed here. An exact key, another tenant's included,
// still wins over the wildcard, and either way the route found is the
// one judged below.
- up, ok := lookupIn(cur, host)
- if !ok {
+ if caseThreeCacheable(t, host) {
// A front host this box does not serve (path routes across
// boxes, case 3): rt's pages under it are rt's own while a
// prefix of it selects rt here, judged in t. Anything else,
@@ -542,6 +541,7 @@ func (e *Edge) resolvePurgeOwnersIn(t *routeTable, rt registry.Route, public str
unserved = append(unserved, host)
continue
}
+ up, _ := lookupIn(cur, host)
if !servingChecked {
// The SERVING side is a value captured when this proxy was built,
// and a reload can land between that capture and this response —
@@ -566,6 +566,15 @@ func (e *Edge) resolvePurgeOwnersIn(t *routeTable, rt registry.Route, public str
}
servingChecked = true
}
+ if otherAccountFront(t, host, rt) {
+ // Served here only through a catch-all, while another account's
+ // claim path-routes it: that account's pages may be cached
+ // under it (spec §7.2), and a catch-all never purges them.
+ e.logPurgeIssue("override-tenancy", rt.Host, fmt.Sprintf(
+ "%s (account %q) declared Carlos-Purge-Host %q, which another account's path routes send to this box — a purge never crosses an account boundary; the whole declaration is ignored",
+ rt.Host, rt.Account, truncateForLog(host)))
+ return nil, false
+ }
if !purgeHostInTenancy(rt, up) {
e.logPurgeIssue("override-tenancy", rt.Host, fmt.Sprintf(
"%s (app %q, account %q) declared Carlos-Purge-Host %q, which belongs to app %q, account %q — a purge crosses an app boundary only when %q path-routes to the serving route, and never crosses an account boundary; the whole declaration is ignored",
@@ -801,7 +810,7 @@ func (e *Edge) applyPurgeTargets(t *routeTable, from string, hosts, pageHosts []
if e.purgeOwnsIn(t, p.owner, p.host) {
e.cache.PurgeHost(p.host)
}
- for _, h := range instanceHostsIn(t.hosts, p.owner, cached) {
+ for _, h := range instanceHostsIn(t, p.owner, cached) {
if h != p.host {
e.cache.PurgeHost(h)
}
@@ -876,20 +885,28 @@ func (e *Edge) applyPurgeTargets(t *routeTable, from string, hosts, pageHosts []
// by a response or a control listener built before the move, leaves its LRU
// entries alone.
//
-// A host t.hosts does not resolve at all is owner's only as a front another
-// box serves whose prefix selects owner here (frontPathOwns, spec §7.2).
-// Every host this box resolves, through a catch-all included, is judged as
-// before path routes crossed boxes.
+// A host t.hosts does not resolve at all (caseThreeCacheable) is owner's
+// only as a front another box serves whose prefix selects owner here
+// (frontPathOwns, spec §7.2). A host t.hosts resolves only through a
+// catch-all while another account's claim path-routes it is never owner's
+// unless owner is in that account (otherAccountFront). Every other host is
+// judged as before path routes crossed boxes.
func (e *Edge) purgeOwnsIn(t *routeTable, owner registry.Route, host string) bool {
if t == nil {
return false
}
m := t.hosts
- up, ok := lookupIn(m, host)
- if !ok {
+ if caseThreeCacheable(t, host) {
rm, mapped := t.remote[host]
return mapped && e.frontPathOwns(m, rm, owner, host)
}
+ if otherAccountFront(t, host, owner) {
+ return false
+ }
+ up, ok := lookupIn(m, host)
+ if !ok {
+ return false
+ }
if sameInstance(up.route, owner) {
return true
}
@@ -945,6 +962,36 @@ func (e *Edge) frontPathOwns(hosts map[string]upstream, rm remotePathMap, owner
return false
}
+// caseThreeCacheable reports whether nothing on this box answers host in t:
+// no exact entry and no catch-all (lookupIn misses). It is the one rule for
+// a front host's cache namespace (spec §7.2): a case 3 response (up.front
+// set) is looked up in and stored to the response LRU under host only then
+// (ServeHTTP), and a case 3 target purges host only then (purgeOwnsIn,
+// resolvePurgeOwnersIn). So a front shadowed here by a catch-all never
+// shares its LRU entries between the catch-all's tenancy and the target's:
+// the target stores nothing under it and purges nothing under it. Nil t (no
+// table yet) answers false.
+func caseThreeCacheable(t *routeTable, host string) bool {
+ if t == nil {
+ return false
+ }
+ _, served := lookupIn(t.hosts, host)
+ return !served
+}
+
+// otherAccountFront reports whether a claim of another account than owner's
+// path-routes host from this box: t.remote holds host's map, whose account
+// is not release.RouteAccount(owner). The remote map never holds a name this
+// box serves exactly (buildRemotePaths), so host is answered here, if at
+// all, through a catch-all, and that catch-all holds no purge authority over
+// it: not by Carlos-Purge-Host, "*" or "all" (purgeOwnsIn,
+// resolvePurgeOwnersIn, instanceHostsIn). A catch-all of the claim's own
+// account keeps today's authority.
+func otherAccountFront(t *routeTable, host string, owner registry.Route) bool {
+ rm, ok := t.remote[host]
+ return ok && rm.account != release.RouteAccount(owner)
+}
+
// purgeTable is the published routing snapshot, or an empty one before the
// first Reload, so a purge judged then finds nothing to own.
func (e *Edge) purgeTable() *routeTable {