fix: keep unusable DOCX numeric references as literal text - #123
Open
mldangelo wants to merge 2 commits into
Open
fix: keep unusable DOCX numeric references as literal text#123mldangelo wants to merge 2 commits into
mldangelo wants to merge 2 commits into
Conversation
decodeXml passed the parsed value of a numeric character reference straight to String.fromCodePoint with no bound, so any reference above U+10FFFF raised RangeError. That propagated out of extractDocx as "Cannot extract text from knowledge base DOCX", which reads like a corrupt file, and failed the whole prepareKnowledgeBase call -- one stray reference in one document aborted the scan along with every other knowledge-base file that was fine. Leave a reference that cannot name a Unicode scalar value as literal text, matching the unrecognized-named-entity fallback directly above it. Surrogates are excluded on the same grounds: XML forbids them, and writing one would silently encode as U+FFFD rather than throwing. Fixes openai#40
mldangelo
force-pushed
the
fix/docx-numeric-entity
branch
from
July 30, 2026 13:43
aea2f3b to
e5beb12
Compare
Collaborator
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Security review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
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.
Fixes #40.
Summary
decodeXmlinsrc/knowledge-base.tspassed the parsed value of a numeric character reference straight toString.fromCodePointwith no magnitude bound. The regex admits#\d+and#x[\da-f]+with no upper limit, so�and�both reached it and threwRangeError.That propagated out of
extractDocxasCannot extract text from knowledge base DOCX: <path>— which reads like a corrupt or unreadable file and sends you looking in the wrong place, with the realRangeErrorvisible only via the error'scause— and failed the entireprepareKnowledgeBasecall. One stray reference in one document aborted the scan before Codex started, taking every other knowledge-base file with it.What this changes
A reference that cannot name a Unicode scalar value is left as literal text, exactly matching the
entities[name.toLowerCase()] ?? entityfallback for unrecognized named entities on the line above. This is the direction suggested in the issue.I also excluded surrogates (
U+D800–U+DFFF), which the issue's suggested snippet does not cover. They do not throw —String.fromCodePoint(0xD800)happily returns a lone surrogate — so this is a separate, quieter bug: XML forbids surrogates in theCharproduction, and the lone surrogate would then be written out bywriteFile(..., "utf8")asU+FFFD, silently corrupting the extracted text rather than failing. Same root cause, same one-line guard, so it seemed wrong to leave it.Number.isIntegercovers theNaNcase; huge decimals like�parse to1e20, which is an integer by float semantics but is caught by the upper bound.Deliberately out of scope
The broader policy question. The issue's Impact section objects that "a single stray entity anywhere in one document blocks the entire scan." With the decode fixed, that no longer happens for this cause — the document extracts normally. But
prepareKnowledgeBasestill aborts wholesale for a genuinely unextractable document, and I did not change that, because failing loudly may well be the right behavior for a security knowledge base: silently proceeding with a document missing is arguably worse than refusing to start. If you want skip-and-warn semantics instead, that is a deliberate product change and I am happy to do it separately.The full XML
Charproduction.�and other C0 controls still decode, so a NUL can reach the extracted text. XML forbids those too (Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]), and implementing the production in full would be a few more lines. I left it out because it is neither a crash nor a silent corruption, and it is not what the issue reports. Flagging it as a possible follow-up.Testing / QA instructions
Baseline before this branch: 470 pass / 6 skip / 0 fail. After: 472 pass / 6 skip / 0 fail (two added tests).
Confirm the tests reproduce the issue
Both added tests must fail against
main's source. Revert only the source, keeping the tests (plaingit stashwill not work — the tests are in a different file, but reverting both defeats the check):New coverage
Both tests build a real
.docxthrough the existingdocx()helper (fflatezipSyncofword/document.xml) and assert on the extracted text, so they exercise the wholeprepareKnowledgeBase→extractDocx→decodeXmlpath rather than the decoder in isolation.keeps DOCX numeric references that cannot name a code point as literal text— seven cases, covering both the regression and the no-regression side:����(lone surrogate)AA😀The last three matter as much as the first four — the bound must not clip valid astral characters or the max code point.
keeps one unusable reference from failing the other knowledge-base documents— a directory holding one good.mdand one.docxwith an out-of-range reference. Asserts both documents are extracted. This is the user-visible impact from the issue: onmainthe.mdis lost too.Manual reproduction from the issue
Expect
prepared: ...on this branch; onmainit throwsCannot extract text from knowledge base DOCX.Note for maintainers
PRs #117 and #95 are competing rewrites of this same file (both add
MAX_DOCUMENTS/MAX_DIRECTORY_DEPTH/size caps). Neither fixes this — I checked, they only rename thedecodeXml(...)call site toconst text = decodeXml(...). This PR touches onlydecodeXmland one new constant, so it should rebase cleanly onto whichever of those you land, but merge order matters.