Skip to content

Commit

Permalink
Support sanitization of attributes that require Trusted Types
Browse files Browse the repository at this point in the history
  • Loading branch information
tosmolka committed Jul 18, 2022
1 parent 52c8eb1 commit 4ec6d6f
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/purify.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ const _createTrustedTypesPolicy = function (trustedTypes, document) {
createHTML(html) {
return html;
},
createScriptURL(scriptUrl) {
return scriptUrl;
},
});
} catch (_) {
// Policy creation failed (most likely another DOMPurify script has
Expand Down Expand Up @@ -1202,6 +1205,28 @@ function createDOMPurify(window = getGlobal()) {
continue;
}

/* Handle attributes that require Trusted Types */
if (
trustedTypesPolicy &&
typeof trustedTypes === 'object' &&
typeof trustedTypes.getAttributeType === 'function'
) {
if (namespaceURI) {
/* Namespaces are not yet supported, see https://bugs.chromium.org/p/chromium/issues/detail?id=1305293 */
} else {
switch (trustedTypes.getAttributeType(lcTag, lcName)) {
case 'TrustedHTML':
value = trustedTypesPolicy.createHTML(value);
break;
case 'TrustedScriptURL':
value = trustedTypesPolicy.createScriptURL(value);
break;
default:
break;
}
}
}

/* Handle invalid data-* attribute set by try-catching it */
try {
if (namespaceURI) {
Expand Down

0 comments on commit 4ec6d6f

Please sign in to comment.