Content Security Policy (CSP) Bypass

Content Security Policy (CSP) Bypass

What is Content Security Policy (CSP)?

CSP is an HTTP response header designed to mitigate attacks such as:

  • Cross-Site Scripting (XSS)
  • Data injection
  • Clickjacking
  • Content spoofing

A basic example of a CSP might be:

Content-Security-Policy: default-src 'self'; script-src 'self'

This instructs the browser:

Only permit content (particularly scripts) from my own domain — not from any other source!

It functions as a browser-enforced whitelist, rejecting any unexpected or dubious content.

What Is a CSP Bypass?

A CSP Bypass occurs when an attacker discovers a method to execute harmful scripts or load external content despite the presence of a CSP.

How? Through vulnerabilities such as:

  • Misconfigured or excessively permissive policies
  • Unsafe inline scripts or eval()
  • Exploitation of trusted third-party domains
  • JSONP or open redirect endpoints
  • Loading harmful code from permitted CDNs (e.g., cdnjs, Google APIs)

Real-World Bypass Techniques

  • Allowed CDN Abuse

Load a harmful script from an authorized CDN (e.g., cdn.example.com)

Numerous CDNs permit user uploads or contain outdated libraries.

  • unsafe-inline or unsafe-eval in Policy

These permit inline scripts or dynamic code execution (which is inadvisable!)

  • Injection into JSONP

If a trusted endpoint returns JavaScript-wrapped JSON, it can be exploited.

  • Open Redirects & Frame Injection

Utilizing whitelisted sources to redirect or embed harmful content.

  • Browser Quirks or CSP Downgrade

Older browsers may fail to enforce CSP correctly or at all.

Why CSP Bypass Is Significant

If an attacker successfully bypasses CSP:

  • They can still execute XSS attacks
  • Steal cookies, tokens, and localStorage data
  • Exfiltrate sensitive information
  • Load harmful code from "trusted" sources
  • Achieve complete control over victim sessions.

How to Safeguard Against CSP Bypass

  • Refrain from using unsafe-inline and unsafe-eval

Implement nonce-based policies to permit only designated scripts

  • Avoid indiscriminate whitelisting of CDNs

Permit only precise file paths, not entire domains

  • Limit the use of * (wildcards)

Do not permit scripts, styles, or frames from any origin

  • Review third-party dependencies

If they are compromised, your CSP is also at risk

  • Employ strict frame-ancestors and object-src policies

Prevent iframe and Flash injection

  • Evaluate your CSP with tools such as

CSP Evaluator by Google

Report URI

  • Record CSP Violations

Utilize report-uri or report-to to track attacks:

Content-Security-Policy: default-src 'self'; report-uri /csp-report        

Pro Tip

A weak CSP resembles a locked door with the key hidden under the mat.

You may appear secure, but attackers are aware of where to look.

#ContentSecurityPolicy #CSPBypass #CyberSecurity #WebSecurity #AppSec ADITH AJITHKUMAR Shonith Mohan TBH

To view or add a comment, sign in

Others also viewed

Explore topics