Skip to content

fix(server): key the API rate limiter by auth token instead of IP - #537

Merged
gkhngyk merged 2 commits into
mainfrom
fix/api-rate-limit-per-user
Jul 27, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
fix/api-rate-limit-per-user

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A user pressing Analyze Prompts got a bare Volume analysis failed toast. Vercel runtime logs show the real cause: the Express API answered 429 Too Many Requests (Error: Server error: 429 on POST /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, getPromptVolumes page 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:

  • decodeToken already 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).
  • apiLimiter keys requests whose token is in that cache as user:<id> with a 1000/15min ceiling (sized for one user's own burst: several tabs polling every 15s plus navigation).
  • Everything else — anonymous traffic AND forged/garbage Authorization headers — stays pinned to the caller's 250/15min IP bucket, so spamming random tokens cannot mint fresh buckets. A legit token's first-ever request counts against the IP bucket once (it isn't verified yet); after that it rides its user bucket. A refreshed token maps to the same user bucket.
  • Anonymous IPv6 collapses to its /64 prefix so a caller can't rotate through a subnet; IPv4 and v4-mapped IPv6 stay per-address. IP rotation at scale remains an infra concern (WAF/CDN) — same as before this change.
  • publicLimiter, trafficLimiter, authLimiter are unchanged (they guard unauthenticated surfaces where IP keying is correct).

Related issue

—

Type of change

  • fix — Bug fix

How to test

  1. cd server && yarn test — rate-limiter.test.js covers: 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.
  2. Manually: authenticated /api traffic no longer 429s at 250/15min per shared IP; requests with a made-up bearer token still 429 at the IP limit.
  3. In production: Analyze Prompts on a brand with many prompts completes instead of failing instantly.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/)
  • Commits follow Conventional Commits
  • Server test suite passes (cd server && yarn test)
  • Prettier passes on changed files
  • Changes are focused — one concern per PR

gkhngyk added 2 commits July 27, 2026 14:45
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.
@gkhngyk
gkhngyk merged commit a8dafce into main Jul 27, 2026
3 checks passed
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