Summary
fast-xml-parser versions 4.0.0-beta.3 through 5.5.5 contain an XML entity expansion vulnerability where numeric character references and standard XML entities bypass configured expansion limits.
An attacker who can supply XML input to be parsed can trigger excessive memory allocation and CPU usage, potentially crashing the process, even when developers believe they have enforced strict safety limits.
Affected context
This vulnerability affects applications that use fast-xml-parser (versions 4.0.0-beta.3 to 5.5.5) to parse untrusted or semi-trusted XML, particularly where:
- Entity expansion limits (e.g., maxTotalExpansions, maxExpandedLength) are configured to mitigate XML bombs
- The parser is invoked on user-supplied XML documents or data from external services
- The resulting JSON/JS object is processed in memory in a long-lived Node.js or browser process
The risk is higher in services that perform synchronous parsing on the main event loop or in resource-constrained environments where spikes in memory or CPU can cause outages.
Exploit preconditions
- Attack surface: Any endpoint, message handler, or job that accepts XML and uses fast-xml-parser for processing.
- Required access: Ability to submit or influence XML input (e.g., HTTP request body, message queue payload, file upload, webhook payload, or API integration).
- Trigger point: Parsing of XML that contains a large number of numeric character references (e.g.,
&#NNN;,&#xHH;) or standard XML entities. - Why exploitation is possible: The parser's entity expansion limits are only applied to DOCTYPE-defined entities; numeric character references and built-in XML entities are expanded in a separate code path (
lastEntitiesloop) that does not increment or enforce expansion counters. This allows an attacker to craft XML with massive effective expansion while the configured safety limits appear to be respected.
Why this happens
The underlying bug is a partial and inconsistent enforcement of resource limits on XML entity expansion. To mitigate XML bombs, fast-xml-parser introduced options such as maxTotalExpansions and maxExpandedLength, but the implementation only accounted for entities defined in the XML DOCTYPE.
In OrderedObjParser.js, replaceEntitiesValue() correctly counts expansions for DOCTYPE-defined entities, yet the subsequent lastEntities loop that replaces numeric character references (&#NNN;, &#xHH;) and standard entities (&, <, etc.) performs replacements without updating these counters. This mismatch between the intended security policy and its implementation maps directly to CWE-776: Improper Restriction of Recursive Entity Expansion in XML, where XML constructs can trigger uncontrolled resource consumption because protective limits are incomplete or bypassable.
Potential impact
- XML parsing may consume large amounts of memory, potentially exhausting heap space.
- CPU utilization can spike as the parser processes vast numbers of expansions, degrading performance.
- The Node.js process or service can crash, causing denial of service for all users of the affected application.
- In multi-tenant or shared environments, resource exhaustion could impact other services on the same host.
- Upstream systems, load balancers, or autoscalers may experience cascading failures due to repeated crashes and restarts.
General mitigation guidance
Effective mitigation focuses on limiting trust in XML inputs and ensuring entity handling is safe by default, regardless of configuration:
- Prefer XML parsing configurations that fully disable DTDs and external entities when they are not strictly required.
- Treat all forms of entity or character reference expansion (DOCTYPE entities, numeric references, and standard entities) as potentially dangerous, and apply consistent counting and bounding across them.
- Introduce application-level safeguards such as:
- Global request body size limits at reverse proxies or API gateways.
- Timeouts and concurrency limits for parsing operations.
- Isolation of XML parsing into separate worker processes with explicit memory limits.
- Avoid parsing untrusted XML on the main event loop of high-value services; instead, offload to sandboxed or rate-limited components.
- Apply a defense-in-depth strategy: combine parser-level limits with infrastructure-level protections (WAF rules for obviously abusive XML payloads, rate limiting, and anomaly detection).
How teams detect issues like this
Organizations can use several approaches to detect and monitor for this class of vulnerability:
- Static analysis and dependency review:
- Search code for usage of
fast-xml-parserin versions known to be affected and flag any use where the input might be attacker-controlled. - Code review for patterns where XML parsing is done on untrusted data without upstream size or rate limits.
- Configuration and policy checks:
- Enforce policies that DTDs and entity expansion features are disabled unless explicitly justified.
- Integrate software composition analysis (SCA) tools into CI to surface vulnerable parser versions and require review before deployment.
- Runtime monitoring:
- Instrument services to log unusually large XML payloads or repeated parsing errors.
- Monitor for spikes in memory usage or CPU consumption correlated with XML parsing endpoints.
- Use application performance monitoring (APM) to identify slow or resource-heavy parsing operations indicative of entity expansion attacks.
- Security testing:
- During QA and security assessments, include fuzzing or stress tests with XML payloads containing many numeric character references to validate that limits are enforced effectively.

