Skip to content

[AI-4157] Refuse Word and OpenDocument XML nested too deep - #2706

Open
kanat wants to merge 7 commits into
mainfrom
kanat/ai-4157-docx-odt-depth
Open

kanat wants to merge 7 commits into
mainfrom
kanat/ai-4157-docx-odt-depth

Conversation

@kanat

@kanat kanat commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

A small Word or OpenDocument file with deeply nested XML made the router allocate far more memory than the file is long. The workbook reader already refused such files. The Word and OpenDocument readers now refuse a part nested deeper than the same limit, as too large.

Why

Ticket: AI-4157. The attribute-count gap (AI-4161) and wide table rows (AI-4323) are separate tickets and are not changed here.

How it works

upload ─> files.Extract ─> readDOCX / readODT
                            └─ every XML part
                               bytes ─> raw tokens ─> depthTokens ─> xml.Decoder ─> text
                                                      └─ nesting.enter()
                                                         depth > maxXMLDepth ─> TooLarge

readXLSX ─> limitedTokens ─> nesting.enter()   (same counter, same limit, same error)
Change Result Test
nesting holds the depth rule that limitedTokens had inline; limitedTokens now uses it One rule and one error for all three formats. Each format keeps its own wording: "a spreadsheet program" for a workbook, "a word processor" for the others the existing workbook depth tests
depthTokens and newDocumentDecoder wrap the six DOCX/ODT decoders A part nested past 256 levels stops at the 257th TestAWordOrOpenDocumentPartNestedPastTheLimitIsRefusedBeforeItIsDecoded
xmlFailure (also used by unreadable) The refusal is TooLarge; any other XML error stays Corrupt same test, each part
depthTokens keeps its first error; PageLimit marks a refusal for pages A reader that goes on after the limit is refused again. A file sent with a question and refused for nesting is no longer told it has too many pages TestAPartStaysRefusedOnceItIsNestedPastTheLimit, TestAFileRefusedForItsNestingIsNotToldItHasTooManyPages
A broken part that is not nested too deep Stays Corrupt TestABrokenWordOrOpenDocumentPartIsRefusedAsCorrupt
Control at the cap A part nested exactly 256 deep still reads its text, and any number of sibling elements still read TestAWordOrOpenDocumentElementMayBeNestedAsDeepAsTheLimit

The refused read allocates under six times the length of the nested part. Reading every level allocates more than fourteen times it.

Mutations and commands

Each mutant was applied alone, then go test ./internal/knowledge/extract/ -run TestExtractSuite was run, then the file was restored.

Mutant Result
each of the four DOCX decoders and the ODT list-style decoder read with a plain xml.NewDecoder killed by the part test (the body decoder also by the cap control)
ODT body decoder read with a plain decoder killed by TestTheOpenDocumentBodyReaderRefusesABodyNestedPastTheLimit, which reads the body reader alone (in a whole read the list-style decoder refuses first)
each xmlFailure call site, and unreadable, answering Corrupt killed
xmlFailure never answering TooLarge killed
depth > max as >=, and as > max+1 killed by both cap controls
depthTokens never entering, never leaving killed (never leaving breaks every document with sibling elements)
workbook: nesting never entered, never left, or its error overwritten by limit() killed

Commands, in acceleration/: go test ./internal/knowledge/..., go vet ./internal/knowledge/....

🤖 Generated with Claude Code

… limit

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Text review: 4 unclear of 8

Three code comments are hard to read cold (T1.1), and the PR opening names encoding/xml and #2600 (T1.2, nit): suggested opening: "A small Word or OpenDocument file with deeply nested XML made the router allocate far more memory than the file is long. The workbook reader already refused such files. The Word and OpenDocument readers now refuse a part nested deeper than the same limit, as too large."

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

[security R1] VERDICT: GO

I checked every XML decoder in the Word and OpenDocument readers. All six now go through the depth-capped reader, and the workbook reader keeps its own cap. I tried prefixed and default namespaces, self-closing elements, comments and CDATA between levels, end tags that were never opened, entities declared in a DTD, and a part stored twice in the archive. Each one is refused or read within the limit. The refusal text is fixed wording plus the part name, so it shows nothing about the server.

Two small things, both optional:

id where what
S1.1 Nit odt.go:114 No test fails if the cap is taken off the OpenDocument body reader alone
S1.2 Nit xmldepth.go:40 The depth reader forgets its error after it returns it

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

VERDICT: NO-GO

Reviewed 5dae1dc on top of main 6dff757. The six Word and OpenDocument decoders all go through the depth limit, and the new tests hold up: I ran 24 mutants and 22 were killed. One gap blocks the merge (R1.1): nothing checks that a broken part that is not nested too deep is still refused as corrupt.

