Описание
xmldom: Attribute name injection via setAttribute() bypasses requireWellFormed
Summary
Element.setAttribute() in @xmldom/xmldom bypasses attribute name validation by calling the private _createAttribute(name) method, which performs no validation. The public createAttribute() method correctly validates names against an anchored QName pattern, but setAttribute() never uses it. The serializer escapes attribute values but trusts attribute names, allowing an attacker to inject additional attributes (including event handlers) into serialized output. The requireWellFormed: true option did not catch this.
Details
Element.setAttribute(name, value) creates attribute nodes by calling the private _createAttribute(name) method, which performs no validation on the name parameter. In contrast, the public Document.createAttribute(name) method validates the name against the QName production before creating the attribute node.
The result is a two-tier validation system where the most commonly used API (setAttribute) takes the unvalidated path:
doc.createAttribute("bad name")— throwsINVALID_CHARACTER_ERR(correct).el.setAttribute("bad name", "value")— succeeds silently (vulnerable).
The serializer emits attribute names verbatim into the output. Because attribute values ARE escaped (quotes, ampersands, etc.), the injection must occur through the name. An attacker can terminate the current attribute and inject new ones by including quote and space characters in the attribute name.
Root Cause
setAttribute()calls_createAttribute()(private, no validation) instead ofcreateAttribute()(public, validates againstQName).- The serializer trusts attribute names and emits them unescaped.
- The serializer's
requireWellFormedcode path did not validate attribute names during serialization.
Proof of Concept
Demonstrating the validation gap
Impact
Applications that use setAttribute() with any user-controlled portion of the attribute name are vulnerable to attribute injection attacks. This includes:
- Cross-Site Scripting (XSS): Injecting event handler attributes into HTML output consumed by browsers.
- Security attribute override: Overriding security-relevant attributes such as
integrity,nonce,sandbox, orContent-Security-Policymeta attributes. - Validation bypass: The public
createAttribute()API validates whilesetAttribute()does not, creating an inconsistent security boundary that developers cannot rely on. - requireWellFormed bypass: Applications that adopted
requireWellFormed: trueas a mitigation for prior CVEs remained vulnerable.
@xmldom/xmldom can also be used inside browsers, where it mirrors the DOM API. Unlike the browser's setAttribute(), which rejects an invalid attribute name with InvalidCharacterError, xmldom accepts it — developers may assume the same safety and skip validation.
Fix Applied
⚠ Opt-in required. Protection is not automatic. Existing serialization calls remain vulnerable unless
{ requireWellFormed: true }is explicitly passed. Applications that serialize untrusted DOM content should audit allserializeToString()call sites and add it.
When { requireWellFormed: true } is passed, the serializer now validates each serialized attribute's qualified name against the XML QName production and throws InvalidStateError before emitting it. This covers ordinary attribute names and synthesized xmlns:PREFIX namespace declarations (the namespace-prefix sub-vector).
Fixed under requireWellFormed: true in @xmldom/xmldom 0.9.11 and 0.8.14. Default serialization is unchanged.
PoC — fixed path
Why the default stays verbatim
The W3C DOM Parsing and Serialization spec defines a require well-formed flag whose default value is false. With the flag unset, the serializer emits attribute names verbatim, matching the XMLSerializer behavior of Chrome, Firefox, and Safari. Unconditionally throwing would be a behavioral breaking change with no spec justification; the opt-in requireWellFormed: true flag lets applications that require injection safety enable strict mode without breaking existing code.
Residual limitation
setAttribute(name, value) does not validate name at creation time (unlike the public createAttribute(), which already does). Making setAttribute() reject invalid names unconditionally is a breaking change and is deferred to the next breaking release. When the default serialization path is used (without requireWellFormed: true), attribute names set via setAttribute() are still emitted verbatim; applications that do not pass requireWellFormed: true remain exposed.
Creation-time validation is tracked in a public issue on the next breaking-release milestone (filed at publication — issue link to be added), targeting the next breaking release.
Ссылки
- https://github.com/xmldom/xmldom/security/advisories/GHSA-4w3w-2rp5-g8jm
- https://nvd.nist.gov/vuln/detail/CVE-2026-83605
- https://github.com/xmldom/xmldom/pull/1043
- https://github.com/xmldom/xmldom/pull/1050
- https://github.com/xmldom/xmldom/commit/cba1321218b069182695813fa7565653708e172e
- https://github.com/xmldom/xmldom/commit/d8212e632507eaf1d9f609657dd4c56abeb12d44
- https://github.com/xmldom/xmldom/releases/tag/0.8.14
- https://github.com/xmldom/xmldom/releases/tag/0.9.11
Пакеты
@xmldom/xmldom
>= 0.9.0, <= 0.9.10
0.9.11
@xmldom/xmldom
>= 0.7.0, <= 0.8.13
0.8.14
xmldom
<= 0.6.0
Отсутствует
Связанные уязвимости
(xmldom is a pure JavaScript W3C standard-based (XML DOM Level 2 Core) ...)
xmldom is a pure JavaScript W3C standard-based (XML DOM Level 2 Core) DOMParser and XMLSerializer module. Prior to @xmldom/xmldom versions 0.8.14 and 0.9.11, and in xmldom version 0.6.0 and earlier, Element.setAttribute() calls the private _createAttribute(name) path without validating the attribute name, while Document.createAttribute(name) validates against QName. XMLSerializer.serializeToString() emits attribute names verbatim, and requireWellFormed: true did not validate them, so a crafted name can terminate the intended attribute and inject additional attributes, including event handlers, into browser-consumed output; synthesized xmlns:PREFIX declarations expose the same unchecked-name boundary. This issue is fixed in @xmldom/xmldom versions 0.8.14 and 0.9.11; no fixed version is available for xmldom.
xmldom is a pure JavaScript W3C standard-based (XML DOM Level 2 Core) DOMParser and XMLSerializer module. Prior to @xmldom/xmldom versions 0.8.14 and 0.9.11, and in xmldom version 0.6.0 and earlier, Element.setAttribute() calls the private _createAttribute(name) path without validating the attribute name, while Document.createAttribute(name) validates against QName. XMLSerializer.serializeToString() emits attribute names verbatim, and requireWellFormed: true did not validate them, so a crafted name can terminate the intended attribute and inject additional attributes, including event handlers, into browser-consumed output; synthesized xmlns:PREFIX declarations expose the same unchecked-name boundary. This issue is fixed in @xmldom/xmldom versions 0.8.14 and 0.9.11; no fixed version is available for xmldom.
xmldom: Attribute name injection via setAttribute() bypasses requireWellFormed
xmldom is a pure JavaScript W3C standard-based (XML DOM Level 2 Core) ...