Submit a enquiry






    Back to all posts

    Adding a Content Security Policy to your website

    Guide
    by Iain Thomson Project Lead

    One often overlooked but highly effective method of fortifying your website’s defences is by adding a Content Security Policy (CSP).

    What is Content Security Policy (CSP)?

    Content Security Policy (CSP) is a security standard that helps mitigate various types of attacks, such as Cross-Site Scripting (XSS) and data injection attacks, by defining and enforcing a set of rules for the resources that a web page can load. CSP works by allowing website owners to specify the origins from which certain types of content can be loaded, thereby reducing the risk of unauthorised code execution.

    Why Should You Add CSP to Your Website?

    • Mitigate Cross-Site Scripting (XSS) Attacks: XSS attacks remain one of the most common security vulnerabilities on the web. By implementing CSP, you can significantly reduce the risk of XSS attacks by specifying which scripts can be executed on your website, thereby preventing malicious scripts from being injected into your pages.
    • Prevent Data Injection Attacks: CSP helps prevent data injection attacks by restricting the origins from which resources such as images, scripts, stylesheets, and fonts can be loaded. This ensures that only trusted sources are allowed to load content, minimizing the risk of unauthorized data being injected into your web pages.
    • Protect User Privacy: With CSP, you can control the use of resources such as cookies, ensuring that sensitive user data is not exposed to unauthorised parties. By specifying which domains can set cookies, you can prevent third-party tracking and enhance user privacy.
    • Enhance Website Performance: While CSP primarily focuses on security, it can also help improve website performance by reducing the number of unnecessary HTTP requests and mitigating the impact of malicious or poorly optimized third-party scripts.
    • Compliance with Security Standards: Many security standards and frameworks, such as OWASP (Open Web Application Security Project) and GDPR (General Data Protection Regulation), recommend or require the implementation of CSP as part of a comprehensive security strategy. By adhering to these standards, you demonstrate a commitment to protecting your website and its users’ data.

    How to add a CSP to your WordPress site?

    You can add a plugin such as GD Security Headers which will help you add CSP headers to your website – but it can be confusing if you are not sure what you are doing, as some rules for example will block the loading of external CSS, JS/scripts and iFrame’s etc – so you can easily break your site. To counter this you would simply list out the websites and CDNs you trust in the policy, so for example Google Analytics, Google Ads (for ad scripts), Google Maps (for maps), YouTube (for embedded videos), JS CDN’s you may be using etc. An example finished script (to go in .htaccess) may look like this:

    Or an alternative way would be to add a Meta tag to your website head, a basic Meta tag with a basic CSP policy may look like:

    You can then validate your CSP policy with sites such as cspvalidator.org

    If you need any help beefing up your website security drop us a line 🙂