--- title: "hx-csp" description: "Make htmx work under strict Content Security Policy" category: "Security" icon: "icon-[mdi--shield-lock-outline]" keywords: ["nonce", "csp", "security", "trusted types", "xss", "content security policy"] --- The `hx-csp` extension provides Content Security Policy enforcement for htmx across three layers: nonce gating to prevent HTML injection, Trusted Types integration to restrict DOM writes, and safe eval to eliminate `unsafe-eval` from your CSP. ## Installing ```html ``` ## The hx-nonce Attribute The `hx-nonce` attribute is how the server signals to the extension that an htmx element was intentionally rendered by trusted server-side code. When htmx initialises an element, the extension checks that its `hx-nonce` matches the page nonce — if it doesn't match or is missing, all htmx attributes are stripped and the element is inert. Your templating engine should stamp `hx-nonce` on every element that carries htmx attributes: ```html ``` **Important:** `hx-nonce` alone does not guarantee XSS protection. It proves the element was rendered by the server, but it says nothing about the safety of the attribute values on that element. Any htmx attribute that includes user-supplied data must still be properly escaped by your templating engine: ```html
...
...
``` All major templating engines escape HTML entities (`"`, `'`, `<`, `>`, `&`) by default — make sure you are not bypassing this with raw/unescaped output filters (`| raw`, `| safe`, `raw()` etc.), which would allow a `"` in user input to break out of the attribute and inject new attributes. URL-valued attributes (`hx-get`, `hx-post`, etc.) are also blocked by the extension if the value is a `js:` or `javascript:` URI — these schemes survive entity encoding and are not valid action URLs. **Never put user-controlled data in JS-evaluated attributes.** `hx-on:*`, `hx-vals js:`, `hx-confirm js:`, and trigger filters are evaluated as JavaScript. HTML escaping does not protect you here — the browser decodes HTML entities *before* passing the string to the JS engine, so an attacker can break out of a string literal even if your template engine has escaped the value. There is no standard JavaScript string escaping filter in most templating language. Even frameworks that provide one (Django's `escapejs`, Rails' `escape_javascript`) require you to apply it correctly every time, and they may not protect against every injection vector in a JS context. The only safe pattern is to keep user data out of the attribute entirely and pass it via `data-*`: ```html ``` The extension reads the page nonce from the first `script[nonce]` element and blocks any htmx element whose `hx-nonce` doesn't match. If no page nonce is found, all htmx processing is blocked — fail closed. ## Trusted Types Loading `hx-csp` automatically creates a `'htmx'` Trusted Types policy. Add it to your CSP to enforce that only htmx can write HTML into DOM sinks: ```http Content-Security-Policy: script-src 'self' 'nonce-'; require-trusted-types-for 'script'; trusted-types htmx ``` No extra configuration needed. Falls back transparently on browsers without Trusted Types support. If `'htmx'` is not in your `trusted-types` whitelist, all htmx processing is blocked — fail closed. ## Safe Eval htmx's JS expression features (`hx-on:`, `hx-vals js:`, `hx-confirm js:`, trigger filters) are entirely optional. If you don't use them, you can omit `unsafe-eval` from your CSP entirely with no further configuration. If you do use these features, set `safeEval:true` to replace htmx's `new Function()` eval with nonce-based script injection, enabling them without `unsafe-eval`: ```http Content-Security-Policy: script-src 'self' 'nonce-' ``` ```html ``` ## Partial Responses Partial responses should include a `Content-Security-Policy` header with a fresh nonce. The extension rewrites the response nonce to the page nonce before fragment parsing so swapped-in elements pass subsequent nonce checks: ```http Content-Security-Policy: script-src 'nonce-' ``` ## Nonce Reuse Protection The extension rewrites the response nonce to the page nonce so swapped-in elements pass subsequent nonce checks. Before doing that rewrite, it scrubs any element that already carries the page nonce value from the raw response text. The server cannot know the page nonce — it only knows its own per-response nonce. So if the page nonce appears in a response, it was put there by an attacker, not the server. Scrubbing it first means the rewrite pass cannot accidentally promote attacker-controlled elements to trusted status. The risk: unlike `