Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Richardmillson fix 608 decryption fails when no id in trailer #812

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion PyPDF2/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2205,7 +2205,13 @@ def _authenticateUserPassword(self, password):
rev = encrypt['/R'].getObject()
owner_entry = encrypt['/O'].getObject()
p_entry = encrypt['/P'].getObject()
id_entry = self.trailer[TK.ID].getObject()
if TK.ID in self.trailer:
id_entry = self.trailer[TK.ID].getObject()
else:
# Some documents may not have a /ID, use two empty
# byte strings instead. Solves
# https://github.com/mstamy2/PyPDF2/issues/608
id_entry = ArrayObject([ByteStringObject(b''), ByteStringObject(b'')])
id1_entry = id_entry[0].getObject()
real_U = encrypt['/U'].getObject().original_bytes
if rev == 2:
Expand Down
Binary file added Resources/encrypted_doc_no_id.pdf
Binary file not shown.
15 changes: 15 additions & 0 deletions Tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,18 @@ def test_do_not_get_stuck_on_large_files_without_start_xref():
# parsing is expected take less than a second on a modern cpu, but include a large
# tolerance to account for busy or slow systems
assert parse_duration < 60


def test_PdfReaderDecryptWhenNoID():
"""
Decrypt an encrypted file that's missing the 'ID' value in its
trailer.
https://github.com/mstamy2/PyPDF2/issues/608
"""

with open(
os.path.join(RESOURCE_ROOT, "encrypted_doc_no_id.pdf"), "rb"
) as inputfile:
ipdf = PdfFileReader(inputfile)
ipdf.decrypt("")
assert ipdf.getDocumentInfo() == {"/Producer": "European Patent Office"}