[Nit] R1.2 (lines outside the diff). Two more comments are no longer true. In xlsx.go, the maxXMLDepth comment (line 55) says the limit is for "an element of a workbook part", and the overLimit comment (line 389) says "a workbook past one of the limits". Both now also apply to Word and OpenDocument parts. In extract.go, the unreadable comment (lines 198-199) still says "otherwise a file that is corrupt", but it now answers too large for an over-limit error. The inline comment on line 207 covers the rest.

Author questions

  • The shared nesting type, rather than running Word and OpenDocument parts through limitedTokens: accepted. limitedTokens carries limits that only make sense for a workbook (element, cell and run counts, the tag guard, the deadline tick), and they would refuse or slow ordinary documents. The depth rule and its error are written once, in nesting.enter.
  • The workbook error text change: R1.3, inline.
  • The ODT body decoder mutant is equivalent, as the author says. odtListStyles reads all of content.xml with the same decoder before the body reader runs (odt.go:43-47), so the body reader never sees a part nested past the limit. When I also made the list-style pass skip content.xml, both new tests killed the mutant.

Two items in code this PR does not change will go to Linear tickets.

CI: the PR is a draft, so the go and integration jobs were skipped. They run when it is marked ready. Locally on the head, go vet and go test ./internal/knowledge/... pass, and the new tests pass 10 runs in a row.

kanat and others added 5 commits November 29, 2026 10:56
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…files

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…s own cap

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…ts reason

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

VERDICT: NO-GO

Reviewed 5973741 on main 6dff757 (draft, so no CI runs; go and integration are skipped).

Earlier findings:

  • R1.1 fixed: TestABrokenWordOrOpenDocumentPartIsRefusedAsCorrupt kills the mutant that answers every XML error as TooLarge.
  • R1.2 and T1.1 fixed: the five comments are true now.
  • R1.3 fixed: each format keeps its wording. Changing either writer to "a program" fails its test.
  • R1.6 fixed in code: the pages text is the base text, byte for byte, when errors.As finds PageLimit. Unwrapping it in pdf.go, or flipping the check in documents.go either way, fails the new tests.
  • T1.2 fixed: the PR body opens with the rewrite.
  • S1.1 and S1.2 fixed: a plain decoder in the ODT body reader, and removing the sticky error, each fail a test. Replacing newDocumentDecoder at each of the six sites (docx.go 83, 112, 157, 227; odt.go 69, 114) fails a test.

New finding:

  • R2.1 [Should fix] The second PageLimit site, extract.go:320, is not pinned. That is the page check for every file that is not a PDF. Removing the wrap there passes the extract and api suites, and a Word file over the page limit is then told "too large or too deeply nested" instead of "(at most 100 pages)". That is a wording change from base. Probe (reverted): a.docx with <w:lastRenderedPageBreak/> in the 2nd and 3rd paragraphs, read with MaxPages: 1. It passes on head and fails under the mutant. The inline comment has the row.

R1.4, R1.5 and the S1.3 follow-ups stay tickets.

Suites on head (same as base): go test ./internal/knowledge/..., api TestDocumentFailureSuite and TestAreaSuite, and go vet with and without the integration tag pass. The api integration suite was not run (needs a DB). 17 mutants checked, 1 survived (R2.1).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

VERDICT: GO

Delta round 3 of this PR, head 874f1fc (base origin/main 6dff757; no merge needed, the old head is an ancestor).

Prior findings:

  • R2.1 fixed. TestAWordFileOverThePageLimitIsToldHowManyPagesAreRead fails when the PageLimit wrap at extract/extract.go:320 is removed (it gets the generic "too large or too deeply nested" wording) and passes once restored.
  • R1.1, R1.2, R1.3, R1.6, T1.1, T1.2, S1.1, S1.2: fixed in round 2. This delta does not touch them.
  • R1.4, R1.5: Question items with a ticket, not touched by this delta. The ticket policy stands.

New findings: none.

Checks:

  • git diff 5973741..874f1fc --stat: one file, acceleration/internal/api/documents_unit_test.go, 25 insertions.
  • go test ./internal/api -run TestDocumentFailureSuite: passes on head. Mutant: fails the new test. Restored: passes.
  • go test ./internal/knowledge/...: all packages pass.
  • go vet ./internal/api ./internal/knowledge/...: clean.
  • CI: draft. go and integration skipping, labeler and GitGuardian pass, CodeQL analyses pending at the time of reading.

UNVERIFIED:

  • Linear tickets for R1.4 and R1.5 were not searched in this round.

@kanat
kanat marked this pull request as ready for review November 29, 2026 16:13

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant