Skip to content

Fix PDF unlock aborting on streams the producer left unencrypted - #5

Merged
t11z merged 1 commit into
mainfrom
claude/loving-rubin-v3a5ho
Oct 7, 2026
Merged

t11z merged 1 commit into
mainfrom
claude/loving-rubin-v3a5ho

Conversation

@t11z

@t11z t11z commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

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 ].

  • qpdf "decrypts" these plaintext bytes and gets 16 bytes of noise.
  • Because of the array form, qpdf does not recognise the stream as already compressed. When writing, it tries to inflate the stream so it can compress it again.
  • That inflate fails (stream inflate: inflate: data: incorrect header check), and qpdf aborts the whole write with exit code 2. --check fails the same way.

Diagnosis on the original file:

qpdf options Result
default, --decode-level=none, --compress-streams=n, --object-streams=disable, --qdf exit 2, truncated output
--stream-data=preserve exit 0, page renders pixel-identical to the password-opened original

Fix

web/src/core/pdf/qpdf.ts: both qpdf write paths (decryptPdf and removeOpenPasswordKeepRestrictions) 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

  • New fixture unencrypted-stream-aes256.pdf, generated by scripts/gen-fixtures.py (save_unencrypted_stream_pdf): AES-256 with restrictions, plus one /CIDSet stream stored in plaintext with /Filter [ /FlateDecode ]. It reproduces the exact qpdf error.
  • New test in pdf.test.ts covering 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/prettier pass. 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

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
@t11z t11z self-assigned this Oct 7, 2026
@t11z t11z added the bug Something isn't working label Oct 7, 2026
@t11z
t11z merged commit 5426514 into main Oct 7, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants