Redirect already-logged-in users from login page safely#79
Merged
davegaeddert merged 3 commits intoJul 15, 2026
Merged
Conversation
LoginLinkFormView.get() redirected already-logged-in users via get_success_url(form), which reads form.cleaned_data — but the form is never validated on a GET, so cleaned_data doesn't exist and the request 500s (seen in production as AttributeError on GET /login/). Redirect straight to the "next" query param (or "/") instead, matching what LoginLinkSentView already does for logged-in users. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01H1ZivVXdALbLRZT7sCwtt8
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01H1ZivVXdALbLRZT7sCwtt8
An empty ?next= slipped past the query-param default and produced a 302 with an empty Location header (a browser self-redirect loop), and an external ?next= crashed with an unhandled ValueError from RedirectResponse's open-redirect guard, returning a 500. Route all three loginlink views through a shared redirect_to_next_url() helper that falls back to the default destination in both cases. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01H1ZivVXdALbLRZT7sCwtt8
Member
Author
|
@codex review |
|
To use Codex here, create a Codex account and connect to github. |
Member
Author
|
@codex review |
|
To use Codex here, create a Codex account and connect to github. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When an already-logged-in user visits the login page, they are now redirected safely using a new
redirect_to_next_url()helper function that validates the redirect target and prevents open redirect vulnerabilities.Changes
Added
redirect_to_next_url()helper function inviews.pythat:nextquery parameter if present and valid/) for missing, empty, or external URLsValueErrorfromRedirectResponseto handle invalid external URLs safelyUpdated
LoginLinkFormView.get()to use the new helper when redirecting already-logged-in users, removing the previous form-based redirect logicUpdated
LoginLinkSentView.get()to use the new helper for consistencyUpdated
FollowLinkView.get()to use the new helper with a custom default URLAdded comprehensive test coverage in
TestAlreadyLoggedInclass with four test cases:nextparameternextparameter when validnextis emptynextis an external URL (security check)Implementation Details
The helper function uses a try/except pattern to handle
RedirectResponse's built-in validation, which raisesValueErrorfor external URLs. This provides defense-in-depth against open redirect vulnerabilities while keeping the code simple and maintainable.https://claude.ai/code/session_01H1ZivVXdALbLRZT7sCwtt8