fix(admin): #91 abandoned first-run bootstrap no longer bricks SuperAdmin setup #117

Merged
amiryahaya merged 3 commits from fix/91-bootstrap-recovery into main 2026-11-08 13:04:28 +01:00
Owner

#91 (M1-closure audit): losing the TOTP QR before confirm permanently bricked first-run setup.

The brick: /setup creates an Active SuperAdmin candidate (TotpEnabled=false, no expiry). Operator closes the tab, QR lost → setup 403'd forever (the guard counted the candidate as 'a SuperAdmin exists'), confirm 401'd without the lost secret's code, /status reported required=false. Only DB surgery recovered a fresh install.

Fix:

  • /status: required = 'no TotpEnabled SuperAdmin'. An unconfirmed candidate keeps the setup screen up (was: required=false + dead setup). Suspended-but-enabled admins still hold the slot — bootstrap cannot reopen for a hijacker.
  • /setup: re-issues INTO the unconfirmed candidate, under the existing BootstrapLock + Serializable tx: fresh secret (lost QR dies), email/display refresh. A different-email retry takes over the slot (typo recovery). Real users' emails stay protected — explicit 409 plus the unique-index backstop; a candidate cannot swallow a real address.
  • In-tx re-check now counts only TotpEnabled SuperAdmins: unconfirmed strays are overwritten by design (#91), and confirm demotes its own strays as before.

Guards that survive (control tests): confirmed SuperAdmin → setup still 403 (no hijack); non-SuperAdmin email collision → 409; #35 single-slot race + unique index untouched; confirm flows unchanged.

Tests: 5 new (BootstrapRecoveryTests): same-email re-issue with old secret verified dead + confirm completes; different-email takeover; /status stays required with a candidate; confirmed-admin hijack guard; real-user email collision. 2 existing tests updated to the new contract (Setup_AbandonedCandidate_Reissues; concurrent-setup invariant is now 'at most one candidate ROW persists' — serialized re-issues legitimately overwrite the same row).

TDD RED x5 → green. Suite 932/0/39 (Release+Debug), coverage gate 87.12%. Closes #91.

**#91 (M1-closure audit): losing the TOTP QR before confirm permanently bricked first-run setup.** The brick: /setup creates an Active SuperAdmin candidate (TotpEnabled=false, no expiry). Operator closes the tab, QR lost → setup 403'd forever (the guard counted the candidate as 'a SuperAdmin exists'), confirm 401'd without the lost secret's code, /status reported required=false. Only DB surgery recovered a fresh install. **Fix:** - **/status**: required = 'no TotpEnabled SuperAdmin'. An unconfirmed candidate keeps the setup screen up (was: required=false + dead setup). Suspended-but-enabled admins still hold the slot — bootstrap cannot reopen for a hijacker. - **/setup**: re-issues INTO the unconfirmed candidate, under the existing BootstrapLock + Serializable tx: fresh secret (lost QR dies), email/display refresh. A different-email retry takes over the slot (typo recovery). Real users' emails stay protected — explicit 409 plus the unique-index backstop; a candidate cannot swallow a real address. - **In-tx re-check** now counts only TotpEnabled SuperAdmins: unconfirmed strays are overwritten by design (#91), and confirm demotes its own strays as before. **Guards that survive (control tests):** confirmed SuperAdmin → setup still 403 (no hijack); non-SuperAdmin email collision → 409; #35 single-slot race + unique index untouched; confirm flows unchanged. **Tests:** 5 new (BootstrapRecoveryTests): same-email re-issue with old secret verified dead + confirm completes; different-email takeover; /status stays required with a candidate; confirmed-admin hijack guard; real-user email collision. 2 existing tests updated to the new contract (Setup_AbandonedCandidate_Reissues; concurrent-setup invariant is now 'at most one candidate ROW persists' — serialized re-issues legitimately overwrite the same row). TDD RED x5 → green. Suite 932/0/39 (Release+Debug), coverage gate 87.12%. Closes #91.
fix(admin): #91 abandoned first-run bootstrap no longer bricks SuperAdmin setup
Some checks failed
Security CI / security-scan (pull_request) Failing after 25s
API CI / build-and-test (pull_request) Failing after 31s
3f2e227fd7
/setup created an Active SuperAdmin candidate (TotpEnabled=false) with no
expiry. Lose the QR before confirming and the install was dead: setup
403'd forever (the guard counted the candidate as 'a SuperAdmin exists'),
confirm 401'd without the lost secret's code, and /status reported
required=false — only DB surgery recovered a fresh install.

- /status: required now means 'no TotpEnabled SuperAdmin' (an unconfirmed
  candidate keeps the setup screen up; a suspended-but-enabled admin still
  holds the slot so bootstrap cannot reopen for a hijacker)
- /setup: re-issues INTO the unconfirmed candidate under the existing lock
  and Serializable tx — fresh secret (the lost QR dies), email/display
  refresh. Different-email retry takes over the slot; a candidate can no
  more swallow a real user's email than before (explicit 409 + unique
  index backstop). Confirmed-SuperAdmin guard unchanged.
- in-tx re-check now counts only TotpEnabled SuperAdmins (unconfirmed
  strays are overwritten by design; confirm demotes its own strays)

Tests: 5 new in BootstrapRecoveryTests (same-email re-issue with old
secret dead + confirm completes, different-email takeover, status stays
required, confirmed-admin hijack guard, real-user email collision) + 2
existing contract tests updated to the new re-issue contract (single-slot
race invariant now 'at most one candidate row persists'). RED x5 -> green.
Suite 932/0/39 Release+Debug, coverage 87.12%.
chore: retrigger CI (transient GitHub 504 on libkazsign download, run 11958/11959)
Some checks failed
Security CI / security-scan (pull_request) Failing after 25s
API CI / build-and-test (pull_request) Failing after 46s
29035c4034
Merge branch 'main' into fix/91-bootstrap-recovery
All checks were successful
Security CI / security-scan (pull_request) Successful in 2m20s
API CI / build-and-test (pull_request) Successful in 4m18s
a0b22f625c
Sign in to join this conversation.
No description provided.