Repository navigation
Conversation
… edits land _save_pdf re-encrypted an encrypted input with pikepdf.Encryption(owner=pw, user=pw, R=info.R, allow=pdf.allow). Encryption defaults to aes=True and metadata=True, and pikepdf refuses both below R 4, so every edit of an RC4 revision 2 or 3 input (an owner password with an empty user password is the common case) failed with ValueError: Cannot encrypt metadata when R < 4. The encryption_dropped fallback catches only pikepdf.PdfError, so the edit did not land at all. An R 4 input using RC4 or /EncryptMetadata false was re-encrypted as AES with metadata encrypted. Rebuilding the key from the caller's password also collapsed owner and user to that one password, and pikepdf writes an R 2-4 str password as PDFDocEncoding but opens with its UTF-8 bytes, so a UTF-8-keyed non-ASCII password could lock the caller out of the output with success=True. - _save_pdf saves an encrypted input with encryption=True, so qpdf copies R, V, key length, /O, /U, /P, /EncryptMetadata, the first /ID and the file key. The output opens with exactly the passwords that opened the input, each with its own rights. The one scheme change is qpdf's: a V 4 file whose crypt filters use RC4 is written with AESV2 under the same key. The now unused reencrypt_password parameter is removed. - The fallback stays a PdfError from the injected encrypted save only, logged with exc_info. A ValueError (overwriting the input, normalize_content) propagates, and a caller-pinned encryption never arms the fallback. - CHANGELOG, LIMITATIONS and a decisions row describe the result, including that reorder_pages, split_pdf and merge_pdfs with an unencrypted first file write an unencrypted new document. INV-W-5 now checks, for each candidate password, whether it opens the output and with which rights, against the input: RC4 R 2 and R 3 inputs with owner-only and owner+user passwords, owner-password edits (R2, R3, R4, R6), UTF-8- and PDFDoc-keyed non-ASCII R 2-4 passwords as str and bytes, hand-built 40/64-bit R 3 and R 2 /P -3904 RC4 inputs, R 4 RC4 written as AES under the same key, /EncryptMetadata false, ValueError propagation, the fallback's traceback, and an import of the package without pikepdf.models.EncryptionMethod (pikepdf 10.0-10.8).
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
Author
|
When CI runs here, the Typecheck step will fail, and that failure is not from this change: |
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.
What breaks
Every edit of an RC4-encrypted PDF (revision 2 or 3, for example an owner
password with an empty user password) fails instead of landing:
Why
_pathutil._save_pdf, the save helper every edit verb uses (A2.3, INV-W-5),re-encrypts an encrypted input with
pikepdf.Encryption(owner=pw, user=pw, R=info.R, allow=pdf.allow).pikepdf.Encryptiondefaults toaes=Trueandmetadata=True, and pikepdfrefuses both below R 4. The
encryption_droppedfallback catches onlypikepdf.PdfError, so theValueErrorescapes and the edit fails. Seen onpikepdf 10.5.1 and 10.17.0.
Rebuilding the encryption from the caller's password has quieter problems too:
an R 4 input using RC4 or
/EncryptMetadata falsecomes back as AES withmetadata encrypted, owner and user collapse to the one password the edit used,
and pikepdf writes an R 2-4
strpassword as PDFDocEncoding but opens with itsUTF-8 bytes, so a non-ASCII password can lock the caller out of the output
while the edit reports
success=True.Fix
_save_pdfsaves an encrypted input withencryption=True, so qpdf copies theinput's own encryption (
R,V, key length,/O,/U,/P,/EncryptMetadata, the first/IDand the file key). The output opens withexactly the passwords that opened the input, each with its own owner or user
rights. The one scheme change is qpdf's: a V 4 file whose crypt filters use RC4
is written with AESV2 under the same key. The now unused
reencrypt_passwordparameter is removed; the verbs still take
passwordto open the input. Thefallback is unchanged: only a
pikepdf.PdfErrorfrom the injected encryptedsave retries unencrypted and records
encryption_dropped. CHANGELOG,LIMITATIONS and
docs/decisions.mdare updated.Testing
tests/invariants/test_w_5_encryption_round_trip.pynow checks, for eachcandidate password, whether it opens the output and with which rights, against
the input: RC4 R 2 and R 3 with owner-only and owner+user passwords,
owner-password edits (R 2, 3, 4, 6), UTF-8- and PDFDoc-keyed non-ASCII
passwords as
strandbytes, 40/64-bit R 3 keys, R 4 RC4,/EncryptMetadata false,ValueErrorpropagation, the fallback, and thatencrypt_pdfnever saves a plaintext copy when its own encrypted save fails.36 of its 48 tests fail on main and all 48 pass with the fix.
ruff check src/ tests/: all checks passed.python -m pyteston macOS: 871 passed, 357 skipped, 27 failed. The same 27fail on main (831 passed there): 24
TTLibFileIsCollectionErrorfrom macOS.ttcsystem fonts, two from thetests/corpus/cidfont_synthetic.pdffixture missing in a fresh checkout, and
TestCIDFontReplace::test_winAnsi_regression_guard. None of them touchencryption.
python -m mypy: 21unused "type: ignore"errors with pikepdf 10.17.0,the same 21 as on main, none in
_pathutil.py.Reproduction
On main this raises
ValueError: Cannot encrypt metadata when R < 4; with thefix
out.pdfis R 3 RC4 under the input's key.