Home / Blog / Cloud, DevOps and Security

Content Security Policy (CSP): Protect Your Site from XSS & Data Injection Attacks

What is Content Security Policy (CSP)?

Content Security Policy (CSP) is a security feature that helps protect websites against certain types of attacks, such as cross-site scripting (XSS) and data injection attacks. It is an additional layer of defense that can be implemented by website owners to mitigate the risks associated with malicious code execution.

Why should Content Security Policy headers be set up correctly for a website?

When a Content Security Policy is set up, it specifies the allowed sources from which various types of content can be loaded on a web page. This includes JavaScript, CSS stylesheets, images, fonts, and other types of resources. By defining a policy, website owners can restrict the origins from which content can be loaded, thereby reducing the risk of unauthorized or malicious code execution.

Content Security Policy (CSP)

What types of attacks does Content Security Policy help mitigate?

Setting up a Content Security Policy for a website is important for several reasons:


  • Mitigating Cross-Site Scripting (XSS) attacks: XSS attacks involve injecting malicious scripts into a website, which can then be executed by unsuspecting users. CSP can prevent such attacks by restricting the execution of scripts to trusted sources.



  • Protecting against data injection attacks: Content Security Policy can limit where a page can send data and which scripts and frames it loads, which reduces the impact of injected content. It does not replace server-side input validation.



  • Enhancing the security of third-party content: Many websites include content from external sources, such as advertising networks or social media plugins. By defining a Content Security Policy, website owners can control which external sources are allowed to load content, reducing the risk of compromised or malicious content.


Which browsers support Content Security Policy?

All current versions of Chrome, Firefox, Safari, Edge and Opera support Content Security Policy Levels 1 and 2 and most of Level 3. For directive-by-directive support, check the Content Security Policy pages on MDN Web Docs.

How to define Content Security Policy (CSP) for the website?

Defining a Content Security Policy (CSP) for a website can be done by configuring the appropriate HTTP headers on the server. The method for setting up CSP headers may vary depending on the server you are using. Here are examples for some common servers:

Before you enforce a policy, try it with the Content-Security-Policy-Report-Only header. The browser then reports what the policy would block without blocking anything, so you can fix the policy before it breaks the live site.

1. Apache HTTP Server (mod_headers)

If you’re using Apache HTTP Server with the mod_headers module, you can add the following lines to your server configuration or .htaccess file:

Header set Content-Security-Policy "directive value"

Replace “directive value” with the specific directives and values for your CSP. For example, to allow scripts to be loaded only from the same origin and images from any source, you can use:

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

Make sure the mod_headers module is enabled on your server.

2. Nginx:

If you’re using Nginx, you can add the following lines to your server block:

add_header Content-Security-Policy "directive value";

Again, replace “directive value” with the specific directives and values for your CSP.

3. Microsoft IIS:

If you’re using Microsoft Internet Information Services (IIS), you can configure CSP headers using the web.config file. Add the following lines within the <system.webServer> section:

<httpProtocol>
  <customHeaders>
    <add name="Content-Security-Policy" value="directive value" />
  </customHeaders>
</httpProtocol>

Replace “directive value” with the specific directives and values for your CSP.

It’s important to note that the syntax and options for defining Content Security Policy may vary depending on the specific version of CSP you are using (e.g., CSP 1.0, CSP 2.0, etc.). Refer to the documentation for your server software and the CSP specification to ensure you use the correct syntax and directives.

Additionally, some web application frameworks and Content Delivery Networks (CDNs) provide their own mechanisms for setting up Content Security Policy. Consult the documentation for your specific framework or CDN if applicable.

Remember to test your CSP implementation thoroughly to ensure it doesn’t inadvertently block legitimate resources on your website. You can use browser developer tools and CSP reporting mechanisms to identify and address any issues.

Conclusion

Here’s a link to check security headers of the website and some references you can use to set up Content Security Policy:

Want this checked on your own site? See Exivon Tech’s Website & E-commerce Audit.

Dhiren Patel

Principal Full-Stack Engineer · Founder, Exivon Tech

I’ve built and run enterprise web platforms and e-commerce for 13 years, and I built ShieldScope, a free WordPress security scanner.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.