Repository navigation
Conversation
|
Important Review skippedAuto reviews are limited based on label configuration. 🏷️ Required labels (at least one): 1
Please check the settings in the CodeRabbit UI or the
|
|
|
Superseded by the replacement PR from branch sam/fix-first-superadmin-election. It keeps this PR's atomic election design. It drops the migration's demotion of superadmins, adds the only-real-user guard, makes election errors fail closed without blocking sign-in, fixes the 2 failing tests, and documents recovery. I'll close this PR when the replacement merges. |
|
Closing in favour of #2772, which carries a corrected version of this fix. |
…2753) (#2772) * Fix security finding acf_1c430965be49bdf046a1933ae3b533a0 * fix(api): race-safe first-superadmin election without demoting anyone Corrects the bot election from #2753: - Election (services/first-superadmin.ts): promote the signing-in user in one guarded UPDATE only while they are the ONLY real (status != 'system') account, are not suspended, and the election marker is absent; then record the oldest real superadmin as the marker in the same D1 batch. A later sign-up never wins against an existing account (suspended or non-admin), and two sign-ups whose rows both exist fail closed rather than electing by timing. - Report installs with more than one superadmin via a structured first-superadmin.multiple_superadmins warning on sign-in, folded into the existing single read. No user row is changed. - session.create.before: catch election failures, log auth.first_superadmin_election.failed and continue without promotion, so a D1 error never blocks login. - Migration renumbered 0242 -> 0243 (open PR #2721 already uses 0242). It now only inserts the marker for installs that already have a non-system superadmin and never updates or deletes users; the oldest-superadmin demotion is removed. - Tests: real-SQL election/migration tests on drizzle-built tables (attack cases paired with owner controls), the auth.test fixture regression, and workers tests that drive BetterAuth's createSession against real D1 (happy path, existing-account refusal, D1 outage, owner-email skip, and the BOOTSTRAP_OWNER_EMAIL recovery path). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: first-superadmin election, duplicate superadmins and admin recovery Describe the one-time ordinary election (only-real-account guard, durable setup.firstSuperadminEstablished marker, login never blocked), how to review and resolve more than one superadmin, and the recovery paths when no account can administer the instance: the BOOTSTRAP_OWNER_EMAIL Worker secret (bootstrapSessionAllowed) or a direct D1 update. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(api): keep first-superadmin migration at 0242 and locate it by name Coordinator decision: main ends at 0241 and the builder branch also uses 0242; whichever merges second renumbers, so do not renumber speculatively. Tests now find the migration by its `_first_superadmin_election.sql` suffix, and the code comments and docs name it without a number, so a renumber is a file rename only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs: correct first-superadmin recovery details after review - wrangler secret put needs --name (no checked-in env sections) - owner-email recovery does not apply to an installer-bound instance whose owner was already established; use the D1 update there - the duplicate-superadmin warning is logged only without BOOTSTRAP_OWNER_EMAIL - admin-settings and .env.example no longer claim nobody is ever promoted again - rule 40 notes where the promotion lives now Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test: parameterize duplicated first-superadmin attack and self-heal cases Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(api): number the first-superadmin migration 0243 0242 is taken by the Cloudflare build executor migration (PR #2721), which is already applied on staging. Migration-ordering CI rejects duplicate prefixes; the migration is replay-safe and tests locate it by name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore(api): renumber the first-superadmin migration to 0246 after main's 0245 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Raphaël Titsworth-Morin <raphael@raphaeltm.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>



First-admin bootstrap is now atomic, one-time, and repairs duplicate superadmins created by the former race.
Static Verification Checks
Pending registration boundary (Passed) — apps/api/src/auth.ts user.create.before stores approval-gated accounts pending, eliminating privileged data before the raced insert.
Atomic election ownership (Passed) — establishFirstSuperadmin uses one atomic D1 batch and marker owner, permitting only one concurrent promotion.
One-time fallback guard (Passed) — LOGIN_SELF_HEAL_SQL requires the durable election marker absent, preventing later sessions from reopening bootstrap authority.
Historical duplicate repair (Passed) — 0242_first_superadmin_election.sql retains one active winner and demotes duplicate bootstrap superadmins, repairing persisted race outcomes.
Legitimate bootstrap behavior (Passed) — first-superadmin.test.ts asserts one winner, one pending loser, upgrade closure, and sole-operator recovery eligibility.
Superadmin sink unchanged (Passed) — middleware/auth.ts requireSuperadmin still accepts only stored superadmin roles, preserving legitimate platform authorization behavior.
Finding:
acf_1c430965be49bdf046a1933ae3b533a0Patch:
arp_b3419d94d44f6446827c9880f23da099Base commit:
4bdaaf61f6cecb83b6a3bab30e011257ea019710Patch SHA-256:
bf8d7dc467bccc64370fd4cded9f0d5d69e881c3892bf7eb4bf85b0f2b38b08c