CSP and onclick: The Silent Bug That Teaches You the Rules
A button with onclick works for years. Then you deploy CSP and it breaks. Not a bug, a fair rule.
Why onclick = eval()
When the browser sees:
<button onclick="return confirm('Delete?')">Delete</button>
it compiles it to:
button.addEventListener('click', function(event) {
return Function('event', 'return confirm(\'Delete?\')').call(this, event);
})
Function() constructor = eval(). CSP with script-src 'self' 'nonce-...' blocks eval(): your button crashes.
How we catch it automatically
The project has an architecture test that scans PHP files:
// CorePatternTest.php
public function testNoOndefaultHandlersInPhp(): void
{
// Match onclick/onsubmit/onmouseover/etc. in PHP strings
if (preg_match_all(
'/\bon(?:click|submit|mouseover|mouseout|mouseenter|'
. 'mouseleave|focus|blur|change|input|keydown|keyup|'
. 'load|error)\s*=/',
$content, $matches
)) {
$violations[] = $file . ':' . $line . ' → ' . $match;
}
}
The test runs in CI, if someone adds onclick to a PHP template, the build fails. Automatically, no code review needed.
What to use instead of onclick
HTMX hx-confirm
Most common case, delete with confirmation:
<!-- Bad: onclick → eval() → CSP violation -->
<button onclick="return confirm('Delete?')">Delete</button>
<!-- Good: hx-confirm → window.confirm() → CSP-safe -->
<button hx-delete="/mark/media/123/delete"
hx-confirm="Delete this file?">
Delete
</button>
hx-confirm calls window.confirm(): native API, no eval.
data-* + addEventListener
When you need custom logic:
<!-- Bad -->
<button onclick="openPicker('cover')">Choose cover</button>
<!-- Good -->
<button data-action="open-picker" data-target="cover">Choose cover</button>
<script nonce="eeb968a7bce5bb5d66ec151a04c4163d">
document.addEventListener('click', function(e) {
var btn = e.target.closest('[data-action="open-picker"]');
if (!btn) return;
var target = btn.dataset.target;
if (typeof openPicker === 'function') openPicker(target);
});
</script>
Nonce script, CSP allows it because it has the correct nonce.
HTMX hx-on:smth
HTMX has hx-on:click attribute, but it internally uses the Function() constructor. So also eval(). Also crashes.
Instead: hx-get + hx-trigger="click" + hx-target.
Why this is fair
CSP doesn't block onclick because it's bad. It blocks it because:
- XSS protection: if an attacker injects
onclickinto your content, they getFunction()access - Non-reflected inline: inline handlers aren't nonce-ified (no
nonceattribute) - Principle of least privilege: less inline JS = less attack surface
Summary
| Pattern | CSP | Alternative |
|---|---|---|
onclick="..." | NO: eval() | data-* + addEventListener |
hx-on:click | NO: Function() | hx-trigger + hx-get/post |
hx-confirm="..." | YES: window.confirm() | , |
<script nonce="..."> | YES: nonce | , |
addEventListener | YES: addEventListener | , |
The architecture test catches onclick automatically in CI. You don't have to think about it, the test thinks for you.
One line of regex fixed the problem across the entire codebase. The solution was not a framework migration or a new dependency, just a pattern match that caught the mistake before it reached production.
The code you read tomorrow should not make you guess why onclick was replaced with addEventListener. The test explains it. The comment explains it. CSP and operability must go together.
→ CSP & htmx interaction: when nonces meet dynamic content → Rate-limit cache fallback: what happens when the pipeline blocks you → Module isolation: why security lives where it lives
Sources
- MDN, Content Security Policy: CSP specification
- W3C, CSP Level 3: official specification
- OWASP, DOM Based XSS Prevention Cheat Sheet: inline handler risks
- CSP Spec, Inline Event Handlers: why inline handlers need unsafe-inline
- htmx.org, Web Security Basics: HTMX and CSP
Comments (0)
No comments yet — be the first to react.