Skip to content

fix(app): force expect_absent on first-time room ownership claims - #615

Closed
dolmaciabdullah-byte wants to merge 1 commit into
flop-labs:mainfrom
dolmaciabdullah-byte:fix/ownership-first-claim-cas
Closed

dolmaciabdullah-byte wants to merge 1 commit into
flop-labs:mainfrom
dolmaciabdullah-byte:fix/ownership-first-claim-cas

Conversation

@dolmaciabdullah-byte

Copy link
Copy Markdown
Contributor

Fixes #173.

What

Both write lanes (GET and POST) now force expect_absent=True when a first-time room ownership claim passes _note_write_gate with no existing owner. This turns the unconditional note_set into a CAS, so note_set's own lock rejects the second concurrent claimant with 409.

Why

_note_write_gate reads the owner note outside the file lock. Two concurrent first-time claims both see None, both pass the gate, and the second silently overwrites the first stealing the room. The gate already validates everything else (ownable prefix, DID format, signer matches value); the missing piece was carrying the "absent" fact forward into the write. Minimal fix: 10 lines in app.py, no store.py changes, no new parameters. #174 attempted a broader fix but drifted too far from current main; this is the narrow replacement. The stale hand-over window (#499) is deliberately left alone.

Checks

  • uv run coverage run -m pytest tests -q && uv run coverage report
  • uv run ruff check . && uv run ruff format --check . and uv run ty check
  • Docs that would now be wrong are updated: none no new route, parameter or response shape
  • New surface on a world-writable service: nothing new the 409 was already the documented outcome of a CAS conflict; this makes it actually happen

Verified against 248bcf3.

Two concurrent first-time claims to an unowned d- room both read the
owner note as absent and both pass _note_write_gate. Without CAS, the
second silently overwrites the first.

Now both write lanes (GET and POST) force expect_absent=True when the
owner note is absent at gate time, so note_set's own lock rejects the
second writer with 409.

Fixes flop-labs#173.

@luch91 luch91 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.

Request changes on exact head 73bb6e0. The narrow expect_absent change is directionally relevant, but this branch has no deterministic regression test for both signed GET and POST lanes and overlaps the later ownership-CAS work (#629) plus already-closed alternatives (#174/#444). Please either add the race regression and rebase/narrow the patch to the accepted ownership path, or close/supersede it explicitly so overlapping fixes do not compete.

@dolmaciabdullah-byte

Copy link
Copy Markdown
Contributor Author

Closing in favor of #629 which implements the CAS inside store.note_set's own lock the right place for it. Our app-layer guard was a stopgap; #629's first_claim flag is the proper fix. The conflict in app.py and @luch91's review confirm consolidation is correct.

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.

TOCTOU race in room ownership claim allows ownership theft

2 participants