Summary
xmldom fails to properly validate or neutralize attacker-controlled processing instruction (PI) data before serializing XML, allowing the PI to be prematurely closed with ?>.
When this occurs, an attacker can inject arbitrary XML nodes into the serialized output, which becomes dangerous wherever the resulting XML is consumed as trusted data or configuration.
Affected context
This vulnerability appears in applications that rely on xmldom's DOMParser and XMLSerializer to handle XML documents that may contain processing instructions with data derived from untrusted or partially trusted sources.
The risk is present when serialized XML output is later treated as authoritative input for downstream systems, such as configuration loaders, business logic processors, or security-sensitive integrations, without an intermediate validation or sanitization step.
Exploit preconditions
- Attack surface: XML documents processed through xmldom where processing instructions include attacker-supplied data.
- Required access: Ability to influence or control PI content in XML, such as through a web form, API parameter, imported file, or upstream integration.
- Trigger point: Serialization of an XML DOM that contains the crafted processing instruction, using xmldom's
XMLSerializer. - Why exploitation is possible: The serializer does not escape or validate the PI-closing sequence
?>, allowing the attacker to terminate the PI early and append arbitrary XML nodes that are treated as legitimate serialized content.
Why this happens
The core issue is improper neutralization of special elements in XML processing instructions, mapping directly to CWE-91: Improper Neutralization of XML Special Elements.
xmldom's serialization logic assumes PI data is opaque text and fails to constrain or encode reserved XML tokens such as ?>, which marks the end of a processing instruction.
Because this boundary is not enforced, attacker-controlled PI data can break out of the intended PI context and inject additional XML elements, attributes, or structures, turning a simple data field into a channel for structural XML manipulation.
Potential impact
- Attacker-controlled XML elements may be injected into serialized documents.
- Downstream XML parsers or business logic can be misled by forged nodes and attributes.
- Application configuration, access control rules, or routing metadata may be altered through injected XML.
- Data integrity can be compromised when injected elements are treated as trusted records or commands.
- In some architectures, XML injection can become a stepping stone to further attacks, such as XXE or logic abuse in XML-driven workflows.
General mitigation guidance
Effective mitigation focuses on constraining XML structure and treating processing instruction data as untrusted input that must not alter document boundaries.
Common defensive patterns include:
- Normalizing or encoding PI data so that reserved XML tokens like
?>cannot be interpreted as control sequences. - Applying strict schema or structural validation on XML before serialization and before using it in downstream components.
- Reducing trust in serialized XML by avoiding direct execution of logic, configuration, or routing solely based on XML content.
- Segmenting responsibilities so that untrusted XML cannot directly feed into security-critical or configuration-sensitive subsystems.
- Favoring data-centric representations (e.g., JSON) for user-controlled content when structural XML semantics are not strictly required.
How teams detect issues like this
High-level detection patterns include:
- Static analysis rules that flag direct use of unvalidated user input in XML processing instructions or serializer calls.
- Code review checks for any construction of PIs where the data field originates from request parameters, uploaded files, or external services.
- Policy-driven tests that attempt to inject
?>sequences into PI content and observe whether the serialized XML structure changes unexpectedly. - Integration of automated security scanners that model XML structure and raise alerts when user-controllable fields can influence element boundaries or document structure.

