CSP + HTMX: When Security Breaks Your Frontend (and How to Fix It)
HTMX is a great tool for dynamic content without JavaScript frameworks. But when you enable strict Content Security Policy (CSP), things break fast. In this article we show a real problem from our admin panel and how we solved it.
The Problem: hx-on:* = eval()
HTMX has the hx-on:change="this.form.submit()" attribute, an intuitive way to react to changes. Under the hood, HTMX uses the Function() constructor, which is an internal alias for eval().
With strict CSP:
Content-Security-Policy: script-src 'self' 'nonce-abc123'
The browser blocks:
Uncaught EvalError: call to Function() blocked by CSP
HTMX hx-on:* attributes are incompatible with strict CSP.
Our Architecture Test
In Nativa Minimal we have an architecture test that blocks all inline event handlers in PHP source:
// tests/Architecture/CorePatternTest.php
if (preg_match_all(
'/\bon(?:click|submit|mouseover|change|input|keydown)\s*=/',
$content,
$matches
)) {
$violations[] = $file . ':' . $line . ' → ' . $match;
}
This catches not just hx-on:*, but also onchange=, onclick=, etc. The reason: CSP blocks all inline handlers, not just HTMX-specific ones.
CSP-Safe Patterns
1. Selects with HTMX
Bad (blocks CSP):
<select hx-on:change="this.form.submit()">
<option>Newest</option>
<option>Most viewed</option>
</select>
Good (CSP-safe):
<select name="sort"
hx-get="/mark/media"
hx-trigger="change"
hx-target="#media-content"
hx-swap="outerHTML"
hx-include="closest form">
<option value="newest">Newest</option>
<option value="most_viewed">Most viewed</option>
</select>
Key attributes:
hx-trigger="change": HTMX native trigger, no evalhx-include="closest form": includes all form valueshx-get+hx-target: loads new content without reload
2. Form with Dropdowns + Hidden Fields
When a select changes value, we need to preserve other filter state:
<form method="GET" action="/mark/media">
<input type="hidden" name="type" value="image">
<input type="hidden" name="tag" value="nature">
<!-- HTMX pills — fast switching -->
<a href="/mark/media?type=image"
hx-get="/mark/media?type=image"
hx-target="#media-content"
hx-swap="outerHTML">Images</a>
<!-- HTMX selects — change trigger -->
<select name="sort"
hx-get="/mark/media"
hx-trigger="change"
hx-include="closest form"
hx-target="#media-content"
hx-swap="outerHTML">
<option value="newest">Newest</option>
<option value="most_viewed">Most viewed</option>
</select>
<select name="category"
hx-get="/mark/media"
hx-trigger="change"
hx-include="closest form"
hx-target="#media-content"
hx-swap="outerHTML">
<option value="">All categories</option>
<option value="photos">Photos</option>
</select>
</form>
When the user changes the sort dropdown:
- HTMX captures the
changeevent hx-include="closest form"takes all values (type, tag, sort, category)- Sends GET
/mark/media?type=image&tag=nature&sort=most_viewed&category= - Server returns new
#media-contentdiv - HTMX swaps it, no full page reload
3. Buttons with Confirmation
Bad:
<button onclick="return confirm('Delete?')">Delete</button>
Good (HTMX):
<button hx-delete="/mark/media/123/delete"
hx-confirm="Delete this file?"
hx-target="closest tr"
hx-swap="outerHTML swap:500ms">
Delete
</button>
hx-confirm is a native HTMX attribute, uses window.confirm(), not eval().
4. Inline Scripts with Nonce
When you need JavaScript, use a nonce:
<script nonce="f387a0e874ba54491c920ad26515af45">
(function(){
var sa = document.getElementById('select-all');
if (sa) sa.addEventListener('change', function(){
document.querySelectorAll('.checkbox').forEach(function(c){
c.checked = sa.checked;
});
});
})();
</script>
The nonce is generated per-request and CSP automatically allows it.
What HTMX CSP-Safe Can't Do
HTMX has several features that require eval() and are blocked by strict CSP:
- **
hx-on:*attributes**, use theFunction()constructor. Replace withhx-trigger+hx-get/post hx-vals='js:{...}': evaluates a JS object. Replace with hidden fields orhx-valswithout thejs:prefix- Trigger filters:
hx-trigger="click[isValid()]"evaluates a filter expression. Use standard triggers instead hx-confirm='js:...': JS-evaluated confirm. Usehx-confirm="Text"without thejs:prefix
CSP-safe HTMX features (work without eval()):
hx-get,hx-post,hx-delete: just URL stringshx-trigger="change",hx-trigger="click": native triggershx-target,hx-swap,hx-include: CSS selectorshx-valswithoutjs:prefix, key=value pairshx-ws(WebSockets), uses the nativeWebSocketAPI, noteval()hx-boost: intercepts links/formshx-confirm="Text": standard confirm dialog
Conclusion
HTMX + strict CSP works, but requires discipline:
- **Never use
hx-on:***, usehx-trigger+hx-get/postinstead - Test inline handlers: architecture tests catch problems early
- Use
hx-include="closest form": for dropdowns with multiple filters - Hidden fields for state: preserve values during partial updates
CSP is not an enemy of HTMX, it's a partner. You just need to know the rules.
CSP is not a restriction you must fight. It is a contract you must understand. When you know what eval() means in the browser, the fixes become obvious. The next person who reads your code should not need to guess why hx-on:change was replaced with hx-trigger. The architecture test explains it. CSP and operability must go together.
→ CSP onclick silent security bug: when onclick slips through CSP → Rate-limit cache fallback: what happens when the pipeline blocks you → Module isolation: why security lives where it lives
Sources
- htmx.org, Web Security Basics (with htmx): official HTMX security guide
- htmx.org, hx-csp extension: CSP extension for HTMX 4
- W3C, Content Security Policy Level 3: CSP specification
- MDN, Content-Security-Policy: MDN documentation
- HTMX, Trigger Filters: trigger filters and eval()
Comments (0)
No comments yet — be the first to react.