carlos / platform Public

edge: path ownership judges the front's name; catch-alls (#186)

Task 8 review fixes (Astra A-P1-1, A-P1-2, A-P2; Opus O-I1).

The selection carries the request Host's own entry (Front/FrontOK).
The ownership decision is one side-effect-free verdict
(nameOwnerVerdict) that judges the name by that entry, never by a
path-selected target's, so an attached alias whose prefix selects a
local target still yields to another account's exact owner.
serveNameOwner acts on the verdict. An upgrade's re-check
(upgradeSelection) requires the same verdict, so a re-owned front
cannot keep or win a reservation through a local catch-all's tenancy.

A host served here only through a catch-all while a remote map names
it exactly is a front this box does not serve: case 3 runs for it
ahead of the catch-all, whose answer stays on a miss or refusal. A
verified hop whose remote answer is for such a front is refused.

The race on the between-checks hook goes through a channel.
lookupUpstreamPath moves to a test file (no production caller), and
ServeHTTP's code-health record drops from 417 to 385.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev a9ee50c8f7fb2c348ab31686d3921477680d853c parent 269e277
8 files changed, +367 −114
  • .amadan/code-health.tsv +1 −1
  • internal/edge/edge.go +1 −12
  • internal/edge/linkpin_test.go +6 −4
  • internal/edge/path_across_e2e_test.go +155 −11
  • internal/edge/pathhop.go +87 −41
  • internal/edge/pathselect.go +76 −36
  • internal/edge/pathselect_test.go +37 −7
  • internal/edge/upgrades.go +4 −2
