carlos / platform Public

edge: far asset deliveries meet the hard and probe checks first (asset cache plan 3 final F1)

Every request the far catch keeps (a disk hit, a flight waiter's hit, a
far fill, on the exact-remote and lookup-miss branches) now runs
edgeguard.Check under this box's protection mode before the store is
read, through guardDecide as ServeHTTP does. A block answers the block
page and is billed under the synthetic route; a match the far edge
answers itself is recorded as a Match (observeRecords). Fills and
forwards reach the home as hops and are recorded there.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 11601fafdc81880e53832499bfcb6e63ceea68d0 parent 5a116c8
2 files changed, +123 −0
  • internal/edge/asset_far_test.go +95 −0
  • internal/edge/assetcatch.go +28 −0
diff --git a/internal/edge/asset_far_test.go b/internal/edge/asset_far_test.go
index b6de8d36f..238b68210 100644
--- a/internal/edge/asset_far_test.go
+++ b/internal/edge/asset_far_test.go
@@ -21,6 +21,7 @@ import (
"github.com/carlosframework/platform/internal/plans"
"github.com/carlosframework/platform/internal/ratelimit"
"github.com/carlosframework/platform/internal/registry"
+ "github.com/carlosframework/platform/internal/reqrec"
)
// The far catch and fill (edge asset cache design §5.1 to §5.5, §8; plan 3
@@ -1115,3 +1116,97 @@ func TestLocalFallbackScrubsInformationalVocabulary(t *testing.T) {
}
}
}
+
+// ---- protection on the far catch (final review F1) ------------------------
+
+// Far deliveries meet the hard and probe checks as a request the home serves
+// does. Assets under a hard-tier path (.env) and a probe-tier path (.php) are
+// warmed while protection watches, and a far hit of each is recorded as a
+// watch. Then protection blocks, with the same disk store: every far hit is
+// answered with the block page, recorded as a block and billed like one, and
+// a cold path is refused before any fill, on both forwarding branches. The
+// home is not asked for any of it.
+func TestFarHitsMeetTheGuard(t *testing.T) {
+ rules := map[string]struct {
+ id string
+ tier reqrec.Tier
+ }{
+ "/assets/.env": {"hard.secrets.env", reqrec.TierHard},
+ "/assets/app.php": {"probe.php.php", reqrec.TierProbe},
+ }
+ for _, branch := range []struct {
+ name string
+ locals []string
+ }{
+ {"lookup miss", nil},
+ {"exact remote", []string{"*.example.com"}},
+ } {
+ t.Run(branch.name, func(t *testing.T) {
+ s := newFarScene(t, assetOK(laBody), branch.locals...)
+ rec := &memRecorder{}
+ s.e.EnableRequestRecords(rec)
+ s.e.guard = guardWatch
+ for p := range rules {
+ wantCache(t, s.get(t, p), "miss")
+ }
+ // A fill reached the home as a hop, and the home records it.
+ if len(rec.match) != 0 || len(rec.resp) != 0 {
+ t.Fatalf("the far edge recorded fills: matches %+v responses %+v", rec.match, rec.resp)
+ }
+ s.meter.Drain()
+
+ for p, want := range rules {
+ before := len(rec.match)
+ hit := s.get(t, p)
+ wantCache(t, hit, "hit")
+ if hit.Code != 200 || hit.Body.String() != laBody {
+ t.Fatalf("watched far hit of %s = %d %q", p, hit.Code, hit.Body.String())
+ }
+ if len(rec.match) != before+1 {
+ t.Fatalf("watched far hit of %s: %d matches recorded, want one", p, len(rec.match)-before)
+ }
+ m := rec.match[len(rec.match)-1]
+ if m.Kind != reqrec.KindWatch || m.RuleID != want.id || m.Tier != want.tier || m.Status != 200 ||
+ m.Acct != "abc" || m.App != "shop" || m.Host != laHost || m.Path != p || m.Ref != "" {
+ t.Fatalf("watched far hit of %s recorded %+v", p, m)
+ }
+ }
+
+ s.e.guard = guardBlock
+ calls := s.rec.get().n
+ cold := "/assets/cold.php"
+ for _, p := range []string{"/assets/.env", "/assets/app.php", cold} {
+ before := len(rec.match)
+ got := s.get(t, p)
+ code := got.Header().Get("X-Carlos-Ref")
+ if got.Code != http.StatusNotFound || code == "" || got.Header().Get("Carlos-Cache") != "" || strings.Contains(got.Body.String(), laBody) {
+ t.Fatalf("blocked far request for %s = %d ref %q Carlos-Cache %q body %q, want the block page",
+ p, got.Code, code, got.Header().Get("Carlos-Cache"), got.Body.String())
+ }
+ if len(rec.match) != before+1 {
+ t.Fatalf("blocked far request for %s: %d matches recorded, want one", p, len(rec.match)-before)
+ }
+ if m := rec.match[len(rec.match)-1]; m.Kind != reqrec.KindBlock || m.Ref != code || m.Status != 404 || m.Acct != "abc" {
+ t.Fatalf("blocked far request for %s recorded %+v", p, m)
+ }
+ }
+ if n := s.rec.get().n; n != calls {
+ t.Fatalf("the home saw %d request(s) while the far edge blocked", n-calls)
+ }
+ if _, ok := s.store.Get(s.key(cold)); ok {
+ t.Fatal("a blocked cold path was filled")
+ }
+ for p := range rules {
+ if _, ok := s.store.Get(s.key(p)); !ok {
+ t.Fatalf("the warm entry for %s went", p)
+ }
+ }
+ if row := s.meter.Drain()[laHost]; row.Requests != int64(len(rules)+3) {
+ t.Fatalf("row = %+v, want the %d watched hits and the 3 block pages metered", row, len(rules))
+ }
+ if len(rec.resp) != 0 {
+ t.Fatalf("plain responses recorded %+v, want matches only", rec.resp)
+ }
+ })
+ }
+}
diff --git a/internal/edge/assetcatch.go b/internal/edge/assetcatch.go
index 1861a59f5..434199cb1 100644
--- a/internal/edge/assetcatch.go
+++ b/internal/edge/assetcatch.go
@@ -16,6 +16,7 @@ import (
"time"
"github.com/carlosframework/platform/internal/assetcache"
+ "github.com/carlosframework/platform/internal/edgeguard"
"github.com/carlosframework/platform/internal/geoip"
"github.com/carlosframework/platform/internal/registry"
"github.com/carlosframework/platform/internal/release"
@@ -1012,6 +1013,12 @@ func (e *Edge) farAssetCatch(r *http.Request, host string) (a HostAssets, eip, b
// limit applies at the home, which the fill reaches as a hop (D7); the
// owner's account throttle applies here, to every delivery this edge answers
// itself, with a synthetic instance route (D8).
+//
+// Protection (edge request protection, internal/edgeguard): every request
+// this catch keeps first meets the hard and probe checks under this box's
+// mode, before the store is read, exactly as a request the home serves meets
+// them in ServeHTTP. A block is answered with the block page; a match it
+// answers itself is recorded as a Match under the synthetic route.
func (e *Edge) serveAssetFar(w http.ResponseWriter, r *http.Request, host string) bool {
a, eip, box, key, ok := e.farAssetCatch(r, host)
if !ok {
@@ -1027,13 +1034,34 @@ func (e *Edge) serveAssetFar(w http.ResponseWriter, r *http.Request, host string
var n int64
cw := &countingWriter{ResponseWriter: w, n: &n}
var billed, acked, reforwarded bool
+ var out *outcome
defer func() {
if billed || (acked && !reforwarded) {
if m := e.usageMeter.Load(); m != nil {
m.Record(rt, 1, n, false)
}
}
+ // What this edge answered itself (a hit, the block page, a throttle
+ // refusal) is recorded here when a rule matched it, as ServeHTTP's
+ // observer defer records a matched request it serves. A fill or a
+ // forward reached the home as a hop, and the home records it there.
+ if billed && out != nil && out.guard != nil && out.guard.verdict != nil {
+ e.observeRecords(rt, false, r, cw, out)
+ }
}()
+ // The hard and probe checks, as ServeHTTP runs them for a request this
+ // box is the first to see, before anything is answered from the store or
+ // filled: a stored body is never served to a probe the home would refuse.
+ // farAssetCatch has already excluded door callers and verified hops. A
+ // block is billed and recorded like any answer.
+ if e.guard != guardOff && !fromTunnel(r.Context()) {
+ r, out = withOutcome(r)
+ out.guard = e.newGuardState()
+ if hit, matched := edgeguard.Check(r.URL.Path, r.URL.RawQuery); matched && guardDecide(cw, r, hit) {
+ billed = true
+ return true
+ }
+ }
var judged, admitted bool
throttle := func() bool {
if !judged {