Content Security Policy (CSP) is a W3C recommendation since 2016 and the industry standard for securing web applications against cross-site scripting (XSS) attacks.
Overview
Content Security Policy (CSP) significantly reduces the risk and impact of cross-site scripting attacks in modern browsers by controlling which resources can be loaded and executed on your web pages.Current Vulnerability
The Normo Unsecure PWA currently lacks CSP headers, making it vulnerable to:- Cross-site scripting (XSS) attacks
- Unauthorized script execution
- Data injection attacks
- Clickjacking
templates/layout.html:1-35, there are no CSP headers defined, and main.py:1-74 shows no CSP decorator usage on routes.
Implementation Guide
Design Your CSP Policy
Design a policy that fits your application’s needs. You can:
- Follow W3C CSP documentation
- Use a CSP generator tool
- Start with the recommended policy below
CSP Directives Explained
Restricts the URLs that can be used in a document’s
<base> element. Setting to 'self' prevents base tag hijacking.Serves as a fallback for other CSP fetch directives. Only allows resources from the same origin.
Controls which scripts can be executed.
'self' only allows scripts from your domain, preventing inline scripts and external malicious scripts.Specifies valid sources for stylesheets. Restricts to same-origin CSS files.
Defines valid sources for images. Using
* allows images from any source (adjust based on your needs).Restricts the URLs which can be used as the target of form submissions. Prevents forms from posting to external sites.
Specifies valid parents that may embed a page using
<frame>, <iframe>, etc. 'none' prevents clickjacking attacks.Specifies valid sources for nested browsing contexts (frames/iframes).
Instructs the browser to send CSP violation reports to this endpoint for monitoring.
Testing Your CSP
Check Browser Console
Open your browser’s developer console and look for CSP violation warnings when loading pages.
Monitor CSP Reports
Check your application logs for CSP violation reports sent to the
/csp_report endpoint:Use CSP Evaluator
Use Google’s CSP Evaluator to analyze your policy for weaknesses.
Common Issues
Security Benefits
Protection Against XSSCSP prevents execution of malicious scripts injected through user input, protecting against the most common web vulnerability.
Defense in DepthEven if an attacker finds an injection point, CSP limits what malicious code can do (e.g., preventing data exfiltration to external domains).
Clickjacking PreventionThe
frame-ancestors directive prevents your site from being embedded in iframes on malicious sites.