diff --git a/.amadan/code-health.tsv b/.amadan/code-health.tsv
index f864db29b..444e972f7 100644
--- a/.amadan/code-health.tsv
+++ b/.amadan/code-health.tsv
@@ -2186,7 +2186,7 @@ func-lines internal/console (*Store).migrate - 335
func-lines internal/console TestDemoProtectionsSourceToLimitAndTakedown - 412
func-lines internal/console TestDemoSourceToLiveAndUpdate - 383
func-lines internal/console TestHAP2ProductionDoors - 304
-func-lines internal/edge (*Edge).ServeHTTP - 417
+func-lines internal/edge (*Edge).ServeHTTP - 385
func-lines internal/edge TestDemoAPIAliasBoundary - 360
func-lines internal/edge TestRegionalNameNeverLeavesItsRegion - 330
func-lines internal/edge TestTunnelWebSocketEndToEnd - 327
diff --git a/internal/edge/edge.go b/internal/edge/edge.go
index b9479bdd7..23203c5a4 100644
--- a/internal/edge/edge.go
+++ b/internal/edge/edge.go
@@ -1887,17 +1887,6 @@ func (e *Edge) lookupUpstream(host string) (upstream, bool) {
return e.lookupDemoAware(e.current(), host)
}
-// lookupUpstreamPath resolves a hostname and path on this box alone: pathMatch's
-// up/ok over the current map (pathselect.go holds the matcher and its
-// reasoning). Never a request's selection, which is selectUpstream's
-// (TestSelectUpstreamIsTheOnlySite). No production code calls it: the
-// upgrade re-checks select as their request did (upgradeSelection); tests use
-// it to read the local map.
-func (e *Edge) lookupUpstreamPath(host, urlPath string) (upstream, bool) {
- up, ok, _, _, _ := e.pathMatch(e.current(), host, urlPath)
- return up, ok
-}
-
// LinkedTarget is what a home deployment's link handler (internal/host,
// linked deployments spec §6.3) pins a request to: the published app's
// home route Host, its home Account and App, and PublicHost, the name the
@@ -2381,7 +2370,7 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// never an alias or a catch-all (pinnedUpstream), and it is never
// backhauled: skipped outright, so a reload between the selection and
// servesExactly cannot send it to another box.
- if ok && !pinned && e.serveNameOwner(w, r, host, up, hopped, isDoor) {
+ if ok && !pinned && e.serveNameOwner(w, r, host, sel, hopped, isDoor) {
return
}
diff --git a/internal/edge/linkpin_test.go b/internal/edge/linkpin_test.go
index fcfb94f4c..59b230286 100644
--- a/internal/edge/linkpin_test.go
+++ b/internal/edge/linkpin_test.go
@@ -29,6 +29,8 @@ import (
// The functions allowed to call lookupUpstream or lookupUpstreamPath, and
// why none of them selects where a request goes. selectUpstream calls
// neither: it selects through pathMatch (selectionEntrySites).
+// lookupUpstreamPath is a test helper now (pathselect_test.go), so no
+// production function may call it.
var lookupNonRequestSites = map[string]string{
"httpFallbackHandler": "the :80 listener asks only whether this box serves a hostname before redirecting it to https; it never dispatches",
"demoTrafficAllowed": "re-reads the row of a demo route already selected, by that route's own host",
@@ -39,8 +41,7 @@ var lookupNonRequestSites = map[string]string{
// fails, and so does an entry that no longer calls.
var selectionEntrySites = map[string]map[string]string{
"pathMatch": {
- "selectUpstream": "THE selection: the pinned branch first, then pathMatch on the local map",
- "lookupUpstreamPath": "the compatibility wrapper, pathMatch's up/ok; its own callers are restricted to lookupNonRequestSites",
+ "selectUpstream": "THE selection: the pinned branch first, then pathMatch on the local map",
},
"selectAcross": {
"selectUpstream": "cases 2 and 3 run only inside the one selection, after the pinned branch and pathMatch, where the arrived-hop rule is settled",
@@ -84,8 +85,9 @@ var mapReadSites = map[string]map[string]string{
// The purge owner check for a front this box does not serve
// (purgeOwnsIn, resolvePurgeOwnersIn) joins when it reads the map.
"routeTable.remote": {
- "selectAcross": "case 3 of the one selection, reached only from selectUpstream",
- "Reload": "the one publisher of the table (TestReloadIsTheOnlyPublisher) sets it beside hosts",
+ "selectAcross": "case 3 of the one selection, reached only from selectUpstream",
+ "selectUpstream": "THE selection: whether a host it answers only through a catch-all has a remote map, so case 3 runs for it",
+ "Reload": "the one publisher of the table (TestReloadIsTheOnlyPublisher) sets it beside hosts",
},
}
diff --git a/internal/edge/path_across_e2e_test.go b/internal/edge/path_across_e2e_test.go
index 9a17369be..e3a75a9d0 100644
--- a/internal/edge/path_across_e2e_test.go
+++ b/internal/edge/path_across_e2e_test.go
@@ -664,28 +664,34 @@ func TestPathAcrossFrontOwnerChangedBetweenChecks(t *testing.T) {
} {
t.Run(tc.name, func(t *testing.T) {
s := newPathAcrossScene(t)
- var hookErr error
- ran := false
+ // The hook runs on B's handler goroutine; its result reaches the
+ // test through the channel.
+ hooked := make(chan error, 1)
adm := &acrossAdmission{open: true}
adm.between = func() {
- ran = true
if tc.entry == "" {
delete(s.files["annie"], acrossHub)
} else {
s.files["annie"][acrossHub] = tc.entry
}
- if _, etag, err := s.store.Get(context.Background(), "control/edges/annie.json"); err != nil {
- hookErr = err
- } else if _, err := s.store.Put(context.Background(), "control/edges/annie.json", []byte(s.fileBody(s.hub)), etag); err != nil {
- hookErr = err
- } else {
- hookErr = s.tenant.homes.Refresh(context.Background())
+ _, etag, err := s.store.Get(context.Background(), "control/edges/annie.json")
+ if err == nil {
+ _, err = s.store.Put(context.Background(), "control/edges/annie.json", []byte(s.fileBody(s.hub)), etag)
+ }
+ if err == nil {
+ err = s.tenant.homes.Refresh(context.Background())
}
+ hooked <- err
}
s.tenant.edge.admission = adm
res := s.get(t, s.tenant, acrossHub, "/acme/x")
- if !ran || hookErr != nil {
- t.Fatalf("the republish between the checks did not run cleanly: ran=%v err=%v", ran, hookErr)
+ select {
+ case err := <-hooked:
+ if err != nil {
+ t.Fatalf("the republish between the checks failed: %v", err)
+ }
+ default:
+ t.Fatal("the republish between the checks never ran")
}
res.want(t, tc.code, tc.body)
s.tenantApp.wantSeen(t)
@@ -696,6 +702,64 @@ func TestPathAcrossFrontOwnerChangedBetweenChecks(t *testing.T) {
}
}
+// Review A-P1-1: the attached-alias counterpart of the catch-all case. An
+// exact attached alias other.example of B's prim.example, whose prefix
+// selects a target on B in the alias's own account, while C publishes an
+// exact route for other.example in another account. The ownership block
+// judges the name by the alias (the front's own entry), not by the selected
+// target, so the request goes to C, as it does without a prefix.
+func TestPathAcrossAttachedAliasDoesNotBeatExactName(t *testing.T) {
+ s := newPathAcrossScene(t)
+ const prim, other = "prim.example", "other.example"
+ s.ca.publish(t, s.store, other)
+ primApp := newAcrossApp(t, "prim-1")
+ otherApp := newAcrossApp(t, "other-1")
+ s.route(t, s.tenant, acrossInstance(prim, acrossAcct, "prim", primApp.addr()))
+ if err := s.tenant.reg.UpsertAlias(registry.RouteAlias{Alias: other, Host: prim, Account: acrossAcct, App: "prim"}); err != nil {
+ t.Fatal(err)
+ }
+ s.path(t, s.tenant, prim, acrossPrefix, acrossTenant, acrossAcct, "prim")
+ if up := s.tenant.edge.current()[other]; !up.alias || up.paths[acrossPrefix] != acrossTenant {
+ t.Fatalf("fixture: %s is not an attached alias carrying the prefix map", other)
+ }
+ s.route(t, s.third, acrossInstance(other, acrossOther, "other", otherApp.addr()))
+ s.publish(t, s.third, other, acrossRouteEntry(acrossOther, "other", 5))
+
+ s.get(t, s.tenant, other, "/acme/x").want(t, http.StatusOK, "other-1|"+other+"|/acme/x")
+ s.get(t, s.tenant, other, "/plain").want(t, http.StatusOK, "other-1|"+other+"|/plain")
+ s.tenantApp.wantSeen(t)
+ primApp.wantSeen(t)
+}
+
+// Review O-I1: a catch-all on the target's box does not shadow the front's
+// remote map. B holds *.example (another account's app); a request for
+// hub.example/acme, as a client's or as A's hop, still reaches the target,
+// and a request with no prefix goes where it did (the front's home).
+func TestPathAcrossCatchAllOnTargetBox(t *testing.T) {
+ s := newPathAcrossScene(t)
+ const wild = "wild.example"
+ wildApp := newAcrossApp(t, "wild-1")
+ s.route(t, s.tenant, acrossInstance(wild, acrossOther, "wild", wildApp.addr()))
+ if err := s.tenant.reg.UpsertAlias(registry.RouteAlias{Alias: "*.example", Host: wild, Account: acrossOther, App: "wild"}); err != nil {
+ t.Fatal(err)
+ }
+ s.reload(t, s.tenant)
+ if _, ok := s.tenant.edge.current()["*.example"]; !ok {
+ t.Fatal("fixture: B has no catch-all")
+ }
+
+ s.get(t, s.tenant, acrossHub, "/acme/client").want(t, http.StatusOK, "tenant-1|"+acrossHub+"|/acme/client")
+ s.hop(t, s.tenant, acrossHub, "/acme/hop", false).want(t, http.StatusOK, "tenant-1|"+acrossHub+"|/acme/hop")
+ s.get(t, s.hub, acrossHub, "/acme/via-a").want(t, http.StatusOK, "tenant-1|"+acrossHub+"|/acme/via-a")
+ s.get(t, s.tenant, acrossHub, "/other").want(t, http.StatusOK, "hub-1|"+acrossHub+"|/other")
+ wildApp.wantSeen(t)
+ s.tenantApp.wantSeen(t,
+ acrossSeen{acrossHub, "/acme/client"}, acrossSeen{acrossHub, "/acme/hop"}, acrossSeen{acrossHub, "/acme/via-a"})
+ if n := s.dialsFrom(s.tenant, acrossIPHub); n != 1 {
+ t.Fatalf("B dialled A %d times (only the unprefixed request goes there), want 1", n)
+ }
+}
+
// Spec §5.5: the page cache's exclusion of path-routed hosts holds for case
// 3. The target serves the front's request every time and nothing is
// stored, while the target's own host is cached as usual.
@@ -773,3 +837,83 @@ func TestPathAcrossUpgradeCase3Reserved(t *testing.T) {
t.Fatalf("reserve after the owner changed: %v, want errTenancyStale", err)
}
}
+
+// Review A-P1-2: an upgrade's re-check judges the front name's authority as
+// well as the tenancy. The front's published owner moves to another account
+// and a reload installs a local catch-all pointing at the target, which
+// would lend the re-selection the target's tenancy; an ordinary request
+// would yield to the new exact owner, so the reservation is refused (the
+// transition inside beforeReserve) and an existing one is swept.
+func TestPathAcrossUpgradeFrontAuthority(t *testing.T) {
+ const front, target = "hub.tunnel.example", "t.example.com"
+ ten := tenancy{ArmTunnel, fwdOther, "blog", "", "t1", ""}
+ adm := upgradeAdmission{Host: front, Path: "/acme/cable", Tenancy: ten}
+ // scene is a hub serving the target over a tunnel, with the front's
+ // remote map and the front published for the target's account; move
+ // republishes the front for another account and installs the catch-all,
+ // and its Reload runs the sweep.
+ scene := func(t *testing.T) (hub *Edge, tc *tunnelConn, move func() error) {
+ _, hub, _ = forwardHubAndBox(t, []registry.Route{
+ {Host: target, Kind: "instance", Status: "ready", Tunnel: "t1", Account: fwdOther, App: "blog"},
+ }, nil, namedHandler("box-1"))
+ remotePathRow(t, hub.reg, front, acrossPrefix, target, fwdOther, "hub")
+ if err := hub.Reload(); err != nil {
+ t.Fatal(err)
+ }
+ m := newHomeMap(meshTestStore(t), "annie")
+ putPathFile(t, m, "betty", "2.2.2.2", map[string]string{front: frontRow(fwdOther, "hub")})
+ refreshHomeMap(t, m)
+ hub.homes.Store(m)
+ if tc = hub.tunnels.channel("home/box-1"); tc == nil {
+ t.Fatal("home/box-1 has no channel connection")
+ }
+ otherFile := fmt.Sprintf(`{"v":1,"box":"betty","eip":"2.2.2.2","hosts":{%q:%s}}`, front, frontRow(fwdOwner, "other"))
+ move = func() error {
+ _, etag, err := m.s.Get(context.Background(), "control/edges/betty.json")
+ if err != nil {
+ return err
+ }
+ if _, err := m.s.Put(context.Background(), "control/edges/betty.json", []byte(otherFile), etag); err != nil {
+ return err
+ }
+ if err := m.Refresh(context.Background()); err != nil {
+ return err
+ }
+ if err := hub.reg.UpsertAlias(registry.RouteAlias{Alias: "*.tunnel.example", Host: target, Account: fwdOther, App: "blog"}); err != nil {
+ return err
+ }
+ return hub.Reload()
+ }
+ return hub, tc, move
+ }
+
+ t.Run("sweep", func(t *testing.T) {
+ hub, tc, move := scene(t)
+ r, _, err := hub.reserveUpgrade(context.Background(), adm, tc, 10)
+ if err != nil {
+ t.Fatalf("reserve before the move: %v, want admitted", err)
+ }
+ defer r.release(relBody)
+ if err := move(); err != nil {
+ t.Fatal(err)
+ }
+ if up, ok := hub.lookupUpstreamPath(front, "/acme/cable"); !ok || up.tenancy != ten {
+ t.Fatalf("fixture: the catch-all does not lend the front the target's tenancy (%v %+v)", ok, up.tenancy)
+ }
+ if n := heldFor(hub, front); n != 0 {
+ t.Fatalf("after the move and its reload sweep: %d held, want 0", n)
+ }
+ })
+ t.Run("beforeReserve", func(t *testing.T) {
+ hub, tc, move := scene(t)
+ moved := make(chan error, 1)
+ hub.open.beforeReserve = func() { moved <- move() }
+ _, _, err := hub.reserveUpgrade(context.Background(), adm, tc, 10)
+ if merr := <-moved; merr != nil {
+ t.Fatal(merr)
+ }
+ if !errors.Is(err, errTenancyStale) {
+ t.Fatalf("reserve across the move: %v, want errTenancyStale", err)
+ }
+ })
+}
diff --git a/internal/edge/pathhop.go b/internal/edge/pathhop.go
index b3775bb6a..b56ad7f65 100644
--- a/internal/edge/pathhop.go
+++ b/internal/edge/pathhop.go
@@ -101,27 +101,46 @@ func (e *Edge) frontMarkHolds(host string, up upstream) bool {
return ok && fi.Front && fi.Account == f.Account && fi.App == f.App && fi.Stamp == f.Stamp && fi.Incarnation == f.Incarnation
}
-// serveNameOwner is ServeHTTP's ownership block: a selected upstream reached
-// through a name this box does not serve exactly (a catch-all, or a case 3
-// target), or through an attached alias, is resolved against peer ownership
-// before it is served. It reports whether it answered. A held exact name
-// refuses even a signed hop; a hop or door request never starts another
-// backhaul here.
+// ownerVerdict is the ownership block's decision for a request's name.
+type ownerVerdict int
+
+const (
+ // ownerLocal: serve the selection here.
+ ownerLocal ownerVerdict = iota
+ // ownerHeld: the exact name is held; refused (503), even on a hop.
+ ownerHeld
+ // ownerElsewhere: another box owns the name (the resolution says which).
+ ownerElsewhere
+ // ownerRefused: a case 3 target whose front mark no longer holds and
+ // whose name no other box owns; refused (503), never served unchecked.
+ ownerRefused
+)
+
+// nameOwnerVerdict is the ownership block's decision, with no side effect:
+// ServeHTTP acts on it (serveNameOwner), and an admitted upgrade's re-check
+// requires ownerLocal (upgradeSelection), so both judge the name alike.
//
-// The one exception (spec §5.5): a case 3 target whose front mark still holds
-// (frontMarkHolds) is served here without it. A case 3 target whose mark no
-// longer holds (the front was re-owned or re-claimed since selection) runs the
-// block as any name this box does not serve, and is never served here
-// unchecked: when the block finds no other owner, the 503 a known remote
-// owner gets, so the next request selects again.
-func (e *Edge) serveNameOwner(w http.ResponseWriter, r *http.Request, host string, up upstream, hopped, isDoor bool) bool {
+// The NAME is judged by the request Host's own entry here (sel.Front: an
+// exact route, an attached alias, a catch-all), never by a path-selected
+// target's entry, which would hide the front's alias class. A name this box
+// does not serve exactly, or reaches through an attached alias, is resolved
+// against peer ownership. The one exception (spec §5.5): a case 3 target
+// whose front mark still holds (frontMarkHolds) is served here without it;
+// one whose mark no longer holds (the front was re-owned or withdrawn since
+// selection) is always resolved, and refused when no other box owns it.
+func (e *Edge) nameOwnerVerdict(host string, sel requestSelection, hopped, isDoor bool) (ownerVerdict, edgesfile.NameResolution) {
+ up := sel.Up
if e.frontMarkHolds(host, up) {
- return false
+ return ownerLocal, edgesfile.NameResolution{}
+ }
+ id := up
+ if sel.FrontOK {
+ id = sel.Front
}
exact := e.servesExactly(host)
- attached := up.alias && !up.platformAlias
+ attached := id.alias && !id.platformAlias
if up.front == nil && exact && !attached {
- return false
+ return ownerLocal, edgesfile.NameResolution{}
}
name := host
if !exact {
@@ -133,36 +152,51 @@ func (e *Edge) serveNameOwner(w http.ResponseWriter, r *http.Request, host strin
if attached {
class = edgesfile.NameAlias
}
- local := edgesfile.NameClaim{Name: name, Class: class, Account: registry.EffectiveAccount(up.route.Account), App: up.route.App,
- Stamp: int64(up.route.ClaimStamp), Incarnation: up.route.Incarnation, ServingSeq: up.route.ServingSeq, Eligible: true, Local: true}
+ local := edgesfile.NameClaim{Name: name, Class: class, Account: registry.EffectiveAccount(id.route.Account), App: id.route.App,
+ Stamp: int64(id.route.ClaimStamp), Incarnation: id.route.Incarnation, ServingSeq: id.route.ServingSeq, Eligible: true, Local: true}
if class == edgesfile.NameAlias {
- local.TargetHost = up.route.Host
+ local.TargetHost = id.route.Host
}
resolved := e.homes.Load().resolveName(host, []edgesfile.NameClaim{local}, false)
- if resolved.State == edgesfile.NameHeld {
+ switch {
+ case resolved.State == edgesfile.NameHeld:
+ return ownerHeld, resolved
+ case resolved.State == edgesfile.NameDialable && !resolved.Serving.Local && (resolved.AdmissionAware || (!hopped && !isDoor)):
+ return ownerElsewhere, resolved
+ case up.front != nil:
+ return ownerRefused, resolved
+ }
+ return ownerLocal, resolved
+}
+
+// serveNameOwner is ServeHTTP's ownership block: it acts on
+// nameOwnerVerdict and reports whether it answered. A held exact name
+// refuses even a signed hop; a hop or door request never starts another
+// backhaul here.
+func (e *Edge) serveNameOwner(w http.ResponseWriter, r *http.Request, host string, sel requestSelection, hopped, isDoor bool) bool {
+ v, resolved := e.nameOwnerVerdict(host, sel, hopped, isDoor)
+ switch v {
+ case ownerLocal:
+ return false
+ case ownerHeld, ownerRefused:
AdmissionResponse(w, r)
return true
}
- if resolved.State == edgesfile.NameDialable && !resolved.Serving.Local && (resolved.AdmissionAware || (!hopped && !isDoor)) {
- if hopped {
- e.homes.Load().staleHopRefusal(w, host)
- return true
- }
- if isDoor {
- e.unknownHost(w, r)
- return true
- }
- if e.serveAssetFar(w, r, host) || e.backhaulTo(w, r, host, resolved.Serving.Dial, resolved.Serving.Box, backhaulOwnerOf(resolved.Authority)) {
- return true
- }
- // A known remote owner must not fall through to another local app
- // when its mesh key or transport is unavailable.
- if resolved.AdmissionAware {
- AdmissionResponse(w, r)
- return true
- }
+ if hopped {
+ e.homes.Load().staleHopRefusal(w, host)
+ return true
}
- if up.front != nil {
+ if isDoor {
+ e.unknownHost(w, r)
+ return true
+ }
+ if e.serveAssetFar(w, r, host) || e.backhaulTo(w, r, host, resolved.Serving.Dial, resolved.Serving.Box, backhaulOwnerOf(resolved.Authority)) {
+ return true
+ }
+ // A known remote owner must not fall through to another local app when
+ // its mesh key or transport is unavailable, nor a case 3 target whose
+ // front has moved.
+ if resolved.AdmissionAware || sel.Up.front != nil {
AdmissionResponse(w, r)
return true
}
@@ -174,8 +208,20 @@ func (e *Edge) serveNameOwner(w http.ResponseWriter, r *http.Request, host strin
// that admitted it was, from that request's context (its pin, door caller,
// verified hop and door hop marks), so a target selected for a front this
// box does not serve (case 3) still maps to the tenancy it was admitted
-// under while its front holds. A Remote selection is no upstream here.
+// under while its front holds. A Remote selection is no upstream here, and
+// neither is one ServeHTTP's ownership block would not serve here
+// (nameOwnerVerdict): a name another box now owns, or holds, loses its
+// upgrade whatever tenancy a local catch-all would lend it.
func (e *Edge) upgradeSelection(ctx context.Context, host, urlPath string) (upstream, bool) {
sel := e.selectUpstream(new(http.Request).WithContext(ctx), host, urlPath)
- return sel.Up, sel.OK
+ if !sel.OK {
+ return upstream{}, false
+ }
+ if _, pinned := linkPinFrom(ctx); !pinned {
+ _, isDoor := doorCallerFrom(ctx)
+ if v, _ := e.nameOwnerVerdict(host, sel, hopVerified(ctx), isDoor); v != ownerLocal {
+ return upstream{}, false
+ }
+ }
+ return sel.Up, true
}
diff --git a/internal/edge/pathselect.go b/internal/edge/pathselect.go
index a0e930b48..477211638 100644
--- a/internal/edge/pathselect.go
+++ b/internal/edge/pathselect.go
@@ -54,10 +54,18 @@ type requestSelection struct {
// use for "a target was selected"; never compare Target with Up.route.Host,
// which differ for a platform-alias target.
LocalTarget bool
+ // Front/FrontOK: the request Host's own entry on this box as pathMatch's
+ // host lookup found it (exactly, or through an attached alias or a
+ // catch-all), before any prefix selected. The ownership check judges the
+ // NAME by it (nameOwnerVerdict), never by a selected target's entry,
+ // which drops the front's alias class. FrontOK is false when the Host is
+ // not served here (case 3's front).
+ Front upstream
+ FrontOK bool
- // sameBoxUp/sameBoxOK are pathMatch's own answer on the same snapshot
- // (lookupUpstreamPath's): the front's entry, or nothing when the front
- // is not served here. sameBox reads them.
+ // sameBoxUp/sameBoxOK are pathMatch's own up/ok on the same snapshot:
+ // the front's entry (or a same-box target), or nothing when the front is
+ // not served here. sameBox reads them.
sameBoxUp upstream
sameBoxOK bool
}
@@ -88,28 +96,51 @@ func (e *Edge) selectUpstream(r *http.Request, host, urlPath string) requestSele
if !ok {
return requestSelection{}
}
- return requestSelection{Up: *up, OK: true, sameBoxUp: *up, sameBoxOK: true}
+ return requestSelection{Up: *up, OK: true, Front: *up, FrontOK: true, sameBoxUp: *up, sameBoxOK: true}
}
t := e.table()
if t == nil {
return requestSelection{}
}
- up, ok, prefix, target, local := e.pathMatch(t.hosts, host, urlPath)
- if local {
- return requestSelection{Up: up, OK: true, Prefix: prefix, Target: target, LocalTarget: true, sameBoxUp: up, sameBoxOK: true}
+ up, ok, prefix, target, local, front := e.pathMatch(t.hosts, host, urlPath)
+ // shadowed: host is served here only through a catch-all, while a
+ // claim's remote map names it exactly. A front is matched exactly, never
+ // through a wildcard key (spec §5.3), so the front is not served here
+ // and case 3 runs for it ahead of the catch-all's own answer, which
+ // stays the answer whenever case 3 selects nothing.
+ _, exact := t.hosts[host]
+ _, mapped := t.remote[host]
+ shadowed := ok && !exact && mapped
+ if local && !shadowed {
+ return requestSelection{Up: up, OK: true, Prefix: prefix, Target: target, LocalTarget: true, Front: front, FrontOK: true, sameBoxUp: up, sameBoxOK: true}
}
_, door := doorCallerFrom(ctx)
- sel := e.selectAcross(t, host, urlPath, up, ok, prefix, target, door, hopDoorFrom(ctx))
+ sel := e.selectAcross(t, host, urlPath, pathHit{up: up, ok: ok, prefix: prefix, target: target, local: local, shadowed: shadowed}, door, hopDoorFrom(ctx))
sel.sameBoxUp, sel.sameBoxOK = up, ok
+ sel.Front, sel.FrontOK = front, ok
if sel.Remote != nil && hopVerified(ctx) {
// One hop, ever: the front's own entry when served here (pathMatch
- // answered it), else nothing.
+ // answered it), else nothing. A catch-all is not the front: a
+ // shadowed front's hop is refused like an unserved one.
sel.Remote = nil
- sel.Up, sel.OK = up, ok
+ sel.Up, sel.OK = up, ok && !shadowed
+ if shadowed {
+ sel.Up = upstream{}
+ }
}
return sel
}
+// pathHit is pathMatch's answer on the local map, as selectAcross reads it.
+// shadowed: host resolved only through a catch-all and has a remote map
+// (selectUpstream).
+type pathHit struct {
+ up upstream
+ ok bool
+ prefix, target string
+ local, shadowed bool
+}
+
// matchPrefix is the 09-02 matcher: cleaned first segment, then raw; cleaned wins.
//
// BOTH FORMS, cleaned first. The prefix map is consulted for the
@@ -188,11 +219,12 @@ func matchPrefix(prefixes map[string]string, urlPath string) (prefix, target str
return "", "", false
}
-// pathMatch is today's lookupUpstreamPath over one map, plus case 1's guard,
-// reporting what it matched: up/ok are exactly what lookupUpstreamPath returns
+// pathMatch is the pre-#186 lookupUpstreamPath over one map, plus case 1's
+// guard, reporting what it matched: up/ok are exactly what that lookup returned
// (the target's entry on a valid local hit, else the front's), prefix/target
// are set whenever a prefix matched, local says the TARGET's entry was answered
-// (true for a platform-alias target too, whose route.Host is its instance's).
+// (true for a platform-alias target too, whose route.Host is its instance's),
+// and front is the host's own entry, whatever the prefix selected.
// A method: the host half is e.lookupDemoAware, exactly as today, so demo
// namespace rules hold (demo_test.go's reserved-apex witness stays green).
//
@@ -245,16 +277,17 @@ func matchPrefix(prefixes map[string]string, urlPath string) (prefix, target str
// so on an ordinary request that equals rt.Host and takes resolvePurgeHosts'
// own-host branch; on a path-routed one it names the front hostname and takes
// the path-routing branch. Nothing in the app has to know which it is.
-func (e *Edge) pathMatch(m map[string]upstream, host, urlPath string) (up upstream, ok bool, prefix, target string, local bool) {
+func (e *Edge) pathMatch(m map[string]upstream, host, urlPath string) (up upstream, ok bool, prefix, target string, local bool, front upstream) {
up, ok = e.lookupDemoAware(m, host)
+ front = up
// A regional entry (#447) never selects by path: regionalGate decides
// on the request's Host, before any path is selected.
if !ok || up.paths == nil || up.regional != regionalNone {
- return up, ok, "", "", false
+ return up, ok, "", "", false, front
}
prefix, target, hit := matchPrefix(up.paths, urlPath)
if !hit {
- return up, true, "", "", false
+ return up, true, "", "", false, front
}
// m[target], not lookupIn(m, target): the TARGET is resolved exactly,
// while the host half above also accepts a one-label wildcard key. Every
@@ -272,7 +305,7 @@ func (e *Edge) pathMatch(m map[string]upstream, host, urlPath string) (up upstre
// never a route in another account than the front's. Every route kind
// stays eligible here, unlike across boxes.
if !found || (tu.alias && !tu.platformAlias) || release.RouteAccount(tu.route) != release.RouteAccount(up.route) {
- return up, true, prefix, target, false
+ return up, true, prefix, target, false, front
}
// ONE HOP, deliberately: if tu itself carries a prefix map, it is NOT
// re-resolved against this request's path. A prefix selects a route, and
@@ -280,31 +313,32 @@ func (e *Edge) pathMatch(m map[string]upstream, host, urlPath string) (up upstre
// this one's. Do not add recursion here — chaining buys nothing the
// publisher cannot express by pointing the prefix at the final target
// directly, and it would introduce a cycle to detect.
- return tu, true, prefix, target, true
+ return tu, true, prefix, target, true, front
}
// selectAcross runs cases 2 and 3 over one table; called only by selectUpstream.
-// front/frontOK and prefix/target are pathMatch's answer for host on
-// t.hosts, which did not select a local target. door: a door caller, which
-// never selects across boxes. doorHop: a door-marked hop, which never takes
-// a remote-map answer (case 3).
+// h is pathMatch's answer for host on t.hosts, which did not select a local
+// target unless h.shadowed (a catch-all's answer for a host with a remote
+// map). door: a door caller, which never selects across boxes. doorHop: a
+// door-marked hop, which never takes a remote-map answer (case 3).
//
-// Every answer that is not a cross-box selection is the front's entry (or
-// nothing, when the front is not served here): today's answer.
-func (e *Edge) selectAcross(t *routeTable, host, urlPath string, front upstream, frontOK bool, prefix, target string, door, doorHop bool) requestSelection {
- stay := requestSelection{Up: front, OK: frontOK, Prefix: prefix, Target: target}
+// Every answer that is not a cross-box selection is pathMatch's own (the
+// front's entry, a shadowing catch-all's answer, or nothing when the front
+// is not served here): today's answer.
+func (e *Edge) selectAcross(t *routeTable, host, urlPath string, h pathHit, door, doorHop bool) requestSelection {
+ stay := requestSelection{Up: h.up, OK: h.ok, Prefix: h.prefix, Target: h.target, LocalTarget: h.local}
if door {
return stay
}
- if frontOK {
- if prefix == "" {
+ if h.ok && !h.shadowed {
+ if h.prefix == "" {
return stay
}
- if _, here := t.hosts[target]; here {
+ if _, here := t.hosts[h.target]; here {
// A local target pathMatch refused: same-box rules decided it.
return stay
}
- return e.selectFromServedFront(t, host, front, prefix, target, stay)
+ return e.selectFromServedFront(t, host, h.up, h.prefix, h.target, stay)
}
if doorHop {
return stay
@@ -340,11 +374,13 @@ func (e *Edge) selectFromServedFront(t *routeTable, host string, front upstream,
return requestSelection{Remote: &remoteTarget{Target: target, Route: info}, Prefix: prefix, Target: target}
}
-// selectForUnservedFront is case 3: this box does not serve host, and holds
-// its remote map. The home map's exact row for host must be a front route of
-// the map's own account and app; otherwise the map is ignored. A prefix
-// miss, and every refusal, answer nothing (the backhaul to the host's home,
-// as today).
+// selectForUnservedFront is case 3: this box does not serve host exactly
+// (nothing, or only a catch-all, answers it here), and holds its remote map.
+// The home map's exact row for host must be a front route of the map's own
+// account and app; otherwise the map is ignored. A prefix miss, and every
+// refusal, answer stay: nothing (the backhaul to the host's home, as today),
+// or a shadowing catch-all's own answer, which the ownership block then
+// judges as today.
func (e *Edge) selectForUnservedFront(t *routeTable, host, urlPath string, rm remotePathMap, stay requestSelection) requestSelection {
homes := e.homes.Load()
fi, ok := homes.exactPathRoute(host)
@@ -356,7 +392,11 @@ func (e *Edge) selectForUnservedFront(t *routeTable, host, urlPath string, rm re
if !hit {
return stay
}
- stay.Prefix, stay.Target = prefix, target
+ if !stay.OK {
+ // A shadowing catch-all's answer (stay.OK) is kept whole on a
+ // refusal; an unserved front's refusal reports what matched.
+ stay.Prefix, stay.Target = prefix, target
+ }
if tu, here := t.hosts[target]; here {
if tu.alias || tu.platformAlias || tu.regional != regionalNone || tu.route.Host != target ||
!registry.PathTarget(tu.route) || release.RouteAccount(tu.route) != rm.account {
diff --git a/internal/edge/pathselect_test.go b/internal/edge/pathselect_test.go
index 7bb8fae99..5afeb763c 100644
--- a/internal/edge/pathselect_test.go
+++ b/internal/edge/pathselect_test.go
@@ -487,9 +487,12 @@ func TestSelectBothSpellings(t *testing.T) {
// ---- fronts that never select across ---------------------------------------
-// A catch-all is never a front route: its prefix map stays same-box, and the
-// concrete name's own remote map is not consulted while the catch-all
-// serves it.
+// A catch-all is never a front route: its prefix map stays same-box, so a
+// name it answers with no remote map of its own keeps the catch-all's entry
+// (no log keyed on the requested name). A name with its own remote map is a
+// front this box does not serve exactly (spec §5.3, Task 8 review O-I1):
+// case 3 runs for it ahead of the catch-all, and a prefix miss keeps the
+// catch-all's answer.
func TestSelectWildcardFrontNeverAcross(t *testing.T) {
logs := captureLog(t)
s := newSelScene(t)
@@ -503,10 +506,27 @@ func TestSelectWildcardFrontNeverAcross(t *testing.T) {
if s.e.current()["*.example.com"].paths["acme"] != selTarget {
t.Fatal("fixture: the catch-all has no prefix map")
}
- wantFront(t, s.sel("shop.example.com", "/acme"), "*.example.com")
- if strings.Contains(logs.String(), "shop.example.com") {
+ wantFront(t, s.sel("cafe.example.com", "/acme"), "*.example.com")
+ if strings.Contains(logs.String(), "cafe.example.com") {
t.Errorf("a log is keyed on the requested name, not the catch-all; log:\n%s", logs.String())
}
+ got := s.sel("shop.example.com", "/acme")
+ wantLocal(t, got, "t2.example.com", "acme", "t2.example.com")
+ if got.Up.front == nil || got.Up.front.Host != "shop.example.com" {
+ t.Fatalf("the shadowed front's case 3 answer has no front mark: %s", describeSel(got))
+ }
+ wantFront(t, s.sel("shop.example.com", "/other"), "*.example.com")
+ // On a verified hop a remote answer for a shadowed front is refused, not
+ // the catch-all's entry.
+ s.path("shop.example.com", "far", selTarget, selAcct, "hub")
+ if err := s.e.Reload(); err != nil {
+ t.Fatal(err)
+ }
+ wantRemote(t, s.sel("shop.example.com", "/far"), selTarget, "betty")
+ wantNone(t, s.sel("shop.example.com", "/far", asHop))
+ // A door call and a door hop never take the remote map.
+ wantFront(t, s.sel("shop.example.com", "/acme", asDoor), "*.example.com")
+ wantFront(t, s.sel("shop.example.com", "/acme", asDoorHop), "*.example.com")
}
func TestSelectAliasFrontNeverAcross(t *testing.T) {
@@ -642,6 +662,16 @@ func TestSelectPinnedIgnoresPathMaps(t *testing.T) {
// pathMatch reports what it matched, and lookupUpstreamPath answers its
// up/ok.
+//
+// lookupUpstreamPath is pathMatch's up/ok over the current map: this box's
+// local answer for a host and path, for tests. No production code calls it;
+// a request selects through selectUpstream, and an upgrade's re-check
+// through upgradeSelection.
+func (e *Edge) lookupUpstreamPath(host, urlPath string) (upstream, bool) {
+ up, ok, _, _, _, _ := e.pathMatch(e.current(), host, urlPath)
+ return up, ok
+}
+
func TestPathMatchReportsTheMatch(t *testing.T) {
s := newSelScene(t)
s.instance(selFront, selAcct, "hub")
@@ -660,8 +690,8 @@ func TestPathMatchReportsTheMatch(t *testing.T) {
{"/x", selFront, "x", "other.example.com", false},
{"/none", selFront, "", "", false},
} {
- up, ok, prefix, target, local := e.pathMatch(e.current(), selFront, c.path)
- if !ok || up.route.Host != c.upHost || prefix != c.prefix || target != c.target || local != c.local {
+ up, ok, prefix, target, local, front := e.pathMatch(e.current(), selFront, c.path)
+ if !ok || up.route.Host != c.upHost || prefix != c.prefix || target != c.target || local != c.local || front.route.Host != selFront {
t.Errorf("pathMatch(%s) = %q %v %q %q %v, want %q %q %q %v", c.path, up.route.Host, ok, prefix, target, local, c.upHost, c.prefix, c.target, c.local)
}
lu, lok := e.lookupUpstreamPath(selFront, c.path)
diff --git a/internal/edge/upgrades.go b/internal/edge/upgrades.go
index aa675a780..d577d1523 100644
--- a/internal/edge/upgrades.go
+++ b/internal/edge/upgrades.go
@@ -7,8 +7,10 @@ package edge
//
// The lock rules (§4.6.1): openUpgrades.mu is never held together with
// TunnelHub.mu or e.upgradesMu, and nothing runs under it but map
-// operations and lookupUpstreamPath, which is lock-free over the atomically
-// loaded map. A release closes streams, so it never runs under the lock.
+// operations and upgradeSelection, which reads the atomically loaded
+// routing table and takes only leaf locks (the home map's snapshot read
+// lock, the log throttle). A release closes streams, so it never runs under
+// the lock.
import (
"context"