Repository navigation
stages/user_login: refuse to log in an unsaved pending user - #21691
Conversation
The identification stage plants an unsaved User (pk is None) in PLAN_CONTEXT_PENDING_USER when the supplied identifier is unknown; it exists only to normalise the timing against make_password() so the enumeration oracle goes away. Since the pending_user now propagates further down the flow plan, that dummy user reaches do_login, which calls is_known_device (which does `AuthenticatedSession.objects.filter(..., user=user).exists()`) and then Django's login(), which fires user_logged_in_session and on_user_logged_in signal handlers that also open related-field filters. Related-field filters reject unsaved instances and throw: ValueError: Model instances passed to related filters must be saved. The flow currently returns HTTP 500 on any "unknown email on a default flow with enrollment linked" path instead of redirecting to the enrollment flow or showing the usual invalid-login message. Guard at do_login entry so any code path that produced an unsaved PENDING_USER ends in stage_invalid, never in is_known_device or the login signal chain. The existing is_active check stays: an unsaved user defaults to is_active=True so the pk guard has to run first. Fixes goauthentik#21036 Signed-off-by: SAY-5 <[email protected]>
✅ Deploy Preview for authentik-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #21691 +/- ##
==========================================
+ Coverage 93.15% 93.20% +0.05%
==========================================
Files 1037 1045 +8
Lines 60783 61220 +437
Branches 0 424 +424
==========================================
+ Hits 56621 57059 +438
+ Misses 4162 4161 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
Signed-off-by: Jens L. <[email protected]>
✅ Deploy Preview for authentik-integrations ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for authentik-storybook ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
The fallback comment around the user.pk None check was a single 137-character line. ruff's E501 line-length rule (max 100) flagged it. Wrapped onto four 80-column lines with no behavioural change. Signed-off-by: SAY-5 <[email protected]>
|
The only failing check is |
* main: (30 commits) providers/saml: catch InvalidVersion from cryptography when parsing metadata (#21185) stages/user_login: refuse to log in an unsaved pending user (#21691) web/admin: hide previous loglist when no previous logs available (#23954) stages/email: fix ungrammatical expiry time in password reset templates (#22758) tests/openid_conformance: Add more conformance coverage (#22174) sources/ldap: derive TLS SNI name from hostname, not full server URI (#22456) core: bump github.com/grafana/pyroscope-go from 1.3.1 to 1.4.0 (#23885) web: Fix Log Viewer Intersection Observer. (#23861) web: bump the goauthentik group across 1 directory with 2 updates (#23926) ci: bump regclient/actions/regctl-login from 4b4db1dcc7dad75ad67a788a380f75a20cc8a040 to 39bf30ced4be9d147faaabbde2682e9725fb8bd1 (#23890) ci: bump regclient/actions/regctl-installer from 4b4db1dcc7dad75ad67a788a380f75a20cc8a040 to 39bf30ced4be9d147faaabbde2682e9725fb8bd1 (#23891) core: bump pnpm/pnpm from 11.10.0 to 11.11.0 in /website (#23930) core: bump pnpm/pnpm from 11.10.0 to 11.11.0 in /lifecycle/container (#23927) core: bump github.com/sethvargo/go-envconfig from 1.3.0 to 1.3.1 (#23921) core: bump goauthentik/selenium from 149.0-ak-0.50.1 to 149.0-ak-0.50.2 in /tests/e2e (#23924) core: bump uvicorn[standard] from 0.49.0 to 0.50.0 (#23925) core: bump ruff from 0.15.20 to 0.15.21 (#23928) core: bump goauthentik/fips-python from `aaaab46` to `0798f3b` in /lifecycle/container (#23929) ci: bump actions/stale from 10.3.0 to 10.4.0 (#23935) ci: bump github/codeql-action/autobuild from 4.36.3 to 4.37.0 (#23931) ...
* main: (48 commits) web/bug: harmonize and make routine the display of tasks (part 2: description list abuse) (#23839) providers/oauth2: fix iframe logout stage progression (#23919) providers/scim: improve error display when error doesn't conform to scim schema (#23955) providers/saml: catch InvalidVersion from cryptography when parsing metadata (#21185) stages/user_login: refuse to log in an unsaved pending user (#21691) web/admin: hide previous loglist when no previous logs available (#23954) stages/email: fix ungrammatical expiry time in password reset templates (#22758) tests/openid_conformance: Add more conformance coverage (#22174) sources/ldap: derive TLS SNI name from hostname, not full server URI (#22456) core: bump github.com/grafana/pyroscope-go from 1.3.1 to 1.4.0 (#23885) web: Fix Log Viewer Intersection Observer. (#23861) web: bump the goauthentik group across 1 directory with 2 updates (#23926) ci: bump regclient/actions/regctl-login from 4b4db1dcc7dad75ad67a788a380f75a20cc8a040 to 39bf30ced4be9d147faaabbde2682e9725fb8bd1 (#23890) ci: bump regclient/actions/regctl-installer from 4b4db1dcc7dad75ad67a788a380f75a20cc8a040 to 39bf30ced4be9d147faaabbde2682e9725fb8bd1 (#23891) core: bump pnpm/pnpm from 11.10.0 to 11.11.0 in /website (#23930) core: bump pnpm/pnpm from 11.10.0 to 11.11.0 in /lifecycle/container (#23927) core: bump github.com/sethvargo/go-envconfig from 1.3.0 to 1.3.1 (#23921) core: bump goauthentik/selenium from 149.0-ak-0.50.1 to 149.0-ak-0.50.2 in /tests/e2e (#23924) core: bump uvicorn[standard] from 0.49.0 to 0.50.0 (#23925) core: bump ruff from 0.15.20 to 0.15.21 (#23928) ...
What
UserLoginStageView.do_loginreadsPLAN_CONTEXT_PENDING_USERand hands the resultingUsertois_known_device()and Django'slogin():The identification stage plants an unsaved
User(pk is None) inPLAN_CONTEXT_PENDING_USERwhen the supplied identifier is unknown, it exists only so thatmake_password()runs against something and the timing side-channel that would otherwise leak "user exists vs doesn't" goes away (#18883). That dummy user was previously scoped to the identification stage, but the stage-state refactor (#18641) means it now propagates down the flow plan intodo_login.is_known_deviceimmediately does:and
login()fires theuser_logged_insignal, whichuser_logged_in_sessionandon_user_logged_inhandle with further ORM lookups. All of those are related-field filters, and Django rejects unsaved instances with:Observable effect: on a default authentication flow that has an enrollment flow linked, typing an unknown email into the identification stage returns HTTP 500 instead of redirecting to enrollment or showing the usual invalid-login message. Per #21036.
Fix
Guard at the top of
do_loginbefore any user-dependent work runs:Any code path that produced an unsaved
PENDING_USERnow ends instage_invalid, never reachesis_known_device,login(), or the signal chain. The existingis_activecheck stays: an unsavedUser()defaults tois_active=True, so the pk guard has to run first.Why here rather than in the signal handlers
The signal handlers (
user_logged_in_session,on_user_logged_in) andis_known_deviceare three separate call sites that would each need a pk guard, and the bug would come back the next time someone added a fourth caller. Gating atdo_loginis the single chokepoint for "we are about to log a user in" and matches the existingis_activecheck placement.Fixes #21036