Repository navigation
RTOP-315: Fix TOCTOU race in first-user bootstrap (/api/create-user) and reconcile stale RBAC docs #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+181
−18
Merged
RTOP-315: Fix TOCTOU race in first-user bootstrap (/api/create-user) and reconcile stale RBAC docs #214
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ab06a45
docs: reconcile RBAC comment and SECURITY.md with the shipped role model
thiagoralves 92bf096
fix(users): serialize first-admin bootstrap to close a TOCTOU race
thiagoralves afcedee
test(users): regression test for the first-admin bootstrap race
thiagoralves 0db007f
docs: mention bootstrap_marker and the full "delete restapi.db" recov…
thiagoralves File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| # SPDX-License-Identifier: MIT | ||
| # Copyright (c) 2026 Autonomy® | ||
|
|
||
| """Regression test for the first-admin bootstrap TOCTOU race. | ||
|
|
||
| Before the fix, N concurrent POST /api/create-user calls on an empty database | ||
| all passed the User.query.first() check and all committed with role=admin. | ||
| The fix serializes the bootstrap branch with a threading.Lock, re-checks | ||
| inside the lock, and writes a UNIQUE sentinel row in the same transaction | ||
| as the first user. This test fires N concurrent requests and asserts exactly | ||
| one admin lands. | ||
| """ | ||
|
|
||
| import threading | ||
|
|
||
| from webserver import restapi | ||
|
|
||
|
|
||
| def _race_create_user(app, n: int) -> list[tuple[str, int]]: | ||
| """Fire ``n`` concurrent create-user POSTs, released by a shared barrier. | ||
|
|
||
| Each thread owns its own test client. The barrier lines every thread up | ||
| before the simultaneous release so the contention window is as tight as | ||
| the host can produce. | ||
| """ | ||
| results: list[tuple[str, int]] = [] | ||
| results_lock = threading.Lock() | ||
| release = threading.Barrier(n) | ||
|
|
||
| def post(index: int) -> None: | ||
| username = f"racer{index}" | ||
| client = app.test_client() | ||
| release.wait(timeout=10) | ||
| resp = client.post( | ||
| "/api/create-user", | ||
| json={"username": username, "password": "race-test-pw-12"}, | ||
| ) | ||
| with results_lock: | ||
| results.append((username, resp.status_code)) | ||
|
|
||
| threads = [threading.Thread(target=post, args=(i,)) for i in range(n)] | ||
| for t in threads: | ||
| t.start() | ||
| for t in threads: | ||
| t.join() | ||
| return results | ||
|
|
||
|
|
||
| def test_concurrent_bootstrap_creates_exactly_one_admin(app): | ||
| """Five concurrent POSTs on an empty DB produce one admin and four refusals.""" | ||
| try: | ||
| results = _race_create_user(app, n=5) | ||
|
|
||
| successes = [r for r in results if r[1] == 201] | ||
| refusals = [r for r in results if r[1] != 201] | ||
|
|
||
| assert len(successes) == 1, ( | ||
| f"expected exactly one 201, got {len(successes)} out of {len(results)}: {results}" | ||
| ) | ||
| # Losing racers see "a user exists" inside the lock and return 401. A | ||
| # lock bypass that reached the commit would land on IntegrityError | ||
| # and also 401. | ||
| for username, status in refusals: | ||
| assert status == 401, f"losing racer {username} got {status}, expected 401" | ||
|
|
||
| with app.app_context(): | ||
| users = restapi.User.query.all() | ||
| assert len(users) == 1 | ||
| assert users[0].role == restapi.ADMIN_ROLE | ||
| markers = restapi.BootstrapMarker.query.all() | ||
| assert len(markers) == 1, "sentinel row must land in the same transaction" | ||
| finally: | ||
| # The worker threads each opened a pooled connection; dispose the pool | ||
| # so no stale schema-inspection cache survives into the next test. | ||
| with app.app_context(): | ||
| restapi.db.engine.dispose() | ||
|
|
||
|
|
||
| def test_bootstrap_sentinel_blocks_a_second_bootstrap_after_user_deletion(app): | ||
| """Even if the user table is wiped later, the sentinel keeps bootstrap closed. | ||
|
|
||
| This exercises the belt-and-suspenders path: the lock's re-check reads BOTH | ||
| User and BootstrapMarker, so a device whose users were deleted outside the | ||
| API cannot be re-bootstrapped without authentication. | ||
| """ | ||
| client = app.test_client() | ||
| first = client.post( | ||
| "/api/create-user", | ||
| json={"username": "founder", "password": "pw-abcd-1234"}, | ||
| ) | ||
| assert first.status_code == 201, first.get_json() | ||
|
|
||
| with app.app_context(): | ||
| restapi.User.query.delete() | ||
| restapi.db.session.commit() | ||
| assert restapi.User.query.count() == 0 | ||
| assert restapi.BootstrapMarker.query.count() == 1 | ||
|
|
||
| second = client.post( | ||
| "/api/create-user", | ||
| json={"username": "pretender", "password": "pw-abcd-1234"}, | ||
| ) | ||
| assert second.status_code == 401 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Behaviour change worth documenting: once this row exists, wiping only the
userstable outside the API no longer reopens the bootstrap window; the operator has to removerestapi.db. Please add a sentence todocs/SECURITY.md(database section) anddocs/TROUBLESHOOTING.md(reset procedure) so the recovery path is explicit.