Skip to content

fix: ensure set_metadata({}) and scrub() clear metadata on xref-stream PDFs - #5236

Merged
JorjMcKie merged 2 commits into
pymupdf:mainfrom
charloteberlein:fix-xref-stream-metadata-scrubbing
Nov 1, 2026
Merged

JorjMcKie merged 2 commits into
pymupdf:mainfrom
charloteberlein:fix-xref-stream-metadata-scrubbing

Conversation

@charloteberlein

Copy link
Copy Markdown
Contributor

Summary of the issue

When calling set_metadata({}) (or scrub()) on xref-stream PDFs, xref_set_key() does not correctly apply the update to the trailer, and so the metadata is not cleared. Fetching the trailer again before copying the updated dictionary prevents this.

This is a one-line addition in src/__init__.py:

obj = mupdf.pdf_trailer(pdf)

Regression tests and how to reproduce the error

The regression tests I’ve written minimally reproduce the error. One checks set_metadata({}) directly, and the other checks it through scrub() (which calls set_metadata({})).

Running the new tests with python -m pytest -k xref_stream ./tests:

  • With the change reverted, they fail:
====================================== short test summary info ======================================
FAILED tests/test_metadata.py::test_clear_metadata_xref_stream - AssertionError: assert not 'Title'
FAILED tests/test_metadata.py::test_scrub_xref_stream - AssertionError: assert not 'Title'
================================= 2 failed, 524 deselected in 0.47s =================================
  • With the change applied, they both pass:
tests/test_metadata.py ..                                                                     [100%]

================================= 2 passed, 524 deselected in 0.47s =================================

@github-actions

github-actions Bot commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@charloteberlein

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

@JorjMcKie

Copy link
Copy Markdown
Collaborator

Thanks for spotting this - well done "first contribution" 😎!

@JorjMcKie
JorjMcKie self-requested a review November 1, 2026 10:28
github-actions Bot added a commit that referenced this pull request Nov 1, 2026
@JorjMcKie
JorjMcKie merged commit 773a0d9 into pymupdf:main Nov 1, 2026
3 of 4 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants