-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Unauthenticated api throttling #20993
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import hashlib | ||
from rest_framework.throttling import SimpleRateThrottle | ||
|
||
|
||
class UserOrEmailRateThrottle(SimpleRateThrottle): | ||
""" | ||
Typically throttling is on the user or the IP address. | ||
For unauthenticated signup/login requests we want to throttle on the email address. | ||
""" | ||
|
||
scope = "user" | ||
|
||
def get_cache_key(self, request, view): | ||
if request.user and request.user.is_authenticated: | ||
ident = request.user.pk | ||
else: | ||
# For unauthenticated requests, we want to throttle on something unique to the user they are trying to work with | ||
# This could be email for example when logging in or uuid when verifying email | ||
ident = request.data.get("email") or request.data.get("uuid") or self.get_ident(request) | ||
ident = hashlib.sha256(ident.encode()).hexdigest() | ||
|
||
return self.cache_format % {"scope": self.scope, "ident": ident} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -434,6 +434,23 @@ def test_cant_reset_more_than_six_times(self): | |
# Three emails should be sent, fourth should not | ||
self.assertEqual(len(mail.outbox), 6) | ||
|
||
def test_is_rate_limited_on_email_not_ip(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm lazily assuming there's a test in here that shows the reverse (if we have the same IP but varying addresses then we don't limit (or we're slower to limit)) |
||
set_instance_setting("EMAIL_HOST", "localhost") | ||
|
||
for email in ["[email protected]", "[email protected]"]: | ||
for i in range(7): | ||
with self.settings(CELERY_TASK_ALWAYS_EAGER=True, SITE_URL="https://my.posthog.net"): | ||
response = self.client.post("/api/reset/", {"email": email}) | ||
if i < 6: | ||
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) | ||
else: | ||
# Fourth request should fail | ||
self.assertEqual(response.status_code, status.HTTP_429_TOO_MANY_REQUESTS) | ||
self.assertDictContainsSubset( | ||
{"attr": None, "code": "throttled", "type": "throttled_error"}, | ||
response.json(), | ||
) | ||
|
||
# Token validation | ||
|
||
def test_can_validate_token(self): | ||
|
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a note here as I wasn't sure why we had no throttler for logging in