WordPress Content Security Policies
Table of Contents
The Problem
Brian Krebs wrote an informative article about an attack on the Optimizely service that caused many web sites to push hostile Javascript [1]. This concerns me because I have a number of WordPress plugins running on my blog and it’s not practical to track what they are all doing.
What I Did
I set a Content Security Policy as described in the Mozilla docs [2].
As I already had the Apache headers module installed I should have been able to set the policy with the following in /etc/wordpress/htaccess:
Header set Content-Security-Policy "default-src 'self'"
I used the program HEAD from the Debian package libwww-perl to see the headers that were being set and that wasn’t in it. I don’t know why it didn’t work and would appreciate any comments making suggestions. I’m not particularly bothered about this as I found a solution that is better for my needs, but the top Google results weren’t helpful and it would be good if the information was out there if anyone already knows how to solve this.
To set the header I put the following in /etc/wordpress/config-etbe.coker.com.au.php:
header("Content-Security-Policy: default-src 'self'");
I tested that with the HEAD command and it was set. I tested it by loading a blog post that had images sourced from www.coker.com.au and the images weren’t loaded according to the policy.
So then I changed the line in the configuration file to the following and it worked and allowed my images.
header("Content-Security-Policy: default-src 'self'; img-src 'self' www.coker.com.au");
Did I Solve This?
find /usr/share/wordpress/wp-content -name "*.js"|wc -l
Currently on my blog server the above command shows that there are 1299 Javascript files included in themes and plugins. If one of them has hostile code then it can just run things in the browser when someone visits my site. So if a hostile party gains access to release an update to one of the plugins or themes and I install the update then the code runs.
A hostile party wants their code to be concealed and putting a large piece of malware in a package will be easier to spot. Also Krebs documented the malware authors as running A/B tests which also requires taking code from outside sources.
The Content-Security-Policy header doesn’t stop hostile code being run if it was installed on the server but it does prevent that code from calling out to other code from other servers. This means that it will be harder for attackers to hide code which is a benefit for defenders. If an attacker aims for the common case of this header not being used then their malware will work on most sites but not on mine which is a win for me. When protecting against large scale attacks you often only need to be slightly more secure than most sites.
If most sites do this then attackers will include all the malware inline which means that my site will be vulnerable but also means that the malware will be easier to spot. So that is still a win for defenders.
I don’t think I have completely solved a problem, but I think I have improved things and that others will benefit from doing the same thing.
Future Investigation
The site securityheaders.com [3] is of a similar nature to the more well known ssltest [4] site. According to securityheaders.com my site only gets a “C” rating so there’s work to do there.
Is there a good way of using free software to automatically check Javascript for hostile code? A google search has turned up a few options but it will take some time to investigate them even after ruling one out for being non-free.