Skip to content

Commit

Permalink
fix: Addressed a possible bypass issue caused by deep-nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
cure53 committed Apr 23, 2024
1 parent 632f122 commit c5369f2
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 5 deletions.
14 changes: 14 additions & 0 deletions dist/purify.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/purify.cjs.js.map

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions dist/purify.es.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,9 @@ function createDOMPurify() {
/* 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 = 511;

/* Ideally, do not touch anything below this line */
/* ______________________________________________ */

Expand Down Expand Up @@ -1283,6 +1286,7 @@ function createDOMPurify() {
let importedNode = null;
let currentNode = null;
let returnNode = null;
let depth = 0;
/* Make sure we have a string to sanitize.
DO NOT return early, as this will return the wrong type if
the user has requested a DOM object rather than a string */
Expand Down Expand Up @@ -1374,6 +1378,16 @@ function createDOMPurify() {
continue;
}

/* Count the nesting depth of an element */
if (currentNode.hasChildNodes()) {
depth++;
}

/* Remove an element if nested too deeply to avoid mXSS */
if (depth >= MAX_NESTING_DEPTH) {
_forceRemove(currentNode);
}

/* Shadow DOM detected, sanitize it */
if (currentNode.content instanceof DocumentFragment) {
_sanitizeShadowDOM(currentNode.content);
Expand Down
2 changes: 1 addition & 1 deletion dist/purify.es.mjs.map

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions dist/purify.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/purify.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/purify.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/purify.min.js.map

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions src/purify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 511;

/* Ideally, do not touch anything below this line */
/* ______________________________________________ */

Expand Down Expand Up @@ -1397,6 +1400,7 @@ function createDOMPurify(window = getGlobal()) {
let importedNode = null;
let currentNode = null;
let returnNode = null;
let depth = 0;
/* Make sure we have a string to sanitize.
DO NOT return early, as this will return the wrong type if
the user has requested a DOM object rather than a string */
Expand Down Expand Up @@ -1497,6 +1501,16 @@ function createDOMPurify(window = getGlobal()) {
continue;
}

/* Count the nesting depth of an element */
if (currentNode.hasChildNodes()) {
depth++;
}

/* Remove an element if nested too deeply to avoid mXSS */
if (depth >= MAX_NESTING_DEPTH) {
_forceRemove(currentNode);
}

/* Shadow DOM detected, sanitize it */
if (currentNode.content instanceof DocumentFragment) {
_sanitizeShadowDOM(currentNode.content);
Expand Down

0 comments on commit c5369f2

Please sign in to comment.