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

BUG: Encrypt / decrypt Stream object dictionaries #2228

Merged
merged 4 commits into from
Oct 7, 2023

Conversation

pilotandy
Copy link
Contributor

@pilotandy pilotandy commented Sep 29, 2023

Fixes #2227

@exiledkingcc
Copy link
Contributor

exiledkingcc commented Sep 30, 2023

this pr can fix #2227 with no doubt.

@pubpub-zz
Copy link
Collaborator

@stefan6419846
thanks for your analysis

@pilotandy
here is a way to get your test working without calling the to induce the error:

def test_encrypt_stream_dictionary(pdf_file_path):
    user_password = secrets.token_urlsafe(10)

    reader = PdfReader(RESOURCE_ROOT / "cmyk_image.pdf")
    page = reader.pages[0]
    original_image_obj = page["/Resources"]["/XObject"]["/I"]

    writer = PdfWriter()
    writer.add_page(reader.pages[0])
    writer.encrypt(
       user_pass
word=user_password,
       owner_password=None,
       algorithm="RC4-128",
    )
    with open(pdf_file_path, "wb") as output_stream:
        writer.write(output_stream)

    reader = PdfReader(pdf_file_path)
    assert reader.is_encrypted
    assert reader.decrypt(user_password) == PasswordType.OWNER_PASSWORD
    page = reader.pages[0]
    decrypted_image_obj = page["/Resources"]["/XObject"]["/I"]
    assert decrypted_image_obj["/ColorSpace"][3] == original_image_obj["/ColorSpace"][3]

pubpub-zz added a commit to pubpub-zz/pypdf that referenced this pull request Oct 1, 2023
@codecov
Copy link

codecov bot commented Oct 1, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (ba9491c) 94.36% compared to head (6819b7b) 94.40%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2228      +/-   ##
==========================================
+ Coverage   94.36%   94.40%   +0.04%     
==========================================
  Files          43       43              
  Lines        7591     7595       +4     
  Branches     1497     1499       +2     
==========================================
+ Hits         7163     7170       +7     
+ Misses        264      262       -2     
+ Partials      164      163       -1     
Files Coverage Δ
pypdf/_encryption.py 96.65% <100.00%> (+0.81%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@MartinThoma
Copy link
Member

(I've moved/removed a couple of comments from this discussion, as it was not primarily about the PR)

@MartinThoma MartinThoma changed the title Encrypt / decrypt Stream object dictionaries BUG: Encrypt / decrypt Stream object dictionaries Oct 1, 2023
@MartinThoma
Copy link
Member

@pilotandy May I move the PDF to https://github.com/py-pdf/sample-files (docs)?

I typically want files to be in the sample-files for two reasons:

  1. pypdf repository size: I want to make sure that it is super easy to get pypdf. That includes that the git repository file size should not get too big.
  2. sample-files can be used for other projects easily. I hope that the sample-files repository might get used by other PDF libraries and that we create a good re-usable set of test cases

@pilotandy
Copy link
Contributor Author

@MartinThoma Here's the PR py-pdf/sample-files#25 for the sample files

@MartinThoma MartinThoma added the soon PRs that are almost ready to be merged, issues that get solved pretty soon label Oct 7, 2023
@MartinThoma MartinThoma merged commit 8e22f01 into py-pdf:main Oct 7, 2023
12 checks passed
@MartinThoma
Copy link
Member

Thank you @pilotandy !

If you want,I can add you to https://pypdf.readthedocs.io/en/latest/meta/CONTRIBUTORS.html :-)

@MartinThoma MartinThoma removed the soon PRs that are almost ready to be merged, issues that get solved pretty soon label Oct 7, 2023
@pilotandy
Copy link
Contributor Author

pilotandy commented Oct 7, 2023 via email

MartinThoma added a commit that referenced this pull request Oct 7, 2023
MartinThoma added a commit that referenced this pull request Oct 8, 2023
## What's new

### Bug Fixes (BUG)
-  invalid cm/tm in visitor functions (#2206) by @pubpub-zz
-  Encrypt / decrypt Stream object dictionaries (#2228) by @pilotandy
-  Support nested color spaces for the /DeviceN color space (#2241) by @Stefan
-  images property fails if NullObject in list (#2215) by @pubpub-zz

### Robustness (ROB)
-  XYZ destination to cope with missing left and top param (#2237) by @pubpub-zz

### Documentation (DOC)
-  Add pilotandy for #2228 as a contributor by @MartinThoma
-  Link to CONTRIBUTING.md in README.md (#2232) by @markpfeifle
-  Changelog by @MartinThoma

### Developer Experience (DEV)
-  Exclude tests from mypy checks by @MartinThoma
-  Unify mypy options and warn redundant workarounds (#2223) by @exiledkingcc
-  Stabilize Pillow test with Pillow missing (#2229) by @Stefan

### Maintenance (MAINT)
-  Update pinned packages (#2243) by @MartinThoma

### Testing (TST)
-  Regression test against non-deterministic accidental object reuse (#2244) by @MartinThoma

[Full Changelog](3.16.2...3.16.3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants