Skip to content

fix: strip illegal XML chars from rendered output#3

Merged
JackByrne merged 2 commits into
developfrom
illegal-xml-chars
Jul 7, 2026
Merged

fix: strip illegal XML chars from rendered output#3
JackByrne merged 2 commits into
developfrom
illegal-xml-chars

Conversation

@JackByrne

Copy link
Copy Markdown
Member

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.

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.
@JackByrne
JackByrne requested a review from Copilot July 7, 2026 10:27
@JackByrne JackByrne self-assigned this Jul 7, 2026
@JackByrne JackByrne added the bug Something isn't working label Jul 7, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds XML 1.0 illegal-character sanitization to docxtpl’s rendering pipeline so control/noncharacter code points coming from context values don’t corrupt generated DOCX XML parts (including core properties). This improves robustness when templates render user-supplied data that may contain disallowed code points.

Changes:

  • Introduces a module-level regex and helper to strip XML-1.0-illegal characters from rendered XML strings.
  • Applies sanitization to both rendered XML parts (render_xml_part) and rendered core properties (render_properties).
  • Adds a regression test script/template to exercise illegal-character injection across body text, escaped content, header/footer, and core properties.

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

File Description
docxtpl/template.py Adds and applies illegal-XML character stripping during part/property rendering.
tests/illegal_xml_chars.py Adds a regression test script generating a DOCX from a template containing illegal characters in multiple locations.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docxtpl/template.py
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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 3 changed files in this pull request and generated no new comments.

@JackByrne
JackByrne merged commit 3b0083d into develop Jul 7, 2026
1 check passed
@JackByrne
JackByrne deleted the illegal-xml-chars branch July 7, 2026 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants