Skip to content

Commit 7b4312b

Browse files
author
Weedshaker
committed
trustedTypes policy added
1 parent 099b947 commit 7b4312b

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/Shadow.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -774,14 +774,20 @@ export const Shadow = (ChosenHTMLElement = HTMLElement) => class Shadow extends
774774
* Avoid XSS attacks by sanitizing the html according to: https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/XSS
775775
* and the target list: https://github.com/cure53/DOMPurify/blob/27e8496bcd689a16acc7d0bf7c88b933efad569a/demos/hooks-mentaljs-demo.html#L20
776776
* plus: https://stackoverflow.com/questions/6976053/xss-which-html-tags-and-attributes-can-trigger-javascript-events
777-
* conclusion: stackoverflow citation: "I didn't knew about those new attributes. I checked, and it seems that the only attributes that start with on are all Javascript event triggers. I will probably just remove all that match that pattern."
777+
* stackoverflow citation and conclusion: "I didn't knew about those new attributes. I checked, and it seems that the only attributes that start with on are all Javascript event triggers. I will probably just remove all that match that pattern."
778778
* NOTE: script tags are already automatically escaped by modern browsers, so we only target <image, <img starting tags and "javascript:"
779779
*
780780
* @static
781781
* @param {string} html
782782
* @return {string}
783783
*/
784784
static htmlPurify (html) {
785+
// Do not purify the html, if there is already a TrustedTypePolicy, typically found in th environment: https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicyFactory
786+
// @ts-ignore
787+
if (self.trustedTypes?.defaultPolicy) {
788+
Shadow.htmlPurify = string => string
789+
return html
790+
}
785791
// first sanitize tags eg.: <img src="xyz" onload=alert('XSS')>, <img src="xyz" onmouseover=alert('XSS')>, <image/src/onerror=alert('XSS')>, etc.
786792
// second sanitize tags eg.: <a href="javascript:alert(document.location);">XSS</a>, <form action="javascript:alert(document.location);"><input type="submit" /></form>, etc.
787793
return html.replace(/<[a-z]*[\s|\/][^>]*on[a-z]{4,10}=[^>]*>/gi, '').replace(/<[a-z]*[\s|\/][^>]*javascript:[^>]*>/gi, '')

src/helpers/Environment.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* global self */
2+
/* global location */
3+
4+
const currentScriptUrl = new URL(document.currentScript.src)
5+
6+
// @ts-ignore
7+
self.Environment = {
8+
isTestingEnv: location.hostname === 'localhost',
9+
language: currentScriptUrl.searchParams.get('language') || document.documentElement.getAttribute('lang') || 'en',
10+
stage: currentScriptUrl.searchParams.get('stage') || document.documentElement.getAttribute('stage') || 'alpha',
11+
version: currentScriptUrl.searchParams.get('version') || document.documentElement.getAttribute('version') || '6.1.0', // https://semver.org/
12+
/**
13+
* Get custom mobile breakpoint
14+
* @param {{constructor?: string, tagName?: string, namespace?: string}} organism
15+
* @return {string}
16+
*/
17+
mobileBreakpoint: ({ constructor, tagName, namespace } = {}) => {
18+
switch (true) {
19+
default:
20+
return '767px'
21+
}
22+
}
23+
}
24+
25+
/**
26+
* XSS Content Security Policy
27+
*
28+
* https://content-security-policy.com/examples/meta/
29+
* is enforced by: <meta http-equiv="Content-Security-Policy" content="require-trusted-types-for 'script'">
30+
*
31+
* Sink uses trusted type only: https://web.dev/articles/trusted-type
32+
* Avoid XSS attacks by sanitizing the html according to: https://developer.mozilla.org/en-US/docs/Web/Security/Attacks/XSS
33+
* and the target list: https://github.com/cure53/DOMPurify/blob/27e8496bcd689a16acc7d0bf7c88b933efad569a/demos/hooks-mentaljs-demo.html#L20
34+
* plus: https://stackoverflow.com/questions/6976053/xss-which-html-tags-and-attributes-can-trigger-javascript-events
35+
* stackoverflow citation and conclusion: "I didn't knew about those new attributes. I checked, and it seems that the only attributes that start with on are all Javascript event triggers. I will probably just remove all that match that pattern."
36+
* NOTE: script tags are already automatically escaped by modern browsers, so we only target <image, <img starting tags and "javascript:"
37+
*
38+
* @static
39+
* @param {string} html
40+
* @return {string}
41+
*/
42+
if (typeof self.trustedTypes?.createPolicy === 'function' && document.querySelector('meta[http-equiv=Content-Security-Policy][content*=require-trusted-types-for]')) {
43+
self.trustedTypes.createPolicy('default', {
44+
// first sanitize tags eg.: <img src="xyz" onload=alert('XSS')>, <img src="xyz" onmouseover=alert('XSS')>, <image/src/onerror=alert('XSS')>, etc.
45+
// second sanitize tags eg.: <a href="javascript:alert(document.location);">XSS</a>, <form action="javascript:alert(document.location);"><input type="submit" /></form>, etc.
46+
createHTML: string => string.replace(/<[a-z]*[\s|\/][^>]*on[a-z]{4,10}=[^>]*>/gi, '').replace(/<[a-z]*[\s|\/][^>]*javascript:[^>]*>/gi, ''),
47+
createScriptURL: string => string, // unsafe but including webworker's, service workers, etc. is okay
48+
createScript: string => string // unsafe but eval at css templates is okay
49+
})
50+
}

0 commit comments

Comments
 (0)