diff --git a/lib/Service/SignFileService.php b/lib/Service/SignFileService.php index 05f653c6c4..3db7d12a38 100644 --- a/lib/Service/SignFileService.php +++ b/lib/Service/SignFileService.php @@ -652,6 +652,10 @@ private function signSequentially(array $signRequests): ?DateTimeInterface { continue; } $this->signRequest = $signRequestData['signRequest']; + if ($this->signRequest->getSigned()) { + // Never sign a child twice for the same signer: a retried request would stack signatures + continue; + } $this->engine = null; $this->setVisibleElements($this->elementsInput); $this->fileToSign = null; @@ -1664,8 +1668,15 @@ public function getNextcloudFiles(FileEntity $fileData): array { ]), AppFrameworkHttp::STATUS_NOT_FOUND); } $storageUserId = $this->fileMapper->getStorageUserIdByUuid($fileData->getUuid()); + $previousUserId = $this->folderService->getUserId(); $this->folderService->setUserId($storageUserId); - $fileToSign = $this->folderService->getFileByNodeId($nodeId); + try { + $fileToSign = $this->folderService->getFileByNodeId($nodeId); + } finally { + // Restore the signer context, otherwise later lookups (e.g. visible elements of the next + // envelope child) run as the file owner + $this->folderService->setUserId($previousUserId); + } if (!$fileToSign instanceof File) { throw new LibresignException(json_encode([ 'action' => JSActions::ACTION_DO_NOTHING, diff --git a/tests/php/Unit/Service/SignFileServiceTest.php b/tests/php/Unit/Service/SignFileServiceTest.php index a4c0fe68c1..9b9245d5ef 100644 --- a/tests/php/Unit/Service/SignFileServiceTest.php +++ b/tests/php/Unit/Service/SignFileServiceTest.php @@ -3163,4 +3163,25 @@ public function testCleanupUnsignedSignedFile(bool $hasFile, ?\Exception $delete $this->assertNull(self::invokePrivate($service, 'createdSignedFile')); } + + public function testGetNextcloudFilesRestoresFolderServiceUserId(): void { + $fileData = new \OCA\Libresign\Db\File(); + $fileData->setNodeId(42); + $fileData->setUuid('file-uuid'); + + $this->fileMapper->method('getStorageUserIdByUuid')->with('file-uuid')->willReturn('owner'); + $userIds = []; + $this->folderService->method('getUserId')->willReturn('signer'); + $this->folderService->method('setUserId') + ->willReturnCallback(function (?string $userId) use (&$userIds): void { + $userIds[] = $userId; + }); + $this->folderService->method('getFileByNodeId')->with(42) + ->willReturn($this->createMock(\OCP\Files\File::class)); + + $service = $this->getService(); + $service->getNextcloudFiles($fileData); + + $this->assertSame(['owner', 'signer'], $userIds); + } }