edge: a checked hop is still judged by the home; the mark only skips its record (asset cache plan 3 fix wave 3)
Controller ruling: X-Carlos-Hop-Checked means already recorded, not already judged. The home now runs its own hard and probe checks on a marked hop and answers its block page when its mode blocks, so a far edge in watch mode cannot carry a request past a blocking home: the stricter mode wins. Only the home's request record (observeRecords) is skipped, so the match is still recorded once, by the far edge. TestAssetE2EFarGuardRecordsOnce gains far watch + home block (the fill is refused with the home's block page, nothing is stored, the app is not called, one record); TestCheckedMarkCountsOnlyBesideAVerifiedHop gains a mark beside a forged hop and a marked hop at a blocking home.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+69
−29
internal/edge/asset_e2e_test.go+21 −5internal/edge/asset_far_test.go+30 −9internal/edge/assetcatch.go+7 −6internal/edge/edge.go+6 −5internal/edge/homemap.go+5 −4
diff --git a/internal/edge/asset_e2e_test.go b/internal/edge/asset_e2e_test.go| index 25d5cbf14..759c9b932 100644 |
| --- a/internal/edge/asset_e2e_test.go |
| +++ b/internal/edge/asset_e2e_test.go |
| @@ -1454,7 +1454,11 @@ func (l *lockedRecorder) counts() (resp, match int, last reqrec.Match) { |
| // not lost; with both watching, the home does not record it a second time |
| // (the fill hop carries the far edge's checked mark). With far protection |
| // blocking, the far edge answers the block page and the home never sees the |
| -// request. An unmatched path is unchanged: no mark, recorded by the home. |
| +// request. With far protection watching and the home's blocking, the mark |
| +// buys nothing: the home still checks the fill hop and answers its block |
| +// page (the stricter mode wins), nothing is stored, and the one record is |
| +// still the far edge's (fix wave 3). An unmatched path is unchanged: no |
| +// mark, recorded by the home. |
| func TestAssetE2EFarGuardRecordsOnce(t *testing.T) { |
| for _, tc := range []struct { |
| name string |
| @@ -1463,6 +1467,7 @@ func TestAssetE2EFarGuardRecordsOnce(t *testing.T) { |
| {"far watch, home off", guardWatch, guardOff}, |
| {"far watch, home watch", guardWatch, guardWatch}, |
| {"far block, home watch", guardBlock, guardWatch}, |
| + {"far watch, home block", guardWatch, guardBlock}, |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| x := newAssetE2E(t, assetE2EOpts{handler: assetOK(laBody)}) |
| @@ -1471,10 +1476,11 @@ func TestAssetE2EFarGuardRecordsOnce(t *testing.T) { |
| x.a.edge.EnableRequestRecords(homeRec) |
| x.b.edge.EnableRequestRecords(farRec) |
| block := tc.far == guardBlock |
| + homeBlocks := tc.home == guardBlock |
| for i, cache := range []string{"miss", "hit"} { |
| resp, body := x.fetch(t, x.b, "/assets/.env") |
| x.rows(t) // every handler on both edges has returned |
| - if block { |
| + if block || homeBlocks { |
| if resp.StatusCode != http.StatusNotFound || resp.Header.Get("X-Carlos-Ref") == "" { |
| t.Fatalf("request %d = %d ref %q, want the block page", i, resp.StatusCode, resp.Header.Get("X-Carlos-Ref")) |
| } |
| @@ -1490,14 +1496,23 @@ func TestAssetE2EFarGuardRecordsOnce(t *testing.T) { |
| t.Fatalf("after request %d: home %d responses %d matches, far %d responses %d matches; want the far edge's one match each", |
| i, hr, hm, fr, fm) |
| } |
| - wantKind := reqrec.KindWatch |
| + wantKind, wantStatus := reqrec.KindWatch, http.StatusOK |
| if block { |
| wantKind = reqrec.KindBlock |
| } |
| - if m.Kind != wantKind || m.RuleID != "hard.secrets.env" || m.Acct != "abc" || m.App != "shop" || m.Path != "/assets/.env" { |
| + if block || homeBlocks { |
| + wantStatus = http.StatusNotFound |
| + } |
| + if m.Kind != wantKind || m.Status != wantStatus || m.RuleID != "hard.secrets.env" || m.Acct != "abc" || m.App != "shop" || m.Path != "/assets/.env" { |
| t.Fatalf("request %d recorded %+v", i, m) |
| } |
| } |
| + if homeBlocks { |
| + if n := x.app.n.Load(); n != 0 { |
| + t.Fatalf("the home's app saw %d request(s) its guard blocks", n) |
| + } |
| + x.wantNothingStored(t, "a fill the home blocked") |
| + } |
| if block { |
| if n := x.a.calls.Load(); n != 0 { |
| t.Fatalf("the home saw %d request(s) the far edge blocked", n) |
| @@ -1507,7 +1522,8 @@ func TestAssetE2EFarGuardRecordsOnce(t *testing.T) { |
| if mark := req.Get(hopCheckedHeader); !verifyHopCheckedEitherKey(x.key, nil, laHost, mark, time.Now()) { |
| t.Fatalf("the fill hop carried checked mark %q, want a valid one", mark) |
| } |
| - if got := x.app.lastReq().Header.Get(hopCheckedHeader); got != "" { |
| + if r := x.app.lastReq(); r != nil && r.Header.Get(hopCheckedHeader) != "" { |
| + got := r.Header.Get(hopCheckedHeader) |
| t.Fatalf("the home's app received the checked mark %q", got) |
| } |
| } |
diff --git a/internal/edge/asset_far_test.go b/internal/edge/asset_far_test.go| index 4b5f58869..f919cc4cb 100644 |
| --- a/internal/edge/asset_far_test.go |
| +++ b/internal/edge/asset_far_test.go |
| @@ -1345,38 +1345,59 @@ func TestFarReforwardIsRecordedOnceAcrossTheEdges(t *testing.T) { |
| } |
| // The checked mark counts only beside a verified hop, and only in its own MAC |
| -// domain: a client's mark, even one signed with the mesh key, and a hop |
| -// signature passed off as a mark both leave the home's checks and record in |
| -// place. The mark never reaches the app. |
| +// domain: a client's mark, even one signed with the mesh key, a mark beside |
| +// a hop whose own auth fails, and a hop signature passed off as a mark all |
| +// leave the home's record in place. The mark is deleted at entry and never |
| +// reaches the app. Even a verified hop's mark skips only the record: a home |
| +// that blocks still answers its block page (fix wave 3). |
| func TestCheckedMarkCountsOnlyBesideAVerifiedHop(t *testing.T) { |
| s := newLAScene(t, assetOK(laBody)) |
| key := s.armMesh(t) |
| rec := &memRecorder{} |
| s.e.EnableRequestRecords(rec) |
| s.e.guard = guardWatch |
| + valid := func() string { return signHopChecked(key, laHost, time.Now()) } |
| for _, tc := range []struct { |
| name string |
| - hop bool |
| + hop string // "" none, "valid", "forged" |
| mark func() string |
| + block bool |
| recorded bool |
| }{ |
| - {"a client's mark", false, func() string { return signHopChecked(key, laHost, time.Now()) }, true}, |
| - {"a hop signature as the mark", true, func() string { return signHop(key, laHost, time.Now()) }, true}, |
| - {"a verified hop's mark", true, func() string { return signHopChecked(key, laHost, time.Now()) }, false}, |
| + {"a client's mark", "", valid, false, true}, |
| + {"a mark beside a forged hop", "forged", valid, false, true}, |
| + {"a hop signature as the mark", "valid", func() string { return signHop(key, laHost, time.Now()) }, false, true}, |
| + {"a verified hop's mark", "valid", valid, false, false}, |
| + {"a verified hop's mark at a blocking home", "valid", valid, true, false}, |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| - before := len(rec.match) |
| + s.e.guard = guardWatch |
| + if tc.block { |
| + s.e.guard = guardBlock |
| + } |
| + before, app := len(rec.match), s.b.n.Load() |
| req := httptest.NewRequest(http.MethodGet, "http://"+laHost+"/.env", nil) |
| req.Header.Set(hopCheckedHeader, tc.mark()) |
| - if tc.hop { |
| + switch tc.hop { |
| + case "valid": |
| req.Header.Set(hopHeader, "1") |
| req.Header.Set(hopAuthHeader, signHop(key, laHost, time.Now())) |
| + case "forged": |
| + req.Header.Set(hopHeader, "1") |
| + req.Header.Set(hopAuthHeader, signHop([]byte("not the mesh key"), laHost, time.Now())) |
| } |
| w := httptest.NewRecorder() |
| s.e.ServeHTTP(w, req) |
| if got := len(rec.match) - before; (got == 1) != tc.recorded || got > 1 { |
| t.Fatalf("%d match(es) recorded at the home, want recorded=%v", got, tc.recorded) |
| } |
| + if tc.block { |
| + if w.Code != http.StatusNotFound || w.Header().Get("X-Carlos-Ref") == "" || s.b.n.Load() != app { |
| + t.Fatalf("a marked hop at a blocking home = %d ref %q, app saw %d new; want the block page", |
| + w.Code, w.Header().Get("X-Carlos-Ref"), s.b.n.Load()-app) |
| + } |
| + return |
| + } |
| if v := s.b.lastReq().Header.Get(hopCheckedHeader); v != "" { |
| t.Fatalf("the app received the checked mark %q", v) |
| } |
diff --git a/internal/edge/assetcatch.go b/internal/edge/assetcatch.go| index 17ffae4bb..dba0da5de 100644 |
| --- a/internal/edge/assetcatch.go |
| +++ b/internal/edge/assetcatch.go |
| @@ -1042,7 +1042,7 @@ func (e *Edge) farAssetCatch(r *http.Request, host string) (a HostAssets, eip, b |
| // them in ServeHTTP. A block is answered with the block page. A match is |
| // recorded here as one Match under the synthetic route, however the request |
| // was answered, and every hop sent for it carries the checked mark, so the |
| -// home neither checks nor records it again. |
| +// home, which still checks it under its own mode, does not record it again. |
| func (e *Edge) serveAssetFar(w http.ResponseWriter, r *http.Request, host string) bool { |
| a, eip, box, key, ok := e.farAssetCatch(r, host) |
| if !ok { |
| @@ -1067,8 +1067,8 @@ func (e *Edge) serveAssetFar(w http.ResponseWriter, r *http.Request, host string |
| } |
| // A request a rule matched is recorded here, once, however it was |
| // answered: a hit, the block page, a throttle refusal, a fill or a |
| - // forward. Every hop sent for it carried this edge's checked mark, |
| - // so the home neither checks nor records it, whatever its own mode. |
| + // forward, a home's block page included. Every hop sent for it |
| + // carried this edge's checked mark, so the home does not record it. |
| // viaHome: a wake, if any, happened on the home, out of sight. |
| if out != nil && out.guard != nil && out.guard.verdict != nil { |
| e.observeRecords(rt, viaHome, r, cw, out) |
| @@ -1090,9 +1090,10 @@ func (e *Edge) serveAssetFar(w http.ResponseWriter, r *http.Request, host string |
| // From here on, when a rule matched (a watch: a block has returned), |
| // every hop this edge sends for the request (a fill, a forward) carries |
| // its checked mark (signHopChecked), signed when the hop is sent: the |
| - // home then neither checks nor records the request, as for a tunnel |
| - // delivery, and this edge's defer records it once. A request no rule |
| - // matched carries no mark, and the home records it as an ordinary hop. |
| + // home still runs its own checks, and refuses when its mode blocks, but |
| + // does not record the request, which this edge's defer records once. A |
| + // request no rule matched carries no mark, and the home records it as an |
| + // ordinary hop. |
| checked := out != nil && out.guard != nil && out.guard.verdict != nil |
| forward = func() { |
| viaHome = true |
diff --git a/internal/edge/edge.go b/internal/edge/edge.go| index 4e17b69c6..b779fa442 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -1805,11 +1805,12 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| // backhauled it here (hopDoorFrom, set only beside a verified hop, |
| // as the rate limit reads it). A request a tunnel delivered was |
| // checked, and is recorded, by the hub that forwarded it, so |
| - // checking it again here would answer or count it twice. So was a |
| - // hop a far edge's asset catch sent with its checked mark: its |
| - // checks matched, and it records the request (hopCheckedFrom, set |
| - // only beside a verified hop). |
| - if e.guard != guardOff && !isDoor && !hopDoorFrom(r.Context()) && !fromTunnel(r.Context()) && !hopCheckedFrom(r.Context()) { |
| + // checking it again here would answer or count it twice. A hop a far |
| + // edge's asset catch sent with its checked mark (hopCheckedFrom) is |
| + // still checked here, so a home that blocks refuses it whatever the |
| + // far edge's mode (the stricter mode wins); only its record is left |
| + // to the far edge, which has filed it (observeRecords). |
| + if e.guard != guardOff && !isDoor && !hopDoorFrom(r.Context()) && !fromTunnel(r.Context()) { |
| out.guard = e.newGuardState() |
| } |
| defer func() { |
diff --git a/internal/edge/homemap.go b/internal/edge/homemap.go| index e58efcb3c..d2c9b1526 100644 |
| --- a/internal/edge/homemap.go |
| +++ b/internal/edge/homemap.go |
| @@ -1236,10 +1236,11 @@ func hopDoorFrom(ctx context.Context) bool { |
| // hopCheckedKey marks a verified hop whose sender vouched, with |
| // X-Carlos-Hop-Checked, that its hard and probe checks matched the request |
| -// and that it records it (a far edge's asset catch, serveAssetFar). Set only |
| -// beside hopVerifiedKey. It exempts the request from this box's checks and |
| -// from its request record (observeRecords), as a tunnel delivery is exempt, |
| -// so the request is decided and recorded once; it exempts nothing else. |
| +// and that it has recorded it (a far edge's asset catch, serveAssetFar). Set |
| +// only beside hopVerifiedKey. It means "already recorded", not "already |
| +// judged": this box still runs its own checks and refuses the request when |
| +// its mode blocks, and only its request record (observeRecords) is skipped, |
| +// so the match is recorded once; it exempts nothing else. |
| type hopCheckedKey struct{} |
| func hopCheckedFrom(ctx context.Context) bool { |