Skip to content

test(routes): rate-limiter-ahead-of-auth guard for every router.<verb> registration (B3) - #1682

Merged
lilyshen0722 merged 2 commits into
mainfrom
wren/limiter-guard
Sep 12, 2026
Merged

lilyshen0722 merged 2 commits into
mainfrom
wren/limiter-guard

Conversation

@lilyshen0722

Copy link
Copy Markdown
Contributor

Summary

Readiness plan docs/plans/production-readiness-and-selling-2026-09-11.md §3B row B3. CodeQL's js/missing-rate-limiting caught three routes after they were pushed on 2026-09-11; this test catches the next one in npm test, before the push.

  • backend/__tests__/utils/routeRateLimitScan.js — walks backend/routes with the TypeScript compiler API and classifies every <router>.<verb>(path, …middleware) chain by name: *limit* is the limiter, *auth* / require<Capital> / isAdmin / signed is the auth guard, inline functions are never classified (so limit = '20' in a handler body cannot pass), and a preceding file-level router.use(<limiter>) covers later registrations.
  • backend/__tests__/unit/routes/routeRateLimitGuard.test.js — fails when a registration is unlimited or limiter-after-auth and 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 (every agentRuntimeAuth, phase4RateLimit route in routes/agentsRuntime.ts, the shape the comment above phase4RateLimit already calls under-protected). Keyed file 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.
  • Negative probe (reverted before commit): appending router.get('/wren-probe', agentRuntimeAuth, …) to routes/agentHooks.ts fails with routes/agentHooks.ts:145 GET /wren-probe (unlimited; auth=agentRuntimeAuth) and the fix text; swapping agentRuntimeAuth, phase4RateLimit to limiter-first in agentsRuntime.ts fails the stale-baseline test listing exactly those rows.
  • eslint clean on both new .js files.

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

lilyshen0722 and others added 2 commits September 12, 2026 01:44
…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]>
@lilyshen0722

Copy link
Copy Markdown
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:

  • the failure message prints the one-line fix (ESM import rateLimit from 'express-rate-limit', limiter const, router.<verb>(path, <name>RateLimit, auth, handler)) beside file:line METHOD path (reason; auth=; limiter=);
  • the plan's B3 row (docs/plans/production-readiness-and-selling-2026-09-11.md) names the 11 routes/agentsRuntime.ts phase4RateLimit limiter-after-auth rows as the baseline's first shrink, follow-up PR.

No other change between the two commits. CI at a2b4762: everything green except Test & Coverage, still running.

@lilyshen0722
lilyshen0722 merged commit b56e9a4 into main Sep 12, 2026
14 checks passed
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant