Skip to content

xml: Reject entity declarations in XmlDocument::Parse - #281

Open
dhivadhiva wants to merge 1 commit into
google:mainfrom
dhivadhiva:xml-entity-expansion
Open

dhivadhiva wants to merge 1 commit into
google:mainfrom
dhivadhiva:xml-entity-expansion

Conversation

@dhivadhiva

Copy link
Copy Markdown
Contributor

XmlDocument::Parse() and ParseFragment() use xml-rs's default ParserConfig, and xml-rs does not bound the total size of entity expansion. A document of a few hundred bytes can make them allocate gigabytes and abort the process.

1. Parameter entities expand while the DTD is read

xml-rs expands parameter entity references inside entity values when it reads each declaration (PEReferenceInValue in inside_doctype.rs), and no limit applies there. Each declaration can multiply the size by 10:

<!DOCTYPE r [<!ENTITY % e0 "xxxxxxxxxx">
             <!ENTITY % e1 "%e0;%e0;%e0;%e0;%e0;%e0;%e0;%e0;%e0;%e0;">
             ...
             <!ENTITY % e8 "%e7;%e7;%e7;%e7;%e7;%e7;%e7;%e7;%e7;%e7;">]><r/>

None of these entities is referenced in content, and the expansion happens before xml-rs returns its first element.

2. References to general entities add up without limit

max_entity_expansion_length and max_entity_expansion_depth apply to each reference on its own, so one reference can expand to 1M characters, and nothing bounds the total. With b at 100 KB and a holding 9 &b;, every 10-byte <x>&a;</x> adds 900 KB of text.

Measured through XmlDocument::Parse() on main

Input Size Result
Parameter entity chain, 7 levels 445 B OK, 0.14 s, 125 MB peak RSS
Parameter entity chain, 8 levels 502 B OK, 1.6 s, 1.1 GB
Parameter entity chain, 9 levels 559 B 10 GB allocation fails, process aborts (SIGABRT under a 4 GiB limit; without one, the OOM killer)
100 references to &a; 101 KB OK, 12.9 s, 95 MB
400 references to &a; 104 KB OK, 52 s, 360 MB

The nesting depth scan in parse_element runs the same parser over the input before the real parse, so it does not stop either case. It doubles the work instead.

Fix

xml-rs has no option to ignore entity declarations, and checking a budget on its events would come too late, because both expansions happen inside xml-rs before it returns an event. So parse_element now rejects the input before xml-rs reads it if it contains <!ENTITY, and returns INVALID_ARGUMENT with "XML entity declarations are not supported".

  • xml-rs only recognizes an entity declaration spelled exactly <!ENTITY (uppercase, in DoctypeSubstate::InsideName). The check looks for it in UTF-8, which also covers xml-rs's other ASCII-compatible encodings, and in UTF-16 in both byte orders, since xml-rs reads UTF-16 input too.
  • Predefined entities (&lt; …), character references (&#98;) and DTDs without entity declarations still parse.
  • This matches QuickReader, which does not expand user-defined entities either ("to prevent XXE attacks").
  • The scan costs 84 ms on a 50 MB document, which takes 2.6 s to parse once.
  • After the fix, all five inputs above return INVALID_ARGUMENT in under 0.1 ms with 6 MB peak RSS.

Compatibility

  • Documents that declare entities no longer parse with XmlDocument, even if they never reference those entities.
  • The check is a byte search, so <!ENTITY in a comment or CDATA section also rejects the document. Entities can only be declared in the DTD, but telling the DTD apart without parsing is what this avoids.
  • The Parse() comment in xmltree.h and a new section in xml/README.md describe both points.

Testing

New xmltree_test target (gtest, set up like roaring_bridge_cc_test) with 5 tests:

  • ParseRejectsEntityDeclarations, ParseRejectsNestedEntityReferences (900 KB from 1.3 KB), ParseRejectsParameterEntityChain (5 levels, 1 MB) and ParseRejectsUtf16EntityDeclarations (LE and BE, with a check that plain UTF-16 input still parses) all fail on main, because the documents parse.
  • ParseAcceptsDoctypeWithoutEntityDeclarations covers behavior that has to stay.

All 6 xml tests (these 5 and xml_example) pass on this branch, built from a clean checkout with the CI recipe (cmake -B build -S xml, Crubit b715411, nightly-2026-11-11). The tests use inputs that stay small on main, so a regression fails a test instead of running CI out of memory.

The xml crate (xml-rs) does not bound the total size of entity
expansion, and XmlDocument::Parse() and ParseFragment() use its
defaults.

- It expands parameter entities inside entity values while it reads the
  DTD, with no size limit, so each declaration can multiply the size by
  10:

      <!DOCTYPE r [<!ENTITY % e0 "xxxxxxxxxx">
                   <!ENTITY % e1 "%e0;%e0;%e0;%e0;%e0;%e0;%e0;%e0;%e0;%e0;">
                   ...
                   <!ENTITY % e8 "%e7;%e7;%e7;%e7;%e7;%e7;%e7;%e7;%e7;%e7;">]><r/>

  This 502-byte document takes 1.1 GB in XmlDocument::Parse(). With one
  more level (559 bytes) the 10 GB allocation fails and aborts the
  process.
- Its limits on general entities apply to each reference on its own: a
  reference in content can expand to a million characters, and nothing
  bounds their sum. 104 KB of references that each expand to 900 KB took
  52 s and 360 MB.

The nesting depth scan runs the same parser over the input first, so it
does not stop either case.

xml has no option to ignore entity declarations, and a budget checked
on its events would come too late, because both expansions happen
inside xml before it returns an event. So reject the input before xml
reads it if it contains `<!ENTITY`, the only spelling xml recognizes, in
UTF-8 or UTF-16. Predefined entities, character references and DTDs
without entity declarations still parse. QuickReader does not expand
user-defined entities either.

Add xmltree_test, a gtest target, with tests for both kinds of
expansion, UTF-16 input and a DTD without entity declarations.
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