Skip to content

fix(docmdp): ignore unreferenced signature dictionaries - #9436

Open
mateuscmtropical wants to merge 1 commit into
LibreSign:mainfrom
mateuscmtropical:fix/docmdp-unreferenced-signature
Open

mateuscmtropical wants to merge 1 commit into
LibreSign:mainfrom
mateuscmtropical:fix/docmdp-unreferenced-signature

Conversation

@mateuscmtropical

Copy link
Copy Markdown

Resolves: #9271

📝 Summary

DocMdpHandler scans the raw PDF with regexes, so a leftover signature dictionary with DocMDP /P 1 blocked signing even when nothing in the document points to it. This PR drops signature dictionaries that no other object references (N G R) before the DocMDP checks run. A dictionary referenced from a signature field /V or from /Perms /DocMDP is kept, so a genuine P=1 certification still blocks.

PDFs with compressed object streams are left unchanged, because a reference inside an object stream is not visible in the raw bytes. In that case the current behavior stays, which is the safe direction. Supporting them would mean inflating the object streams, and I kept that out of this PR.

🧪 How to test

Run the reporter's files from the issue through DocMdpHandler::allowsAdditionalSignatures():

File Before After
01-unsigned-control.pdf allowed allowed
02-unreferenced-docmdp-p1.pdf blocked allowed
03-rewritten-control.pdf allowed allowed
04-referenced-docmdp-p1-negative-control.pdf blocked blocked

Validation evidence

  • Focused regression/acceptance test
  • Relevant lint/type/static-analysis checks
  • Broader suite required by the changed surface
  • CI reviewed

New testOnlyReferencedSignatureDictionariesCertify cases:

  • unreferenced P=1 dictionary is allowed
  • a reference to another object number (15 0 R) does not keep object 5
  • P=1 referenced from /Perms still blocks
  • P=1 referenced from a signature field still blocks
  • P=1 in a file with an object stream still blocks

The unsigned control is covered by testUnsignedPdfIsDetectedAsLevelNone. The rewritten control has no signature object, so it is the same case. The correctly signed negative control is testRealJSignPdfWithDocMdpLevel1, which uses the real JSignPdf P=1 fixture and goes through the new filter.

Commands that ran (PHP 8.3 in Docker):

  • phpunit tests/php/Unit/Handler/DocMdpHandlerTest.php: 46 tests pass. With the old handler, the unreferenced case fails.
  • php-cs-fixer fix --dry-run on the three changed files: no changes.

Not run locally: the full PHPUnit suite and Psalm, since they need a Nextcloud checkout.

⚙️ API / Back-end changes

  • Ignore unreferenced signature dictionaries in DocMDP detection
  • Unit and/or integration tests added or updated
  • Authorization and negative paths checked when applicable
  • Capabilities updated when applicable
  • Documentation updated when applicable
  • API documentation regenerated with composer openapi when necessary

✅ Checklist

  • I have read and followed the contribution guide.
  • The diff does not contain unrelated refactors or generated changes.
  • Commits follow Conventional Commits where applicable.
  • All commits include the required DCO sign-off.

🤖 AI (if applicable)

  • The content of this PR was partially or fully generated using AI

Signed-off-by: mateuscmtropical <mateuscmtropical@gmail.com>

vitormattos commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

This feedback has now been submitted as a formal Request changes review on #9436. Please follow the review discussion for the proposed changes before merging.

@vitormattos vitormattos left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for investigating #9271 and adding the regression tests!

After reviewing the existing architecture, I suggest a different approach before merging. Removing unreferenced signature dictionaries using regex can produce incorrect results, particularly with incremental revisions and compressed objects.

The proposed solution is:

  1. LibreSign/pdf-signature-validator — Add structural detection of active signatures and DocMDP levels (P=1/2/3), using signerphp/pdf-core rather than raw-text matching. It should distinguish orphaned signatures and report inconclusive cases safely, with regression tests.

  2. This PR (#9436) — Replace the regex-based logic in DocMdpHandler with the new validation API. Also update signature detection in PhpNativeHandler and JSignPdfHandler, ensuring orphaned signatures neither block signing nor prevent the configured certification.

Important: PR #9375, by Paulo Raoni, already handles freezing the request's DocMDP policy. That work is complementary, so we should avoid duplicating it and integrate this PR with #9375 once it is merged.

Would you be interested in continuing this work, starting with a PR in LibreSign/pdf-signature-validator and then updating #9436?

If you'd prefer to handle only the validation package, we can coordinate the integration separately.

I'd like to keep this PR open and preserve your existing tests and contribution.

@mateuscmtropical

Copy link
Copy Markdown
Author

Thanks for the review! Yes, I'd like to continue. I'll start with a PR in pdf-signature-validator and then update this one after #9375 is merged.

@mateuscmtropical

Copy link
Copy Markdown
Author

Opened the first step: LibreSign/pdf-signature-validator#85. I'll update this PR to use it after #9375 is merged.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unsigned PDF blocked as DocMDP P=1 because of an unreferenced signature object

2 participants