Skip to content

Raise OpcError for a package part with invalid XML instead of a bare lxml error - #1598

Open
eeshsaxena wants to merge 1 commit into
python-openxml:masterfrom
eeshsaxena:fix/invalid-xml-part-opcerror
Open

Raise OpcError for a package part with invalid XML instead of a bare lxml error#1598
eeshsaxena wants to merge 1 commit into
python-openxml:masterfrom
eeshsaxena:fix/invalid-xml-part-opcerror

Conversation

@eeshsaxena

Copy link
Copy Markdown

Opening a .docx whose XML is corrupt surfaces a raw lxml.etree.XMLSyntaxError out of Document() rather than a python-docx error. It happens for the main document part and for the OPC parts:

docx.Document(path_to_docx_with_malformed_document_xml)
# lxml.etree.XMLSyntaxError: Specification mandates value for attribute ...

The XML is parsed in two spots while loading a package: XmlPart.load (document parts) and the OPC parse_xml ([Content_Types].xml and .rels). I wrapped both so a malformed part raises OpcError with the offending partname, which lines up with a non-package file already raising PackageNotFoundError (both are OpcError subclasses, so except OpcError catches either).

Added a unit test for XmlPart.load with a malformed blob; it raises XMLSyntaxError on master and passes with the change, and the opc part/oxml tests still pass. Found it by fuzzing Document() with corrupted docx packages.

… XML

Opening a .docx whose document.xml, [Content_Types].xml or a .rels part is not
well-formed XML let a bare lxml.etree.XMLSyntaxError escape Document()/open.
Wrap the two package-loading parse points (XmlPart.load and the OPC parse_xml)
so a malformed part raises OpcError, matching how a non-package file already
raises PackageNotFoundError.
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.

1 participant