How to remove unsafe-inline from your CSP
If your Content Security Policy contains 'unsafe-inline' in script-src, browsers will run any script written into the page, including one an attacker injects. You remove it by moving scripts into files or marking trusted ones with a nonce or hash, and testing in report-only mode first.
Your site sends a Content-Security-Policy (CSP) header, but it contains 'unsafe-inline'. That keyword tells the browser to run any script written directly into the page, which is exactly what an attacker tries to sneak in. To remove it, you move scripts into files, or mark the trusted ones with a nonce or a hash, and you test before you switch the stricter policy on.
What the problem is and what you see
A CSP is a list of rules that tells the browser where scripts, styles, images and other content may come from. The script-src rule matters most. When it includes 'unsafe-inline', the browser accepts inline scripts: <script> blocks written into the page and event attributes such as onclick.
Most owners meet this as a line in a security report saying the CSP allows unsafe-inline. Visitors see nothing, and the site works fine, which is why it often stays for years. It is usually there because something needed it: a theme, a tracking snippet, a chat widget, or a developer who hit a broken page and added it to make the error go away.
Why it matters
The main job of a CSP is to limit the damage from cross-site scripting (XSS). That is when an attacker gets their own script into your page, for example through a comment field, a search box or a vulnerable plugin. With 'unsafe-inline' in script-src, the browser cannot tell your scripts from the injected one, so the CSP gives you very little protection against that attack.
For a business, the possible results are stolen logins, skimmed payment details on a checkout page, visitors redirected to scam sites, and browser or security tool warnings that cost trust. Security questionnaires from bigger customers may also ask about your CSP.
'unsafe-inline' in style-src is a smaller risk than in script-src. Fix scripts first, and treat styles as a second step.
How to check if your site has it (free)
- Open your site in Chrome or Firefox and press F12. Go to the Network tab, reload, click the first request (your page), and look under Response Headers for
content-security-policy. - Or run this in a terminal, replacing the domain with yours:
curl -sI https://example.com | grep -i content-security-policy
- Paste the policy into Google's CSP Evaluator (csp-evaluator.withgoogle.com). It flags
'unsafe-inline'and other weak settings. - If you see no header, also look in the page source for a
<meta http-equiv>tag. Some sites set the policy there.
One detail that causes false alarms: if the same script-src also contains a nonce or a hash, modern browsers ignore 'unsafe-inline'. It is then only a fallback for very old browsers, and the policy is doing its job.
How to fix it in general
- Test without breaking the site. Keep your current policy running. Add a second header named
Content-Security-Policy-Report-Onlywith the stricter policy (no'unsafe-inline'). The browser console will list every inline script it would have blocked, but nothing breaks. - Move your own inline scripts into `.js` files and load them with
<script src=...>. - Replace inline event attributes such as
onclickwith event listeners in those script files. - Handle scripts you cannot move. For third-party snippets, use a nonce: your server creates a random value for every page load, puts it in the header and on each trusted
<script>tag. It must be new for every response, so full-page caching can get in the way. For a fixed inline script that never changes, use a hash instead ('sha256-...'). The browser console shows the right hash when it blocks a script. - Remove `'unsafe-inline'` from
script-srconce the report-only version is quiet. Adding'strict-dynamic'lets a nonce-approved script load the further scripts it needs.
A typical result looks like this:
Content-Security-Policy: script-src 'nonce-R4nd0mValue' 'strict-dynamic'; object-src 'none'; base-uri 'self'
Common platforms
- WordPress: themes and plugins print many inline scripts, so this is often the hardest case. The header may come from a security plugin or from the server. Expect to test plugin by plugin. See the WordPress guide.
- Next.js: nonces are usually created in middleware, and pages that use them must be rendered per request. See the Next.js guide.
- Google Tag Manager: tags often inject inline code. GTM supports nonces, but each custom tag needs review. See the Google Tag Manager guide.
- Nginx, Apache, Cloudflare: these only send the header (
add_headerin Nginx,Header setin Apache, response header rules in Cloudflare). They cannot create a per-page nonce on their own, so static sites usually rely on hashes.
Make sure only one place sets the header. If two layers each send a CSP, the browser enforces both, so the stricter combination wins and pages can break in confusing ways.
How to confirm the fix worked
- Reload your pages with the console open and check there are no "Refused to execute inline script" messages.
- Click through the parts that depend on scripts: checkout, contact forms, chat, maps, the cookie banner and any login.
- Run the
curlcommand again and paste the policy into CSP Evaluator.'unsafe-inline'should be gone fromscript-src, or be ignored because of a nonce or hash. - Try at least two browsers. Keep report-only running for a week or so before you enforce, so rarely used pages get tested too.
How to stop it coming back
- Keep the policy in one place, ideally in version control, so changes are visible.
- Agree a simple rule for new work: no inline scripts and no
onclickstyle attributes. - Retest after you add a plugin, widget, ad tag or tracking snippet. These are the usual way
'unsafe-inline'creeps back in. - Scan again on a schedule. SiteMonday's week 2 OpenVAS security scan checks for this and explains it in the report, after you prove you own the site. Fixing it is still up to you or your developer.
Work on this in small steps: report-only first, enforce later. A strict policy that quietly breaks your checkout is worse than none, so the testing is not optional.
Step by step for your platform
- CSP allows unsafe-inline on Next.js
- CSP allows unsafe-inline on WordPress
- CSP allows unsafe-inline on Nginx
- CSP allows unsafe-inline on Apache
- CSP allows unsafe-inline on Cloudflare
- CSP allows unsafe-inline on Google Tag Manager
- CSP allows unsafe-inline on React
Questions
Is unsafe-inline always bad?
In `script-src` it removes most of the protection a CSP gives against XSS, so you should aim to remove it. In `style-src` the risk is lower, so it is a reasonable second step after scripts.
Will removing unsafe-inline break my website?
It can, if the site relies on inline scripts or `onclick` attributes. Testing with `Content-Security-Policy-Report-Only` first shows what would break without affecting visitors.
What is the difference between a nonce and a hash?
A nonce is a random value created for each page load and added to the header and the script tag. A hash is a fingerprint of one fixed script, so it suits static sites where the script never changes.
Why does my scanner still flag it when I use a nonce?
Some tools only look for the keyword. If a nonce or hash is in the same `script-src`, modern browsers ignore `'unsafe-inline'`, so the policy is working, but you can drop the keyword to silence the warning.
Can I fix this without a developer?
On a simple site you may manage it by moving scripts into files and testing in report-only mode. On WordPress or a custom app with many plugins and tags, a developer will usually save you time and avoid broken pages.
Does a CSP replace keeping my software updated?
No. A CSP limits the damage if something goes wrong, but outdated plugins and themes are still a way in. Keep both in place.