Repository navigation
fix(server): key the API rate limiter by auth token instead of IP - #537
Merged
Merged
Conversation
All of the web app's server actions egress through Vercel's small shared IP pool, so the IP-keyed 250/15min limiter lumped every user's server-side traffic (volume analyses, tracking-status polls, page loads) into a handful of buckets and returned 429 to legitimate users - surfaced as a bare 'Volume analysis failed' toast on Analyze Prompts. Authenticated requests now get a per-token bucket (hashed, so raw JWTs never sit in the limiter store) with a 1000/15min ceiling sized for a single user's own burst; anonymous traffic keeps the tighter 250/15min IP limit, with IPv6 collapsed to /64 so a subnet can't rotate around it.
Keying purely on the Authorization header would let an attacker mint a fresh bucket per forged token. decodeToken now records each Supabase-verified token in a short-lived hashed cache, and the limiter only promotes cached tokens to their user's 1000/15min bucket — forged or garbage headers stay pinned to the caller's 250/15min IP bucket. A refreshed token maps to the same user bucket.
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
A user pressing Analyze Prompts got a bare
Volume analysis failedtoast. Vercel runtime logs show the real cause: the Express API answered 429 Too Many Requests (Error: Server error: 429onPOST /dashboard/prompts, repeatedly).The dashboard API limiter (
app.use('/api', apiLimiter)) allows 250 requests / 15 min per IP. But every web-app server action (volume analyses,getPromptVolumespage loads, tracking-status polls every 15s per open tab…) reaches the Express server from Vercel's small shared egress IP pool — so the whole platform's server-side traffic competes for a handful of IP buckets and legitimate users get 429s, while browser-direct calls (user's own IP) sail through. That's why only server-action-backed features appeared broken.Fix — per-user buckets, but only for verified users:
decodeTokenalready verifies every token against Supabase Auth; on success it now records the token (SHA-256 hash only, never the raw JWT) in a short-lived capped cache (verified-tokens.js, 30 min TTL, 5k entries, oldest-first eviction).apiLimiterkeys requests whose token is in that cache asuser:<id>with a 1000/15min ceiling (sized for one user's own burst: several tabs polling every 15s plus navigation).publicLimiter,trafficLimiter,authLimiterare unchanged (they guard unauthenticated surfaces where IP keying is correct).Related issue
—
Type of change
fix— Bug fixHow to test
cd server && yarn test—rate-limiter.test.jscovers: unverified/forged headers share the IP bucket, verified tokens bucket per user across IPs, refreshed token ⇒ same user bucket, distinct users ⇒ distinct buckets, anonymous per-IP fallback, IPv6 /64 collapsing, v4-mapped IPv6 per-address./apitraffic no longer 429s at 250/15min per shared IP; requests with a made-up bearer token still 429 at the IP limit.Checklist
feature/,fix/,chore/,docs/)cd server && yarn test)