Repository navigation
fix: freeze the PDF certification level with the request snapshot #9375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d1cb212
ee62fb4
4fdbaab
c573796
5713047
34678ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,32 +10,32 @@ | |
|
|
||
| use OCA\Libresign\Db\File as FileEntity; | ||
| use OCA\Libresign\Enum\DocMdpLevel; | ||
| use OCA\Libresign\Exception\LibresignException; | ||
| use OCA\Libresign\Service\Policy\AbstractFilePolicyApplier; | ||
| use OCA\Libresign\Service\Policy\Model\ResolvedPolicy; | ||
| use OCA\Libresign\Service\Policy\Provider\DocMdp\DocMdpPolicy; | ||
| use OCP\AppFramework\Http; | ||
| use OCP\IUser; | ||
|
|
||
| class DocMdpFilePolicyApplier extends AbstractFilePolicyApplier { | ||
|
|
||
| #[\Override] | ||
| public function apply(FileEntity $file, array $data): void { | ||
| $user = ($data['userManager'] ?? null) instanceof IUser ? $data['userManager'] : null; | ||
| $requestOverrides = $this->getOverrides($data); | ||
| $activeContext = $this->extractActiveContext($data); | ||
| $resolvedPolicy = $activeContext === null | ||
| ? $this->policyService->resolveForUser(DocMdpPolicy::KEY, $user, $requestOverrides) | ||
| : $this->policyService->resolveForUser(DocMdpPolicy::KEY, $user, $requestOverrides, $activeContext); | ||
| $file->setDocmdpLevelEnum(DocMdpLevel::tryFrom((int)$resolvedPolicy->getEffectiveValue()) ?? DocMdpLevel::NOT_CERTIFIED); | ||
| $resolvedPolicy = $this->resolveForRequester($data); | ||
| $file->setDocmdpLevelEnum($this->toLevel($resolvedPolicy)); | ||
| $this->storePolicySnapshot($file, $resolvedPolicy); | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function sync(FileEntity $file, array $data): void { | ||
| $requestOverrides = $this->getOverrides($data); | ||
| $activeContext = $this->extractActiveContext($data); | ||
| $resolvedPolicy = $activeContext === null | ||
| ? $this->policyService->resolveForUserId(DocMdpPolicy::KEY, $file->getUserId(), $requestOverrides) | ||
| : $this->policyService->resolveForUserId(DocMdpPolicy::KEY, $file->getUserId(), $requestOverrides, $activeContext); | ||
| $newLevel = DocMdpLevel::tryFrom((int)$resolvedPolicy->getEffectiveValue()) ?? DocMdpLevel::NOT_CERTIFIED; | ||
| if ($this->isPolicySnapshotFrozen($file, DocMdpPolicy::KEY)) { | ||
| $this->assertFrozenLevelIsKept($file, $requestOverrides); | ||
| return; | ||
| } | ||
|
|
||
| $resolvedPolicy = $this->resolveForFileOwner($file, $data); | ||
| $newLevel = $this->toLevel($resolvedPolicy); | ||
| $metadataBeforeUpdate = $file->getMetadata() ?? []; | ||
| $this->storePolicySnapshot($file, $resolvedPolicy); | ||
| $metadataChanged = ($file->getMetadata() ?? []) !== $metadataBeforeUpdate; | ||
|
|
@@ -51,8 +51,71 @@ public function supportsCoreFlowSync(): bool { | |
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * The level a request certifies with once saved with $data, so its signers | ||
| * can be checked before it is: the stored one when frozen, otherwise the | ||
| * one apply() or sync() resolves. Null when a frozen request stores none, in | ||
| * which case the current level applies. | ||
| */ | ||
| public function resolveLevel(?FileEntity $file, array $data): ?DocMdpLevel { | ||
| if ($file === null) { | ||
| return $this->toLevel($this->resolveForRequester($data)); | ||
| } | ||
|
|
||
| if (!$this->isPolicySnapshotFrozen($file, DocMdpPolicy::KEY)) { | ||
| return $this->toLevel($this->resolveForFileOwner($file, $data)); | ||
| } | ||
|
|
||
| $policySnapshot = ($file->getMetadata() ?? [])['policy_snapshot'] ?? null; | ||
| return is_array($policySnapshot) && array_key_exists(DocMdpPolicy::KEY, $policySnapshot) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Consequently, signer validation may fall back to the current system level even though the children will be signed using their frozen levels. Please add a parameterized API regression test in Suggested named datasets, expressed as
Use a shared fixture helper to create a two-document envelope with one signing participant, then PATCH the envelope UUID with two signing participants after changing the system level. Assert the effective DocMDP level and The initial single-signer request also serves as a control proving that the override was authorized. If the regression reproduces, align envelope signer validation with the effective frozen policy of its documents. Avoid introducing a separate policy-resolution implementation solely for envelopes. |
||
| ? $file->getDocmdpLevelEnum() | ||
| : null; | ||
| } | ||
|
|
||
| private function resolveForRequester(array $data): ResolvedPolicy { | ||
| $user = ($data['userManager'] ?? null) instanceof IUser ? $data['userManager'] : null; | ||
| $requestOverrides = $this->getOverrides($data); | ||
| $activeContext = $this->extractActiveContext($data); | ||
| return $activeContext === null | ||
| ? $this->policyService->resolveForUser(DocMdpPolicy::KEY, $user, $requestOverrides) | ||
| : $this->policyService->resolveForUser(DocMdpPolicy::KEY, $user, $requestOverrides, $activeContext); | ||
| } | ||
|
|
||
| private function resolveForFileOwner(FileEntity $file, array $data): ResolvedPolicy { | ||
| $requestOverrides = $this->getOverrides($data); | ||
| $activeContext = $this->extractActiveContext($data); | ||
| return $activeContext === null | ||
| ? $this->policyService->resolveForUserId(DocMdpPolicy::KEY, $file->getUserId(), $requestOverrides) | ||
| : $this->policyService->resolveForUserId(DocMdpPolicy::KEY, $file->getUserId(), $requestOverrides, $activeContext); | ||
| } | ||
|
|
||
| private function toLevel(ResolvedPolicy $resolvedPolicy): DocMdpLevel { | ||
| return DocMdpLevel::tryFrom((int)$resolvedPolicy->getEffectiveValue()) ?? DocMdpLevel::NOT_CERTIFIED; | ||
| } | ||
|
|
||
| /** @return array<string, int> */ | ||
| private function getOverrides(array $data): array { | ||
| return $this->extractSinglePolicyOverride($data, DocMdpPolicy::KEY); | ||
| } | ||
|
|
||
| /** | ||
| * A frozen request keeps the certification level it was sent with. A client | ||
| * may still send that same level along an unrelated update, which changes | ||
| * nothing. | ||
| * | ||
| * @param array<string, mixed> $requestOverrides | ||
| */ | ||
| private function assertFrozenLevelIsKept(FileEntity $file, array $requestOverrides): void { | ||
| $submittedLevel = $requestOverrides[DocMdpPolicy::KEY] ?? null; | ||
| if ($submittedLevel === null || (is_numeric($submittedLevel) && (int)$submittedLevel === $file->getDocmdpLevel())) { | ||
| return; | ||
| } | ||
|
|
||
| throw new LibresignException( | ||
| // TRANSLATORS Error shown when someone tries to change the PDF certification (DocMDP level) of a document after the signing flow already started. | ||
| $this->l10n?->t('The PDF certification cannot be changed after the signing flow has started.') | ||
| ?? 'The PDF certification cannot be changed after the signing flow has started.', | ||
| Http::STATUS_UNPROCESSABLE_ENTITY, | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
| use OCA\Libresign\Db\SignRequest as SignRequestEntity; | ||
| use OCA\Libresign\Db\SignRequestMapper; | ||
| use OCA\Libresign\Db\UserElementMapper; | ||
| use OCA\Libresign\Enum\DocMdpLevel; | ||
| use OCA\Libresign\Enum\FileStatus; | ||
| use OCA\Libresign\Enum\IdentifyMethodRequirement; | ||
| use OCA\Libresign\Enum\SignRequestStatus; | ||
|
|
@@ -47,6 +48,7 @@ | |
| use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\IToken; | ||
| use OCA\Libresign\Service\Policy\PolicyService; | ||
| use OCA\Libresign\Service\Policy\Provider\CollectMetadata\CollectMetadataPolicy; | ||
| use OCA\Libresign\Service\Policy\Provider\DocMdp\DocMdpPolicy; | ||
| use OCA\Libresign\Service\Policy\Provider\Footer\FooterPolicy; | ||
| use OCA\Libresign\Service\Policy\Provider\Footer\FooterPolicyValue; | ||
| use OCA\Libresign\Service\SignRequest\SignRequestService; | ||
|
|
@@ -1246,7 +1248,8 @@ private function configureEngine(): void { | |
| $this->restoreMissingVisibleElementImages(); | ||
| $this->engine | ||
| ->setVisibleElements($this->getVisibleElements()) | ||
| ->setSignatureParams($this->getSignatureParams()); | ||
| ->setSignatureParams($this->getSignatureParams()) | ||
| ->setDocMdpLevel($this->getDocMdpLevel()); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1492,6 +1495,21 @@ private function getFooterPolicyValue(): string { | |
| return $this->footerPolicyValue; | ||
| } | ||
|
|
||
| /** | ||
| * Once frozen, the request certifies with the level it stores, whatever the | ||
| * current level. Without a stored level, the current one applies. | ||
| */ | ||
| private function getDocMdpLevel(): ?DocMdpLevel { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The signing engine now uses the frozen request's DocMDP level, but signer validation still relies on the current system configuration in some cases.
This can allow two signers on a request whose effective certification is level 1, or reject signers on a frozen level-0 request after the administrator changes the global configuration. Please align signer validation with the backend-resolved effective policy. Add regression tests covering:
The tests should assert the observable API behavior and persisted policy, rather than only checking calls to the validator. |
||
| $policySnapshot = ($this->libreSignFile->getMetadata() ?? [])['policy_snapshot'] ?? null; | ||
| if (!is_array($policySnapshot) || !array_key_exists(DocMdpPolicy::KEY, $policySnapshot)) { | ||
| return null; | ||
| } | ||
|
|
||
| return FileStatusService::isPolicySnapshotFrozen($this->libreSignFile, $this->fileMapper->findEnvelope($this->libreSignFile)) | ||
| ? $this->libreSignFile->getDocmdpLevelEnum() | ||
| : null; | ||
| } | ||
|
|
||
| protected function getSignedFile(): ?File { | ||
| $nodeId = $this->libreSignFile->getSignedNodeId(); | ||
| if (!$nodeId) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The request-specific DocMDP level may expose stale certification parameters when a JSignPdfHandler instance is reused.
getJSignParam()caches its parameter object, andaddJSignParameters()merges new options without clearing previously supplied ones.When a first document uses a certifying level and a second document uses
NOT_CERTIFIED, the second signing operation does not explicitly remove the previous-cloption.Please add a regression test using the same handler instance for two consecutive signing operations:
NOT_CERTIFIED.-cland that its resulting PDF contains no DocMDP certification.NOT_CERTIFIEDas a control.Please also cover the visible-signature path, which clones the cached parameters.
If the test confirms parameter reuse, reset or rebuild the per-document parameters and retain the regression test.