Skip to main content
Vulnerability Database/CVE-2021-21366

CVE-2021-21366: Xmldom_project Xmldom XXE Vulnerability

CVE-2021-21366 is an XXE vulnerability in Xmldom_project Xmldom that fails to preserve system identifiers when parsing malicious documents. This article covers technical details, affected versions, and mitigation.

Updated:

Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.

Sources: National Vulnerability Database, CISA KEV, Exploit-DB

CVE-2021-21366 Overview

CVE-2021-21366 affects xmldom, a pure JavaScript W3C standard-based XML DOM Level 2 Core DOMParser and XMLSerializer module distributed via npm. Versions 0.4.0 and earlier fail to correctly preserve system identifiers, Formal Public Identifiers (FPIs), and namespaces when a maliciously crafted document is repeatedly parsed and serialized. The inconsistency can introduce unexpected syntactic changes during XML processing in downstream applications that rely on xmldom for input handling. The maintainers fixed the issue in version 0.5.0. The vulnerability is tracked under [CWE-115] (Misinterpretation of Input) and [CWE-436] (Interpretation Conflict).

Critical Impact

Repeated parse/serialize cycles on attacker-controlled XML can mutate document syntax, enabling integrity-affecting downstream behavior in any Node.js application that trusts xmldom round-trips.

Affected Products

  • xmldom (npm package) versions 0.4.0 and earlier
  • Debian Linux 10.0 (node-xmldom package)
  • Node.js applications that consume xmldom as a dependency

Discovery Timeline

  • 2021-03-12 - CVE-2021-21366 published to NVD
  • 2024-11-21 - Last updated in NVD database

Technical Details for CVE-2021-21366

Vulnerability Analysis

The defect lives in how xmldom reconstructs DOCTYPE declarations during serialization. When the serializer rebuilds the <!DOCTYPE> node, it does not faithfully reproduce the original system identifier, public identifier (FPI), or namespace markers. An application that parses XML, performs logic against the DOM, and then serializes the result for forwarding or storage can emit output that is structurally different from the input. Downstream consumers receiving the serialized output may interpret the document under different syntactic assumptions, producing an interpretation conflict.

The practical impact is integrity loss rather than confidentiality or availability. User interaction is required because exploitation depends on the victim application processing attacker-supplied XML. Affected workflows include XML signature pipelines, SAML processors, configuration loaders, and any service that round-trips XML between trust boundaries.

Root Cause

The serializer in lib/dom.js emitted DOCTYPE strings using hard-coded quoting and concatenation that did not account for all valid identifier forms. The fatal-error handler in lib/dom-parser.js also rethrew raw error objects without wrapping them in a ParseError, weakening the boundary between recoverable parse failures and propagated state.

Attack Vector

An attacker supplies a crafted XML document containing carefully shaped DOCTYPE, FPI, system identifier, or namespace declarations. The victim application parses the document with xmldom, then serializes the DOM back to a string. The serialized output differs from the original in a way the attacker can predict, allowing the attacker to influence how a second consumer parses the document.

javascript
// Patch in lib/dom.js - corrected DOCTYPE serialization
var sysid = node.systemId;
buf.push('<!DOCTYPE ', node.name);
if (pubid) {
    buf.push(' PUBLIC ', pubid);
    if (sysid && sysid != '.') {
        buf.push(' ', sysid);
    }
    buf.push('>');
} else if (sysid && sysid != '.') {
    buf.push(' SYSTEM ', sysid, '>');
} else {
    var sub = node.internalSubset;
    if (sub) {
        // ...
    }
}

// Patch in lib/dom-parser.js - fatalError handler now wraps in ParseError
fatalError: function (error) {
    throw new ParseError(error, this.locator);
}

Source: GitHub commit d4201b9

Detection Methods for CVE-2021-21366

Indicators of Compromise

  • Application logs showing differences between received XML and serialized XML, particularly within <!DOCTYPE>, PUBLIC, or SYSTEM declarations.
  • Presence of xmldom versions at or below 0.4.0 in package-lock.json, yarn.lock, or installed node_modules trees.
  • Unexpected XML parse failures or DOCTYPE-related warnings emitted by downstream XML consumers in the same pipeline.

Detection Strategies

  • Run npm ls xmldom or equivalent SBOM queries across build artifacts to enumerate vulnerable versions, including transitive dependencies.
  • Use Software Composition Analysis (SCA) tooling configured to flag GHSA-h6q6-9hqw-rwfv against any service or container image.
  • Add unit tests that parse and re-serialize representative XML inputs, asserting byte-for-byte stability of DOCTYPE and namespace declarations.

Monitoring Recommendations

  • Monitor application logs for ParseError exceptions originating from xmldom after upgrade to 0.5.0+.
  • Track outbound XML traffic from services that round-trip XML, looking for DOCTYPE drift compared to ingress payloads.
  • Alert on builds that introduce or pin xmldom@<=0.4.0 through CI policy checks.

How to Mitigate CVE-2021-21366

Immediate Actions Required

  • Upgrade xmldom to version 0.5.0 or later in every Node.js project and rebuild affected container images.
  • Audit transitive dependencies with npm audit or yarn audit and update parent packages that pin a vulnerable xmldom.
  • For Debian 10 systems, apply the package updates announced in the Debian LTS Announcement.

Patch Information

The fix landed in the GitHub release for xmldom 0.5.0 and is described in GitHub Security Advisory GHSA-h6q6-9hqw-rwfv. The patch commit d4201b9 rewrites DOCTYPE serialization and converts fatal parse errors into a typed ParseError. The latest package is available on npm as xmldom.

Workarounds

  • Validate untrusted XML against an explicit schema or allowlist before passing it to xmldom, rejecting documents that contain DOCTYPE, FPI, or system identifier declarations.
  • Avoid architectures that re-serialize attacker-controlled XML for forwarding to other consumers; pass the original bytes when possible.
  • Wrap xmldom calls in a defensive layer that compares the serialized output to the original input and rejects mismatched documents.
bash
# Upgrade xmldom across a Node.js project
npm install xmldom@^0.5.0 --save
npm ls xmldom

# Or pin the modern @xmldom/xmldom successor package
npm install @xmldom/xmldom --save

Disclaimer: This content was generated using AI. While we strive for accuracy, please verify critical information with official sources.

Sources: National Vulnerability Database, CISA KEV, Exploit-DB

Experience the Most Advanced Cybersecurity Platform

See how the world’s most intelligent, autonomous cybersecurity platform can protect your organization today and into the future.