How to fix: CSP Allows 'unsafe-inline' Scripts
The Content-Security-Policy header contains 'unsafe-inline' in the script-src directive. This negates most XSS protection the CSP provides, since inline scripts injected by an attacker will be allowed.
Why it matters
Any XSS payload that injects an inline <script> tag or event handler (onclick, onerror, etc.) will execute despite the CSP. The attacker only needs to find an injection point; the browser will happily run the payload.
CSP's main defense against XSS is blocking inline script execution. 'unsafe-inline' turns that defense off. The correct fix is to remove inline scripts from the codebase and use nonces or hashes for the remaining legitimate ones.
How to fix it
- 1Remove 'unsafe-inline' from script-src.
- 2Move all inline scripts to external .js files.
- 3For unavoidable inline scripts (e.g., Next.js hydration), add a per-request nonce: script-src 'nonce-{random}'.
- 4Use 'strict-dynamic' with a nonce to allow dynamically-loaded scripts while blocking inline injection.
Code examples
Nonce-based CSP (Next.js middleware)
const nonce = Buffer.from(crypto.randomUUID()).toString('base64');
const csp = [
`script-src 'nonce-${nonce}' 'strict-dynamic'`,
`style-src 'nonce-${nonce}'`,
"object-src 'none'",
"base-uri 'self'",
].join('; ');
response.headers.set('Content-Security-Policy', csp);References
Detected automatically by VulnRadar. The check id is cs-csp-unsafe-inline-script, stable across every scan so you can filter or diff on it.
More client-side checks
- MediumCSP Allows 'unsafe-eval'
- HighpostMessage Without Origin Validation
- HighSensitive Data Stored in localStorage
- HighDOM XSS via location.hash Assignment
- Highdocument.write() in Page Scripts
- Higheval() in Client-Side JavaScript
- MediumExternal Script Without Subresource Integrity
- MediumJavaScript Source Map Exposed in Production
Questions
- How severe is CSP Allows 'unsafe-inline' Scripts?
- VulnRadar rates this high severity in the client-side category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
- Does VulnRadar check for this automatically?
- Yes. It runs as part of every scan under the check id cs-csp-unsafe-inline-script, which stays the same between runs so you can filter, diff, or gate CI on it.
- Which standards does CSP Allows 'unsafe-inline' Scripts map to?
- It maps to CWE-693. In the OWASP Top 10 it falls under A05:2021.
Missing a check?
The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.