Skip to content

fix: strip illegal XML chars from rendered output#653

Merged
JackByrne merged 3 commits into
elapouya:devfrom
start-software:develop
Jul 7, 2026
Merged

fix: strip illegal XML chars from rendered output#653
JackByrne merged 3 commits into
elapouya:devfrom
start-software:develop

Conversation

@JackByrne

Copy link
Copy Markdown
Collaborator

This pull request improves the robustness of XML output by ensuring that any XML-1.0-illegal characters injected through context values are stripped from rendered output. This prevents document corruption due to invalid XML characters and adds a test to verify this behavior.

XML character sanitization:

  • Added a regular expression _ILLEGAL_XML_CHARS_RE and a helper function _strip_illegal_xml_chars to remove illegal XML-1.0 characters from rendered output, mirroring the approach used in other libraries like openpyxl.
  • Applied _strip_illegal_xml_chars in render_xml_part and render_properties to sanitize all rendered XML strings and core properties before they are parsed or written, preventing any single bad character from corrupting the output.

Testing:

  • Added a new test script tests/illegal_xml_chars.py to verify that XML-1.0-illegal characters injected through context values are correctly stripped from all relevant parts of the document.

JackByrne added 3 commits July 7, 2026 11:25
Add XML 1.0 illegal-character sanitization to template rendering and core property rendering so control/noncharacter code points injected via context values no longer break DOCX XML parsing. The change uses a regex-based fast path and preserves valid XML whitespace controls. Also adds a regression test template/script covering body text, escaped content, header/footer, and core properties.
Update the illegal XML character regex to better match XML 1.0 constraints by covering the full DEL/C1 control range (`\x7f-\x9f`) and excluding UTF-16 surrogate code points (`\ud800-\udfff`). This prevents invalid characters from leaking into generated document XML.
fix: strip illegal XML chars from rendered output
@JackByrne JackByrne self-assigned this Jul 7, 2026
@JackByrne JackByrne added the bug label Jul 7, 2026
@JackByrne JackByrne merged commit 8f1f40d into elapouya:dev Jul 7, 2026
0 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant