fix(middleware): stop rate-limiting auth page loads, cap only auth attempts#161
Merged
Conversation
…tempts The /login, /signup, /accept-invite, /auth/callback, and /reset-password routes shared a 10 requests/min per-IP budget that counted GET page loads, Next.js Link prefetches, and RSC requests alongside actual sign-in attempts. A user bouncing between login and signup tripped it within a minute and got a bare Too Many Requests page (seen on staging). Split the budget by method: mutating requests (real auth attempts) keep the strict 10/min production cap; GET/HEAD page loads get a separate 120/min bucket a human cannot plausibly trip but that still slows scanners. Extracted the decision into src/lib/rate-limit.ts with a contract verifier (pnpm test:rate-limit) wired into CI.
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.
Problem
staging.getminutia.com/signup intermittently served a bare
Too Many Requestspage. The middleware capped ALL requests to auth pages (/login,/signup,/accept-invite,/auth/callback,/reset-password) at 10/min per IP in production, and that budget was consumed by GET page loads, Next.js Link prefetches, and RSC requests, not just sign-in attempts. A user bouncing between login and signup trips it inside a minute and is locked out of the page for the rest of the window. Credential POSTs go straight to GoTrue from the browser client, so the old cap was in practice throttling only page views.Fix
Split the budget by method in a new pure module
src/lib/rate-limit.ts:auth-pagebucket, 120/min prod (2000 dev), a ceiling a human cannot plausibly trip but that still slows scannersauth-attemptbucketTests
scripts/verify-rate-limit.test.mjs(pnpm test:rate-limit, wired into CI): every branch ofauthRateBudgetandisRateLimited(window reset, key independence, bucket separation, prod/dev budgets)