Skip to content

Instantly share code, notes, and snippets.

@april
Last active October 5, 2017 20:53
Show Gist options
  • Save april/a02a6338a62823b8ba29b174f587e426 to your computer and use it in GitHub Desktop.
Save april/a02a6338a62823b8ba29b174f587e426 to your computer and use it in GitHub Desktop.
strict-dynamic
Note that instead of using a unique nonce on each page load, you could instead
have the sha256 hash of the contents of that inline script and be completely static.
> Content-Security-Policy: script-src 'strict-dynamic' 'nonce-abcdef123467890' 'unsafe-inline' https:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<script nonce="abcdef1234567890">
var shim = document.createElement('script');
shim.src = '/scripts/shim.js';
document.head.appendChild(shim);
</script>
</body>
</html>
This will tell the browser to trust shim.js and any other scripts that get inserted
into the page by shim.js or scripts that shim.js or its children loads If the browser understands
'strict-dynamic' (currently Chrome and Firefox), then you get XSS protections despite
the presence of 'unsafe-inline'. If it doesn't understand 'strict-dynamic', then it operates
by allowing 'unsafe-inline', the same as it works now.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment