Repository navigation
Fix PDF unlock aborting on streams the producer left unencrypted - #5
Merged
Merged
Conversation
Some producers write AES-encrypted PDFs in which a few small streams (seen with font /CIDSet streams) are stored unencrypted and declare their filter as a one-element array, /Filter [ /FlateDecode ]. qpdf does not recognise such a stream as already compressed, so when writing it decrypts the stream, tries to inflate it to compress it again, and aborts the whole write on the resulting "incorrect header check". The user saw "file is corrupt or could not be read" although the password was right. qpdf now runs with --stream-data=preserve for both decryption and the re-encryption that keeps restrictions: stream data is only decrypted and otherwise written as it is. The affected streams end up exactly as a conforming reader sees them in the original; page rendering is unchanged. Adds a fixture that reproduces the failure, with a regression test covering the decrypt and the keep-restrictions path. Docs-Impact: none – supported protections and results are unchanged; the fix only makes qpdf stop re-compressing stream data, which no document describes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DBSwp8dMkpVxCSR4ArHAU5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A real-world PDF (AES-256, R5) still failed in v0.1.1 with "Die Datei ist beschädigt oder konnte nicht gelesen werden.", even with the correct password. That password is both the user and the owner password, so the file takes the plain decrypt path. The accessibility fix from #4 was correct for RC4 files but did not cover this one.
Root cause
The producer left five small font streams (
/CIDSet, 23–28 bytes) unencrypted in an otherwise AES-encrypted file, and declared their filter as a one-element array,/Filter [ /FlateDecode ].stream inflate: inflate: data: incorrect header check), and qpdf aborts the whole write with exit code 2.--checkfails the same way.Diagnosis on the original file:
--decode-level=none,--compress-streams=n,--object-streams=disable,--qdf--stream-data=preserveFix
web/src/core/pdf/qpdf.ts: both qpdf write paths (decryptPdfandremoveOpenPasswordKeepRestrictions) now run with--stream-data=preserve. Stream data is only decrypted and otherwise written as it is: no re-compression, no attempt to decode it.The affected streams end up exactly as a conforming reader sees them in the original, since such a reader decrypts every stream too. For all other files the result stays equivalent. The difference is only that qpdf no longer re-encodes streams, which is closer to the original anyway.
Tests
unencrypted-stream-aes256.pdf, generated byscripts/gen-fixtures.py(save_unencrypted_stream_pdf): AES-256 with restrictions, plus one/CIDSetstream stored in plaintext with/Filter [ /FlateDecode ]. It reproduces the exact qpdf error.pdf.test.tscovering both paths: open password (restrictions kept) and owner password (full decryption). It fails without the fix and passes with it.vitest(70 passed),tsc --noEmit,eslint/prettierpass. The original file now unlocks and renders identically. E2E could not run locally because the environment has a Playwright/Chromium version mismatch; CI covers it.Documentation
Docs-Impact: none(commit trailer). Supported protections and results are unchanged; no document describes stream re-compression. The fixtures README lists the new fixture. No legal texts changed.🤖 Generated with Claude Code
https://claude.ai/code/session_01DBSwp8dMkpVxCSR4ArHAU5
Generated by Claude Code