From 428c0a854e063e566befa7c406665e69cf55c87d Mon Sep 17 00:00:00 2001 From: Askar Dyussekeyev <91150176+dyussekeyev@users.noreply.github.com> Date: Wed, 11 Nov 2026 23:17:28 +0500 Subject: [PATCH] Let the enrolment page render the QR of the secret it was issued The credential-onboarding gate listed `mfa/setup` and `mfa/enable` as part of the factor step and not `mfa/qr`, so the image the page requests straight after `setup` answered 200 came back as a 403 from the gate. An administrator the deployment policy holds on that page could only type the secret by hand. --- CHANGELOG.md | 12 ++++++++++++ backend/app/api/deps.py | 8 ++++++++ backend/tests/test_onboarding.py | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 960ad2b..996f41a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,18 @@ with it. ## [Unreleased] +### Fixed + +- The MFA QR code can be rendered while an account is being walked through + enrolment. `GET /api/v1/auth/mfa/qr` draws the pending secret that + `POST /api/v1/auth/mfa/setup` has just written, but the credential-onboarding + gate listed only `setup` and `enable` as part of the factor step, so the image + the enrolment page requests immediately after `setup` answered 200 was refused + with a 403 from the gate — an administrator held on that page by + `REQUIRE_MFA_FOR_ADMINS` could only type the secret by hand. The route now + belongs to the same step as the two calls it draws from, and a test pins the + QR's reachability for an account with a pending factor. + ## [0.2.4] - 2026-11-07 ### Added diff --git a/backend/app/api/deps.py b/backend/app/api/deps.py index 840ad4a..daceba1 100644 --- a/backend/app/api/deps.py +++ b/backend/app/api/deps.py @@ -43,9 +43,17 @@ # re-check the password: if a temporary password were still in force, the new # factor would be bound to the credential its owner is about to replace — and the # administrator who chose that password would have been present at the enrolment. +# +# The QR belongs to the same step and not to a later one: it renders the pending +# secret `setup` has just written, which is the artefact the enrolment page shows +# beside the manual one. Leaving it out refused the image that the page requests +# immediately after `setup` answered 200 — an administrator forced into enrolment +# by the deployment policy could only ever type the secret by hand, and the +# refusal arrived as a 403 from the gate rather than from the endpoint. _ONBOARDING_MFA_ALLOWED = frozenset( { "/api/v1/auth/mfa/setup", + "/api/v1/auth/mfa/qr", "/api/v1/auth/mfa/enable", } ) diff --git a/backend/tests/test_onboarding.py b/backend/tests/test_onboarding.py index 5e6264b..0153d80 100644 --- a/backend/tests/test_onboarding.py +++ b/backend/tests/test_onboarding.py @@ -224,6 +224,32 @@ async def test_a_pending_factor_refuses_the_platform_and_opens_its_own_step( ) assert setup.status_code == 200, setup.text + @pytest.mark.anyio + async def test_the_factor_step_can_show_the_qr_of_the_secret_it_just_issued( + self, client, db_session + ): + """The QR belongs to the enrolment step rather than behind it. + + The gate allowed `setup` and `enable` and not `qr`, so the image built from + the pending secret was refused the moment the enrolment page requested it — + an administrator forced into enrolment by the deployment policy could only + type the secret by hand. The endpoint itself never refuses with 403 (it + answers 409 when nothing is pending), which is why the gate is what this + test holds to account. + """ + user = await _user_with_flags(db_session, "qrcode@example.com", mfa=True) + headers = _headers(user) + + setup = await client.post( + "/api/v1/auth/mfa/setup", json={"password": PASSWORD}, headers=headers + ) + assert setup.status_code == 200, setup.text + + qr = await client.get("/api/v1/auth/mfa/qr", headers=headers) + assert qr.status_code == 200, qr.text + assert qr.headers["content-type"].startswith("image/svg+xml") + assert b"