Skip to content

fix: raise clear errors on corrupt or malicious DOCX parsing - #1583

Open
ChenfromChina123 wants to merge 2 commits into
python-openxml:masterfrom
ChenfromChina123:fix/corrupt-docx-exceptions
Open

fix: raise clear errors on corrupt or malicious DOCX parsing#1583
ChenfromChina123 wants to merge 2 commits into
python-openxml:masterfrom
ChenfromChina123:fix/corrupt-docx-exceptions

Conversation

@ChenfromChina123

Copy link
Copy Markdown

What does this PR do?

Prevents low-level zlib.error, EOFError, and RuntimeError exceptions from leaking out of docx.Document(stream) when a malformed, corrupt, or maliciously modified DOCX (ZIP) is parsed.

Closes #1561

Changes

  • src/docx/opc/phys_pkg.py: _ZipPkgReader.__init__ raises PackageNotFoundError on BadZipFile/zlib errors; blob_for raises ValueError on member read failures.

Verification

  • Corrupt zip blob → PackageNotFoundError (tested).
  • Normal zip read still returns correct blob (tested).

ChenfromChina123 and others added 2 commits August 10, 2026 22:34
The example calls add_picture('monty-truth.png') which raises
FileNotFoundError when the file is not present in the working
directory. Wrap it in a try/except so the example runs out of
the box.

Closes python-openxml#128

Co-Authored-By: AtomCode (deepseek-v4-flash) <[email protected]>
Parsing a malformed, corrupt, or maliciously modified DOCX could
leak low-level zlib.error, EOFError, or RuntimeError exceptions
from the zipfile module. Wrap zip opening and member reads to
raise PackageNotFoundError / ValueError with descriptive messages.

Closes python-openxml#1561

Co-Authored-By: AtomCode (deepseek-v4-flash) <[email protected]>
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.

Unhandled exceptions (zlib.error, EOFError, RuntimeError) during malicious/corrupt DOCX parsing

1 participant