Repository navigation
Key rate limiter on user id for admins - #467
Conversation
PR Review — #467 Rate limiter per-token bucketingReviewed by 5 specialized agents: security, test coverage, error handling, code quality, admin capacity. CriticalNone. Important[security] Short-token sends land in shared IP bucket, not their own bucket [security/operational] "unknown" IP pools all headerless requests into one bucket const ip = c.req.header("CF-Connecting-IP")
|| c.req.header("X-Forwarded-For")
|| crypto.randomUUID(); // don't pool unknown IPs[code] export function __selectBucket(...): { keyKind: "auth-ip"|"ip"|"token"; rawKey: string; maxRequests: number }[code] AUTH_PATHS.some((p) => path === p || path.startsWith(p + "/") || path.startsWith(p + "?"))[tests] In-memory cache never expires entries [code] Naming: Suggestions[tests] No E2E test for [docs] Document that the rate limit is per-PoP, not globally consistent [docs] Explicitly state the window type is fixed (not sliding) [code] Nice to haves
Admin capacity verdict500/60s is sufficient for all known admin workflows:
False positives / non-issues
Overall verdict: No blocking issues. The core fix (per-token bucketing to eliminate admin/IP bucket contention) is correct and well-tested. The |
- Rename BucketSelection -> __BucketSelection (test-only convention) - Rename AUTH_MAX_REQUESTS_AUTHED -> TOKEN_MAX_REQUESTS_AUTHED (clearer) - Tighten AUTH_PATHS matching: exact or /- or ?-prefixed subpath only - Replace "unknown" IP fallback with crypto.randomUUID() to avoid pooling - Add TTL-aware InMemoryCache and window-reset test (14th test)
Review findings addressed (commit 3ada43e)All 5 Important findings resolved:
All 14 tests pass ( Suggestions / nice-to-haves not addressed (false positives or intentional):
|
📦 Package PreviewYour package is ready to test! bun install -g nemar-cli@PR467Version: 🤖 Updated on each push. |
Closes #275.
What changed
The Worker rate limiter used to key every request on IP and exempt
/admin/*entirely. Exempting admin paths gave an admin running amalformed loop unbounded access to the worker, and keying everything
else on IP meant several admins (or one admin running back-to-back
publication approves) shared one 100/60s bucket — exactly the regression
the 4 stuck datasets in #275 hit.
The middleware now picks the bucket from the request:
/auth/*endpoints stay IP-keyed at 10/60s (pre-auth, must resistpassword guessing).
(length ≥ 32, matching what the auth middleware accepts) is keyed by
the SHA-256 hash of that token at 500/60s. Admin orchestration
fits comfortably (the heaviest single
publish approvewe've seenfires ~165 sequential subrequests; 500/60s gives ~3× headroom and
allows a small queue of back-to-back operations) and a runaway loop
still 429s.
The admin-path exemption is gone — admin requests are bounded, just at
the higher cap.
Why 500 (not 1000)
The user note in #275 suggested 500 or 1000. 500 covers the worst
real orchestration we observe by ~3× while still leaving a meaningful
floor on a misbehaving admin script. 1000 would push the cap past the
point where a runaway loop is bounded by the limiter and would be
bounded only by Worker CPU/subrequest budgets — at that point the
limiter is decorative. Sized to be useful, not theatrical.
What is NOT in this PR
The 4 currently-stuck datasets (on005515, on005516, nm000110 at
s3_lock; on002718 at ci_check) are not touched here — the user owns
that recovery action per the issue's scoping note. The fix lets those
resume commands actually go through once it lands.
Operator runbook — clear the stuck datasets after this lands
The CLI's retry-with-delay loop (
approvePublicationinsrc/lib/api.ts)will automatically retry transient 5xx/429 on top of these commands.
Test plan
backend/test/rate-limit-buckets.test.tsexercising:__selectBucketdecision logic across auth-path,token-bearing, and unauthenticated requests.
with an in-memory
Cache(not a mock — a working Cache backed byMap): 150 authenticated requests on the same IP don't 429, 150
unauthenticated requests 429 after the 100th, two distinct tokens
on the same IP don't share a bucket, and an authenticated client
still 429s past the 500 cap.
bun testin backend/: 49 pass, 0 fail (was 35 pre-change).bun run typecheck(root + backend) clean.bun run buildclean.