fix: block SSO logins and revoke sessions for spam accounts - #4767
fix: block SSO logins and revoke sessions for spam accounts#4767prayag78 wants to merge 6 commits into
Conversation
Reviewer's GuideBlocks logins for users flagged as spam across native and SSO auth flows by centralizing a spam suspension message, enforcing spam checks in both allauth account and social adapters, and revoking active sessions when a user is marked as spam in the admin UI. Sequence diagram for native login rejection of spam accountssequenceDiagram
actor User
participant DjangoAuth as DjangoAuthBackend
participant AccountAdapter as CustomAccountAdapter
User->>DjangoAuth: authenticate
DjangoAuth->>AccountAdapter: pre_login(request, user, email_verification, signal_kwargs, email, signup, redirect_url)
alt user.is_spam
AccountAdapter->>AccountAdapter: messages.error(request, SPAM_ACCOUNT_ERROR)
AccountAdapter-->>User: HttpResponseRedirect(auth.login)
else not user.is_spam
AccountAdapter->>DjangoAuth: super().pre_login(...)
DjangoAuth-->>User: login successful
end
Sequence diagram for SSO login blocking of spam accountssequenceDiagram
actor User
participant SSOProvider
participant SocialAuth as CustomSocialAccountAdapter
participant AuthView as AuthLoginView
User->>SSOProvider: initiate SSO login
SSOProvider-->>SocialAuth: callback with sociallogin
SocialAuth->>SocialAuth: pre_social_login(request, sociallogin)
alt sociallogin.is_existing
SocialAuth->>SocialAuth: sync_wikimedia_username(sociallogin.user, sociallogin)
end
SocialAuth->>SocialAuth: require_not_spam(request, sociallogin.user)
alt user.is_spam
SocialAuth->>SocialAuth: messages.error(request, SPAM_ACCOUNT_ERROR)
SocialAuth-->>AuthView: ImmediateHttpResponse(HttpResponseRedirect(auth.login))
AuthView-->>User: redirected to login with error
else not spam
SocialAuth-->>AuthView: proceed with normal SSO login
AuthView-->>User: login successful
end
Sequence diagram for revoking sessions when user is marked as spamsequenceDiagram
actor Admin
participant UserListView
participant TargetUser as user_to_update
Admin->>UserListView: toggle spam in /admin/users/
UserListView->>TargetUser: _handle_toggle_spam(request, target_user)
UserListView->>TargetUser: is_spam = not is_spam
UserListView->>TargetUser: save(update_fields=['is_spam'])
alt TargetUser.is_spam
UserListView->>TargetUser: update_session_token()
end
UserListView-->>Admin: response with updated spam status
File-Level Changes
Assessment against linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Thank you for contributingPlease complete the checklist. Screenshots in the PR description and completed AI reviews are checked automatically.
🤖 AI reviews✅ Completed (1)
❌ 1 failed or rate-limited AI reviewer
ℹ️ Does not count toward the checklist
Feedback status: ✅ All AI review feedback is resolved. 📎 Attached media (1)Screenshots and screen recordings
Thank you for your contribution feel free to reach out if you have any questions. |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The spam-check logic is now implemented separately in
CustomAccountAdapter.pre_loginandrequire_not_spam; consider extracting a shared helper (e.g., aensure_not_spam(request, user)function) to avoid divergence in future changes. - In
CustomAccountAdapter.pre_login, consider mirroring the social adapter behavior by raisingImmediateHttpResponseinstead of returning anHttpResponseRedirect, to align with allauth’s expected short-circuit pattern and keep adapter behavior consistent. - When rejecting spam users in
pre_login, you may want to add a warning log similar torequire_not_spamso that spam-related login attempts are traceable across both native and SSO flows.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The spam-check logic is now implemented separately in `CustomAccountAdapter.pre_login` and `require_not_spam`; consider extracting a shared helper (e.g., a `ensure_not_spam(request, user)` function) to avoid divergence in future changes.
- In `CustomAccountAdapter.pre_login`, consider mirroring the social adapter behavior by raising `ImmediateHttpResponse` instead of returning an `HttpResponseRedirect`, to align with allauth’s expected short-circuit pattern and keep adapter behavior consistent.
- When rejecting spam users in `pre_login`, you may want to add a warning log similar to `require_not_spam` so that spam-related login attempts are traceable across both native and SSO flows.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Suppressed comments (4)
app/eventyay/eventyay_common/adapter.py:5
- CustomAccountAdapter.pre_login() should raise ImmediateHttpResponse to abort allauth login, but ImmediateHttpResponse is not imported in this module.
from allauth.core import context
app/eventyay/plugins/socialauth/adapter.py:121
- New spam-blocking behavior in pre_social_login() is not covered by tests. There are existing adapter tests in app/tests/tickets/plugins/test_socialauth.py, so this should add a case asserting spam-marked users trigger ImmediateHttpResponse and do not link/login via SSO.
# Runs last so it also covers the wikimedia_username fallback above.
require_not_spam(request, getattr(sociallogin, 'user', None))
app/eventyay/control/views/users.py:307
- Marking a user as spam now revokes sessions by updating the session_token, but the existing toggle_spam tests only assert is_spam flips. Add a test asserting session_token changes when spam is set (and remains unchanged when unmarking).
user_to_update.is_spam = not user_to_update.is_spam
user_to_update.save(update_fields=['is_spam'])
if user_to_update.is_spam:
# Revoke active sessions upon flagging as spam.
user_to_update.update_session_token()
app/eventyay/eventyay_common/adapter.py:50
- New spam-blocking logic added in CustomAccountAdapter.pre_login() does not appear to have test coverage. There are existing adapter tests in app/tests/tickets/plugins/test_socialauth.py; add a test asserting spam-marked users are blocked via the allauth adapter path as well.
def pre_login(
self,
request: HttpRequest,
user,
*,
Rachit7168
left a comment
There was a problem hiding this comment.
simplescreenrecorder-2026-08-09_10.48.36.mp4
For Wikipedia as well , It should be blocked as well
Fixes #4591
This PR resolves an issue where users marked as spam in the admin panel (
/admin/users/) could still log in using SSO providers (Google, GitHub, MediaWiki). It also prevents spam accounts from being bypassed via automatic social account linking and revokes active sessions when a user is flagged.Changes Included
SPAM_ACCOUNT_ERROR.require_not_spam()check inCustomSocialAccountAdapter.pre_social_loginto abort SSO authentication and prevent unlinked social accounts from auto-connecting to spam-flagged users.pre_login()inCustomAccountAdapterto reject allauth login paths for spam-flagged accounts.UserListViewto invokeupdate_session_token()when marking a user as spam, invalidating active session tokens across all devices immediately.Video.Project.64.mp4
Summary by Sourcery
Block authentication and revoke sessions for users marked as spam across native and SSO login flows.
Bug Fixes:
Enhancements: