Content Security Policy
Content Security Policy
Content Security Policy
Example6-XSS-contentSecurityPolicy.php
CONTENT SECURITY POLICY
Content-Security-Policy: script-src 'self' https://apis.google.com
Separated by commas
CONTENT SECURITY POLICY
font-src form-action
object-src style-src
plugin-types
media-src
connect-src
CONTENT SECURITY POLICY
When no Content-Security-Policy
is specified for a directive it’s like
the gate is wide open
default-src
CONTENT SECURITY POLICY
default-src
default-src http://www.mysite.com
default-src http://www.mysite.com
script-src http://mysite.com:8080
script-src http://mysite.com:*
script-src http://*.mysite.com
script-src https://*
Example7-XSS-disallowInline.php
Example7-XSS-disallowInline.js
STAY AWAY FROM INLINE CODE
Content-Security-Policy places a
blanket ban on all inline scripts
no inline <script> tags
no inline event handlers
no javascript: scripts
STAY AWAY FROM INLINE CODE
<script>
function handleButtonClick() {
alert("You clicked the button!");
}
</script>
<button onclick="handleButtonClick()"> Click me! </button>
Example8-XSS-nonce.php
THE NONCE ATTRIBUTE
<?php
$nonce = sha1(uniqid('n', true));
header("Content-Security-Policy: script-src 'nonce-$nonce'");
?>
<!DOCTYPE html>
<html lang="en">
Generate a nonce
<head>
<meta charset="UTF-8">
<title>XSS - Nonce</title>
</head>
<body>
<script nonce=<?php echo $nonce ?>>
window.onload = doSomething();
function doSomething() {
for every response
alert("This alert is inline with the nonce");
}
</script>
</body>
</html>
THE NONCE ATTRIBUTE
<?php
$nonce = sha1(uniqid('n', true));
header("Content-Security-Policy: script-src 'nonce-$nonce'");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>XSS - Nonce</title>
}
page is requested
function doSomething() {
alert("This alert is inline with the nonce");
</script>
</body>
</html>
THE NONCE ATTRIBUTE
<?php
$nonce = sha1(uniqid('n', true));
header("Content-Security-Policy: script-src 'nonce-$nonce'");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>XSS - Nonce</title>
</head>
<body>
Include the nonce in the Content-
<script nonce=<?php echo $nonce ?>>
window.onload = doSomething();
}
Security-Policy header
function doSomething() {
alert("This alert is inline with the nonce");
</script>
</body>
</html>
THE NONCE ATTRIBUTE
<?php
$nonce = sha1(uniqid('n', true));
header("Content-Security-Policy: script-src 'nonce-$nonce'");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>XSS - Nonce</title>
Now the inline
script should work!
</head>
<body>
<script nonce=<?php echo $nonce ?>>
window.onload = doSomething();
function doSomething() {
alert("This alert is inline with the nonce");
}
</script>
</body>
</html>
THE NONCE ATTRIBUTE
<?php
?>
$nonce = sha1(uniqid('n', true));
header("Content-Security-Policy: script-src 'nonce-$nonce'"); Remove the
<!DOCTYPE html>
<html lang="en">
<head>
nonce from the
inline script and
<meta charset="UTF-8">
<title>XSS - Nonce</title>
</head>
<body>
<script nonce=<?php echo $nonce ?>>
window.onload = doSomething();
function doSomething() {
you’ll find that it
}
alert("This alert is inline with the nonce");
</script>
does not execute
</body>
</html>
THE NONCE ATTRIBUTE
<?php
$nonce = sha1(uniqid('n', true));
header("Content-Security-Policy: script-src 'nonce-$nonce'");
?>
<!DOCTYPE html>
<html lang="en">
<head>
Generate a nonce for
every response, the
<meta charset="UTF-8">
<title>XSS - Nonce</title>
</head>
}
alert("This alert is inline with the nonce");
</script>
simple or guessable
</body>
</html>
THE SCRIPT HASH
THE SCRIPT HASH
Example9-XSS-hash.php
THE SCRIPT HASH
<script>
window.onload = doSomething();
function doSomething() {
alert("This alert is inline with the hash");
}
</script>
Content-Security-Policy simply
blocks this completely
STAY AWAY FROM EVAL AS WELL
setTimeout(
“document.getElementById('some-id').style.display = 'none';",
10);
setTimeout(
“document.getElementById('some-id').style.display = 'none';",
10);