Repository navigation
xml: Reject entity declarations in XmlDocument::Parse - #281
Open
dhivadhiva wants to merge 1 commit into
Open
dhivadhiva wants to merge 1 commit into
dhivadhiva wants to merge 1 commit into
Conversation
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.
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.
XmlDocument::Parse()andParseFragment()use xml-rs's defaultParserConfig, 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 (
PEReferenceInValueininside_doctype.rs), and no limit applies there. Each declaration can multiply the size by 10: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_lengthandmax_entity_expansion_depthapply to each reference on its own, so one reference can expand to 1M characters, and nothing bounds the total. Withbat 100 KB andaholding 9&b;, every 10-byte<x>&a;</x>adds 900 KB of text.Measured through
XmlDocument::Parse()onmain&a;&a;The nesting depth scan in
parse_elementruns 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_elementnow rejects the input before xml-rs reads it if it contains<!ENTITY, and returnsINVALID_ARGUMENTwith "XML entity declarations are not supported".<!ENTITY(uppercase, inDoctypeSubstate::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.<…), character references (b) and DTDs without entity declarations still parse.QuickReader, which does not expand user-defined entities either ("to prevent XXE attacks").INVALID_ARGUMENTin under 0.1 ms with 6 MB peak RSS.Compatibility
XmlDocument, even if they never reference those entities.<!ENTITYin 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.Parse()comment inxmltree.hand a new section inxml/README.mddescribe both points.Testing
New
xmltree_testtarget (gtest, set up likeroaring_bridge_cc_test) with 5 tests:ParseRejectsEntityDeclarations,ParseRejectsNestedEntityReferences(900 KB from 1.3 KB),ParseRejectsParameterEntityChain(5 levels, 1 MB) andParseRejectsUtf16EntityDeclarations(LE and BE, with a check that plain UTF-16 input still parses) all fail onmain, because the documents parse.ParseAcceptsDoctypeWithoutEntityDeclarationscovers 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 onmain, so a regression fails a test instead of running CI out of memory.