fix: per-user rate limiting and surface 429/502 errors in the UI - #22
Merged
Conversation
The 2/min limit was keyed on request.client.host, which behind the Next.js proxy is always the proxy IP, so it acted as a whole-service budget instead of a per-user one. Key slowapi on the leftmost X-Forwarded-For entry (the proxy forwards the real client IP) and bump the limit to 10/min. The proxy route also swallowed the upstream status code, so a 429/502 reached the browser as a 200 and the SSE parser silently dropped the JSON error body, leaving the UI stuck on loading forever. Forward status/statusText and the upstream content-type, and map 429 to a clear rate-limit message in useSearch.
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…status # Conflicts: # frontend/app/api/search/route.ts
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 freeto 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
request.client.host, which behind the Next.js proxy is always the proxy IP — so2/minuteacted as a whole-service budget, not per-user. Addedrate_limit_key()inbackend/app/main.pythat reads the leftmostX-Forwarded-Forentry (the proxy forwards the real client IP) and bumped the limit to10/minute.frontend/app/api/search/route.ts) usednew Response(upstream.body)without forwarding the upstream status, so a 429/502 reached the browser as a 200. The SSE parser then silently consumed the JSON error body (noevent:/data:lines), dispatched nothing, and the UI sat onloadingforever.useSearch.tsnow parses the error body and maps 429 → "Rate limited — too many searches" instead of hanging.rate_limit_key(backend/app/test/test_rate_limit.py).Test plan
uv run ruff check app/clean,uv run pytest -q→ 20 passednpx eslintclean,npx jest --silent→ 25 passed429 {"detail":"10 per 1 minute"}, different IP unaffected