---
description: Content Security Policy
---

# CSP

{% embed url="https://portswigger.net/web-security/cross-site-scripting/content-security-policy" %}

{% embed url="https://csp-evaluator.withgoogle.com/" %}

**ENTRY POINTS:**

* Search box

CSP is a browser security mechanism that aims to mitigate XSS and some other attacks. It works by restricting the resources (such as scripts and images) that a page can load and restricting whether a page can be framed by other pages.

It's quite common for a CSP to block resources like script. However, many CSPs do allow image requests. This means you can often use img elements to make requests to external servers in order to disclose CSRF tokens, for example.

Some browsers, such as Chrome, have built-in dangling markup mitigation that will block requests containing certain characters, such as raw, unencoded new lines or angle brackets.

Some policies are more restrictive and prevent all forms of external requests. However, it's still possible to get round these restrictions by eliciting some user interaction. To bypass this form of policy, you need to inject an HTML element that, when clicked, will store and send everything enclosed by the injected element to an external server.



&#x20;                                                         **Bypassing CSP with policy injection**

You may encounter a website that reflects input into the actual policy, most likely in a report-uri directive. If the site reflects a parameter that you can control, you can inject a semicolon to add your own CSP directives. Usually, this report-uri directive is the final one in the list. This means you will need to overwrite existing directives in order to exploit this vulnerability and bypass the policy.

Normally, it's not possible to overwrite an existing script-src directive. However, Chrome recently introduced the script-src-elem directive, which allows you to control script elements, but not events. Crucially, this new directive allows you to overwrite existing script-src directives.



**//EXPLOITS**

Reflected XSS protected by very strict CSP, with dangling markup attack

{% embed url="https://portswigger.net/web-security/cross-site-scripting/content-security-policy/lab-very-strict-csp-with-dangling-markup-attack" %}
BURP PRO: COLLABERATOR
{% endembed %}

Reflected XSS protected by CSP, with CSP bypass

{% embed url="https://portswigger.net/web-security/cross-site-scripting/content-security-policy/lab-csp-bypass" %}

