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..b02ecaa76 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -5952,6 +5952,66 @@ 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]); + $updatedAt = $auth->skip(fn () => $database->getDocument($collection, 'private'))->getUpdatedAt(); + + 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) { + } + } + + $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()); + + $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();