Add rate limiting to feeds endpoints.#927
Add rate limiting to feeds endpoints.#927regulartim merged 3 commits intoGreedyBear-Project:developfrom
Conversation
7508c97 to
6abffeb
Compare
regulartim
left a comment
There was a problem hiding this comment.
Please make sure CI is happy.
| return feeds_response(iocs_queryset, feed_params, valid_feed_types) | ||
|
|
||
|
|
||
| feeds.throttle_scope = "feeds" |
There was a problem hiding this comment.
The feeds.throttle_scope assignment sitting between function definitions is weird. According to the DRF docs there are cleaner ways to do that.
|
Thanks for the feedback! |
6abffeb to
c5658b7
Compare
c5658b7 to
ff91817
Compare
ff91817 to
48e0cbb
Compare
ScopedRateThrottle.allow_request() always overwrites self.scope from view.throttle_scope, which our function-based views don't set. This caused scope to be None and all requests to bypass throttling entirely. Switch to SimpleRateThrottle (like DRF's built-in AnonRateThrottle and UserRateThrottle), which properly reads scope from the class attribute and applies throttling in __init__.
|
Hope CI is good, switched to |
regulartim
left a comment
There was a problem hiding this comment.
Hey @manik3160 , core implementation looks good. A few things:
- cache.clear() runs on every of our ~500 test cases although it is only needed on the throttle tests
- throttle rates should be configurable via environment variables in the env_file_templte
- I can't find any test for unauthenticated access to public endpoints
|
@regulartim Thanks for the review and feedback. I’ve updated the implementation by limiting |
regulartim
left a comment
There was a problem hiding this comment.
Looks good now, thanks! :)
…reedyBear-Project#927) * Add rate limiting to feeds endpoints. * fix: use SimpleRateThrottle instead of ScopedRateThrottle ScopedRateThrottle.allow_request() always overwrites self.scope from view.throttle_scope, which our function-based views don't set. This caused scope to be None and all requests to bypass throttling entirely. Switch to SimpleRateThrottle (like DRF's built-in AnonRateThrottle and UserRateThrottle), which properly reads scope from the class attribute and applies throttling in __init__. * refactor: address review — scope cache.clear, env-configurable rates, unauth tests --------- Co-authored-by: Manik <maniksingh3606@gmaile.com>
Description
Adds rate limiting to all feeds API endpoints using DRF's
ScopedRateThrottleto prevent abuse and reduce unnecessary load on the database and Elasticsearch.Per @regulartim's feedback on #923, throttling is applied only to feeds endpoints, not to enrichment, command_sequence, or cowrie_session (since researchers may need to use these heavily, and they require authentication).
Changes
api/views/feeds.py: AddedScopedRateThrottleto all 4 feeds views with scoped rates:feeds+feeds_pagination(public) → 30 requests/minute per IPfeeds_advanced+feeds_asn(authenticated) → 100 requests/minute per usergreedybear/settings.py: AddedDEFAULT_THROTTLE_RATESconfigurationtests/api/views/test_feeds_throttle.py: Added 6 tests verifying throttle behaviorRelated issues
Closes #923
Type of change
Checklist
Formalities
<feature name>. Closes #999develop.develop.Docs and tests
Ruff) gave 0 errors. If you have correctly installed pre-commit, it does these checks and adjustments on your behalf.