-
Notifications
You must be signed in to change notification settings - Fork 563
fix(auth): allow invite-based signup when PREVENT_SIGNUP is enabled #8408
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: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| from users.models import SignUpType | ||
|
|
||
| from .models import Invite, InviteLink | ||
|
|
||
|
|
||
| def is_valid_registration_invite( | ||
| *, sign_up_type: str | None, email: str, invite_hash: str | None | ||
| ) -> bool: | ||
| match sign_up_type: | ||
| case SignUpType.INVITE_LINK.value: | ||
| return InviteLink.objects.filter(hash=invite_hash).exists() | ||
|
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. 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
rg -n -C5 'class AbstractBaseInviteModel|class InviteLink|objects\s*=|class .*Manager|expires_at|is_expired' api
rg -n -C6 'is_valid_registration_invite|PREVENT_SIGNUP|_validate_registration_invite' api/custom_auth api/organisationsRepository: Flagsmith/flagsmith Length of output: 50375 🏁 Script executed: #!/bin/bash
set -euo pipefail
printf '%s\n' '--- invite models ---'
sed -n '1,65p' api/organisations/invites/models.py
printf '%s\n' '--- shared registration checks ---'
sed -n '1,80p' api/custom_auth/permissions.py
sed -n '1,60p' api/custom_auth/serializers.py
printf '%s\n' '--- scoped review conventions and learnings ---'
find /tmp/coderabbit-repo-knowledge/flagsmith-flagsmith-a74d157a -type f \
\( -path '*/api/*' -o -path '*/organisations/*' -o -path '*/custom_auth/*' \) \
-name '*.md' -printRepository: Flagsmith/flagsmith Length of output: 6009 🏁 Script executed: #!/bin/bash
set -euo pipefail
sed -n '1,55p' api/core/models.py
sed -n '1,30p' api/organisations/invites/services.pyRepository: Flagsmith/flagsmith Length of output: 2214 Authorization Bypass (CWE-862): Missing Authorization Reachability: External · Exploitability: Moderate Exclude expired invite links from registration validation.
|
||
| case SignUpType.INVITE_EMAIL.value: | ||
| return Invite.objects.filter(email__iexact=email.lower()).exists() | ||
| case _: | ||
| return False | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| from unittest import mock | ||
|
|
||
| from pytest_django.fixtures import SettingsWrapper | ||
|
|
||
| from custom_auth.permissions import IsSignupAllowed | ||
| from organisations.invites.models import Invite, InviteLink | ||
| from organisations.models import Organisation | ||
| from users.models import SignUpType | ||
|
|
||
|
|
||
| def test_is_signup_allowed__prevent_signup_disabled__returns_true( | ||
| settings: SettingsWrapper, | ||
| ) -> None: | ||
| # Given | ||
| settings.PREVENT_SIGNUP = False | ||
| permission = IsSignupAllowed() | ||
| mock_request = mock.MagicMock(data={}) | ||
|
|
||
| # When | ||
| result = permission.has_permission(mock_request, mock.MagicMock()) | ||
|
|
||
| # Then | ||
| assert result is True | ||
|
|
||
|
|
||
| def test_is_signup_allowed__prevent_signup_enabled_no_invite__returns_false( | ||
| settings: SettingsWrapper, | ||
| ) -> None: | ||
| # Given | ||
| settings.PREVENT_SIGNUP = True | ||
| permission = IsSignupAllowed() | ||
| mock_request = mock.MagicMock(data={"email": "test@example.com"}) | ||
|
|
||
| # When | ||
| result = permission.has_permission(mock_request, mock.MagicMock()) | ||
|
|
||
| # Then | ||
| assert result is False | ||
|
|
||
|
|
||
| def test_is_signup_allowed__prevent_signup_enabled_valid_invite_link__returns_true( | ||
| db: None, | ||
| settings: SettingsWrapper, | ||
| organisation: Organisation, | ||
| ) -> None: | ||
| # Given | ||
| settings.PREVENT_SIGNUP = True | ||
| invite_link = InviteLink.objects.create(organisation=organisation) | ||
| permission = IsSignupAllowed() | ||
| mock_request = mock.MagicMock( | ||
| data={ | ||
| "email": "test@example.com", | ||
| "sign_up_type": SignUpType.INVITE_LINK.value, | ||
| "invite_hash": invite_link.hash, | ||
| } | ||
| ) | ||
|
|
||
| # When | ||
| result = permission.has_permission(mock_request, mock.MagicMock()) | ||
|
|
||
| # Then | ||
| assert result is True | ||
|
|
||
|
|
||
| def test_is_signup_allowed__prevent_signup_enabled_invalid_invite_hash__returns_false( | ||
| db: None, | ||
| settings: SettingsWrapper, | ||
| ) -> None: | ||
| # Given | ||
| settings.PREVENT_SIGNUP = True | ||
| permission = IsSignupAllowed() | ||
| mock_request = mock.MagicMock( | ||
| data={ | ||
| "email": "test@example.com", | ||
| "sign_up_type": SignUpType.INVITE_LINK.value, | ||
| "invite_hash": "invalid-hash", | ||
| } | ||
| ) | ||
|
|
||
| # When | ||
| result = permission.has_permission(mock_request, mock.MagicMock()) | ||
|
|
||
| # Then | ||
| assert result is False | ||
|
|
||
|
|
||
| def test_is_signup_allowed__prevent_signup_enabled_valid_invite_email__returns_true( | ||
| db: None, | ||
| settings: SettingsWrapper, | ||
| organisation: Organisation, | ||
| ) -> None: | ||
| # Given | ||
| settings.PREVENT_SIGNUP = True | ||
| Invite.objects.create(email="test@example.com", organisation=organisation) | ||
| permission = IsSignupAllowed() | ||
| mock_request = mock.MagicMock( | ||
| data={ | ||
| "email": "Test@Example.com", | ||
| "sign_up_type": SignUpType.INVITE_EMAIL.value, | ||
| } | ||
| ) | ||
|
|
||
| # When | ||
| result = permission.has_permission(mock_request, mock.MagicMock()) | ||
|
|
||
| # Then | ||
| assert result is True |
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.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
Normalise raw email input before invite validation.
When
PREVENT_SIGNUPis enabled, a request withsign_up_typeset to invited email and"email": ["user@example.com"]passes this truthy list tois_valid_registration_invite. The service then callsemail.lower()and raisesAttributeErrorbefore serializer validation. Reject non-string values so this request returnsFalseinstead of a server error.Proposed fix
📝 Committable suggestion