
Talking tech, Facebook and data security
Dive deeper into this topic with our insight: Talking tech, Facebook and data security.

If you are unfamiliar with Content Security Policies (CSPs), then you can find out more info on the Mozilla site.
A Content Security Policy is probably the best defence that you can introduce to your web services. They provide a high return on time invested as they are straightforward to implement when you know what you are doing and provide protection against a wide variety of attacks. A poorly-configured CSP is likely to provide at least some protection but obviously a properly-configured CSP is much more effective at securing your site.
We are going to discuss a few common mistakes that people make when adding a CSP to their site and how to avoid them.
Report-only mode is really useful for introducing a content security policy to an existing site but it should only be used for a short amount of time to ensure no parts of your site are broken by your CSP. In report-only mode, your CSP will merely report violations. It will not enforce your configured rules and so it offers little to nothing in the way of security.
It should be clearly planned beforehand how long your CSP will be left in this mode so it doesn’t get forgotten. When you are ready to turn off ‘report-only’ mode, it’s essential that your CSP is checked in your development environments with ‘report-only’ turned off so that there are no issues when it gets to your production environment.
Allowing ‘Unsafe-inline’ for script sources on its own is a great way to reduce the effectiveness of your CSP. Many sites add this directive to stop it breaking their site but adding the directive removes one of the key defences from your CSP which is that it only allows scripts that you have whitelisted to run.
It is called ‘unsafe-inline’ for a reason! Why add a CSP if you’re not going to allow it to do anything?
If you specify ‘unsafe-inline’ on its own then your CSP will not block any scripts that an attacker manages to inject into your site.
There are 3 options if you currently have inline scripts:
The same issues and options also apply with regards to inline styles.
If no rules are specified for a particular directive then all sources are allowed. I.e. If you don’t specify ‘font-src’ then it’s the equivalent of adding the directive ‘font-src: *’. This means it is important to be aware of all the directives that can be specified so that you can restrict them appropriately.
The ‘default-src’ directive allows you to specify the default. In general, the ‘default-src’ can be used as a fallback for all directives that have ‘-src’ at the end (complete list of directives that ‘default-src’ applies to). ‘Default-src’ won’t be applied to the following directives:
These directives need to be explicitly set, if they aren’t, there are various attacks that can exploit them; the lack of a ‘base-uri’ directive is exploited in base tag hijacking attacks. If your site doesn’t make use of a resource type then you should still add a directive for it with ‘none’ specified.
An issue sometimes encountered when creating CSPs is that often assets such as fonts are loaded using a data url and you get an error like the one below.
The simple solution, as you can read on this stack overflow article, is of course to update the ‘font-src’ directive on your CSP to: ‘font-src: self https: *.gstatic.com data:’
WRONG! Turns out Stack Overflow doesn’t always have the correct answer! Whilst adding the ‘data:’ directive to your whitelist of allowed sources will stop them being blocked, you will also be creating a large security hole within your CSP’s defences as it will now allow any font to be loaded using any data URL.
Probably the simplest solution is just to not use data URLs at all but if you need to use them for some reason then you should specify the exact data URL rather than allowing any URLs that match the pattern ‘data:’
There is no silver bullet when it comes to web security but implementing a CSP correctly is a useful way to greatly reduce the attack surface for malicious visitors. There are many security flaws that a CSP won’t help to prevent such as logic-based authentication flaws and that’s why it’s important to be able to trust a system’s architects and developers to create it securely.
At Razor, we create systems that are secure by design and use open source projects such as NWebSec to keep up to speed with the ever-changing security requirements. We then implement the appropriate checks to continually monitor them.
If you need any help or just want to check that you have the right security in place, not just security headers but across your entire application and infrastructure, get in touch, we can help.