From 10ecbd16081700f19a3c8d31a78ad70a5296237f Mon Sep 17 00:00:00 2001 From: no84by <50858621+no84by@users.noreply.github.com> Date: Wed, 18 Nov 2026 02:05:11 +0000 Subject: [PATCH] fix: restore folder service user and skip already signed children in envelopes getNextcloudFiles() left the folder service on the file owner, so the visible element lookup of later envelope children ran as the owner and failed for guest signers. signSequentially() also re-signed children already signed by the same signer on retry, stacking signatures. --- lib/Service/SignFileService.php | 13 +++++++++++- .../php/Unit/Service/SignFileServiceTest.php | 21 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) 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); + } }