CSP useful, a collection of scripts, thoughts about CSP
I'm testing and using CSP (Content Security Policy), and here are some thoughts, resources, scripts and ideas on it.
In folder "report-uri", you may find examples of CSP parsers you can use for report-uri.
- csp-parser-basic.php : the most basic one, it sends an e-mail.
- csp-parser-enhanced.php : avoids some bugs (listed below as CSP WTF), with a LOT of filters
- csp-parser-with-database.php : put notifications in a database, then you can do whatever you want with all these informations! :)
- csp-parser-with-database-pdo.php : also puts CSP notifications in a database but uses PDO instead of the mysqli extension.
CSP directives for third-party services
In folder "CSP for third party services", you may find examples of directives you need to use for some services.
CSP Check folder
In folder "csp-check", you may find the source of a proof of concept: this script was a quick and dirty way to reproduce a bug in Firefox, you can see it in action here: https://csp.nicolas-hoffmann.net/
Basically, the page generates an unique id, notifications sent to report-uri are put in database, the page makes an AJAX call to database, and the unique id helps to find CSP errors in database.
This is useful to prove bugs, not only for Firefox. ^^
To reproduce the bug:
- Open https://csp.nicolas-hoffmann.net/
- The page is going to generate a unique id, ex https://csp.nicolas-hoffmann.net/?id=foo
- Wait some seconds. The page doesn't find any notification in the database.
- Now inspect the page with Firefox inspector, please highlight some elements.
- Close the inspector
- Refresh the page with the id you have : https://csp.nicolas-hoffmann.net/?id=foo
- It is going to find a lot of CSP errors.
At the beginning, I've made it to prove that some Chrome extensions are sending notifications to report-uri (while they should not), and it helped to find/prove a bug in Firefox Inspector.
Here is the reported bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1195302
should be is fixed with Firefox 42 https://bugzilla.mozilla.org/show_bug.cgi?id=1185351 :)
In folder "CSP WTF", you may find examples of strange notifications you may receive. Feel free to add/explain some.
Now the list is splitted in two, explained or not yet explained notifications.
Small tips and tricks
Be careful if you have multiple domain names (foo.com, foo.net) pointing to a single website while using
'self' as value. Example: if a user is using a full url for an image, let's say
'self' won't be enough if the user is on foo.net. Be sure to allow all necessary domains.
Generate a hash
If you really have to use some inline scripts/css, for example:
You might add
'sha256-qznLcsROx4GACP2dm0UCKCzCG-HiZ1guq6ZZDob_Tng=' as valid source in your
script-src directives. The hash generated is the result of:
base64_encode(hash('sha256', "alert('Hello, world.');", true))
in PHP for example.
Chrome PDF viewer blocking
According to Philippe De Ryck, setting CSP's
'none' blocks Chrome's PDF viewer. As he says, "unless you're hosting vulnerable flash files yourself, go with 'self', and ignore the warning on the CSP Evaluator".
According to, Safari’s default media controls get blocked when applying a Content-Security-Policy, see https://www.ctrl.blog/entry/safari-csp-media-controls (hint, allow
What CSP is really good for
I use CSP to clean up some bad old contents (with inline-styles for example).
- Just activate CSP on a site with a report-uri
- Ask your boss/collegues/grandma to browse the website
- All notifications will come without doing anything (yes, I’m lazy)
- Yay, you know where you have to make some cleanup
Moreother, if you don't have the time to clean it, setting up CSP policy will avoid bad old styles from breaking the nice/clean new design. Or it will tell you when contributors are doing shit on the website.
To migrate a website to HTTPS
You might read how the Guardian moved to HTTPS using CSP: https://www.theguardian.com/info/developer-blog/2016/nov/29/the-guardian-has-moved-to-https
Progressive enhancement and orthogonality
So it is a great help for progressive enhancement, orthogonality and clean front-end.
How to see easily CSP directives on a website
For Firefox: make Maj+F2 and type "security csp". It will show you directives and advices.
If you have webdevelopper toolbar, go into infos - HTTP headers.
JS/jQuery plugins should provide the CSP requirements they need to work (especially inline-styles or inline-js), so:
- we will know what they need instead of having to discover it
- we would be able to choose a plugin according to its capabilities to respect orthogonality (see https://openweb.eu.org/articles/orthogonality-with-css)
Bugs I've found
Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1195302 (inspector, extensions?)(fixed in Firefox 42)
- Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1215108 (bookmarklets)
Chrome/Blink : https://code.google.com/p/chromium/issues/detail?id=524356 (extensions)(fixed in last versions of Chrome)
- Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=595004 (bookmarklets too, see too https://bugs.chromium.org/p/chromium/issues/detail?id=233903)
- Safari/Webkit : https://bugs.webkit.org/show_bug.cgi?id=149000 (extensions)
Edge (no URL tracker yet, same workaround than for Firefox)(seems to be fixed on Edge)
- Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1445643 (extension triggers CSP violation using base-uri directive)
- Chrome: https://tests.borisschapira.com/upgrade-insecure-requests/ (about
upgrade-insecure-requests; block-all-mixed-content, bugs on Chrome 615885, 617584
- https://openweb.eu.org/articles/content-security-policy (in french)
- https://openweb.eu.org/articles/content-security-policy (in french)
- https://www.nicolas-hoffmann.net/content-security-policy-parisweb-2015 (slides of my talk in Paris Web 2015, in french)
- dotSecurity 2016 - Scott Helme - Content Security Policy: The application security Swiss Army Knife (video)
- InfoShare 2016 - Scott Helme - Content Security Policy: The application security Swiss Army Knife (video)
- Paris Web 2015 - Nicolas Hoffmann - Content Security Policy (video in french-speaking)
- Codeurs en Seine 2016 - Nicolas Hoffmann - Content Security Policy (video in french-speaking)
- Paris Web 2017 - slides of a workshop in french
- Tour d´horizon HTTPS et en-têtes de sécurité (in french)
CSP with Google
About collecting and filtering reports
- CSP implementations are broken
- CSP report for Netlify
Why you should use CSP
- CSS Exfil vulnerability
- CSS Exfil vulnerability, POCs
- CSS used as keylogger
- Disqus started injecting adverts into pages that use their comment system, blocked by CSP :)
- Protect your site from Cryptojacking with CSP + SRI
- Content Security Policy - LinkedIn as an Example!
Interesting posts on how to deploy CSP
- Déployer CSP : une approche en 5 étapes (in french)
- Deploying CSP: a 5-step approach
- https://github.com/nico3333fr/CSP-useful (yes you are here, and yes, it was stupid)
Future of CSP
Online tools that test CSP
- https://observatory.mozilla.org/ (with CSP Analyzer to help you understand your policy)