Repository navigation
test(routes): rate-limiter-ahead-of-auth guard for every router.<verb> registration (B3) - #1682
Merged
Merged
Conversation
…ter ahead of auth (B3) Readiness plan §3B row B3. CodeQL's js/missing-rate-limiting found three routes after they were pushed on 2026-09-11; this jest test finds the next one in `npm test`, before the push. `__tests__/utils/routeRateLimitScan.js` walks backend/routes with the TypeScript compiler API and classifies each `<router>.<verb>(path, …)` chain by name: a `*limit*` argument is the limiter, an `*auth*` / `require<Capital>` / `isAdmin` / `signed` argument is the auth guard, inline functions are never classified, and a preceding file-level `router.use(<limiter>)` covers the registration. Two reasons are reported: `unlimited` and `limiter-after-auth` (the auth lookup is the DB work the CodeQL query anchors on, per the phase4RateLimit comment in routes/agentsRuntime.ts). The guard does not try to detect "does DB work": CodeQL treats any handler that reaches a model or an auth check as expensive, which is every route here, so the rule applies to all of them and today's exceptions live in `routeRateLimitGuard.baseline.json` — 261 rows measured at cd91e2e (250 unlimited, 11 limiter-after-auth, all the agentsRuntime phase4RateLimit routes). The baseline may only shrink: a new violation fails with the file:line and the fix; a row that becomes compliant fails until it is deleted, so the burn-down list never over-reports. Co-Authored-By: Claude Fable 5.1 <[email protected]>
…the guard and its first baseline shrink Sam's delegate (68036): the failure message now prints the import, the limiter construction and the registration shape a builder pastes, next to the file:line METHOD path it already named. The readiness plan's B3 row records the guard and names the 11 agentsRuntime limiter-after-auth rows as the baseline's first shrink, a separate follow-up PR. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Contributor
Author
|
Head for gating is a2b4762 (base cd91e2e), not ea6d228. The second commit carries the two delegate asks from pod message 68036 and Sam's follow-up in 68041:
No other change between the two commits. CI at a2b4762: everything green except Test & Coverage, still running. |
lilyshen0722
added a commit
that referenced
this pull request
Sep 12, 2026
…utes; close the guard's three blind spots (B3 first shrink) (#1689) * fix(routes): phase4RateLimit ahead of auth on the 11 agentsRuntime routes; close the guard's three blind spots (B3 first shrink) Readiness plan §3B row B3, first baseline shrink. routes/agentsRuntime.ts: every remaining `agentRuntimeAuth, phase4RateLimit` / `dualAuth, phase4RateLimit` registration now runs the limiter first, as the note above the limiter has specified since 2026-08-04. Pre-auth the key generator takes its header-hash branch (same per-caller isolation, different key prefix); no key-generator change. The 11 `limiter-after-auth` rows leave routeRateLimitGuard.baseline.json (254 → 243). __tests__/utils/routeRateLimitScan.js closes the three blind spots Vera probed at #1682 (68057), each with a classification test: - a file-level `router.use(auth)` ahead of a route's limiter now classifies as limiter-after-auth (file-level use calls are prepended to the chain in file order, so `use(limiter)` before `use(auth)` still covers); - the last argument is the handler and is never classified, so a handler named `getLimits` no longer passes as a limiter; - `router.route(path).all(…).get(…)` chains are scanned per verb, with `.all()` middleware ahead of the verb's own. None of the three changes any current row: the scan against main matches the shrunk baseline exactly (431 registrations, 243 violations). TESTING.md and the plan row updated. Co-Authored-By: Claude Fable 5.1 <[email protected]> * fix(routes): coarse per-IP tier ahead of the per-token phase4 limiter Pre-auth, agentRateLimitKeyGenerator keys on sha256(Authorization), so a caller rotating the header minted a fresh bucket per request (Vera's gate on #1689: 300 rotating headers reached auth 300 times). phase4RateLimit is now [phase4IpRateLimit (Cloudflare-keyed, 3000/60s), phase4AgentRateLimit (120/60s)] — the same two-tier shape as the B2 webhook routes — so every registration in agentsRuntime.ts picks up the IP tier first, including the routes that were already pre-auth on main with the same hole. agentsRuntime.rateLimitTiers.test.js drives the real router: a fixed header reaches auth 120 times of 300; 3000 rotating headers from one IP reach auth 3000 times and the 3001st is 429. Co-Authored-By: Claude Fable 5.1 <[email protected]> --------- Co-authored-by: Claude Fable 5.1 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Readiness plan
docs/plans/production-readiness-and-selling-2026-09-11.md§3B row B3. CodeQL'sjs/missing-rate-limitingcaught three routes after they were pushed on 2026-09-11; this test catches the next one innpm test, before the push.backend/__tests__/utils/routeRateLimitScan.js— walksbackend/routeswith the TypeScript compiler API and classifies every<router>.<verb>(path, …middleware)chain by name:*limit*is the limiter,*auth*/require<Capital>/isAdmin/signedis the auth guard, inline functions are never classified (solimit = '20'in a handler body cannot pass), and a preceding file-levelrouter.use(<limiter>)covers later registrations.backend/__tests__/unit/routes/routeRateLimitGuard.test.js— fails when a registration isunlimitedorlimiter-after-authand is not in the baseline; fails when a baseline row is no longer a violation (fixed, removed or renamed) until it is deleted. Eight fixture tests pin the classifier.backend/__tests__/unit/routes/routeRateLimitGuard.baseline.json— 261 rows at cd91e2e: 250 unlimited, 11 limiter-after-auth (everyagentRuntimeAuth, phase4RateLimitroute inroutes/agentsRuntime.ts, the shape the comment abovephase4RateLimitalready calls under-protected). Keyedfile METHOD path [reason], no line numbers, so unrelated edits don't churn it. May only shrink.backend/TESTING.md— one authoring rule pointing at the guard and the canonical shape (routes/agentHooks.ts).Deliberately not detected: whether a handler "does DB work". CodeQL treats a handler that reaches a model, the filesystem or an auth check as expensive, which is every route here, so the guard asks the same of all of them and keeps today's exceptions in the baseline rather than in a heuristic.
Proof
npx jest __tests__/unit/routes/routeRateLimitGuard→ 12 passed.router.get('/wren-probe', agentRuntimeAuth, …)toroutes/agentHooks.tsfails withroutes/agentHooks.ts:145 GET /wren-probe (unlimited; auth=agentRuntimeAuth)and the fix text; swappingagentRuntimeAuth, phase4RateLimitto limiter-first inagentsRuntime.tsfails the stale-baseline test listing exactly those rows.eslintclean on both new.jsfiles.Not in this PR
Fixing the 11
phase4RateLimit-after-auth rows is burn-down, not guard work; it is the "8 route registrations in a different subsystem" diff that comment already specifies. Each fix deletes its baseline row.🤖 Generated with Claude Code