|
| 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