Skip to main content

Research

CSP Bypass in Single Page Applications

Testing for CSP bypass vulnerabilities through allowlisted hosts in single page applications

Pentrova Research Pentrova Research
8 min read

Reading mode

What is Content Security Policy (CSP)?#

Content Security Policy (CSP) is a security feature that helps prevent cross-site scripting () attacks by controlling which resources can be loaded on a website and what actions they can perform. CSP works by instructing the browser to restrict the loading of resources and the execution of code, thereby preventing malicious scripts from being injected into a website. CSP is a crucial defense mechanism against attacks, as it helps to prevent malicious scripts from being executed on a website. CSP can be implemented using a set of directives that define which resources can be loaded and what actions can be performed on a website. These directives are specified in the Content-Security-Policy HTTP header and can include rules for loading scripts, styles, images, and other resources. For example, a CSP directive might specify that only scripts from a specific domain can be loaded, or that inline scripts are not allowed. By implementing CSP, website owners can help to prevent attacks and protect their users’ sensitive information. CSP can be used in conjunction with other security measures, such as input validation and output encoding, to provide an additional layer of protection against attacks.

How CSP Works#

CSP works by using a set of directives that define which resources can be loaded and what actions can be performed on a website. These directives are specified in the Content-Security-Policy HTTP header and can include rules for loading scripts, styles, images, and other resources. According to MDN, CSP can help prevent XSS attacks by controlling which resources are loaded and what actions they can perform. For example, a CSP directive might specify that only scripts from a specific domain can be loaded, or that inline scripts are not allowed.

CSP Bypass Techniques#

Despite its effectiveness, CSP can be bypassed using various techniques. One common method is to exploit allowlisted hosts, which are domains or URLs that are explicitly allowed to load resources on a website. As explained by PortSwigger Research, an attacker can use script gadgets to bypass CSP. Another method is to use script gadgets, which are pieces of code that can be used to execute malicious scripts. These gadgets can be embedded in a website’s code or loaded from an external source.

Comparison of CSP Bypass Methods#

The following table compares different CSP bypass techniques:

MethodDescriptionAdvantagesDisadvantages
Exploiting allowlisted hostsUsing allowlisted hosts to load malicious resourcesEasy to implement, high success rateLimited to specific domains or URLs
Using script gadgetsEmbedding or loading script gadgets to execute malicious scriptsHighly flexible, can be used with various CSP directivesRequires knowledge of script gadgets and their implementation
DOM-based attacksUsing DOM manipulation to inject malicious scriptsCan be used with or without CSPRequires knowledge of DOM manipulation and JavaScript
Bottom line: Each CSP bypass method has its advantages and disadvantages, and the choice of method depends on the specific use case and the attacker’s goals.

Identifying Allowlisted Hosts#

Identifying allowlisted hosts in a single page application involves several steps. One common method is to use Burp Suite to analyze the website’s traffic and identify allowlisted hosts. This can be done by configuring Burp Suite to intercept the website’s traffic and then analyzing the HTTP requests and responses to identify any allowlisted hosts. Another method is to use the website’s source code to identify allowlisted hosts and other security-related configurations. This can be done by reviewing the website’s code and looking for any references to allowlisted hosts or other security-related configurations. Allowlisted hosts can be identified by looking for any references to specific domains or URLs in the website’s code or in the HTTP requests and responses. For example, a website may have a CSP directive that allows scripts to be loaded from a specific domain, such as https://example.com. In this case, https://example.com would be an allowlisted host. By identifying allowlisted hosts, it is possible to determine which hosts are allowed to load resources on the website and which hosts may be vulnerable to CSP bypass attacks. Additionally, it is recommended to use tools such as grep or find to search for allowlisted hosts in the website’s code and configuration files. It is also important to review the website’s configuration files, such as the web.config file, to identify any allowlisted hosts that may be specified in the configuration.

How to Test for CSP Bypass Vulnerabilities#

Testing for CSP bypass vulnerabilities involves several steps. The first step is to use Burp Suite to analyze the website’s traffic and identify allowlisted hosts. This can be done by configuring Burp Suite to intercept the website’s traffic and then analyzing the HTTP requests and responses to identify any allowlisted hosts. The next step is to use the website’s source code to identify allowlisted hosts and other security-related configurations. This can be done by reviewing the website’s code and looking for any references to allowlisted hosts or other security-related configurations. Once the allowlisted hosts have been identified, the next step is to test them for CSP bypass vulnerabilities using various techniques, such as exploiting script gadgets or using DOM-based attacks. This can be done by using tools such as Burp Suite or ZAP to send malicious requests to the website and then analyzing the responses to determine if the CSP bypass was successful. Finally, it is important to verify the results of the testing and identify any vulnerabilities that need to be remediated. This can be done by reviewing the test results and determining which allowlisted hosts are vulnerable to CSP bypass attacks. By following these steps, it is possible to effectively test for CSP bypass vulnerabilities and identify any potential security threats.

Remediation and Verification#

To remediate CSP bypass vulnerabilities, it is essential to identify and address the root causes of the vulnerabilities. This can include updating the website’s CSP directives, removing allowlisted hosts, or implementing additional security measures. A strict CSP can help prevent attacks by controlling which resources are loaded and what actions they can perform. After remediation, it is crucial to verify the effectiveness of the remediation using various testing techniques. One approach is to use Burp Suite to analyze the website’s traffic and identify any remaining allowlisted hosts that could be exploited. Another approach is to use the website’s source code to identify any security-related configurations that may need to be updated. It is also important to test the identified allowlisted hosts for CSP bypass vulnerabilities using various techniques, such as exploiting script gadgets or using DOM-based attacks. Additionally, it is recommended to implement a web application firewall (WAF) to detect and prevent CSP bypass attacks. The WAF can be configured to detect and block malicious traffic, and can also be used to monitor the website’s traffic for any potential security threats. By taking these steps, it is possible to effectively remediate CSP bypass vulnerabilities and prevent future attacks.

FAQ#

What is the difference between a strict CSP and a non-strict CSP?#

A strict CSP is a CSP that uses nonce- or hash-based fetch directives to ensure that only scripts and/or styles that include the correct nonce or hash will be executed. A non-strict CSP, on the other hand, does not use these directives and may allow inline scripts or other potentially malicious code to be executed.

How can I use Burp Suite to test for CSP bypass vulnerabilities?#

Burp Suite can be used to analyze a website’s traffic and identify allowlisted hosts, which can then be tested for CSP bypass vulnerabilities using various techniques.

What are some common pitfalls to avoid when implementing CSP?#

Some common pitfalls to avoid when implementing CSP include not using a strict CSP, not properly configuring CSP directives, and not regularly testing for CSP bypass vulnerabilities.

Written by

Pentrova Research Pentrova Research

Pentrova Research writes about offensive security, LLM-driven pentest chains, and how to ship exploit-grade evidence into engineering pipelines.

Cross-Role Authorization Testing

Catch BOLA flaws that return HTTP 200 OK

Traditional scanners miss logic flaws in valid JSON responses. Pentrova maps multi-tenant object access across roles to prove BOLA before merge.

Test API Authorization →

Keep reading

Site search

↑↓ navigateEnter openEsc close