Skip to content

fix(upsert): check permission before skipping unchanged documents - #1090

Merged
ChiragAgg5k merged 2 commits into
mainfrom
fix/upsert-noop-authorization
Nov 30, 2026
Merged

ChiragAgg5k merged 2 commits into
mainfrom
fix/upsert-noop-authorization

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Refs appwrite/appwrite code-scanning alert 5048.

The bug

upsert() in src/Database/Trait/Documents.php dropped a document that matched the stored row before checking create/update permission (base main, lines 2474-2496):

if (! $hasChanges) {
    // If not updating a single attribute and the document is the same as the old one, skip it
    unset($documents[$key]);
    continue;                      // <- returns before any authorization check
}

if ($old->isEmpty()) {
    // Create check
} elseif (! $this->authorization->isValid(new Input(PermissionType::Update, ...))) {
    throw new AuthorizationException(...);
}

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.

before:  upsert ──> load $old (auth skipped) ──> unchanged? ──yes──> skip, 200   <- leaks
                                                     │
                                                     no──> can update? ──no──> 401
after:   upsert ──> load $old (auth skipped) ──> can create/update? ──no──> 401  (same for every payload)
                                                     │
                                                     yes──> unchanged? ──yes──> skip (no write, count 0)
                                                              └──no──> write

The fix

The permission check moves above the unchanged skip:

if ($old->isEmpty()) {
    // Create check, unchanged
} elseif (! $this->authorization->isValid(new Input(PermissionType::Update, ...))) {
    throw new AuthorizationException(...);
}

if (! $hasChanges) {
    unset($documents[$key]);
    continue;
}

A caller without permission gets the same Authorization error whatever it sends, so the response no longer depends on the stored content. Computing $hasChanges first has no side effects.

Behaviour changes

  • An unchanged upsert by a caller without update permission used to be a silent no-op (count 0). It now throws 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.
  • In a bulk upsert, one such document now fails the whole batch instead of being dropped silently. (Bulk upserts in Appwrite normally run with an API key, which skips authorization.)
  • Callers with permission see no change. Their unchanged upsert is still skipped, not written and not counted.

Not changed

  • Create vs. update still differ: "created" means the id was free, and 401 can mean the id exists. That is inherent to upsert and out of scope.
  • The Appwrite HTTP handler is not changed. Appwrite picks up this fix through a utopia-php/database release and a composer update (it currently locks 10.2.5, and its constraint ^10.2.5 allows 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 stored secret, once with a guess. Both must throw AuthorizationException, and $updatedAt must not move. The owner's unchanged upsert must still return 0.

Without the fix (src/Database/Trait/Documents.php from the merge base), local run:

== MariaDBTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== MySQLTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== PostgresTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== SQLiteTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== MemoryTest: OK (1 test, 0 assertions)
== RedisTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== MongoDBTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== MirrorTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== PoolTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== SharedTables/MariaDBTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== SharedTables/MySQLTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== SharedTables/PostgresTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== SharedTables/SQLiteTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== SharedTables/RedisTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== SharedTables/MongoDBTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.
== Schemaless/MongoDBTest: FAILURES! Tests: 1, Assertions: 1, Failures: 1.

Every lane fails the same way: the exact-copy upsert returns 0 instead of being refused.

1) Tests\E2E\Adapter\MariaDBTest::testUpsertDocumentsNoopStillRequiresPermission
An upsert of a document the caller cannot update must be refused, changed or not

With the fix, local run:

== MariaDBTest: OK (1 test, 2 assertions)
== MySQLTest: OK (1 test, 2 assertions) [rerun after MySQL finished starting]
== PostgresTest: OK (1 test, 2 assertions)
== SQLiteTest: OK (1 test, 2 assertions)
== MemoryTest: OK (1 test, 0 assertions)
== RedisTest: OK (1 test, 2 assertions)
== MongoDBTest: OK (1 test, 2 assertions)
== MirrorTest: OK (1 test, 2 assertions)
== PoolTest: OK (1 test, 2 assertions)
== SharedTables/MariaDBTest: OK (1 test, 2 assertions)
== SharedTables/MySQLTest: OK (1 test, 2 assertions)
== SharedTables/PostgresTest: OK (1 test, 2 assertions)
== SharedTables/SQLiteTest: OK (1 test, 2 assertions)
== SharedTables/RedisTest: OK (1 test, 2 assertions)
== SharedTables/MongoDBTest: OK (1 test, 2 assertions)
== Schemaless/MongoDBTest: OK (1 test, 2 assertions)

Memory has no upsert support and skips the test.

To reproduce the failure:

git checkout $(git merge-base origin/main HEAD) -- src/Database/Trait/Documents.php
docker compose exec -T tests vendor/bin/phpunit --configuration phpunit.xml tests/e2e/Adapter/MariaDBTest.php --filter testUpsertDocumentsNoopStillRequiresPermission
git checkout HEAD -- src/Database/Trait/Documents.php

CI runs the test on every adapter lane; Linter (Pint + PHPStan) and CodeQL pass.

🤖 Generated with Claude Code

Related: appwrite-labs/cloud#6666

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>
@coderabbitai

coderabbitai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You'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.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 0b2dbbd and 020716d.

Files selected for processing: 2

  • src/Database/Trait/Documents.php
  • tests/e2e/Adapter/Scopes/DocumentTests.php
  • 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

@appwrite

appwrite Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Code review

Commit Status Tier Verdict Details
020716d Reviewed Reviewed 🟢 S · Ready to merge ✅ Approved View review

No actionable defect was found in the incremental timestamp assertion or the earlier upsert permission change.

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)
File Change
src/Database/Trait/Documents.php Checks create or update permission before skipping unchanged upsert documents.
tests/e2e/Adapter/Scopes/DocumentTests.php Covers unauthorized unchanged and changed upserts, authorized no-op behavior, and unchanged timestamps after refusals.

Reviewed the commits since 292ae3d

Tip

Comment @appwrite review to re-run, or mention @appwrite with a question.

@appwrite appwrite Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Tier S · Looks good to merge. Summary

@ChiragAgg5k ChiragAgg5k self-assigned this Nov 30, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ChiragAgg5k
ChiragAgg5k merged commit 9d98f6a into main Nov 30, 2026
31 checks passed
@ChiragAgg5k
ChiragAgg5k deleted the fix/upsert-noop-authorization branch November 30, 2026 13:21
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