Update DOMPurify to version 3.1.2

This commit is contained in:
Théophile Diot 2024-05-02 09:24:38 +02:00
parent 55c560e3b9
commit 13b17cd505
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
2 changed files with 59 additions and 9 deletions

File diff suppressed because one or more lines are too long

View file

@ -388,6 +388,9 @@ function createDOMPurify(window = getGlobal()) {
/* Keep a reference to config to pass to hooks */
let CONFIG = null;
/* Specify the maximum element nesting depth to prevent mXSS */
const MAX_NESTING_DEPTH = 255;
/* Ideally, do not touch anything below this line */
/* ______________________________________________ */
@ -640,8 +643,6 @@ function createDOMPurify(window = getGlobal()) {
const HTML_INTEGRATION_POINTS = addToSet({}, [
'foreignobject',
'desc',
'title',
'annotation-xml',
]);
@ -934,7 +935,13 @@ function createDOMPurify(window = getGlobal()) {
const _isClobbered = function (elm) {
return (
elm instanceof HTMLFormElement &&
(typeof elm.nodeName !== 'string' ||
// eslint-disable-next-line unicorn/no-typeof-undefined
((typeof elm.__depth !== 'undefined' &&
typeof elm.__depth !== 'number') ||
// eslint-disable-next-line unicorn/no-typeof-undefined
(typeof elm.__removalCount !== 'undefined' &&
typeof elm.__removalCount !== 'number') ||
typeof elm.nodeName !== 'string' ||
typeof elm.textContent !== 'string' ||
typeof elm.removeChild !== 'function' ||
!(elm.attributes instanceof NamedNodeMap) ||
@ -1060,10 +1067,9 @@ function createDOMPurify(window = getGlobal()) {
const childCount = childNodes.length;
for (let i = childCount - 1; i >= 0; --i) {
parentNode.insertBefore(
cloneNode(childNodes[i], true),
getNextSibling(currentNode)
);
const childClone = cloneNode(childNodes[i], true);
childClone.__removalCount = (currentNode.__removalCount || 0) + 1;
parentNode.insertBefore(childClone, getNextSibling(currentNode));
}
}
}
@ -1371,8 +1377,30 @@ function createDOMPurify(window = getGlobal()) {
continue;
}
const parentNode = getParentNode(shadowNode);
/* Set the nesting depth of an element */
if (shadowNode.nodeType === 1) {
if (parentNode && parentNode.__depth) {
/*
We want the depth of the node in the original tree, which can
change when it's removed from its parent.
*/
shadowNode.__depth =
(shadowNode.__removalCount || 0) + parentNode.__depth + 1;
} else {
shadowNode.__depth = 1;
}
}
/* Remove an element if nested too deeply to avoid mXSS */
if (shadowNode.__depth >= MAX_NESTING_DEPTH) {
_forceRemove(shadowNode);
}
/* Deep shadow DOM detected */
if (shadowNode.content instanceof DocumentFragment) {
shadowNode.content.__depth = shadowNode.__depth;
_sanitizeShadowDOM(shadowNode.content);
}
@ -1497,8 +1525,30 @@ function createDOMPurify(window = getGlobal()) {
continue;
}
const parentNode = getParentNode(currentNode);
/* Set the nesting depth of an element */
if (currentNode.nodeType === 1) {
if (parentNode && parentNode.__depth) {
/*
We want the depth of the node in the original tree, which can
change when it's removed from its parent.
*/
currentNode.__depth =
(currentNode.__removalCount || 0) + parentNode.__depth + 1;
} else {
currentNode.__depth = 1;
}
}
/* Remove an element if nested too deeply to avoid mXSS */
if (currentNode.__depth >= MAX_NESTING_DEPTH) {
_forceRemove(currentNode);
}
/* Shadow DOM detected, sanitize it */
if (currentNode.content instanceof DocumentFragment) {
currentNode.content.__depth = currentNode.__depth;
_sanitizeShadowDOM(currentNode.content);
}