Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Trusted Types with DOMPurify #417

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
helix-importer-ui
helix-importer-ui
scripts/purify.min.js
4 changes: 4 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module.exports = {
env: {
browser: true,
},
globals: {
DOMPurify: 'readonly',
trustedTypes: 'readonly',
},
parser: '@babel/eslint-parser',
parserOptions: {
allowImportExportEverywhere: true,
Expand Down
6 changes: 6 additions & 0 deletions head.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<!-- Content Security Policies should ideally be moved as a header for better security -->
<meta
http-equiv="Content-Security-Policy"
content="require-trusted-types-for 'script'; trusted-types default aempolicy dompurify"
>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<script type="text/javascript" src="scripts/purify.min.js"></script>
<script src="/scripts/aem.js" type="module"></script>
<script src="/scripts/scripts.js" type="module"></script>
<link rel="stylesheet" href="/styles/styles.css"/>
9 changes: 9 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "1.3.0",
"description": "Starter project for Adobe Helix",
"scripts": {
"postinstall": "cp node_modules/dompurify/dist/purify.min.js scripts/purify.min.js",
"lint:js": "eslint .",
"lint:css": "stylelint blocks/**/*.css styles/*.css",
"lint": "npm run lint:js && npm run lint:css"
Expand All @@ -20,6 +21,7 @@
"homepage": "https://github.com/adobe/aem-boilerplate#readme",
"devDependencies": {
"@babel/eslint-parser": "7.25.1",
"dompurify": "3.1.7",
"eslint": "8.57.0",
"eslint-config-airbnb-base": "15.0.0",
"eslint-plugin-import": "2.29.1",
Expand Down
3 changes: 3 additions & 0 deletions scripts/purify.min.js

Large diffs are not rendered by default.

62 changes: 62 additions & 0 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,68 @@ async function loadLazy(doc) {
loadFonts();
}

/**
* Check if the browser supports Trusted Types (currently Google Chrome only)
* https://developer.mozilla.org/en-US/docs/Web/API/Trusted_Types_API
* Polyfill can be added from: https://github.com/w3c/trusted-types#polyfill
* @returns {boolean}
*/
function browserSupportsTrustedTypes() {
try {
return window.trustedTypes && trustedTypes.createPolicy;
} catch (e) {
return false;
}
}

const TRUSTED_SCRIPT_ORIGINS = [
window.location.origin,
// add more trusted origins here
];

/* eslint-disable no-unused-vars, import/no-mutable-exports */
/* Default Trusted Types policy applies for all Javascript code on the page,
* including libraries loaded
*/
let defaultPolicy = {
createHTML: (s, type, sink) => DOMPurify.sanitize(s),
createScriptURL: (s, type, sink) => {
if (!s) return undefined;
const canonical = s.startsWith('//') ? `https:${s}` : s;

try {
if (TRUSTED_SCRIPT_ORIGINS.includes(new URL(canonical).origin)) {
return s;
}
} catch (e) {
// no-op
}

// eslint-disable-next-line no-console
console.warn(`Blocked script from ${s} by default policy`);
return undefined; // reject
},
createScript: (s, type, sink) => {
// disallow inline scripts.
// eslint-disable-next-line no-console
console.warn('Blocked inline script by default policy');
return undefined;
},
};

/* Define your custom Trusted Types policy here */
export let aemPolicy = {
createHTML: (s, type, sink) => undefined,
createScriptURL: (s, type, sink) => undefined,
createScript: (s, type, sink) => undefined,
};

if (browserSupportsTrustedTypes()) {
defaultPolicy = trustedTypes.createPolicy('default', defaultPolicy);
aemPolicy = trustedTypes.createPolicy('aempolicy', aemPolicy);
}
/* eslint-enable no-unused-vars, import/no-mutable-exports */

/**
* Loads everything that happens a lot later,
* without impacting the user experience.
Expand Down