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

MAINT: password param of _security._alg32(...) is only a string, not bytes #1259

Merged
merged 3 commits into from
Aug 20, 2022
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
10 changes: 4 additions & 6 deletions PyPDF2/_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@


def _alg32(
password: Union[str, bytes],
password: str,
rev: Literal[2, 3, 4],
keylen: int,
owner_entry: ByteStringObject,
Expand Down Expand Up @@ -135,13 +135,11 @@ def _alg33(owner_pwd: str, user_pwd: str, rev: Literal[2, 3, 4], keylen: int) ->
return val


def _alg33_1(password: Union[bytes, str], rev: Literal[2, 3, 4], keylen: int) -> bytes:
def _alg33_1(password: str, rev: Literal[2, 3, 4], keylen: int) -> bytes:
"""Steps 1-4 of algorithm 3.3"""
# 1. Pad or truncate the owner password string as described in step 1 of
# algorithm 3.2. If there is no owner password, use the user password
# instead.
if isinstance(password, bytes):
password = password.decode()
password_bytes = b_((password + str_(_encryption_padding))[:32])
# 2. Initialize the MD5 hash function and pass the result of step 1 as
# input to this function.
Expand All @@ -161,7 +159,7 @@ def _alg33_1(password: Union[bytes, str], rev: Literal[2, 3, 4], keylen: int) ->


def _alg34(
password: Union[str, bytes],
password: str,
owner_entry: ByteStringObject,
p_entry: int,
id1_entry: ByteStringObject,
Expand All @@ -186,7 +184,7 @@ def _alg34(


def _alg35(
password: Union[str, bytes],
password: str,
rev: Literal[2, 3, 4],
keylen: int,
owner_entry: ByteStringObject,
Expand Down
1 change: 1 addition & 0 deletions tests/test_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def test_both_password(name, user_passwd, owner_passwd):
("pdffile", "password"),
[
("crazyones-encrypted-256.pdf", "password"),
("crazyones-encrypted-256.pdf", b"password"),
],
)
@pytest.mark.skipif(not HAS_PYCRYPTODOME, reason="No pycryptodome")
Expand Down
1 change: 1 addition & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ def test_issue297(caplog):
("pdffile", "password", "should_fail"),
[
("encrypted-file.pdf", "test", False),
("encrypted-file.pdf", b"test", False),
("encrypted-file.pdf", "qwerty", True),
("encrypted-file.pdf", b"qwerty", True),
],
Expand Down
24 changes: 18 additions & 6 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,18 +386,18 @@ def test_fill_form():


@pytest.mark.parametrize(
"use_128bit",
[True, False],
("use_128bit", "user_pwd", "owner_pwd"),
[(True, "userpwd", "ownerpwd"), (False, "userpwd", "ownerpwd")],
)
def test_encrypt(use_128bit):
def test_encrypt(use_128bit, user_pwd, owner_pwd):
reader = PdfReader(RESOURCE_ROOT / "form.pdf")
writer = PdfWriter()

page = reader.pages[0]
orig_text = page.extract_text()

writer.add_page(page)
writer.encrypt(user_pwd="userpwd", owner_pwd="ownerpwd", use_128bit=use_128bit)
writer.encrypt(user_pwd=user_pwd, owner_pwd=owner_pwd, use_128bit=use_128bit)

# write "output" to PyPDF2-output.pdf
tmp_filename = "dont_commit_encrypted.pdf"
Expand All @@ -409,18 +409,30 @@ def test_encrypt(use_128bit):
data = input_stream.read()
assert b"foo" not in data

# Test the user password:
# Test the user password (str):
reader = PdfReader(tmp_filename, password="userpwd")
new_text = reader.pages[0].extract_text()
assert reader.metadata.get("/Producer") == "PyPDF2"
assert new_text == orig_text

# Test the owner password:
# Test the owner password (str):
reader = PdfReader(tmp_filename, password="ownerpwd")
new_text = reader.pages[0].extract_text()
assert reader.metadata.get("/Producer") == "PyPDF2"
assert new_text == orig_text

# Test the user password (bytes):
reader = PdfReader(tmp_filename, password=b"userpwd")
new_text = reader.pages[0].extract_text()
assert reader.metadata.get("/Producer") == "PyPDF2"
assert new_text == orig_text

# Test the owner password (stbytesr):
reader = PdfReader(tmp_filename, password=b"ownerpwd")
new_text = reader.pages[0].extract_text()
assert reader.metadata.get("/Producer") == "PyPDF2"
assert new_text == orig_text

# Cleanup
os.remove(tmp_filename)

Expand Down