DEV Community

Cover image for What is CSP? Why & How to Add it to Your Website.

What is CSP? Why & How to Add it to Your Website.

Matt Ferderer on May 13, 2018

Cross-Site Scripting (XSS) sucks! XSS is when someone sneaks JavaScript or CSS into your site through a comment, a form, an advertisement or a NPM ...
Collapse
 
aidantwoods profile image
Aidan Woods • Edited

CSP fan here :)

Some additional notes:

Shameless plug to a library that'll help with CSP and other security headers if you use PHP :) SecureHeaders.

Please please please do not use unsafe-inline for scripts (unless*), it completely bypasses any XSS protection you might hope to achieve. unsafe-inline in style isn't great either.

(*unless) unsafe-inline is okay if you use if for compatibility purposes: providing a nonce-* or hash (e.g. sha256-*) will disable the effect of unsafe-inline in browsers that support CSP2 (almost everyone these days).

For extra points, consider using strict-dynamic in your script policy to disable the script whitelist (on CSP3 compatible browsers) – strict-dynamic will only permit nonced or hashed scripts to run, but will allow them to "bootload" other scripts from other origins. This get's around the problem of an attacker bypassing your CSP via a domain you whitelist if that domain allows user hosted content/has reflection endpoints.
A good whitelist can be stronger than using strict-dynamic, however this is hard to get right. Not using a whitelist for scripts is generally a good idea because whitelists tend to be too permissive in practice: Google research, which found bypasses in 94.72% of distinct observed CSP policies.

For additional extra points, specify two CSPs (multiple CSPs permit only resources that are allowed by all of them): one CSP with strict-dynamic in it, the other CSP with a whitelist – this will only allow strict-dynamic type requests to be made the whitelisted domains. This is the best of both worlds :D

Once you've settled on a CSP, strongly consider running it though Google's CSP Evaluator, which will do a good job a pointing out any shortcomings, including missing (but important directives: e.g. don't forget to specify object-src 'none' – this doesn't inherit from default-src). It'll also point out any known bypasses that occur due to your whitelisted domains.

Collapse
 
mattferderer profile image
Matt Ferderer

Thanks for reading & taking time to give an excellent reply. Those are all excellent points & I hope people give them a try after getting a basic CSP setup & working. I personally did not know about using two CSPs. I will for sure give that a test.

We need a lot more a lot more CSP fans so fellow developers & frameworks start coding with CSP & security in mind. The biggest hurdle I am currently finding is getting CSP to work with large open source frameworks that change JavaScript on build or inject JavaScript & CSS styles.

Collapse
 
aidantwoods profile image
Aidan Woods

The biggest hurdle I am currently finding is getting CSP to work with large open source frameworks that change JavaScript on build or inject JavaScript & CSS styles.

For scripts, can 'strict-dynamic' help? It'll permit even non-external scripts being added into the document by nonced/hashed scripts so-long as they are not "parser inserted". e.g. a nonced script would be permitted to insert a script into the DOM via something like document.head.appendChild (but not via document.write).

For styles (and perhaps some particular scripts) 'unsafe-hashed-attributes' in scripts and style may be worth looking into (once it's finished). The idea is to allow things like:

<div style="color:red" onclick="foobar()"></div>

to be compatible with CSP (provided you know ahead of time what the attribute will be). I believe the current proposal is to hash the content of the attribute, so something like <img onerror="foobar()"> would have the same script hash as above (even though the attribute and element is different). For this reason it'll be possible to abuse these in certain situations e.g. consider if the following were legitimate code on the page, whitelisted by attribute

<a onclick="deleteAccount()">Delete account</a>

An attacker could then inject

<img src=# onerror="deleteAccount()" />

and have it execute on pageload.

That said, having to "be careful" with 'unsafe-hashed-attributes' is certainly a preferable approach to 'unsafe-inline', which essentially says "run all the things" :)

Thread Thread
 
mattferderer profile image
Matt Ferderer

Using strict-dynamic is an excellent choice when possible. It is something I should investigate closer. A lot of my front ends are static sites, so that brings some challenges there.

Collapse
 
hnhegde profile image
Harsha N Hegde

This is a great approach to building a CSP. Thanks for introducing report-uri. I was thinking of building something on that line! I don't need to!

Collapse
 
rognoni profile image
Rognoni

What is the way to remove it?

Refused to connect to 'https://raw.githubusercontent.com/rognoni/monastic-browser/master/README.md' 
because it violates the following Content Security Policy directive: "
default-src 'self' 'unsafe-eval' 'unsafe-inline' 
data: blob: archive.org web.archive.org analytics.archive.org pragma.archivelab.org". 
Note that 'connect-src' was not explicitly set, so 'default-src' is used as a fallback.

web.archive.org/web/20190601081552...

Collapse
 
mattferderer profile image
Matt Ferderer

You have to disable it in your browser via a config file or extension of some sort. I would suggest searching for "disable browser csp nameOfYourBrowser" to find directions for your specific browser.

Collapse
 
rognoni profile image
Rognoni • Edited

Yes, I found this Chrome extension (but cross-browser would be better)
github.com/PhilGrayson/chrome-csp-...

This is another similar problem with CORS policy

Access to XMLHttpRequest at 'https://monastic.neocities.org/index.md' 
from origin 'https://monastic.netlify.com' has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Collapse
 
aaliyahc profile image
Aaliyahc

What about a WordPress website on shared hosting, adding that code brings 503 error when you reload the site!!
Also adding this code has the same effect, but not always, sometimes it interferes with the theme builder and damages the appearance of my site

Header set Content-Security-Policy "default-src 'self';"

What should I do?

Collapse
 
chan_austria777 profile image
chan 🤖 • Edited

If you develop an SPA app w/c only renders a single page. Isn't it enough to just use <meta> tags for csp directives? When to use meta tags vs server response CSP headers?