From 292ae3d55428e6faef91cb839555813ee93fbedd Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 30 Nov 2026 09:05:11 +0000 Subject: [PATCH 1/2] fix(upsert): check permission before skipping unchanged documents 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 --- src/Database/Trait/Documents.php | 16 +++--- tests/e2e/Adapter/Scopes/DocumentTests.php | 57 ++++++++++++++++++++++ 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/src/Database/Trait/Documents.php b/src/Database/Trait/Documents.php index 282704010..2c384b51b 100644 --- a/src/Database/Trait/Documents.php +++ b/src/Database/Trait/Documents.php @@ -2471,16 +2471,11 @@ private function upsert( } } - if (! $hasChanges) { - // If not updating a single attribute and the document is the same as the old one, skip it - unset($documents[$key]); - - continue; - } - // If old is empty, check if user has create permission on the collection // If old is not empty, check if user has update permission on the collection // If old is not empty AND documentSecurity is enabled, check if user has update permission on the collection or document + // Checked before the unchanged skip below: otherwise a caller without permission could tell an unchanged + // copy of a document it cannot read (skipped) from any other payload (refused), learning its contents if ($old->isEmpty()) { if (! $this->authorization->isValid(new Input(PermissionType::Create, $collection->getPermissionsByType(PermissionType::Create)))) { @@ -2493,6 +2488,13 @@ private function upsert( throw new AuthorizationException($this->authorization->getDescription()); } + if (! $hasChanges) { + // If not updating a single attribute and the document is the same as the old one, skip it + unset($documents[$key]); + + continue; + } + $updatedAt = $document->getUpdatedAt(); $document diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index a7b29b9a5..a99906ea5 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -5952,6 +5952,63 @@ public function testUpsertDocumentsNoop(): void $this->assertEquals(0, $count); } + public function testUpsertDocumentsNoopStillRequiresPermission(): void + { + /** @var Database $database */ + $database = $this->getDatabase(); + + if (! ($database->getAdapter()->hasFeature(Feature\Upserts::class))) { + $this->expectNotToPerformAssertions(); + + return; + } + + $auth = $database->getAuthorization(); + $collection = 'upsertNoopPermission'; + $database->createCollection(Collection::create(id: $collection, permissions: [ + Permission::create(Role::any()), + ], documentSecurity: true)); + $database->createAttribute($collection, Attribute::string(key: 'secret', size: 128, required: true)); + + $document = new Document([ + '$id' => 'private', + 'secret' => 'hunter2', + '$permissions' => [ + Permission::read(Role::user('owner')), + Permission::update(Role::user('owner')), + ], + ]); + $database->upsertDocuments($collection, [$document]); + + try { + $auth->cleanRoles(); + $auth->addRole(Role::any()->toString()); + $auth->addRole(Role::user('intruder')->toString()); + + // An unchanged copy must be refused like any other payload, or the refusal tells a guess apart + foreach (['hunter2', 'guess'] as $secret) { + try { + $database->upsertDocuments($collection, [new Document([ + '$id' => 'private', + 'secret' => $secret, + '$permissions' => $document->getPermissions(), + ])]); + $this->fail('An upsert of a document the caller cannot update must be refused, changed or not'); + } catch (AuthorizationException) { + } + } + + $auth->cleanRoles(); + $auth->addRole(Role::any()->toString()); + $auth->addRole(Role::user('owner')->toString()); + + $this->assertSame(0, $database->upsertDocuments($collection, [$document]), 'The owner\'s unchanged upsert is still a no-op'); + } finally { + $auth->cleanRoles(); + $auth->addRole(Role::any()->toString()); + } + } + public function testUpsertDocumentsReadsStoredRowsInOneBatch(): void { $shared = $this->getDatabase(); From 020716d50b9bd11dc324d5b65e37a3287c8aa8fc Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 30 Nov 2026 13:04:40 +0000 Subject: [PATCH 2/2] test(upsert): a refused no-op upsert leaves $updatedAt untouched Co-Authored-By: Claude Opus 5.5 --- tests/e2e/Adapter/Scopes/DocumentTests.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index a99906ea5..b02ecaa76 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -5979,6 +5979,7 @@ public function testUpsertDocumentsNoopStillRequiresPermission(): void ], ]); $database->upsertDocuments($collection, [$document]); + $updatedAt = $auth->skip(fn () => $database->getDocument($collection, 'private'))->getUpdatedAt(); try { $auth->cleanRoles(); @@ -5998,6 +5999,8 @@ public function testUpsertDocumentsNoopStillRequiresPermission(): void } } + $this->assertSame($updatedAt, $auth->skip(fn () => $database->getDocument($collection, 'private'))->getUpdatedAt(), 'A refused upsert must not touch the document'); + $auth->cleanRoles(); $auth->addRole(Role::any()->toString()); $auth->addRole(Role::user('owner')->toString());