What is a Content Security Policy?
Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross-Site Scripting (XSS) and data injection attacks. These attacks are used for everything from data theft, to site defacement, to malware distribution.
Generally, to enable CSP, you need to configure your web server to return the Content-Security-Policy HTTP header or HTML meta tag. For more details, refer to MDN and OWASP.
On this page, you can find guides on creating Content Security Policies that match your specific needs.
Create a Content Security Policy
In this guide you'll create a simple Content Security Policy template that allows you to generate policies that are ready to be applied to any web application:
- Navigate to Web Security → CSP → Policies and click Create policy button
- Configure a new policy with the following values:
Name |
|
Default source (default-src) |
|
Style source (style-src) |
|
- Click the Save button to save the policy
- Once the policy is set up, it will appear in the policies grid
- Click the policy's Copy policy button and use Policy source dropdown to switch between different policy representations:
HTTP header (enforcing) |
|
HTML meta tag |
|
Watch the video demo below to see all the steps mentioned earlier in action:
Import a Content Security Policy from URL
In this guide you'll import a Content Security Policy from an external URL:
- Navigate to Web Security → CSP → Policies and click Import policy button
- Pick URL tab and use the following values for import:
Policy name |
|
URL |
|
Policy source |
|
- Click the Import button to import the policy
- Once the policy is imported, it will appear in the policies grid
- Refer to the Policy grid column or policy edit flyout to view the content of the imported policy
Watch the video demo below to see all the steps mentioned earlier in action:
Import a Content Security Policy from a string
In this guide you'll import a Content Security Policy from a string (serialized policy text):
- Navigate to Web Security → CSP → Policies and click Import policy button
- Pick Serialized policy tab and use the following values for import:
Policy name |
|
Serialized policy |
|
- Click the Import button to import the policy
- Once the policy is imported, it will appear in the policies grid
- Refer to the Policy grid column or policy edit flyout to view the content of the imported policy
Watch the video demo below to see all the steps mentioned earlier in action:
Test a Content Security Policy
In this guide, you will create a Content Security Policy and test it using a custom HTML responder:
- First, navigate to Webhooks → Responders and click Create responder button
- Configure a new responder with the following values to respond with a simple HTML page that uses eval() function to evaluate JavaScript code represented as a string:
Name |
|
Path |
|
Method |
|
Body |
|
- Click the Save button to save the responder
- Once the responder is set up, it will appear in the responders grid along with its unique URL
- Click the responder's URL and use Eval button on the rendered page to see that nothing prevents you from using eval() function
- Now, navigate to Web Security → CSP → Policies and click Create policy button to create a Content Security Policy to forbid eval()
- Configure a new policy with the following values:
Name |
|
Script source (script-src) |
|
- Click the Save button to save the policy
- Once the policy is set up, it will appear in the policies grid
- Click the policy's Copy policy button and use Policy source dropdown to switch to HTML meta tag policy representation
- Copy
<meta>
HTML tag with the policy and navigate to Webhooks → Responders again - Edit Body property of the previously created CSP Test responder to include
<meta>
HTML tag with the policy inside<head>
HTML tag - Click the Save button and navigate to the responder's URL again
- This time, when you click the Eval button, nothing happens and an error message is logged in the browser console meaning that you have successfully forbidden eval() with the Content Security Policy
Watch the video demo below to see all the steps mentioned earlier in action:
Report Content Security Policy violations
In this guide, you will create a Content Security Policy and collect its violation reports using a custom tracking responder:
- Navigate to Web Security → CSP → Policies and click Create policy button
- Configure a new policy with the following values:
Name |
|
Script source (script-src) |
|
Report to (report-to) |
|
- Click the Save button to save the policy
- Once the policy is set up, it will appear in the policies grid
- Click the policy's Copy policy button, switch Policy source to HTTP header (enforcing) and copy generated policy header
- Now, navigate to Webhooks → Responders and click Create responder button
- Configure a new responder with the following values to collect CSP violation reports:
Name |
|
Path |
|
Method |
|
Tracking |
|
- Click the Save button and copy responder's URL
- Click Create responder button once again
- Configure another responder with the following values to respond with a simple HTML page that will try to use eval() function to evaluate JavaScript code represented as a string:
Name |
|
Path |
|
Method |
|
Headers |
|
Body |
|
- Click the Save button to save the responder
- Once the responder is set up, it will appear in the responders grid along with its unique URL
- Click the responder's URL to navigate to the test page
- On the test page, click the Eval button and notice that nothing happens except that browser logs an error message in its console meaning that you have successfully forbidden eval() with the Content Security Policy
- Go back to the responder's grid and expand the CSP Reporting responder to view the CSP violation report that browser has sent when you tried to use eval()
Watch the video demo below to see all the steps mentioned earlier in action:
Share a Content Security Policy
This guide will walk you through sharing a Content Security Policy template publicly, allowing anyone on the internet to view it:
- Navigate to Web Security → CSP → Policies and pick the policy you'd like to share
- Click the policy's Share policy button and toggle Share policy switch to on position
- Once the policy is shared, the dialog will show a Copy link button
- Click the Copy link button to copy a unique shared policy link to your clipboard
- To stop sharing the policy, click the Share policy button again, and switch the Share policy toggle to the off position.
Watch the video demo below to see all the steps mentioned earlier in action: