Repository navigation
Description
Summary
encodeText escapes only & and <, so literal ]]> and characters outside the XML 1.0 Char range are written raw into word/document.xml, making the saved .docx ill-formed XML.
Reproduction
// npm install @office-kit/docx@0.2.1
import { createDocx, toUint8Array } from "@office-kit/docx";
import { writeFileSync } from "node:fs";
writeFileSync("cdata.docx", toUint8Array(createDocx({ paragraphs: ["if (a[b[0]]> 1) return"] })));
writeFileSync("ctrl.docx", toUint8Array(createDocx({ paragraphs: ["copied\u000Bfrom a PDF"] })));Run node repro.mjs, then parse word/document.xml from each archive with standard XML parsers:
python3 -c '
import zipfile, xml.etree.ElementTree as ET, lxml.etree as LE
for f in ("cdata.docx", "ctrl.docx"):
xml = zipfile.ZipFile(f).read("word/document.xml")
for name, parse in (("ElementTree", ET.fromstring), ("lxml", LE.fromstring)):
try:
parse(xml)
except Exception as exc:
print(f"{f} ({name}): {type(exc).__name__}: {exc}")
'Expected behavior
A well-formed XML 1.0 word/document.xml (https://www.w3.org/TR/xml/#charsets): ]]> in character data must have > escaped as >, and code points outside Char (U+0000-U+0008, U+000B, U+000C, U+000E-U+001F, U+FFFE, U+FFFF) cannot appear in XML 1.0 at all and need to be rejected or stripped at the boundary.
Actual behavior
Both files contain ill-formed word/document.xml (<w:t>if (a[b[0]]> 1) return</w:t> and <w:t>copied\x0bfrom a PDF</w:t>) and fail to parse in standard XML readers (including ElementTree, lxml, and python-docx):
cdata.docx (ElementTree): ParseError: not well-formed (invalid token): line 2, column 481
cdata.docx (lxml): XMLSyntaxError: Sequence ']]>' not allowed in content, line 2, column 480 (<string>, line 2)
ctrl.docx (ElementTree): ParseError: not well-formed (invalid token): line 2, column 476
ctrl.docx (lxml): XMLSyntaxError: PCDATA invalid Char value 11, line 2, column 477 (<string>, line 2)
U+0000, U+0001, U+001F, U+FFFE, and U+FFFF fail the same way (Char 0x0 out of allowed range, PCDATA invalid Char value 1, PCDATA invalid Char value 31, Char 0xFFFE out of allowed range, Char 0xFFFF out of allowed range).
Environment
- Package version:
@office-kit/docx0.2.1 (also reproduced onmainatfaafd7a) - Runtime: Node v26.13.0 on Debian GNU/Linux 12 (bookworm) x86_64
- Other relevant details:
openDocxandvalidateaccept both files without errors because the internal XML parser does not check for]]>or forbiddenCharvalues.
Additional context
encodeText in src/internal/xml/entities.ts (input.replace(/[&<]/g, ...)) only escapes & and < (encodeAttrValue below it escapes >, though neither rejects XML-forbidden code points). For comparison, @office-kit/pptx 0.28.0 escapes > and throws XML-illegal control character U+000B in text via rejectForbiddenChar in src/internal/xml/serialize.ts.
Confirmation
- I have searched existing issues and confirmed this is not a duplicate.
- I am on the latest released version, or I have explained above why an older version is relevant.
- I have included a minimal reproduction that I have actually run myself.
- If I used an LLM to draft this issue, I have read and verified every claim and am willing to defend them in follow-up.
Credit: https://ucsbplse.github.io/
Activity