Content Security Policy (CSP)

  • Fabian Sinner
  • July 31, 2024

Content

Content Security Policy (CSP)

A Content Security Policy (CSP) is a security measure used to protect web applications from various types of attacks, in particular cross-site scripting (XSS) and data injections. A CSP allows website operators to define specific policies that control what content may be loaded and executed from their website. This is achieved by specifying permitted sources for scripts, styles, images, and other resources. 

How does a Content Security Policy (CSP) work?

A CSP works by defining guidelines that determine what content may be loaded and executed from a website.  

Definition of the guidelines 

CSP policies are usually defined in the HTTP header or as a meta tag in HTML. A typical CSP header could look like this: 

Content-Security-Policy: default-src ‘self’; script-src ‘self’ https://apis.example.com; style-src ‘self’ https://fonts.example.com 

Processing the policies 

When a browser loads a web page, it reads the CSP policies and applies them to the subsequent resource requests. Each request is then checked against the CSP guidelines to determine whether it is permitted. 

Directives 

CSP directives consist of several directives, each of which controls a specific type of resource. Some of the most common directives are: 

  • default-src: The default source for all content not covered by a more specific directive. 
  • script-src: Controls the sources for JavaScript. 
  • style-src: Controls the sources for CSS. 
  • img-src: Controls the sources for images. 
  • connect-src: Controls the sources for network requests (e.g. Fetch, XHR). 
  • font-src: Controls the sources for fonts. 
  • frame-src: Controls the sources for embedded frames (e.g. iframes). 

Values 

CSP uses certain values to define which sources are allowed: 

  • ‘self’: Allows content from the same origin (same domain, protocol and port). 
  • ‘none’: Blocks all content. 
  • ‘unsafe-inline’: Allows inline scripts and styles. 
  • ‘unsafe-eval’: Allows the use of eval( ) and similar methods. 

Reporting mechanism 

CSP can be configured to report policy violations. This is done by specifying a report-uri or report-to directive: 

Content-Security-Policy: default-src ‘self’; report-uri /csp-report-endpoint 

Comprehensive cyber security

Contact our experts and find out how your business can be protected with an automated security solution.

What are the advantages?

The implementation of Content Security Policies (CSPs) offer a number of advantages: 

Protection against cross-site scripting (XSS) 

A CSP significantly reduces the risk of cross-site scripting attacks by preventing the execution of untrusted scripts on the website. By restricting this process to authorized sources, attackers cannot insert and execute malicious scripts. 

Prevention of data injections 

A CSP helps prevent various types of data injection attacks, such as SQL injections and code injections, by preventing the execution of malicious code. 

Control over resources 

With a CSP, website operators can control exactly which resources (scripts, styles, images, etc.) may be loaded from their website. This prevents the loading of malicious or unwanted content from third-party providers. 

Protection against clickjacking 

The use of CSP directives, such as frame-ancestors, can prevent a website from being embedded in iframes from untrusted domains. This protects against clickjacking attacks. 

Improved security monitoring 

A CSP provides a reporting function that allows policy violations to be logged and reported to a server. This helps developers to identify and fix security vulnerabilities before they can be exploited. 

Reduction of mixed content vulnerabilities 

A CSP can be used to ensure that all resources are loaded over secure connections (HTTPS), thereby avoiding mixed content vulnerabilities. Mixed content occurs when HTTPS pages load insecure HTTP resources, which can compromise the security of the entire site. 

Improved defense against man-in-the-middle attacks 

A CSP can help prevent man-in-the-middle attacks by only allowing resources from trusted sources. This ensures that attackers cannot inject malicious content even if they intercept the traffic. 

Protection against phishing attacks 

By controlling the sources of content, CSP can help prevent phishing attacks, where malicious content is embedded into legitimate websites to deceive users and steal their data. 

Overall, CSPs provide a comprehensive and flexible way to improve web application security and protect against a variety of threats. By implementing and properly configuring CSP, website operators can significantly reduce the attack surface and ensure the integrity and security of their web applications. 

Limitations of Content Security Policies (CSP)

CSP offer many advantages, but they also have their limitations: 

  • Legacy systems: Older applications or systems that are not designed to use CSP may have difficulty implementing it, as many scripts and styles may be embedded inline. 
  • Certain types of attacks: CSP is specifically designed to prevent XSS attacks and similar threats, but does not provide comprehensive protection against other types of attacks such as CSRF (Cross-Site Request Forgery). 
  • Performance considerations: In some cases, performance concerns could arise if CSP policies are very restrictive and many resources need to be blocked or redirected. 
  • Unsupported browsers: CSP is supported by most modern browsers, but older browsers or specialized environments may not fully or correctly support CSP, which can impact effectiveness. 
  • Development effort: CSPs may require additional effort to implement and maintain, especially if they include numerous third-party scripts and styles that need to be updated regularly. 
Cyber resilience at the EU level: What DORA Means for German Banks
DDoS Competence Center introduced at CeBIT 2015 by Link11
X