| index eed075b06..816f7187d 100644 |
| --- a/internal/edge/burst_edge_test.go |
| +++ b/internal/edge/burst_edge_test.go |
| @@ -220,11 +220,11 @@ func (g *burstRig) wantRefused(t *testing.T, w *httptest.ResponseRecorder, until |
| return code |
| } |
| |
| -// The scanner's 60th not-found trips it. In the default (watch) mode its |
| -// 61st catalogue request still reaches the app, and is recorded as the |
| -// refusal it would have been, saying the cool-down started on this app. |
| +// The scanner's 60th not-found trips it. In watch mode its 61st catalogue |
| +// request still reaches the app, and is recorded as the refusal it would |
| +// have been, saying the cool-down started on this app. |
| func TestBurstTripsAndRecordsWouldRefuse(t *testing.T) { |
| - g := newBurstRig(t, "") |
| + g := newBurstRig(t, "watch") |
| until := g.trip(t, burstHost) |
| tr := g.rec.trips[0] |
| ref, err := reqrec.ParseRef(tr.Ref, g.key) |
| @@ -263,6 +263,51 @@ func TestBurstRefusesInBlockMode(t *testing.T) { |
| } |
| } |
| |
| +// The promotion (#491): in the default mode the burst rule refuses. After |
| +// the trip the cooling client's next catalogue request is refused on the |
| +// app it tripped on and on another app, an ordinary page still reaches the |
| +// app, and the probe rules themselves still only watch for a client that is |
| +// not cooling. The app's record-only switch turns the refusal off for that |
| +// app alone. |
| +func TestBurstCatalogueRefusesByDefault(t *testing.T) { |
| + g := newBurstRig(t, "", burstHost+"/about") |
| + if g.e.guard != guardCatalogue { |
| + t.Fatalf("guard mode %v, want the catalogue's", g.e.guard) |
| + } |
| + until := g.trip(t, burstHost) |
| + n := g.upstream.Load() |
| + code := g.wantRefused(t, g.get(burstHost, "/wp-login.php"), until) |
| + m := g.lastMatch(t, "/wp-login.php") |
| + wantBurstMatch(t, m, reqrec.KindBlock, reqrec.CooldownFromThisApp, until) |
| + if m.Ref != code || m.Status != 429 || g.upstream.Load() != n { |
| + t.Fatalf("this app: %+v, upstream %d; want the refusal", m, g.upstream.Load()-n) |
| + } |
| + g.wantRefused(t, g.get(burstOther, "/xmlrpc.php"), until) |
| + wantBurstMatch(t, g.lastMatch(t, "/xmlrpc.php"), reqrec.KindBlock, reqrec.CooldownFromOtherApp, until) |
| + if w := g.get(burstHost, "/about"); w.Code != 200 || w.Body.String() != "page /about" { |
| + t.Fatalf("/about: %d %q, want the page", w.Code, w.Body.String()) |
| + } |
| + |
| + r := httptest.NewRequest("GET", "http://"+burstHost+"/wp-login.php", nil) |
| + r.RemoteAddr = "198.51.100.7:4000" |
| + w := httptest.NewRecorder() |
| + g.e.ServeHTTP(w, r) |
| + if w.Code != 404 || w.Body.String() != "the app's own 404" { |
| + t.Fatalf("a client not cooling: %d %q, want the app's answer", w.Code, w.Body.String()) |
| + } |
| + if m := g.lastMatch(t, "/wp-login.php"); m.Kind != reqrec.KindWatch || m.Tier != reqrec.TierProbe { |
| + t.Fatalf("a client not cooling: match %+v, want the probe watch", m) |
| + } |
| + |
| + g.e.SetProtectionRecordOnly(map[string]bool{"bdk/shop": true}) |
| + n = g.upstream.Load() |
| + if w := g.get(burstHost, "/wp-login.php"); w.Code != 404 || w.Body.String() != "the app's own 404" || g.upstream.Load() != n+1 { |
| + t.Fatalf("record only: %d %q, want the app's answer", w.Code, w.Body.String()) |
| + } |
| + wantBurstMatch(t, g.lastMatch(t, "/wp-login.php"), reqrec.KindWatch, reqrec.CooldownFromThisApp, until) |
| + g.wantRefused(t, g.get(burstOther, "/wp-login.php"), until) |
| +} |
| + |
| // Review Focus 1: a page that is not in the catalogue is never refused, |
| // whoever shares the address. |
| func TestBurstNeverRefusesAnOrdinaryPage(t *testing.T) { |
| @@ -400,21 +445,31 @@ func TestBurstFleetAppNeverRefused(t *testing.T) { |
| } |
| |
| // One request has one verdict, one record and one Protection count, however |
| -// many rules it meets. |
| +// many rules it meets. In the default mode that count is a block: the burst |
| +// rule refuses, and the Protection tab counts a refusal as blocked. |
| func TestBurstOneVerdictPerRequest(t *testing.T) { |
| - g := newBurstRig(t, "") |
| - until := g.trip(t, burstHost) |
| - g.meter.Drain() |
| - matches := len(g.rec.match) |
| - g.get(burstHost, "/wp-login.php") |
| - if len(g.rec.match) != matches+1 { |
| - t.Fatalf("%d matches for one request", len(g.rec.match)-matches) |
| - } |
| - wantBurstMatch(t, g.lastMatch(t, "/wp-login.php"), reqrec.KindWatch, reqrec.CooldownFromThisApp, until) |
| - f := g.meter.Drain() |
| - d, ok := f.Guard[usage.GuardKey(burstHost, "bdk", "shop")] |
| - if len(f.Guard) != 1 || !ok || d.Checked != 1 || d.Watch != 1 || d.Block != 0 || !maps.Equal(d.Groups, map[string]int64{"burst/probe-burst": 1}) { |
| - t.Fatalf("guard counts %+v, want one recorded burst/probe-burst", f.Guard) |
| + for _, tc := range []struct { |
| + protect string |
| + kind reqrec.Kind |
| + watch, block int64 |
| + }{ |
| + {"", reqrec.KindBlock, 0, 1}, |
| + {"watch", reqrec.KindWatch, 1, 0}, |
| + } { |
| + g := newBurstRig(t, tc.protect) |
| + until := g.trip(t, burstHost) |
| + g.meter.Drain() |
| + matches := len(g.rec.match) |
| + g.get(burstHost, "/wp-login.php") |
| + if len(g.rec.match) != matches+1 { |
| + t.Fatalf("mode %q: %d matches for one request", tc.protect, len(g.rec.match)-matches) |
| + } |
| + wantBurstMatch(t, g.lastMatch(t, "/wp-login.php"), tc.kind, reqrec.CooldownFromThisApp, until) |
| + f := g.meter.Drain() |
| + d, ok := f.Guard[usage.GuardKey(burstHost, "bdk", "shop")] |
| + if len(f.Guard) != 1 || !ok || d.Checked != 1 || d.Watch != tc.watch || d.Block != tc.block || !maps.Equal(d.Groups, map[string]int64{"burst/probe-burst": 1}) { |
| + t.Fatalf("mode %q: guard counts %+v, want one %s burst/probe-burst", tc.protect, f.Guard, tc.kind) |
| + } |
| } |
| } |
| |
| @@ -436,9 +491,11 @@ func TestBurstWatchNeverWeakensABlock(t *testing.T) { |
| } |
| }) |
| t.Run("static rewrite", func(t *testing.T) { |
| - // The default mode, where the burst rule watches and the .env rule |
| - // blocks. The .git rule watches here, so a hard-tier request path |
| - // whose own verdict does not block still reaches the static handler. |
| + // The default mode with the burst rule watching, as an app's own |
| + // exemption will make it, and the .env rule blocking. The .git rule |
| + // watches here, so a hard-tier request path whose own verdict does |
| + // not block still reaches the static handler. |
| + demoteBurstRule(t) |
| g := newBurstStaticRig(t, "", map[string]string{".env": "SECRET=1", "_redirects": "/* /.env 200\n"}) |
| until := g.trip(t, burstHost) |
| env, ok := edgeguard.RuleByID("hard.secrets.env") |
| @@ -471,8 +528,8 @@ func TestBurstWatchNeverWeakensABlock(t *testing.T) { |
| if m := g.lastMatch(t, "/.git/config"); m.Kind != reqrec.KindBlock || m.RuleID != "hard.secrets.env" { |
| t.Fatalf("/.git/config: match %+v, want the hard block", m) |
| } |
| - // Without the promotion the burst watch stands, as the first |
| - // verdict that does not block. |
| + // With the .env rule watching too, the burst watch stands, as the |
| + // first verdict that does not block. |
| env.Mode = edgeguard.ModeWatch |
| g.get(burstHost, "/wp-login.php") |
| wantBurstMatch(t, g.lastMatch(t, "/wp-login.php"), reqrec.KindWatch, reqrec.CooldownFromThisApp, until) |
| @@ -795,13 +852,14 @@ func TestBurstStaleFillCountedOnceAtTheHome(t *testing.T) { |
| } |
| } |
| |
| -// promoteBurstRule sets the burst rule's catalogue mode to block for the |
| -// test, as the later one-line promotion will: in the default mode the burst |
| -// rule then refuses while the probe rules still only watch. |
| -func promoteBurstRule(t *testing.T) { |
| +// demoteBurstRule sets the burst rule's catalogue mode back to watch for the |
| +// test, as it was before the promotion (#491): in the default mode the burst |
| +// rule then only records. |
| +func demoteBurstRule(t *testing.T) { |
| t.Helper() |
| - edgeguard.BurstRule.Mode = edgeguard.ModeBlock |
| - t.Cleanup(func() { edgeguard.BurstRule.Mode = edgeguard.ModeWatch }) |
| + mode := edgeguard.BurstRule.Mode |
| + edgeguard.BurstRule.Mode = edgeguard.ModeWatch |
| + t.Cleanup(func() { edgeguard.BurstRule.Mode = mode }) |
| } |
| |
| // Fix round 1, finding 2: a static site's own file is never refused by the |
| @@ -826,8 +884,7 @@ func TestBurstNeverRefusesAnExistingStaticHardFile(t *testing.T) { |
| } |
| } |
| }) |
| - t.Run("burst rule promoted", func(t *testing.T) { |
| - promoteBurstRule(t) |
| + t.Run("default mode", func(t *testing.T) { |
| // With the .env rule watching (as an app's own allow rule will make |
| // it), a burst refusal still never takes a file the site has. |
| env, ok := edgeguard.RuleByID("hard.secrets.env") |