Skip to content

[WEB-7894] fix: eliminate TOCTOU race in InstanceAdminSignUp (GHSA-p548-28jp-wr4p)#9332

Open
mguptahub wants to merge 2 commits into
previewfrom
web-7894/instance-admin-signup-toctou
Open

[WEB-7894] fix: eliminate TOCTOU race in InstanceAdminSignUp (GHSA-p548-28jp-wr4p)#9332
mguptahub wants to merge 2 commits into
previewfrom
web-7894/instance-admin-signup-toctou

Conversation

@mguptahub

@mguptahub mguptahub commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Advisory: GHSA-p548-28jp-wr4p (High)
  • Root cause: InstanceAdminSignUpEndpoint.post() checked for an existing InstanceAdmin with a non-atomic read (InstanceAdmin.objects.first()) and then created one in a separate write. Two concurrent requests could both pass the check and create two instance admins.
  • Fix: Wrapped the check + create in transaction.atomic() with select_for_update() on the Instance singleton row. The pre-check outside the lock (is_setup_done / existing admin) is a fast early-exit for the common post-setup path; the re-check inside the lock is the authoritative race-free guard.

Changes

apps/api/plane/license/api/views/admin.py

  • Added from django.db import transaction
  • Added fast pre-check on instance.is_setup_done (secondary guard)
  • Wrapped user + admin creation in transaction.atomic() with Instance.objects.select_for_update().get(pk=instance.pk) to serialize concurrent signups
  • Moved user_login() outside the transaction to avoid holding the DB lock during session writes

Test plan

  • Fresh instance: single signup → creates one admin, is_setup_done=True, login redirect works
  • Fresh instance: simulate concurrent requests → only one admin created, second gets ADMIN_ALREADY_EXIST redirect
  • Already-setup instance: signup attempt → blocked immediately by pre-check (no DB lock contention)

Co-authored-by: Plane AI noreply@plane.so

Summary by CodeRabbit

  • Bug Fixes
    • Improved the admin sign-up flow to handle simultaneous requests more safely.
    • Reduced the chance of duplicate or conflicting instance setup during first-time admin creation.
    • Ensured the setup/admin creation process is validated and applied consistently, preventing race-condition outcomes.

…48-28jp-wr4p)

Two concurrent POST requests to InstanceAdminSignUpEndpoint could both
pass the "no admin yet" check before either created the InstanceAdmin
row, resulting in dual instance admins.

Fix: wrap the check + create in transaction.atomic() with
select_for_update() on the Instance singleton row. The pre-check
(is_setup_done / existing admin) outside the lock is kept as a fast
early-exit for the common post-setup path. The re-check inside the
lock is the authoritative guard; user_login() is kept outside the
transaction to avoid holding the DB lock during session writes.

Co-authored-by: Plane AI <noreply@plane.so>
Copilot AI review requested due to automatic review settings June 29, 2026 12:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c2453293-2c24-471b-9c51-e57ca64882d1

📥 Commits

Reviewing files that changed from the base of the PR and between a89b73a and 9f46e2c.

📒 Files selected for processing (1)
  • apps/api/plane/license/api/views/admin.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/api/plane/license/api/views/admin.py

📝 Walkthrough

Walkthrough

InstanceAdminSignUpEndpoint.post now does a fast pre-check, then re-checks setup state inside transaction.atomic() with select_for_update() before creating the instance admin and updating setup fields. Token and session writes stay outside the transaction.

Changes

Atomic admin signup

Layer / File(s) Summary
Pre-check and atomic create with row lock
apps/api/plane/license/api/views/admin.py
Adds transaction import; updates the pre-check to use instance.is_setup_done or InstanceAdmin existence; wraps the user/profile/instance-admin creation path in transaction.atomic() with select_for_update(), then keeps token/session writes outside the transaction.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 I hop through locks and checks so neat,
One admin signup, tidy and complete.
The row stays guarded, snug and true,
While tokens dance just outside view.
Hooray, the instance bootstraps clean! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely summarizes the main change: fixing a TOCTOU race in InstanceAdminSignUp.
Description check ✅ Passed The description covers the bug, fix, and test plan, though the template sections for type of change and screenshots are not explicitly filled out.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch web-7894/instance-admin-signup-toctou

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

Comment @coderabbitai help to get the list of available commands.

@makeplane

makeplane Bot commented Jun 29, 2026

Copy link
Copy Markdown

Linked to Plane Work Item(s)

This comment was auto-generated by Plane

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@apps/api/plane/license/api/views/admin.py`:
- Around line 108-110: The initial-admin guard in the admin setup flow is too
narrowly scoped to a single Instance row, which can bypass protection if another
Instance exists. Update the guard in the relevant admin creation path around the
setup check to use a global InstanceAdmin existence check consistently, or
enforce the singleton assumption with a database-level constraint on Instance so
the shortcut remains safe. Refer to the setup logic in admin.py and the
InstanceAdmin/Instance models when applying the fix.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c3f8b459-ccfb-4a47-8bdb-22c19c25c6ca

📥 Commits

Reviewing files that changed from the base of the PR and between 90ae845 and a89b73a.

📒 Files selected for processing (1)
  • apps/api/plane/license/api/views/admin.py

Comment thread apps/api/plane/license/api/views/admin.py Outdated
The pre-check and re-check inside the atomic block were scoped to
filter(instance=instance), which could be bypassed if a stray second
Instance row existed. Changed both guards to InstanceAdmin.objects.exists()
to match the original global check and make them consistent with each other.

Co-authored-by: Plane AI <noreply@plane.so>
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.

2 participants