fix: strip illegal XML chars from rendered output#3
Merged
Conversation
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.
There was a problem hiding this comment.
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.