| 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) |
| + } |
| + }) |
| + } |
| +} |