-
Notifications
You must be signed in to change notification settings - Fork 4.4k
fix(api): rate-limit magic-code verify, bound per-token attempts (GHSA-9pvm-fcf6-9234) #9130
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
base: preview
Are you sure you want to change the base?
Changes from 1 commit
f817880
4d836df
c14182b
8cdbeb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,11 @@ | |
| class MagicCodeProvider(CredentialAdapter): | ||
| provider = "magic-code" | ||
|
|
||
| # Max wrong-code verification attempts per issued token before the token | ||
| # is invalidated. Prevents brute-forcing the 6-digit code space within | ||
| # the token TTL window. | ||
| MAX_VERIFY_ATTEMPTS = 5 | ||
|
|
||
| def __init__(self, request, key, code=None, callback=None): | ||
| (EMAIL_HOST, ENABLE_MAGIC_LINK_LOGIN) = get_configuration_value( | ||
| [ | ||
|
|
@@ -119,7 +124,34 @@ def set_user_data(self): | |
| return | ||
| else: | ||
| email = str(self.key).replace("magic_", "", 1) | ||
| if User.objects.filter(email=email).exists(): | ||
| user_exists = User.objects.filter(email=email).exists() | ||
|
|
||
| # Increment the verify-attempt counter and persist with the | ||
| # remaining TTL so the lockout window matches the token window. | ||
| verify_attempts = data.get("verify_attempts", 0) + 1 | ||
| data["verify_attempts"] = verify_attempts | ||
| remaining_ttl = ri.ttl(self.key) | ||
| if remaining_ttl is None or remaining_ttl < 0: | ||
| remaining_ttl = 1 | ||
| ri.set(self.key, json.dumps(data), ex=remaining_ttl) | ||
|
|
||
| if verify_attempts >= self.MAX_VERIFY_ATTEMPTS: | ||
| # Invalidate the token so further attempts must regenerate | ||
| # (which is itself attempt-counted on the generation path). | ||
| ri.delete(self.key) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| if user_exists: | ||
|
Comment on lines
+169
to
+174
Member
Author
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. Off-by-one resolved by updating the PR description's test plan to match the actual semantics: |
||
| raise AuthenticationException( | ||
| error_code=AUTHENTICATION_ERROR_CODES["EMAIL_CODE_ATTEMPT_EXHAUSTED_SIGN_IN"], | ||
| error_message="EMAIL_CODE_ATTEMPT_EXHAUSTED_SIGN_IN", | ||
| payload={"email": str(email)}, | ||
| ) | ||
| raise AuthenticationException( | ||
| error_code=AUTHENTICATION_ERROR_CODES["EMAIL_CODE_ATTEMPT_EXHAUSTED_SIGN_UP"], | ||
| error_message="EMAIL_CODE_ATTEMPT_EXHAUSTED_SIGN_UP", | ||
| payload={"email": str(email)}, | ||
| ) | ||
|
sriramveeraghanta marked this conversation as resolved.
|
||
|
|
||
| if user_exists: | ||
| raise AuthenticationException( | ||
| error_code=AUTHENTICATION_ERROR_CODES["INVALID_MAGIC_CODE_SIGN_IN"], | ||
| error_message="INVALID_MAGIC_CODE_SIGN_IN", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.