Our scanner raises Inline Event Handlers when your homepage HTML has four or more tags carrying an attribute like onclick="...", onload="..." or onerror="...". It's rated low. A separate, medium-rated finding, javascript: Protocol in Attributes, fires on even one href, src or action that starts with javascript:.
Neither one is an exploit on its own. They matter because of what they stop you doing: you can't turn on a strict Content Security Policy while they're there, and CSP is the header that limits the damage when an XSS bug does slip through.
TL;DR
A strict CSP blocks every on*= attribute and every javascript: URL, and a nonce can't exempt them. Move each handler into a script file with addEventListener, replace javascript: links with <button> elements, then load the page under Content-Security-Policy-Report-Only and check the console is clean before enforcing.
Why a nonce can't save an onclick
The usual way to allow your own inline JavaScript under CSP is a nonce: a random value in the header that also appears on each <script nonce="..."> tag. The browser runs scripts that carry it and refuses the rest.
An onclick attribute isn't a script element. There's no tag to put the nonce on, so the browser treats it as untrusted inline code. Google's strict CSP guide puts it bluntly: a nonce- or hash-based CSP "prohibits the use of this kind of markup", and it lists inline event handlers and javascript: URIs side by side.
Two details catch people out:
- Hashes don't cover handlers by default. MDN's
script-srcreference says hashes "apply to inline scripts and styles, but not event handlers." You need the extra'unsafe-hashes'keyword for that. 'unsafe-inline'stops working the moment you add a nonce. Per MDN: "If a directive contains a nonce andunsafe-inline, then the browser ignoresunsafe-inline." So you can't keep the old handlers alive with a fallback. The policy is either strict or it isn't.
When the browser blocks one, Chrome logs Refused to execute inline event handler because it violates the following Content Security Policy directive and the button simply does nothing. No error on click, no visible failure. Users just think your app is broken.
What our scanner counts (and what it doesn't)
The check runs against the HTML your server sends for the homepage. It's a pattern match, not a browser, so it's worth knowing its edges before you chase a number:
| Pattern in your HTML | Counted? |
|---|---|
<button onclick="save()"> | Yes |
<img src="a.png" onload="a()" onerror="b()"> | Once. One match per tag, however many handlers it has |
<div data-onclick="x"> | No. The attribute has to start with on |
React onClick={...} or Vue @click | No. They never reach the HTML |
| A handler a third-party script injects after load | No. It isn't in the server response |
<a href="javascript:void(0)"> | Yes, as the separate javascript: finding |
That fourth row surprised us enough to test it. We rendered a button with a click handler through react-dom/server 19.2 and vue/server-renderer 3.5. Both produced exactly <button>Save</button>. The frameworks wire up listeners from their own bundles, which CSP is happy with.
javascript: links behave differently, and the two frameworks disagree. Vue rendered <a href="javascript:void(0)"> straight through. React 19 rewrote it to javascript:throw new Error('React has blocked a javascript: URL as a security precaution.'). React's version is harmless, but it still starts with javascript:, so the HTML still gets flagged, and the honest fix is the same either way: stop putting javascript: in the source.
Where these come from in AI-built apps
If your app is React or Vue end to end, flagged handlers almost always live outside the components. The places we'd look first:
- A plain HTML app. Ask an AI editor for "a simple page with no framework" and you'll often get
onclick="toggleMenu()"on every button. This is the most common source, and the easiest to fix. - The font or CSS preload trick.
<link rel="preload" as="style" onload="this.onload=null;this.rel='stylesheet'">and its cousinmedia="print" onload="this.media='all'"are copied into countless templates for faster first paint. Each one is an inline handler. - Image fallbacks.
<img onerror="this.src='/placeholder.png'">is a favourite when generated code hits a broken avatar URL. - Placeholder links.
<a href="javascript:void(0)" onclick="...">shows up wherever something needed to look like a link but act like a button. - CMS blocks and embeds pasted into a static landing page, like a newsletter form or a booking widget.
Fix it in five steps
List every handler on the live page. View source isn't great for counting, so pull the HTML and let grep do it:
curl -s https://yourapp.com | grep -oiE '\son[a-z]+\s*=' | sort | uniq -c
curl -s https://yourapp.com | grep -oiE '(href|src|action)\s*=\s*["'"'"']javascript:[^"'"'"']*'
Then search your repo for the same attribute names (onclick=, onload=, onerror=, onsubmit=, javascript:) to find which files they come from. Skip .jsx, .tsx and .vue hits that use onClick= or @click; those are fine.
Replace click handlers with one delegated listener. Rather than writing a listener per button, tag each element with what it should do and handle them all in one place:
<button onclick="saveDraft()">Save</button>
<button onclick="return confirm('Delete this?') && deletePost()">Delete</button>
<button type="button" data-action="save">Save</button>
<button type="button" data-action="delete">Delete</button>
<script src="/js/actions.js" defer></script>
const actions = {
save: () => saveDraft(),
delete: () => {
if (confirm('Delete this?')) deletePost();
},
};
document.addEventListener('click', (event) => {
const el = event.target.closest('[data-action]');
if (!el) return;
actions[el.dataset.action]?.(el, event);
});
Delegation also covers buttons added to the page later, which per-element listeners don't.
Handle the awkward ones. Forms, images and stylesheets each need a slightly different shape.
For onsubmit="return validate()", call preventDefault instead of returning false:
document.querySelector('#signup').addEventListener('submit', (event) => {
if (!validate()) event.preventDefault();
});
For onerror image fallbacks, there's a timing trap: an image that fails before your script runs has already fired its error event, so a listener added afterwards never hears it. Check for that case too:
document.querySelectorAll('img[data-fallback]').forEach((img) => {
const useFallback = () => { img.src = img.dataset.fallback; };
if (img.complete && img.naturalWidth === 0) useFallback();
else img.addEventListener('error', useFallback, { once: true });
});
For the preload onload trick, the simplest fix is a normal <link rel="stylesheet">. If the render-blocking cost really matters, add the stylesheet from your script file instead of from an attribute.
Turn javascript: links into buttons. If clicking it doesn't go anywhere, it's a button. Swap <a href="javascript:void(0)" ...> for <button type="button" data-action="..."> and style it however you like. If it does go somewhere, give it the real URL.
Prove it under a report-only policy. Send this header from a preview deployment first:
Content-Security-Policy-Report-Only: script-src 'self' 'report-sample'
Report-only mode logs violations without blocking anything, so nothing breaks while you look. Open the console, click through the page, and watch for inline event handler messages. 'report-sample' adds the first 40 characters of each blocked handler to the report, which makes it easy to find the one you missed. When the console stays quiet, move on to the full policy in our CSP setup guide.
Don't fix this by adding 'unsafe-inline' to script-src. The handlers will run again, and so will any script an attacker manages to inject, which is the whole thing CSP exists to stop. Our scanner flags a CSP containing 'unsafe-inline' as its own finding.
When you can't edit the markup: 'unsafe-hashes'
Sometimes a handler lives in HTML you don't control, like a vendor widget that renders server-side into your page. For a handful of those, CSP has an escape hatch:
Content-Security-Policy: script-src 'self' 'unsafe-hashes' 'sha256-<hash of the exact attribute text>'
The browser will then run an inline handler only if its exact text hashes to a value you listed. That's much narrower than 'unsafe-inline', and MDN calls it "much safer", but it still carries the word unsafe for a reason, and any change to the handler text (even a space) means a new hash. Treat it as a stopgap while you ask the vendor for a version without inline handlers.
Have your AI editor do it
Move inline handlers to addEventListener
Find every inline event handler attribute (onclick, onload, onerror, onsubmit, onchange, oninput, and any other on* attribute) and every href, src or action that starts with "javascript:" in the HTML templates and static files of this project. Ignore React onClick props and Vue @click directives.
For each one:
- Remove the attribute from the markup.
- Add a data-action attribute (or a stable id) so the element can be found.
- Add the behaviour to an external script loaded with defer, using addEventListener. Use one delegated click listener on document for buttons.
- Replace javascript: links that don't navigate with button elements (type="button").
- For img onerror fallbacks, also handle images that already failed before the script ran (img.complete && img.naturalWidth === 0).
Don't add 'unsafe-inline' to any Content-Security-Policy. List every file you changed and anything you couldn't convert.
Then run the two curl commands from step 1 against your preview URL. Both should come back empty.
Does a CSP nonce allow inline event handlers?
No. A nonce goes on a <script> element, and an onclick attribute isn't one, so there's nothing to put the nonce on. Hashes don't cover handlers either unless you also add 'unsafe-hashes'. And once a nonce or hash is in script-src, browsers ignore 'unsafe-inline', so adding it as a fallback won't bring the handlers back.
Why does Chrome say refused to execute inline event handler?
Your page has a Content Security Policy with script-src (or default-src) that doesn't allow inline script, and an element has an on-attribute like onclick, onload or onerror. The browser skips that handler and logs the violation. Move the handler into a script file with addEventListener and the error goes away.
Is javascript:void(0) in an href a security risk?
The void(0) itself does nothing harmful. The problem is the pattern: a strict CSP blocks every javascript: URL, so any link that relies on one stops working, and if user data ever reaches an href that allows javascript:, it becomes an XSS vector. Use a button for actions and a real URL for links.
Do React onClick or Vue @click count as inline event handlers?
No. We rendered both to HTML with react-dom/server 19.2 and vue/server-renderer 3.5 and got a bare <button>Save</button> from each. The framework attaches the listener from its own script, which CSP allows. If our scanner flags handlers on a React or Vue site, they're coming from a static template, an index.html, a CMS block or a third-party snippet.
Should I just use 'unsafe-hashes'?
Only for a handful of handlers you genuinely can't edit, like markup from a vendor. It lets the browser run an attribute whose exact text matches a hash you listed. That's far tighter than 'unsafe-inline', but MDN still labels it unsafe, and every edit to the handler text means a new hash.
Find inline handlers, javascript: links and a weak CSP on your homepage, along with the higher-severity problems worth fixing first.