Skip to content

[Codex Security] Concurrent first-user registrations can create multiple superadmins - #2753

Closed
raphaeltm wants to merge 1 commit into
mainfrom
codex-security/arp_b3419d94d44f6446827c9880f23da099
Closed

raphaeltm wants to merge 1 commit into
mainfrom
codex-security/arp_b3419d94d44f6446827c9880f23da099

Conversation

@raphaeltm

Copy link
Copy Markdown
Owner

First-admin bootstrap is now atomic, one-time, and repairs duplicate superadmins created by the former race.

  • Approval-gated accounts are inserted as pending; superadmin assignment occurs only after BetterAuth has persisted the user.
  • The D1 batch couples a unique marker claim to an owner-bound update, so concurrent sessions cannot both gain platform authority.
  • The self-heal honors the marker, and migration 0242 closes upgraded installations while demoting historical duplicate winners.
  • Existing serial bootstrap, configured-owner election, open registration, and sole-operator recovery paths retain their intended behavior.
  • Verification used source and final-diff evidence in static mode; runtime checks were not performed.

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_1c430965be49bdf046a1933ae3b533a0

  • Patch: arp_b3419d94d44f6446827c9880f23da099

  • Base commit: 4bdaaf61f6cecb83b6a3bab30e011257ea019710

  • Patch SHA-256: bf8d7dc467bccc64370fd4cded9f0d5d69e881c3892bf7eb4bf85b0f2b38b08c

@coderabbitai

coderabbitai Bot commented Nov 15, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are limited based on label configuration.

🏷️ Required labels (at least one): 1

  • coderabbit-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@sonarqubecloud

Copy link
Copy Markdown

@simple-agent-manager

Copy link
Copy Markdown
Contributor

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.

@simple-agent-manager

Copy link
Copy Markdown
Contributor

Closing in favour of #2772, which carries a corrected version of this fix.

simple-agent-manager Bot added a commit that referenced this pull request Nov 16, 2026
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant