Skip to content

Commit 1e52026

Browse files
committed
fix: Hardened the depth tracking code against prototype pollution
1 parent 8df72f1 commit 1e52026

File tree

11 files changed

+59
-117
lines changed

11 files changed

+59
-117
lines changed

dist/purify.cjs.js

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.cjs.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.es.mjs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const stringTrim = unapply(String.prototype.trim);
4848
const objectHasOwnProperty = unapply(Object.prototype.hasOwnProperty);
4949
const regExpTest = unapply(RegExp.prototype.test);
5050
const typeErrorCreate = unconstruct(TypeError);
51+
const numberIsNaN = unapply(Number.isNaN);
5152

5253
/**
5354
* Creates a new function that calls the given function with a specified thisArg and arguments.
@@ -1307,8 +1308,11 @@ function createDOMPurify() {
13071308
}
13081309
}
13091310

1310-
/* Remove an element if nested too deeply to avoid mXSS */
1311-
if (shadowNode.__depth >= MAX_NESTING_DEPTH) {
1311+
/*
1312+
* Remove an element if nested too deeply to avoid mXSS
1313+
* or if the __depth might have been tampered with
1314+
*/
1315+
if (shadowNode.__depth >= MAX_NESTING_DEPTH || numberIsNaN(shadowNode.__depth)) {
13121316
_forceRemove(shadowNode);
13131317
}
13141318

@@ -1445,8 +1449,11 @@ function createDOMPurify() {
14451449
}
14461450
}
14471451

1448-
/* Remove an element if nested too deeply to avoid mXSS */
1449-
if (currentNode.__depth >= MAX_NESTING_DEPTH) {
1452+
/*
1453+
* Remove an element if nested too deeply to avoid mXSS
1454+
* or if the __depth might have been tampered with
1455+
*/
1456+
if (currentNode.__depth >= MAX_NESTING_DEPTH || numberIsNaN(currentNode.__depth)) {
14501457
_forceRemove(currentNode);
14511458
}
14521459

dist/purify.es.mjs.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.js

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/purify.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/purify.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
stringToString,
1616
stringIndexOf,
1717
stringTrim,
18+
numberIsNaN,
1819
regExpTest,
1920
typeErrorCreate,
2021
lookupGetter,
@@ -1426,8 +1427,14 @@ function createDOMPurify(window = getGlobal()) {
14261427
}
14271428
}
14281429

1429-
/* Remove an element if nested too deeply to avoid mXSS */
1430-
if (shadowNode.__depth >= MAX_NESTING_DEPTH) {
1430+
/*
1431+
* Remove an element if nested too deeply to avoid mXSS
1432+
* or if the __depth might have been tampered with
1433+
*/
1434+
if (
1435+
shadowNode.__depth >= MAX_NESTING_DEPTH ||
1436+
numberIsNaN(shadowNode.__depth)
1437+
) {
14311438
_forceRemove(shadowNode);
14321439
}
14331440

@@ -1577,8 +1584,14 @@ function createDOMPurify(window = getGlobal()) {
15771584
}
15781585
}
15791586

1580-
/* Remove an element if nested too deeply to avoid mXSS */
1581-
if (currentNode.__depth >= MAX_NESTING_DEPTH) {
1587+
/*
1588+
* Remove an element if nested too deeply to avoid mXSS
1589+
* or if the __depth might have been tampered with
1590+
*/
1591+
if (
1592+
currentNode.__depth >= MAX_NESTING_DEPTH ||
1593+
numberIsNaN(currentNode.__depth)
1594+
) {
15821595
_forceRemove(currentNode);
15831596
}
15841597

src/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const regExpTest = unapply(RegExp.prototype.test);
5252

5353
const typeErrorCreate = unconstruct(TypeError);
5454

55+
const numberIsNaN = unapply(Number.isNaN);
56+
5557
/**
5658
* Creates a new function that calls the given function with a specified thisArg and arguments.
5759
*
@@ -215,6 +217,8 @@ export {
215217
stringToLowerCase,
216218
stringToString,
217219
stringTrim,
220+
// Number
221+
numberIsNaN,
218222
// Errors
219223
typeErrorCreate,
220224
// Other

0 commit comments

Comments
 (0)