Skip to content

fix: encrypted PDFs with EncryptMetadata false fail with invalid password - #7

Open
mdz wants to merge 1 commit into
mattermost:masterfrom
mdz:claude/pdf-issue-82-597cd4
Open

mdz wants to merge 1 commit into
mattermost:masterfrom
mdz:claude/pdf-issue-82-597cd4

Conversation

@mdz

@mdz mdz commented Aug 18, 2026

Copy link
Copy Markdown

Summary

initEncrypt never read the /EncryptMetadata entry, so PDFs encrypted with the standard security handler (V=4, R=4, AES-128) that set /EncryptMetadata false failed with encrypted PDF: invalid password, even when the actual password is empty.

Per PDF 32000-1:2008 §7.6.3.3, Algorithm 2, step (g): security handlers of revision 4 or greater must pass 4 bytes with the value 0xFFFFFFFF to the MD5 hash after the document ID, if document metadata is not being encrypted. This step was missing entirely, so the derived key never matched the file's stored U value.

This encryption combination (AES-128 content encryption with cleartext metadata for indexing) is common in e-signature/contract-export tools (DocuSign, Adobe Sign, PandaDoc, etc.), so any signed PDF exported that way failed to open.

Originally reported at ledongthuc/pdf#82; filing here since this fork is the actively maintained one.

Changes

  • read.go: implement step (g) of Algorithm 2 — append 0xFFFFFFFF to the MD5 hash when R >= 4 and EncryptMetadata is explicitly false.
  • read_test.go / testdata/encrypted_metadata_false.pdf: regression test using a minimal repro PDF (empty user/owner password, V=4, R=4, AES-128, EncryptMetadata false).

Test plan

  • go build ./...
  • go vet ./...
  • go test ./... — all pass, including new TestOpenEncryptedMetadataFalse

Implement step (g) of PDF 32000-1:2008 Algorithm 2: when R>=4 and
/EncryptMetadata is false, append 0xFFFFFFFF to the MD5 hash before
deriving the encryption key. Without this, AES-128 PDFs with cleartext
metadata (common from e-signature tools) fail with "invalid password"
even when the actual password is empty.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Aug 18, 2026

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f763fdbd-a557-41d4-b7d8-a0ab3f60101f


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

1 participant