feat: implement IP-based rate limiting on scan endpoints - #348
Open
aaryankamdar2005 wants to merge 3 commits into
Open
feat: implement IP-based rate limiting on scan endpoints#348aaryankamdar2005 wants to merge 3 commits into
aaryankamdar2005 wants to merge 3 commits into
Conversation
|
🎉 Thank you @aaryankamdar2005 for submitting a Pull Request! We're excited to review your contribution. ✅ Before Review
⚡ Want faster reviews and contributor support? Join our Discord community: 🔗 https://discord.gg/FcXuyw2Rs Maintainers and mentors are active there and can help resolve blockers quickly. Happy Contributing! 🚀 |
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.
Linked issue
(Add your issue number here if you have one, e.g., Closes #123. Otherwise, you can remove this section)
What this PR does
The
/scanand/scan-urlendpoints execute resource-intensive operations (running external security scanners and ML models). Currently, there is no rate limiting, allowing a single client to overwhelm the server by sending repeated scan requests.This PR implements IP-based rate limiting using
slowapito protect the service from abuse and accidental resource exhaustion. It applies a default limit of 5 requests per minute per IP, configurable via theSCAN_RATE_LIMITenvironment variable.Type of change
ML tier (if applicable)
Stack affected
Changes
Backend
slowapias a backend dependency._scan_rate_limitdependency to guard/scanand/scan-url._get_client_ipto correctly extract IP addresses from theX-Forwarded-Forheader (for proxy/load balancer deployments).Retry-Afterheader.README.mddocumentation for environment variables.anyiowould attempt to runasyncio-specific concurrency tests under thetriobackend by explicitly configuringanyio_backendintests/conftest.py.Frontend
New dependencies
slowapiDatabase / schema changes
Testing
How did you test this?
tests/test_rate_limit.py) verifying that the 6th request within the minute limit window correctly returns an HTTP 429.Checklist
console.erroror unhandled Python exceptions introducedrequirements.txt/package.jsonupdated if new dependencies added.pkl,.pt, etc.) are gitignored, not committedAnything reviewers should focus on
Depends(_scan_rate_limit)rather than@limiter.limitdirectly on the endpoints to sidestep an incompatibility between Python 3.13'sfrom __future__ import annotations, FastAPI's dependency resolver, andslowapi's wrapper logic.conftest.py: Addedtests/conftest.pyto lockanyiotests toasyncioto fix the pre-existing CI test failures on[trio]tests.Screenshots (if UI changed)
N/A