2026-01-27 08:24:22 +00:00
|
|
|
# Copyright (c) 2023-present Plane Software, Inc. and contributors
|
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
# See the LICENSE file for details.
|
|
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
# Django imports
|
|
|
|
|
from django.core.validators import validate_email
|
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
|
from django.views import View
|
2025-09-16 16:07:08 +00:00
|
|
|
from django.utils.http import url_has_allowed_host_and_scheme
|
2024-05-08 17:31:20 +00:00
|
|
|
|
|
|
|
|
# Third party imports
|
|
|
|
|
from rest_framework import status
|
|
|
|
|
from rest_framework.permissions import AllowAny
|
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
|
|
|
|
# Module imports
|
2024-11-21 12:12:44 +00:00
|
|
|
from plane.authentication.provider.credentials.magic_code import MagicCodeProvider
|
2024-05-08 17:31:20 +00:00
|
|
|
from plane.authentication.utils.login import user_login
|
|
|
|
|
from plane.bgtasks.magic_link_code_task import magic_link
|
|
|
|
|
from plane.license.models import Instance
|
|
|
|
|
from plane.authentication.utils.host import base_host
|
2024-06-24 07:24:20 +00:00
|
|
|
from plane.db.models import User
|
2024-05-08 17:31:20 +00:00
|
|
|
from plane.authentication.adapter.error import (
|
|
|
|
|
AuthenticationException,
|
|
|
|
|
AUTHENTICATION_ERROR_CODES,
|
|
|
|
|
)
|
fix(api): rate-limit magic-code verify, bound per-token attempts (GHSA-9pvm-fcf6-9234) (#9130)
* fix(api): rate-limit magic-code verification and bound per-token attempts
The magic-link sign-in / sign-up endpoints accept a 6-digit numeric code
(900k-value space, 600s TTL) but never increment a failure counter on a
wrong-code verify and extend django.views.View rather than DRF APIView,
so DRF's AuthenticationThrottle never runs against them. The space-side
generate endpoint also lacked throttle_classes. Combined, this allowed
an unauthenticated attacker who knew a victim's email to brute-force
the code within the TTL window and log in as the victim.
- Add MAX_VERIFY_ATTEMPTS=5 in MagicCodeProvider.set_user_data: failed
comparisons now persist verify_attempts in Redis under the remaining
TTL and, on hitting the limit, delete the key and raise
EMAIL_CODE_ATTEMPT_EXHAUSTED. This is the load-bearing fix - it caps
total attempts per issued token regardless of request rate.
- Add authentication_throttle_allows() so plain Django Views can apply
AuthenticationThrottle without converting to APIView (would change
CSRF + request-parsing semantics for the redirect-flow endpoints).
- Apply the throttle to MagicSignIn/UpEndpoint and the space variants;
add throttle_classes to MagicGenerateSpaceEndpoint to match its app
sibling.
Refs GHSA-9pvm-fcf6-9234.
* fix(api): make verify-attempt increment atomic, expose throttle rate via env
Address PR review feedback:
- Replace the JSON read-modify-write of verify_attempts with a Lua
EVAL script that INCRs a dedicated counter key and EXPIREs it only
on the first increment. The previous round-trip was racy: parallel
wrong-code requests could read the same value and both write the
same incremented count, letting an attacker exceed MAX_VERIFY_ATTEMPTS
under concurrency. Counter is now reset on each new token issuance
and cleared on successful verify / exhaustion.
- Make AuthenticationThrottle.rate configurable via the
AUTHENTICATION_RATE_LIMIT env var (default 10/minute, down from 30
to tighten the budget on unauth auth-adjacent endpoints). Document
it in deployments/aio and deployments/cli variables.env.
* test(api): cover magic-code attempt cap, counter reset, and auth throttle
Add the contract tests called out in the PR test plan:
- TestMagicSignInVerifyAttempts:
- test_exhausted_after_max_wrong_attempts: after MAX_VERIFY_ATTEMPTS
wrong codes the next verify redirects with EMAIL_CODE_ATTEMPT_
EXHAUSTED_SIGN_IN and both Redis keys are deleted; a follow-up
verify reports EXPIRED.
- test_counter_increments_on_each_wrong_attempt: the dedicated
verify_attempts counter advances by exactly one per wrong POST,
matching the atomic Lua INCR.
- test_counter_resets_on_token_regeneration: regenerating the
magic-link clears the counter so the user isn't pre-locked-out by
a prior session's wrong attempts.
- TestMagicSignUpVerifyAttempts.test_signup_exhausted_after_max_wrong_attempts:
the sign-up endpoint returns EMAIL_CODE_ATTEMPT_EXHAUSTED_SIGN_UP on
the exhausting attempt.
- TestAuthenticationThrottle: exercises authentication_throttle_allows
on the plain-View redirect-flow endpoints by patching the rate down
and asserting RATE_LIMIT_EXCEEDED is appended to the redirect URL
once the per-IP budget is exceeded, for both magic-sign-in and
magic-sign-up.
Each new class clears Django cache (DRF throttle storage) and the
per-email Redis keys around every test so runs are independent.
* fix(api): clamp remaining_ttl to >=1 for verify-attempt counter EXPIRE
ri.ttl() returns 0 when the token has less than one second remaining
(Redis floors to whole seconds). The previous clamp only caught
None and < 0, so a sub-second TTL would pass through and the Lua
script's EXPIRE counter 0 would immediately delete the key — letting
an attacker bypass MAX_VERIFY_ATTEMPTS during the final second of the
token's life. Switch the comparison to <= 0.
Narrow real-world impact (sub-second window, throttle still bounds
the rate) but the cap should hold regardless of timing.
2026-06-01 13:14:57 +00:00
|
|
|
from plane.authentication.rate_limit import (
|
|
|
|
|
AuthenticationThrottle,
|
[WEB-7946] fix: add rate limiting to email/password sign-in and sign-up endpoints (#9335)
* fix: add rate limiting to email/password sign-in and sign-up endpoints
All four password authentication views (app sign-in, app sign-up, space
sign-in, space sign-up) extended django.views.View, so DRF's global
AnonRateThrottle never ran and the endpoints accepted unlimited credential
guesses with no friction (brute-force / credential stuffing, GHSA-349j).
Add authentication_throttle_allows(request) at the top of each post()
method — before any DB access — using the same AuthenticationThrottle
already guarding the magic-code views. On rejection the view redirects with
RATE_LIMIT_EXCEEDED, consistent with all other throttled auth endpoints.
Default limit remains 10/minute, overridable via AUTHENTICATION_RATE_LIMIT.
Co-authored-by: Plane AI <noreply@plane.so>
* refactor: consolidate auth throttle into a decorator + add tests
- Extract the repeated throttle-and-redirect block from the six redirect-flow
auth views (email + magic, app + space) into a single throttle_auth_redirect
decorator in rate_limit.py. Behaviour is unchanged: the throttle still runs
before any DB access; brute-force traffic is rejected without a DB hit.
- Add regression tests for the password sign-in/sign-up throttle on both app
and space endpoints, mirroring the existing magic-code throttle tests.
- Reset the shared AuthenticationThrottle bucket before every test in
test_authentication.py. All auth endpoints share one per-IP throttle scope,
so the newly-throttled password requests exhausted the budget mid-file and
caused unrelated tests to trip RATE_LIMIT_EXCEEDED.
---------
Co-authored-by: Plane AI <noreply@plane.so>
Co-authored-by: sriramveeraghanta <veeraghanta.sriram@gmail.com>
2026-08-27 18:43:58 +00:00
|
|
|
throttle_auth_redirect,
|
fix(api): rate-limit magic-code verify, bound per-token attempts (GHSA-9pvm-fcf6-9234) (#9130)
* fix(api): rate-limit magic-code verification and bound per-token attempts
The magic-link sign-in / sign-up endpoints accept a 6-digit numeric code
(900k-value space, 600s TTL) but never increment a failure counter on a
wrong-code verify and extend django.views.View rather than DRF APIView,
so DRF's AuthenticationThrottle never runs against them. The space-side
generate endpoint also lacked throttle_classes. Combined, this allowed
an unauthenticated attacker who knew a victim's email to brute-force
the code within the TTL window and log in as the victim.
- Add MAX_VERIFY_ATTEMPTS=5 in MagicCodeProvider.set_user_data: failed
comparisons now persist verify_attempts in Redis under the remaining
TTL and, on hitting the limit, delete the key and raise
EMAIL_CODE_ATTEMPT_EXHAUSTED. This is the load-bearing fix - it caps
total attempts per issued token regardless of request rate.
- Add authentication_throttle_allows() so plain Django Views can apply
AuthenticationThrottle without converting to APIView (would change
CSRF + request-parsing semantics for the redirect-flow endpoints).
- Apply the throttle to MagicSignIn/UpEndpoint and the space variants;
add throttle_classes to MagicGenerateSpaceEndpoint to match its app
sibling.
Refs GHSA-9pvm-fcf6-9234.
* fix(api): make verify-attempt increment atomic, expose throttle rate via env
Address PR review feedback:
- Replace the JSON read-modify-write of verify_attempts with a Lua
EVAL script that INCRs a dedicated counter key and EXPIREs it only
on the first increment. The previous round-trip was racy: parallel
wrong-code requests could read the same value and both write the
same incremented count, letting an attacker exceed MAX_VERIFY_ATTEMPTS
under concurrency. Counter is now reset on each new token issuance
and cleared on successful verify / exhaustion.
- Make AuthenticationThrottle.rate configurable via the
AUTHENTICATION_RATE_LIMIT env var (default 10/minute, down from 30
to tighten the budget on unauth auth-adjacent endpoints). Document
it in deployments/aio and deployments/cli variables.env.
* test(api): cover magic-code attempt cap, counter reset, and auth throttle
Add the contract tests called out in the PR test plan:
- TestMagicSignInVerifyAttempts:
- test_exhausted_after_max_wrong_attempts: after MAX_VERIFY_ATTEMPTS
wrong codes the next verify redirects with EMAIL_CODE_ATTEMPT_
EXHAUSTED_SIGN_IN and both Redis keys are deleted; a follow-up
verify reports EXPIRED.
- test_counter_increments_on_each_wrong_attempt: the dedicated
verify_attempts counter advances by exactly one per wrong POST,
matching the atomic Lua INCR.
- test_counter_resets_on_token_regeneration: regenerating the
magic-link clears the counter so the user isn't pre-locked-out by
a prior session's wrong attempts.
- TestMagicSignUpVerifyAttempts.test_signup_exhausted_after_max_wrong_attempts:
the sign-up endpoint returns EMAIL_CODE_ATTEMPT_EXHAUSTED_SIGN_UP on
the exhausting attempt.
- TestAuthenticationThrottle: exercises authentication_throttle_allows
on the plain-View redirect-flow endpoints by patching the rate down
and asserting RATE_LIMIT_EXCEEDED is appended to the redirect URL
once the per-IP budget is exceeded, for both magic-sign-in and
magic-sign-up.
Each new class clears Django cache (DRF throttle storage) and the
per-email Redis keys around every test so runs are independent.
* fix(api): clamp remaining_ttl to >=1 for verify-attempt counter EXPIRE
ri.ttl() returns 0 when the token has less than one second remaining
(Redis floors to whole seconds). The previous clamp only caught
None and < 0, so a sub-second TTL would pass through and the Lua
script's EXPIRE counter 0 would immediately delete the key — letting
an attacker bypass MAX_VERIFY_ATTEMPTS during the final second of the
token's life. Switch the comparison to <= 0.
Narrow real-world impact (sub-second window, throttle still bounds
the rate) but the cap should hold regardless of timing.
2026-06-01 13:14:57 +00:00
|
|
|
)
|
2025-09-16 16:07:08 +00:00
|
|
|
from plane.utils.path_validator import get_safe_redirect_url, validate_next_path, get_allowed_hosts
|
2024-05-08 17:31:20 +00:00
|
|
|
|
2025-05-19 12:04:46 +00:00
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
class MagicGenerateSpaceEndpoint(APIView):
|
2024-11-21 12:12:44 +00:00
|
|
|
permission_classes = [AllowAny]
|
2024-05-08 17:31:20 +00:00
|
|
|
|
fix(api): rate-limit magic-code verify, bound per-token attempts (GHSA-9pvm-fcf6-9234) (#9130)
* fix(api): rate-limit magic-code verification and bound per-token attempts
The magic-link sign-in / sign-up endpoints accept a 6-digit numeric code
(900k-value space, 600s TTL) but never increment a failure counter on a
wrong-code verify and extend django.views.View rather than DRF APIView,
so DRF's AuthenticationThrottle never runs against them. The space-side
generate endpoint also lacked throttle_classes. Combined, this allowed
an unauthenticated attacker who knew a victim's email to brute-force
the code within the TTL window and log in as the victim.
- Add MAX_VERIFY_ATTEMPTS=5 in MagicCodeProvider.set_user_data: failed
comparisons now persist verify_attempts in Redis under the remaining
TTL and, on hitting the limit, delete the key and raise
EMAIL_CODE_ATTEMPT_EXHAUSTED. This is the load-bearing fix - it caps
total attempts per issued token regardless of request rate.
- Add authentication_throttle_allows() so plain Django Views can apply
AuthenticationThrottle without converting to APIView (would change
CSRF + request-parsing semantics for the redirect-flow endpoints).
- Apply the throttle to MagicSignIn/UpEndpoint and the space variants;
add throttle_classes to MagicGenerateSpaceEndpoint to match its app
sibling.
Refs GHSA-9pvm-fcf6-9234.
* fix(api): make verify-attempt increment atomic, expose throttle rate via env
Address PR review feedback:
- Replace the JSON read-modify-write of verify_attempts with a Lua
EVAL script that INCRs a dedicated counter key and EXPIREs it only
on the first increment. The previous round-trip was racy: parallel
wrong-code requests could read the same value and both write the
same incremented count, letting an attacker exceed MAX_VERIFY_ATTEMPTS
under concurrency. Counter is now reset on each new token issuance
and cleared on successful verify / exhaustion.
- Make AuthenticationThrottle.rate configurable via the
AUTHENTICATION_RATE_LIMIT env var (default 10/minute, down from 30
to tighten the budget on unauth auth-adjacent endpoints). Document
it in deployments/aio and deployments/cli variables.env.
* test(api): cover magic-code attempt cap, counter reset, and auth throttle
Add the contract tests called out in the PR test plan:
- TestMagicSignInVerifyAttempts:
- test_exhausted_after_max_wrong_attempts: after MAX_VERIFY_ATTEMPTS
wrong codes the next verify redirects with EMAIL_CODE_ATTEMPT_
EXHAUSTED_SIGN_IN and both Redis keys are deleted; a follow-up
verify reports EXPIRED.
- test_counter_increments_on_each_wrong_attempt: the dedicated
verify_attempts counter advances by exactly one per wrong POST,
matching the atomic Lua INCR.
- test_counter_resets_on_token_regeneration: regenerating the
magic-link clears the counter so the user isn't pre-locked-out by
a prior session's wrong attempts.
- TestMagicSignUpVerifyAttempts.test_signup_exhausted_after_max_wrong_attempts:
the sign-up endpoint returns EMAIL_CODE_ATTEMPT_EXHAUSTED_SIGN_UP on
the exhausting attempt.
- TestAuthenticationThrottle: exercises authentication_throttle_allows
on the plain-View redirect-flow endpoints by patching the rate down
and asserting RATE_LIMIT_EXCEEDED is appended to the redirect URL
once the per-IP budget is exceeded, for both magic-sign-in and
magic-sign-up.
Each new class clears Django cache (DRF throttle storage) and the
per-email Redis keys around every test so runs are independent.
* fix(api): clamp remaining_ttl to >=1 for verify-attempt counter EXPIRE
ri.ttl() returns 0 when the token has less than one second remaining
(Redis floors to whole seconds). The previous clamp only caught
None and < 0, so a sub-second TTL would pass through and the Lua
script's EXPIRE counter 0 would immediately delete the key — letting
an attacker bypass MAX_VERIFY_ATTEMPTS during the final second of the
token's life. Switch the comparison to <= 0.
Narrow real-world impact (sub-second window, throttle still bounds
the rate) but the cap should hold regardless of timing.
2026-06-01 13:14:57 +00:00
|
|
|
throttle_classes = [AuthenticationThrottle]
|
|
|
|
|
|
2024-05-08 17:31:20 +00:00
|
|
|
def post(self, request):
|
|
|
|
|
# Check if instance is configured
|
|
|
|
|
instance = Instance.objects.first()
|
|
|
|
|
if instance is None or not instance.is_setup_done:
|
|
|
|
|
exc = AuthenticationException(
|
2024-11-21 12:12:44 +00:00
|
|
|
error_code=AUTHENTICATION_ERROR_CODES["INSTANCE_NOT_CONFIGURED"],
|
2024-05-08 17:31:20 +00:00
|
|
|
error_message="INSTANCE_NOT_CONFIGURED",
|
|
|
|
|
)
|
2024-11-21 12:12:44 +00:00
|
|
|
return Response(exc.get_error_dict(), status=status.HTTP_400_BAD_REQUEST)
|
2024-05-08 17:31:20 +00:00
|
|
|
|
2024-11-25 09:32:50 +00:00
|
|
|
email = request.data.get("email", "").strip().lower()
|
2024-05-08 17:31:20 +00:00
|
|
|
try:
|
|
|
|
|
validate_email(email)
|
|
|
|
|
adapter = MagicCodeProvider(request=request, key=email)
|
|
|
|
|
key, token = adapter.initiate()
|
|
|
|
|
# If the smtp is configured send through here
|
2025-04-02 17:39:27 +00:00
|
|
|
magic_link.delay(email, key, token)
|
2024-05-08 17:31:20 +00:00
|
|
|
return Response({"key": str(key)}, status=status.HTTP_200_OK)
|
|
|
|
|
except AuthenticationException as e:
|
2024-11-21 12:12:44 +00:00
|
|
|
return Response(e.get_error_dict(), status=status.HTTP_400_BAD_REQUEST)
|
2024-05-08 17:31:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MagicSignInSpaceEndpoint(View):
|
[WEB-7946] fix: add rate limiting to email/password sign-in and sign-up endpoints (#9335)
* fix: add rate limiting to email/password sign-in and sign-up endpoints
All four password authentication views (app sign-in, app sign-up, space
sign-in, space sign-up) extended django.views.View, so DRF's global
AnonRateThrottle never ran and the endpoints accepted unlimited credential
guesses with no friction (brute-force / credential stuffing, GHSA-349j).
Add authentication_throttle_allows(request) at the top of each post()
method — before any DB access — using the same AuthenticationThrottle
already guarding the magic-code views. On rejection the view redirects with
RATE_LIMIT_EXCEEDED, consistent with all other throttled auth endpoints.
Default limit remains 10/minute, overridable via AUTHENTICATION_RATE_LIMIT.
Co-authored-by: Plane AI <noreply@plane.so>
* refactor: consolidate auth throttle into a decorator + add tests
- Extract the repeated throttle-and-redirect block from the six redirect-flow
auth views (email + magic, app + space) into a single throttle_auth_redirect
decorator in rate_limit.py. Behaviour is unchanged: the throttle still runs
before any DB access; brute-force traffic is rejected without a DB hit.
- Add regression tests for the password sign-in/sign-up throttle on both app
and space endpoints, mirroring the existing magic-code throttle tests.
- Reset the shared AuthenticationThrottle bucket before every test in
test_authentication.py. All auth endpoints share one per-IP throttle scope,
so the newly-throttled password requests exhausted the budget mid-file and
caused unrelated tests to trip RATE_LIMIT_EXCEEDED.
---------
Co-authored-by: Plane AI <noreply@plane.so>
Co-authored-by: sriramveeraghanta <veeraghanta.sriram@gmail.com>
2026-08-27 18:43:58 +00:00
|
|
|
@throttle_auth_redirect(is_space=True)
|
2024-05-08 17:31:20 +00:00
|
|
|
def post(self, request):
|
|
|
|
|
# set the referer as session to redirect after login
|
|
|
|
|
code = request.POST.get("code", "").strip()
|
|
|
|
|
email = request.POST.get("email", "").strip().lower()
|
|
|
|
|
next_path = request.POST.get("next_path")
|
|
|
|
|
|
|
|
|
|
if code == "" or email == "":
|
|
|
|
|
exc = AuthenticationException(
|
2025-09-29 13:45:32 +00:00
|
|
|
error_code=AUTHENTICATION_ERROR_CODES["MAGIC_SIGN_IN_EMAIL_CODE_REQUIRED"],
|
2024-05-08 17:31:20 +00:00
|
|
|
error_message="MAGIC_SIGN_IN_EMAIL_CODE_REQUIRED",
|
|
|
|
|
)
|
|
|
|
|
params = exc.get_error_dict()
|
2025-09-15 18:31:06 +00:00
|
|
|
url = get_safe_redirect_url(
|
|
|
|
|
base_url=base_host(request=request, is_space=True),
|
|
|
|
|
next_path=next_path,
|
2025-09-15 19:54:48 +00:00
|
|
|
params=params,
|
2025-09-15 18:31:06 +00:00
|
|
|
)
|
2024-05-08 17:31:20 +00:00
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
2024-05-14 15:23:51 +00:00
|
|
|
existing_user = User.objects.filter(email=email).first()
|
|
|
|
|
|
|
|
|
|
if not existing_user:
|
|
|
|
|
exc = AuthenticationException(
|
|
|
|
|
error_code=AUTHENTICATION_ERROR_CODES["USER_DOES_NOT_EXIST"],
|
|
|
|
|
error_message="USER_DOES_NOT_EXIST",
|
|
|
|
|
)
|
|
|
|
|
params = exc.get_error_dict()
|
2025-09-15 18:31:06 +00:00
|
|
|
url = get_safe_redirect_url(
|
|
|
|
|
base_url=base_host(request=request, is_space=True),
|
|
|
|
|
next_path=next_path,
|
2025-09-15 19:54:48 +00:00
|
|
|
params=params,
|
2025-09-15 18:31:06 +00:00
|
|
|
)
|
2024-05-08 17:31:20 +00:00
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
2024-05-14 15:23:51 +00:00
|
|
|
# Active User
|
2024-05-08 17:31:20 +00:00
|
|
|
try:
|
2025-09-29 13:45:32 +00:00
|
|
|
provider = MagicCodeProvider(request=request, key=f"magic_{email}", code=code)
|
2024-05-08 17:31:20 +00:00
|
|
|
user = provider.authenticate()
|
|
|
|
|
# Login the user and record his device info
|
2024-05-10 12:00:38 +00:00
|
|
|
user_login(request=request, user=user, is_space=True)
|
2024-05-08 17:31:20 +00:00
|
|
|
# redirect to referer path
|
2025-09-16 13:14:26 +00:00
|
|
|
next_path = validate_next_path(next_path=next_path)
|
2025-09-17 10:43:32 +00:00
|
|
|
url = f"{base_host(request=request, is_space=True).rstrip('/')}{next_path}"
|
2025-09-16 16:07:08 +00:00
|
|
|
if url_has_allowed_host_and_scheme(url, allowed_hosts=get_allowed_hosts()):
|
|
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
else:
|
|
|
|
|
return HttpResponseRedirect(base_host(request=request, is_space=True))
|
2024-05-08 17:31:20 +00:00
|
|
|
|
|
|
|
|
except AuthenticationException as e:
|
|
|
|
|
params = e.get_error_dict()
|
2025-09-16 05:27:20 +00:00
|
|
|
url = get_safe_redirect_url(
|
|
|
|
|
base_url=base_host(request=request, is_space=True),
|
|
|
|
|
next_path=next_path,
|
|
|
|
|
params=params,
|
2025-09-15 18:31:06 +00:00
|
|
|
)
|
2024-05-08 17:31:20 +00:00
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MagicSignUpSpaceEndpoint(View):
|
[WEB-7946] fix: add rate limiting to email/password sign-in and sign-up endpoints (#9335)
* fix: add rate limiting to email/password sign-in and sign-up endpoints
All four password authentication views (app sign-in, app sign-up, space
sign-in, space sign-up) extended django.views.View, so DRF's global
AnonRateThrottle never ran and the endpoints accepted unlimited credential
guesses with no friction (brute-force / credential stuffing, GHSA-349j).
Add authentication_throttle_allows(request) at the top of each post()
method — before any DB access — using the same AuthenticationThrottle
already guarding the magic-code views. On rejection the view redirects with
RATE_LIMIT_EXCEEDED, consistent with all other throttled auth endpoints.
Default limit remains 10/minute, overridable via AUTHENTICATION_RATE_LIMIT.
Co-authored-by: Plane AI <noreply@plane.so>
* refactor: consolidate auth throttle into a decorator + add tests
- Extract the repeated throttle-and-redirect block from the six redirect-flow
auth views (email + magic, app + space) into a single throttle_auth_redirect
decorator in rate_limit.py. Behaviour is unchanged: the throttle still runs
before any DB access; brute-force traffic is rejected without a DB hit.
- Add regression tests for the password sign-in/sign-up throttle on both app
and space endpoints, mirroring the existing magic-code throttle tests.
- Reset the shared AuthenticationThrottle bucket before every test in
test_authentication.py. All auth endpoints share one per-IP throttle scope,
so the newly-throttled password requests exhausted the budget mid-file and
caused unrelated tests to trip RATE_LIMIT_EXCEEDED.
---------
Co-authored-by: Plane AI <noreply@plane.so>
Co-authored-by: sriramveeraghanta <veeraghanta.sriram@gmail.com>
2026-08-27 18:43:58 +00:00
|
|
|
@throttle_auth_redirect(is_space=True)
|
2024-05-08 17:31:20 +00:00
|
|
|
def post(self, request):
|
|
|
|
|
# set the referer as session to redirect after login
|
|
|
|
|
code = request.POST.get("code", "").strip()
|
|
|
|
|
email = request.POST.get("email", "").strip().lower()
|
|
|
|
|
next_path = request.POST.get("next_path")
|
|
|
|
|
|
|
|
|
|
if code == "" or email == "":
|
|
|
|
|
exc = AuthenticationException(
|
2025-09-29 13:45:32 +00:00
|
|
|
error_code=AUTHENTICATION_ERROR_CODES["MAGIC_SIGN_UP_EMAIL_CODE_REQUIRED"],
|
2024-05-08 17:31:20 +00:00
|
|
|
error_message="MAGIC_SIGN_UP_EMAIL_CODE_REQUIRED",
|
|
|
|
|
)
|
|
|
|
|
params = exc.get_error_dict()
|
2025-09-15 18:31:06 +00:00
|
|
|
url = get_safe_redirect_url(
|
|
|
|
|
base_url=base_host(request=request, is_space=True),
|
|
|
|
|
next_path=next_path,
|
2025-09-15 19:54:48 +00:00
|
|
|
params=params,
|
2025-09-15 18:31:06 +00:00
|
|
|
)
|
2024-05-08 17:31:20 +00:00
|
|
|
return HttpResponseRedirect(url)
|
2024-05-14 15:23:51 +00:00
|
|
|
# Existing User
|
|
|
|
|
existing_user = User.objects.filter(email=email).first()
|
2024-05-15 16:38:54 +00:00
|
|
|
# Already existing
|
2024-05-14 15:23:51 +00:00
|
|
|
if existing_user:
|
2024-05-08 17:31:20 +00:00
|
|
|
exc = AuthenticationException(
|
|
|
|
|
error_code=AUTHENTICATION_ERROR_CODES["USER_ALREADY_EXIST"],
|
|
|
|
|
error_message="USER_ALREADY_EXIST",
|
|
|
|
|
)
|
|
|
|
|
params = exc.get_error_dict()
|
2025-09-15 18:31:06 +00:00
|
|
|
url = get_safe_redirect_url(
|
|
|
|
|
base_url=base_host(request=request, is_space=True),
|
|
|
|
|
next_path=next_path,
|
2025-09-15 19:54:48 +00:00
|
|
|
params=params,
|
2025-09-15 18:31:06 +00:00
|
|
|
)
|
2024-05-08 17:31:20 +00:00
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
|
|
|
|
|
try:
|
2025-09-29 13:45:32 +00:00
|
|
|
provider = MagicCodeProvider(request=request, key=f"magic_{email}", code=code)
|
2024-05-08 17:31:20 +00:00
|
|
|
user = provider.authenticate()
|
|
|
|
|
# Login the user and record his device info
|
2024-05-10 12:00:38 +00:00
|
|
|
user_login(request=request, user=user, is_space=True)
|
2024-05-08 17:31:20 +00:00
|
|
|
# redirect to referer path
|
2025-09-16 13:14:26 +00:00
|
|
|
next_path = validate_next_path(next_path=next_path)
|
2025-09-17 10:43:32 +00:00
|
|
|
url = f"{base_host(request=request, is_space=True).rstrip('/')}{next_path}"
|
2025-09-16 16:07:08 +00:00
|
|
|
if url_has_allowed_host_and_scheme(url, allowed_hosts=get_allowed_hosts()):
|
|
|
|
|
return HttpResponseRedirect(url)
|
|
|
|
|
else:
|
|
|
|
|
return HttpResponseRedirect(base_host(request=request, is_space=True))
|
2024-05-08 17:31:20 +00:00
|
|
|
|
|
|
|
|
except AuthenticationException as e:
|
|
|
|
|
params = e.get_error_dict()
|
2025-09-15 18:31:06 +00:00
|
|
|
url = get_safe_redirect_url(
|
|
|
|
|
base_url=base_host(request=request, is_space=True),
|
|
|
|
|
next_path=next_path,
|
2025-09-15 19:54:48 +00:00
|
|
|
params=params,
|
2025-09-15 18:31:06 +00:00
|
|
|
)
|
2024-05-08 17:31:20 +00:00
|
|
|
return HttpResponseRedirect(url)
|