Skip to content

Commit fa542df

Browse files
committed
fix: Changed the order for attribute checks slightly for safer hooks
1 parent b8b552c commit fa542df

File tree

9 files changed

+36
-41
lines changed

9 files changed

+36
-41
lines changed

dist/purify.cjs.js

Lines changed: 8 additions & 10 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: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,9 @@ const DATA_ATTR = seal(/^data-[\-\w.\u00B7-\uFFFF]/); // eslint-disable-line no-
209209
const ARIA_ATTR = seal(/^aria-[\-\w]+$/); // eslint-disable-line no-useless-escape
210210
const IS_ALLOWED_URI = seal(/^(?:(?:(?:f|ht)tps?|mailto|tel|callto|sms|cid|xmpp):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i // eslint-disable-line no-useless-escape
211211
);
212-
213212
const IS_SCRIPT_OR_DATA = seal(/^(?:\w+script|data):/i);
214213
const ATTR_WHITESPACE = seal(/[\u0000-\u0020\u00A0\u1680\u180E\u2000-\u2029\u205F\u3000]/g // eslint-disable-line no-control-regex
215214
);
216-
217215
const DOCTYPE_NAME = seal(/^html$/i);
218216
const CUSTOM_ELEMENT = seal(/^[a-z][.\w]*(-[.\w]+)+$/i);
219217

@@ -248,7 +246,6 @@ const NODE_TYPE = {
248246
documentFragment: 11,
249247
notation: 12 // Deprecated
250248
};
251-
252249
const getGlobal = function getGlobal() {
253250
return typeof window === 'undefined' ? null : window;
254251
};
@@ -1010,7 +1007,7 @@ function createDOMPurify() {
10101007
return true;
10111008
}
10121009

1013-
/* Remove any ocurrence of processing instructions */
1010+
/* Remove any occurrence of processing instructions */
10141011
if (currentNode.nodeType === NODE_TYPE.progressingInstruction) {
10151012
_forceRemove(currentNode);
10161013
return true;
@@ -1179,6 +1176,13 @@ function createDOMPurify() {
11791176
hookEvent.forceKeepAttr = undefined; // Allows developers to see this is a property they can set
11801177
_executeHook('uponSanitizeAttribute', currentNode, hookEvent);
11811178
value = hookEvent.attrValue;
1179+
1180+
/* Work around a security issue with comments inside attributes */
1181+
if (SAFE_FOR_XML && regExpTest(/((--!?|])>)|<\/(style|title)/i, value)) {
1182+
_removeAttribute(name, currentNode);
1183+
continue;
1184+
}
1185+
11821186
/* Did the hooks approve of the attribute? */
11831187
if (hookEvent.forceKeepAttr) {
11841188
continue;
@@ -1198,12 +1202,6 @@ function createDOMPurify() {
11981202
continue;
11991203
}
12001204

1201-
/* Work around a security issue with comments inside attributes */
1202-
if (SAFE_FOR_XML && regExpTest(/((--!?|])>)|<\/(style|title)/i, value)) {
1203-
_removeAttribute(name, currentNode);
1204-
continue;
1205-
}
1206-
12071205
/* Sanitize attribute content to be template-safe */
12081206
if (SAFE_FOR_TEMPLATES) {
12091207
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], expr => {

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: 8 additions & 10 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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,13 @@ function createDOMPurify(window = getGlobal()) {
12801280
hookEvent.forceKeepAttr = undefined; // Allows developers to see this is a property they can set
12811281
_executeHook('uponSanitizeAttribute', currentNode, hookEvent);
12821282
value = hookEvent.attrValue;
1283+
1284+
/* Work around a security issue with comments inside attributes */
1285+
if (SAFE_FOR_XML && regExpTest(/((--!?|])>)|<\/(style|title)/i, value)) {
1286+
_removeAttribute(name, currentNode);
1287+
continue;
1288+
}
1289+
12831290
/* Did the hooks approve of the attribute? */
12841291
if (hookEvent.forceKeepAttr) {
12851292
continue;
@@ -1299,12 +1306,6 @@ function createDOMPurify(window = getGlobal()) {
12991306
continue;
13001307
}
13011308

1302-
/* Work around a security issue with comments inside attributes */
1303-
if (SAFE_FOR_XML && regExpTest(/((--!?|])>)|<\/(style|title)/i, value)) {
1304-
_removeAttribute(name, currentNode);
1305-
continue;
1306-
}
1307-
13081309
/* Sanitize attribute content to be template-safe */
13091310
if (SAFE_FOR_TEMPLATES) {
13101311
arrayForEach([MUSTACHE_EXPR, ERB_EXPR, TMPLIT_EXPR], (expr) => {

0 commit comments

Comments
 (0)