Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/Database/Trait/Documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)))) {
Expand All @@ -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
Expand Down
60 changes: 60 additions & 0 deletions tests/e2e/Adapter/Scopes/DocumentTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading