Content Security Policy (CSP) & HTTP Security Headers Guide
How to configure and test Content Security Policy (CSP), HSTS, X-Frame-Options, and Referrer-Policy without breaking production apps or leaving XSS bypasses.
Web & App Security · By Thimmagurudu Bathini, CTO · Published · 5 min read
Why Many Production CSP Policies Still Allow XSS
Security headers are a low-cost defense-in-depth layer, yet many production sites ship CSP policies weakened by unsafe-inline, unsafe-eval, or overly broad CDN allowlists like *.googleapis.com. Testers evaluate both missing headers and real-world CSP bypasses.
Content-Security-Policy:
default-src 'self';
script-src 'self' 'nonce-<random_per_request>';
style-src 'self';
object-src 'none';
frame-ancestors 'self';
base-uri 'self';Header Baseline vs. Common Bypasses
| Header | Recommended Baseline | Common Bypass / Mistake |
|---|---|---|
| Content-Security-Policy | Strict script-src with nonces/hashes; object-src 'none' | Using 'unsafe-inline' or allowlisting JSONP/Angular CDNs |
| Strict-Transport-Security | max-age=31536000; includeSubDomains (when all subdomains have TLS) | Missing HTTPS redirect or uncertified subdomains |
| X-Frame-Options / frame-ancestors | frame-ancestors 'self' or 'none' | Allowing clickjacking on sensitive settings or payment pages |
| X-Content-Type-Options | nosniff | MIME-sniffing uploaded text/HTML files as executable scripts |
Auditing Frame Ancestors & MIME Sniffing
- Audit CSP directives for unsafe-inline, unsafe-eval, data: URIs in script-src, and wildcard domains.
- Test sensitive state-changing pages (transfers, role changes, delete actions) inside an iframe for clickjacking.
- Check that static error pages (404/500) and APIs also return security headers.
- Deploy CSP in Content-Security-Policy-Report-Only mode first before enforcing.
Rolling Out Strict Nonce-Based CSP Safely
Move inline scripts and styles into external files or use cryptographic nonces so you can enforce strict script-src and style-src policies without breaking UI layout.
Validate your CSP and header posture across apps and APIs during web VAPT from ₹36,000 + 18% GST. Request a scoped proposal →
CSP & Security Headers Questions
Does a strong CSP replace XSS output encoding?
No. CSP is defense-in-depth; contextual output encoding and safe DOM APIs remain the primary fix for XSS.
Do you verify security headers across all pages and subdomains?
Yes, external and web application tests check header consistency across main apps, APIs, and error responses.