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

Added support for Trusted Types. #310

Merged
merged 4 commits into from
Oct 31, 2018
Merged
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
107 changes: 107 additions & 0 deletions demos/trusted-types-demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="trusted-types dompurify#demo sanitize-using-dompurify my-policy">
<script src="https://wicg.github.io/trusted-types/dist/es5/trustedtypes.build.js"></script>
<script src="../dist/purify.js" data-tt-policy-suffix="demo"></script>
<style>
div {
border: 1px solid green;
padding: 5px;
margin: 5px;
}
</style>
</head>
<body>
<a href="https://github.com/WICG/trusted-types">Trusted Types DOMPurify demo</a>
<div id="sanitized-directly"></div>
<div id="sanitized-via-policy"></div>
<div id="sanitized-via-policy2"></div>
<div id="raw"></div>
<form name="loadScript">
<input name="url">
<button type="submit">Load script</button>
</form>
<form name="loadImage">
<input name="url">
<button type="submit">Load image</button>
</form>

<script>

// Specify dirty HTML
var dirty = '<p>HELLO<iframe/\/src=JavScript:alert&lpar;1)></ifrAMe><br>goodbye</p>';

// Use DOMPurify directly.
document.getElementById('sanitized-directly').innerHTML = DOMPurify.sanitize(dirty);

// .. or wrap DOMPurify in TT policy itself.
const sanitizer = TrustedTypes.createPolicy('sanitize-using-dompurify', {
// This code block needs security review, as it's capable of causing DOM XSS.
createHTML(dirty) { return DOMPurify.sanitize(dirty) }
});

// You can use policies that don't use DOMPurify as well.
const myPolicy = TrustedTypes.createPolicy('my-policy', {
// This code block needs security review, as it's capable of causing DOM XSS.
createHTML(dirty) { return dirty.replace(/</g, '&lt;') },
createScriptURL(dirty) {
const u = new URL(dirty, document.baseURI);
if (u.origin == window.origin) {
return u.href;
}
throw new Error('Only same-origin scripts, please');
},
createURL(dirty) {
const u = new URL(dirty, document.baseURI);
if (['https:', 'http:'].includes(u.protocol)) {
return u.href;
}
throw new Error('Only http(s): URLs allowed');
},
});


document.getElementById('sanitized-via-policy').innerHTML = sanitizer.createHTML(dirty);

document.getElementById('sanitized-via-policy2').innerHTML = myPolicy.createHTML(dirty);

// Now you know that any DOM XSS* can only be caused by flaws in configured policies.
// (e.g. a bug in DOMPurify.sanitize can cause XSS, but nothing more).

// This will fail. No more DOM XSSes outside of policy definitions.
// Writing strings to DOM is only possible through policies.
try {
document.getElementById('raw').innerHTML = dirty;
} catch(e) {}

document.forms['loadScript'].onsubmit = (e) => {
e.preventDefault();
const url = e.target['url'].value;
const s = document.createElement('script');
try {
// This will fail, you need a TrustedScriptURL.
s.src = url;
console.log('Should not happen');
} catch(e) {};
s.src = myPolicy.createScriptURL(url);
console.log('Will load the script from ' + url);
document.body.appendChild(s);
};

document.forms['loadImage'].onsubmit = (e) => {
e.preventDefault();
const url = e.target['url'].value;
const i = document.createElement('img');
try {
// This will fail, you need a TrustedScriptURL.
i.src = url;
console.log('Should not happen');
} catch(e) {};
i.src = myPolicy.createURL(url);
console.log('Will load the image from ' + url);
document.body.appendChild(i);
};
</script>
</body>
</html>
65 changes: 56 additions & 9 deletions dist/purify.cjs.js

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

2 changes: 1 addition & 1 deletion dist/purify.cjs.js.map

Large diffs are not rendered by default.

65 changes: 56 additions & 9 deletions dist/purify.es.js

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

2 changes: 1 addition & 1 deletion dist/purify.es.js.map

Large diffs are not rendered by default.

Loading