Why it matters
- A Content Security Policy (CSP) tells the browser where the page may load scripts, styles, images and other resources from, and the browser refuses the rest. Its main use is against cross-site scripting (XSS): a script someone slips into the page, through a comment or a search field, does not run when the policy does not allow it.
- Without a policy, the browser runs every script that ends up in the page.
- OWASP calls it a layer on top of the other defences, not a replacement: pages still need to escape what visitors write.
Content-Security-Policy-Report-Onlyis for trying a policy out: the browser reports what it would block, and blocks nothing.
Example
<meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="utf-8" /> <meta http-equiv="Content-Security-Policy" content="default-src 'self'" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
How to fix
Send a policy for what your pages really load. OWASP's basic policy suits a site whose resources all come from its own domain, with no inline code:
http
Content-Security-Policy: default-src 'self'; frame-ancestors 'self'; form-action 'self'- Apache (mod_headers):
Header always set Content-Security-Policy "default-src 'self'; frame-ancestors 'self'; form-action 'self'" - nginx:
add_header Content-Security-Policy "default-src 'self'; frame-ancestors 'self'; form-action 'self'" always; - In nginx, a
locationblock with anadd_headerof its own does not inherit those of theserverblock, so repeat the header there. - Cloudflare: a Response Header Transform Rule that sets the header.
- Without access to the server, put the policy in a
<meta http-equiv="Content-Security-Policy">early in<head>. It covers only what comes after it, and it cannot setframe-ancestors,report-uriorsandbox. - A page that loads scripts from other sites, or has inline scripts, needs those sources in its policy: send it as
Content-Security-Policy-Report-Onlyfirst, read what it would block, then send it asContent-Security-Policy.
How we detect
- The rule applies to HTML pages that answer 2xx on a public site. Local development hosts, such as
localhost, private addresses and names ending in.test, are left out, as for the HTTPS rule: the policy protects a public site's visitors. - We read the
Content-Security-Policyheaders as browsers do (CSP Level 3): a header may hold several policies separated by commas, a policy counts when it has at least one directive, directive names are read whatever their case, and a directive with non-ASCII characters is skipped. - A
<meta http-equiv="Content-Security-Policy">counts inside<head>, lessframe-ancestors,report-uriandsandbox, which browsers drop from a<meta>. - A
Content-Security-Policy-Report-Onlyheader does not count, since it blocks nothing. - We check that a policy is enforced, not what it allows: a loose policy passes this rule, and so does one without
default-srcorscript-src, which limits no scripts at all.