Summary
The xmldom JavaScript XML serializer fails to properly sanitize attacker-controlled comment content, allowing comment-breaking sequences to terminate comments prematurely.
When applications embed untrusted data inside XML comments, an attacker can inject arbitrary XML nodes into the serialized output, potentially altering downstream XML processing logic.
Affected context
This vulnerability occurs in Node.js or browser-based applications that use the xmldom XMLSerializer (or equivalent APIs in @xmldom/xmldom) to generate XML documents containing user- or externally supplied data in comments.
It is most relevant where serialized XML is consumed by other services, stored as configuration, or processed by parsers that trust the document structure. The risk increases when untrusted comment content is embedded without normalization or validation, and downstream logic acts on the injected nodes.
Exploit preconditions
- Attack surface: Any XML serialization path that embeds untrusted data into
<!-- ... -->comment sections using xmldom. - Required access: Ability to control or influence comment content (for example, user input, data from external systems, or logs injected into XML comments).
- Trigger point: Invocation of
XMLSerializer(or related serialization routines) that render the crafted comment into an XML string. - Why exploitation is possible: xmldom does not validate or neutralize comment-breaking sequences (such as
-->) inside comment text, allowing an attacker to close the comment early and introduce arbitrary XML elements or attributes that become part of the document structure.
Why this happens
The underlying issue stems from insufficient normalization and validation of comment content before XML serialization.
In xmldom, attacker-controlled strings inserted into XML comments are treated as opaque text and written directly into the serialized output. When these strings contain comment-terminating sequences, the serializer produces syntactically valid XML that closes the comment and continues with additional nodes specified by the attacker.
This behavior maps to CWE-91: XML Injection, where untrusted data is embedded into XML without enforcing structural integrity, enabling modification of the document tree and downstream interpretation.
Potential impact
- An attacker may inject arbitrary XML elements or attributes into the serialized document.
- XML-based access control rules, routing decisions, or feature toggles could be modified or bypassed by injected nodes.
- Downstream XML consumers can be tricked into processing attacker-defined configuration or policy data.
- Integrity of logs, audit trails, or data interchange formats using XML comments can be compromised.
- In complex XML workflows, injected nodes could contribute to SSRF, data exfiltration, or logic manipulation when combined with other weaknesses.
General mitigation guidance
Effective mitigation focuses on enforcing strict XML structural integrity and reducing trust in comment content.
Recommended patterns include:
- Avoid embedding untrusted data inside XML comments; prefer data elements or attributes with proper escaping and validation.
- Normalize or escape comment text to remove or encode sequences that can terminate comments (such as
-->) before passing data to the serializer. - Introduce an abstraction layer for XML construction that never concatenates raw strings into markup; instead, build documents using safe DOM APIs and fixed schemas.
- Apply schema-driven validation on serialized XML where feasible to ensure that only expected nodes and attributes are present.
- Limit the authority and interpretation scope of XML documents generated from partially untrusted data, using least-privilege principles for any system consuming them.
How teams detect issues like this
Detection typically combines code-level inspection and policy enforcement around XML generation.
Useful strategies include:
- Static analysis rules that flag direct insertion of untrusted strings into XML comments or other markup-sensitive locations.
- Code review checklists focused on XML/HTML construction, requiring explicit escaping functions and discouraging use of string concatenation for document structure.
- Automated scanning for patterns like
<!--combined with variables or user input in serialization code, particularly in xmldom usage sites. - Runtime validation or testing that fuzzes comment content with comment-terminating sequences to reveal structural injection issues.
- Centralized XML generation utilities with security reviews, paired with CI policies that require their use instead of ad hoc serialization.

