edge: burst fix round 1 (verified fills only; static existence before the cool-down)
1. The home exempted any verified hop carrying X-Carlos-Asset-Fill from burst counting, so a visitor's forged marker passed on by an older far edge suppressed counting. The home now exempts only a fill it handles as one (assertion verified, acknowledged: where markUnbilled is called). The far edge counts a fill only when the home acknowledged it or nothing reached the home, so a genuine fill rejected for stale metadata (counted by the home as an ordinary hop) still counts once. 2. A static route's hard-tier hit no longer meets the burst rule in ServeHTTP, where existence is unknown; the hard tier's own verdict stands there. serveTree offers the burst rule for hard and probe paths alike, only where the path is not found and not named by _redirects. Tests: forged marker counted at the home; stale-generation fill counted once (home); an existing static /.env never gets a burst 429, a missing one does once the burst rule blocks.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+156
−27
internal/edge/answered.go+6 −4internal/edge/assetcatch.go+14 −6internal/edge/burst_edge_test.go+105 −0internal/edge/edge.go+11 −9internal/edge/guard.go+2 −2internal/edge/static.go+18 −6
diff --git a/internal/edge/answered.go b/internal/edge/answered.go| index c60c3876b..f83e19692 100644 |
| --- a/internal/edge/answered.go |
| +++ b/internal/edge/answered.go |
| @@ -48,10 +48,12 @@ type outcome struct { |
| // guard is off for it: a door caller, a request a tunnel delivered (the |
| // hub checked it), or CARLOS_EDGE_PROTECT=off. |
| guard *guardState |
| - // assetFill is set on a verified hop that arrived as a far edge's asset |
| - // fill (assetcatch.go). The fill rebuilt the visitor's headers, so the |
| - // far edge, which saw them, counts the request toward the probe-burst |
| - // limit, and this box does not. |
| + // assetFill is set on a far edge's asset fill this box handles as one |
| + // (serveAssetFillForHop: its assertion verified, so it is acknowledged). |
| + // The fill rebuilt the visitor's headers, so the far edge, which saw |
| + // them and sees the acknowledgement, counts the request toward the |
| + // probe-burst limit, and this box does not. A fill header that does not |
| + // verify buys nothing: an older far edge may pass on a visitor's own. |
| assetFill bool |
| } |
diff --git a/internal/edge/assetcatch.go b/internal/edge/assetcatch.go| index 09fe39c60..3b5ebe841 100644 |
| --- a/internal/edge/assetcatch.go |
| +++ b/internal/edge/assetcatch.go |
| @@ -375,6 +375,10 @@ func (e *Edge) serveAssetFillForHop(w http.ResponseWriter, r *http.Request, host |
| return true |
| } |
| markUnbilled(r) |
| + // Acknowledged from here on, so the far edge counts it (noteBurst). |
| + if o := outcomeFrom(r); o != nil { |
| + o.assetFill = true |
| + } |
| ack := func(h http.Header, expires int64) { |
| h.Del(assetAckHeader) |
| h.Del(assetExpiresHeader) |
| @@ -1096,12 +1100,16 @@ func (e *Edge) serveAssetFar(w http.ResponseWriter, r *http.Request, host string |
| } |
| e.observeGuardCount(rt, out) |
| } |
| - // The probe-burst count has its own owner rule: this edge counts |
| - // every request it guarded, filled or not, since a fill rebuilt the |
| - // headers the home would judge (the home skips fills), except one an |
| - // ordinary hop delivered to the home, which the home counts. Each |
| - // visitor response counts once. Counting never refuses here. |
| - if out != nil && out.guard != nil && !hopped.Load() { |
| + // The probe-burst count has its own owner rule, which follows the |
| + // acknowledgement as metering does. This edge counts every request |
| + // it guarded that reached the home only as an acknowledged fill, or |
| + // not at all: the home skips exactly the fills it acknowledges, and |
| + // a fill rebuilt the headers it would judge. A fill the home did not |
| + // acknowledge (an assertion that no longer verifies there, say after |
| + // a generation change) went on as an ordinary hop the home counted, |
| + // and so did any ordinary hop. Each visitor response counts once. |
| + // Counting never refuses here. |
| + if out != nil && out.guard != nil && !hopped.Load() && (!reached.Load() || acked) { |
| e.noteBurst(rt, r, cw.status, out) |
| } |
| }() |
diff --git a/internal/edge/burst_edge_test.go b/internal/edge/burst_edge_test.go| index 69fcf4468..b3ae56012 100644 |
| --- a/internal/edge/burst_edge_test.go |
| +++ b/internal/edge/burst_edge_test.go |
| @@ -726,3 +726,108 @@ func (l *lockedRecorder) Trip(x reqrec.Trip) { |
| defer l.mu.Unlock() |
| l.trips = append(l.trips, x) |
| } |
| + |
| +// Fix round 1, finding 1: a fill header the home cannot verify buys no |
| +// exemption from counting. An older far edge passes a visitor's own |
| +// X-Carlos-Asset-Fill inside a hop it signs properly (as |
| +// TestAssetE2EOldFarEdgeForgedMarkerIsBilledAsAHop models); the home serves |
| +// it as an ordinary hop and counts it. |
| +func TestBurstForgedFillMarkerStillCounts(t *testing.T) { |
| + x := newBurstFar(t, missingAsset) |
| + mac := strings.Repeat("ab", 32) |
| + for i := 0; i < 3; i++ { |
| + now := time.Now() |
| + r := httptest.NewRequest("GET", "http://"+laHost+fmt.Sprintf("/assets/gone-%d.js", i), nil) |
| + r.Header.Set(hopHeader, "1") |
| + r.Header.Set(hopAuthHeader, signHop(x.s.meshKey, laHost, now)) |
| + r.Header.Set(assetFillHeader, fmt.Sprintf("%d:%s", now.Unix(), mac)) |
| + r.Header.Set("X-Forwarded-For", "203.0.113.9") |
| + w := httptest.NewRecorder() |
| + x.home.e.ServeHTTP(w, r) |
| + if w.Code != 404 || w.Header().Get(assetAckHeader) != "" { |
| + t.Fatalf("forged marker %d: %d, ack %q; want an ordinary, unacknowledged 404", i, w.Code, w.Header().Get(assetAckHeader)) |
| + } |
| + } |
| + if n := x.home.e.burst.ringLen(burstAppKey(registry.Route{Account: "abc", App: "shop"}), netip.MustParsePrefix("203.0.113.9/32")); n != 3 { |
| + t.Fatalf("the home counted %d of 3 hops carrying a forged fill marker", n) |
| + } |
| +} |
| + |
| +// publishGen lands betty's edges file as farScene.publish does, under gen: |
| +// a far edge holding an older generation than the home's policy signs fills |
| +// the home can no longer verify. |
| +func (x *burstFar) publishGen(t *testing.T, gen string) { |
| + t.Helper() |
| + putJSON(t, x.s.m.s, "control/edges/betty.json", fmt.Sprintf( |
| + `{"v":1,"box":"betty","eip":%q,"hosts":{%q:{"role":"home","account":"abc","app":"shop",`+ |
| + `"assets":{"paths":["/assets/"],"gen":%q,"at":%d,"primary":%q}}}}`, |
| + x.s.eip, laHost, gen, time.Now().Unix(), laHost)) |
| + refreshHomeMap(t, x.s.m) |
| +} |
| + |
| +// A genuine fill the home rejects for stale metadata goes on there as an |
| +// ordinary hop, unacknowledged: the home counts it, the far edge does not, |
| +// so it still counts once. |
| +func TestBurstStaleFillCountedOnceAtTheHome(t *testing.T) { |
| + x := newBurstFar(t, missingAsset) |
| + x.publishGen(t, "stale-gen") |
| + const n = 5 |
| + for i := 0; i < n; i++ { |
| + if w := x.s.get(t, fmt.Sprintf("/assets/gone-%d.js", i)); w.Code != 404 { |
| + t.Fatalf("stale fill %d: %d", i, w.Code) |
| + } |
| + } |
| + x.waitHome(t, n) |
| + if far, home := burstNotes(x.s.e.burst), burstNotes(x.home.e.burst); far != 0 || home != n { |
| + t.Fatalf("stale fills: far %d, home %d; want 0 and %d", far, home, n) |
| + } |
| +} |
| + |
| +// 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 hard and probe rules still only watch. |
| +func promoteBurstRule(t *testing.T) { |
| + t.Helper() |
| + edgeguard.BurstRule.Mode = edgeguard.ModeBlock |
| + t.Cleanup(func() { edgeguard.BurstRule.Mode = edgeguard.ModeWatch }) |
| +} |
| + |
| +// Fix round 1, finding 2: a static site's own file is never refused by the |
| +// cool-down, hard tier included; whatever the hard tier itself decides |
| +// stands. The same path missing from the site is refused. |
| +func TestBurstNeverRefusesAnExistingStaticHardFile(t *testing.T) { |
| + withEnv := map[string]string{"index.html": "home", ".env": "SECRET=1"} |
| + without := map[string]string{"index.html": "home"} |
| + |
| + t.Run("block mode", func(t *testing.T) { |
| + // The hard tier blocks /.env whether or not the site has it, and a |
| + // block is never replaced: the hard block page, never a burst 429. |
| + for _, files := range []map[string]string{withEnv, without} { |
| + g := newBurstStaticRig(t, "block", files) |
| + g.trip(t, burstHost) |
| + w := g.get(burstHost, "/.env") |
| + if w.Code != 404 || w.Header().Get("X-Carlos-Ref") == "" || strings.Contains(w.Body.String(), "SECRET") { |
| + t.Fatalf("%d %q; want the hard block page", w.Code, w.Body.String()) |
| + } |
| + if m := g.lastMatch(t, "/.env"); m.Kind != reqrec.KindBlock || m.RuleID != "hard.secrets.env" { |
| + t.Fatalf("match %+v, want the hard block", m) |
| + } |
| + } |
| + }) |
| + t.Run("burst rule promoted", func(t *testing.T) { |
| + promoteBurstRule(t) |
| + g := newBurstStaticRig(t, "", withEnv) |
| + g.trip(t, burstHost) |
| + if w := g.get(burstHost, "/.env"); w.Code != 200 || w.Body.String() != "SECRET=1" { |
| + t.Fatalf("existing /.env: %d %q; want the hard tier's watch to stand", w.Code, w.Body.String()) |
| + } |
| + if m := g.lastMatch(t, "/.env"); m.Kind != reqrec.KindWatch || m.RuleID != "hard.secrets.env" { |
| + t.Fatalf("existing /.env: match %+v, want the hard watch", m) |
| + } |
| + |
| + g = newBurstStaticRig(t, "", without) |
| + until := g.trip(t, burstHost) |
| + g.wantRefused(t, g.get(burstHost, "/.env"), until) |
| + wantBurstMatch(t, g.lastMatch(t, "/.env"), reqrec.KindBlock, reqrec.CooldownFromThisApp, until) |
| + }) |
| +} |
diff --git a/internal/edge/edge.go b/internal/edge/edge.go| index 6aa3e426f..7a9d1768f 100644 |
| --- a/internal/edge/edge.go |
| +++ b/internal/edge/edge.go |
| @@ -2181,10 +2181,6 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| if e.guard != guardOff && !isDoor && !hopDoorFrom(r.Context()) && !fromTunnel(r.Context()) { |
| out.guard = e.newGuardState() |
| out.guard.acct, out.guard.app = release.RouteAccount(up.route), up.route.App |
| - // A verified hop carrying a fill assertion is a far edge's |
| - // asset fill, whatever becomes of it here (serveAssetFillForHop |
| - // may still take it as an ordinary hop): the far edge counts it. |
| - out.assetFill = hopped && r.Header.Get(assetFillHeader) != "" |
| } |
| defer func() { |
| // An acknowledged home fill is billed by the far edge that |
| @@ -2225,19 +2221,25 @@ func (e *Edge) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| // offered the burst rule first: refused in place of the block page when |
| // the mode blocks it, and otherwise a verdict that any block below still |
| // replaces (decideVerdict). A fleet app's request, whether a forward row |
| - // or another box's tunnel, is only ever recorded. |
| + // or another box's tunnel, is only ever recorded. A static site this box |
| + // serves is offered it by the static handler, hard tier included, once |
| + // the tree has said whether the path exists: the cool-down never refuses |
| + // a file the site has. |
| if out != nil && out.guard != nil { |
| var hit edgeguard.Hit |
| var matched bool |
| - if up.route.Kind == "static" && !up.viaTunnel { |
| + staticHere := up.route.Kind == "static" && !up.viaTunnel |
| + if staticHere { |
| hit, matched = edgeguard.CheckTier(r.URL.Path, r.URL.RawQuery, reqrec.TierHard) |
| } else { |
| hit, matched = edgeguard.Check(r.URL.Path, r.URL.RawQuery) |
| } |
| if matched { |
| - fleet := up.viaTunnel || up.route.IsForward() |
| - if until, from, cooling := out.guard.cooling(r); cooling && burstDecide(w, r, until, from, fleet) { |
| - return |
| + if !staticHere { |
| + fleet := up.viaTunnel || up.route.IsForward() |
| + if until, from, cooling := out.guard.cooling(r); cooling && burstDecide(w, r, until, from, fleet) { |
| + return |
| + } |
| } |
| if guardDecide(w, r, hit) { |
| return |
diff --git a/internal/edge/guard.go b/internal/edge/guard.go| index ae3008f66..34243563d 100644 |
| --- a/internal/edge/guard.go |
| +++ b/internal/edge/guard.go |
| @@ -267,8 +267,8 @@ func burstNotFound(status int, v *guardVerdict) bool { |
| // the observer defer of the box that saw the visitor's own headers: a |
| // request the guard ran for, that no browser sent (every current browser |
| // sends Sec-Fetch-Mode on every request, scanners do not), and that is a |
| -// not-found by burstNotFound. A request that arrived as a far edge's asset |
| -// fill is the far edge's to count. On a trip it mints the trip's code once, |
| +// not-found by burstNotFound. A far edge's asset fill this box acknowledged |
| +// is the far edge's to count. On a trip it mints the trip's code once, |
| // files the trip record and tells the other edges. |
| func (e *Edge) noteBurst(rt registry.Route, r *http.Request, status int, out *outcome) { |
| if e.burst == nil || out == nil || out.guard == nil || out.assetFill { |
diff --git a/internal/edge/static.go b/internal/edge/static.go| index 4cc913849..7b8fe36fb 100644 |
| --- a/internal/edge/static.go |
| +++ b/internal/edge/static.go |
| @@ -578,12 +578,14 @@ func (s *staticSite) serveTree(tree servedTree, w http.ResponseWriter, r *http.R |
| // below, once its target is known. |
| if guard && !found && !tree.redirects.names(requestHost(r.Host), p) { |
| probe, _ = edgeguard.CheckTier(r.URL.Path, r.URL.RawQuery, reqrec.TierProbe) |
| - // The probe-burst cool-down refuses a catalogue path only |
| - // where the site is not known to have it, which is this |
| - // point. A hard-tier hit on the request path was offered in |
| - // ServeHTTP already: the hard tier refuses even a file that |
| - // is there. |
| - if probe.Rule != nil && staticBurstDecide(w, r) { |
| + // The probe-burst cool-down refuses a catalogue path, hard |
| + // tier or probe, only where the site is not known to have |
| + // it, which is this point; ServeHTTP leaves it here for |
| + // both. The hard tier's own verdict on the request path was |
| + // decided there and stands, for a file that is here too. |
| + // The rewrite branch below still checks what a rewrite |
| + // would send after a burst verdict that does not block. |
| + if staticBurstHit(r, probe) && staticBurstDecide(w, r) { |
| return |
| } |
| } |
| @@ -673,6 +675,16 @@ func staticGuardPending(r *http.Request) bool { |
| return out != nil && out.guard != nil && (out.guard.verdict == nil || !out.guard.verdict.block) |
| } |
| +// staticBurstHit reports whether r's path is in the catalogue: probe is the |
| +// probe tier's hit, and the hard tier is asked only when that missed. |
| +func staticBurstHit(r *http.Request, probe edgeguard.Hit) bool { |
| + if probe.Rule != nil { |
| + return true |
| + } |
| + _, hard := edgeguard.CheckTier(r.URL.Path, r.URL.RawQuery, reqrec.TierHard) |
| + return hard |
| +} |
| + |
| // staticBurstDecide offers the burst rule for a catalogue path the site does |
| // not have, when r's client is cooling. A static route this box serves is |
| // never a fleet's. |