Skip to main content
HighClient-sideOWASP A05:2021CWE-693

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

  1. 1Remove 'unsafe-inline' from script-src.
  2. 2Move all inline scripts to external .js files.
  3. 3For unavoidable inline scripts (e.g., Next.js hydration), add a per-request nonce: script-src 'nonce-{random}'.
  4. 4Use 'strict-dynamic' with a nonce to allow dynamically-loaded scripts while blocking inline injection.

Code examples

Nonce-based CSP (Next.js middleware)

TypeScript
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.

See every client-side check

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.

Propose a checkHow a check is built

Scan your own site for csp allows 'unsafe-inline' scripts

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

How to fix: CSP Allows 'unsafe-inline' Scripts | VulnRadar