Repository navigation
fix(upsert): check permission before skipping unchanged documents - #1090
Conversation
An unchanged upsert was skipped before the create or update check, so a caller without permission could tell an exact copy of a document (skipped) from any other payload (refused), learning whether it exists and what it holds. Authorize first; unchanged documents are still not written. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 11 minutes. View limit details
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. |
Code review
The upsert flow now checks create or update permission before skipping an unchanged document, while authorized no-op upserts still return zero without writing. End-to-end coverage checks that unauthorized callers are refused for both matching and nonmatching payloads and that an authorized owner retains the no-op behavior. The latest test change also verifies that refused upserts leave the stored update timestamp unchanged. Latest changes: The newest commit records the stored document's update timestamp before denied upserts and asserts it remains unchanged afterward. Walkthrough (2)
Reviewed the commits since Tip Comment |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Refs appwrite/appwrite code-scanning alert 5048.
The bug
upsert()insrc/Database/Trait/Documents.phpdropped a document that matched the stored row before checking create/update permission (basemain, lines 2474-2496):The stored row (
$old) is read with authorization skipped, so the comparison runs against data the caller may not be allowed to see.Attack: a user with no read and no update permission on document
private(secret = "hunter2") calls the Appwrite upsert endpoint:secret: "guess"returns 401, because the payload differs, so the auth check runs.secret: "hunter2"returns 200 with an empty body, because the payload matches, so the auth check never runs.One request per guess tells the caller whether the document exists and whether each guessed value is correct. Nothing is written.
The fix
The permission check moves above the unchanged skip:
A caller without permission gets the same
Authorizationerror whatever it sends, so the response no longer depends on the stored content. Computing$hasChangesfirst has no side effects.Behaviour changes
Authorization. This includes callers who can read the document but not update it.updateDocument()still accepts a no-op with read permission only, so upsert is now the stricter of the two.Not changed
utopia-php/databaserelease and acomposer update(it currently locks 10.2.5, and its constraint^10.2.5allows the new version).Verification
New test
DocumentTests::testUpsertDocumentsNoopStillRequiresPermission. An intruder (no read, no update, document security on) upserts the stored document twice: once with the exact storedsecret, once with a guess. Both must throwAuthorizationException, and$updatedAtmust not move. The owner's unchanged upsert must still return 0.Without the fix (
src/Database/Trait/Documents.phpfrom the merge base), local run:Every lane fails the same way: the exact-copy upsert returns 0 instead of being refused.
With the fix, local run:
Memory has no upsert support and skips the test.
To reproduce the failure:
CI runs the test on every adapter lane; Linter (Pint + PHPStan) and CodeQL pass.
🤖 Generated with Claude Code
Related: appwrite-labs/cloud#6666