Summary
HTMX v4 beta appears to copy style attributes during the settle phase of innerHTML swaps and history restore.
That results in calls equivalent to:
element.setAttribute("style", ...)
Under a strict CSP, navigation still works, but the browser logs repeated CSP violations.
Version
CSP
Reproduced with a policy like:
Content-Security-Policy:
default-src 'self';
script-src 'self' 'nonce-<N>' 'strict-dynamic';
style-src 'self' 'nonce-<N>';
style-src-attr 'unsafe-inline';
img-src 'self' data:;
font-src 'self';
connect-src 'self';
frame-ancestors 'none';
object-src 'none';
base-uri 'self';
form-action 'self';
inlineStyleNonce does not help here because the violation is for style="" attributes, not <style> elements.
Reproduction
This reproduces most clearly on browser back/forward navigation with HTMX history enabled.
Typical pattern:
- HTMX navigation swaps in content with
hx-swap="innerHTML"
- Old and new content contain matching element
ids
- Use the browser back button
- Console logs repeated CSP violations for inline style application
Actual behavior
History restore and settle succeed functionally, but the console logs CSP violations from style-attribute writes.
Expected behavior
Normal HTMX settle/history behavior should not require temporary style="" attribute writes in a way that breaks strict CSP.
Notes
From reading the v4 beta source, HTMX appears to temporarily copy old attributes onto matching new elements during settle, including style, then restore them later.
In our app, this specific path stopped when we set:
{
"morphIgnore": ["data-htmx-powered", "style"]
}
That suggests HTMX already has a partial escape hatch, but the default behavior still triggers CSP violations and the intended CSP-safe configuration is not clear.
Questions
- Is
morphIgnore: ["style"] the intended workaround for strict CSP?
- If yes, could that be documented explicitly?
- If not, is there a preferred HTMX-supported way to keep settle/history restore CSP-clean?
Suggested fix
One of these would help:
- skip style attribute copying by default during settle/history restore
- add a dedicated config for attributes ignored during settle
- document the official CSP-safe configuration if one already exists
Summary
HTMX v4 beta appears to copy
styleattributes during the settle phase ofinnerHTMLswaps and history restore.That results in calls equivalent to:
Under a strict CSP, navigation still works, but the browser logs repeated CSP violations.
Version
htmx 4.0.0-beta1CSP
Reproduced with a policy like:
inlineStyleNoncedoes not help here because the violation is forstyle=""attributes, not<style>elements.Reproduction
This reproduces most clearly on browser back/forward navigation with HTMX history enabled.
Typical pattern:
hx-swap="innerHTML"idsActual behavior
History restore and settle succeed functionally, but the console logs CSP violations from style-attribute writes.
Expected behavior
Normal HTMX settle/history behavior should not require temporary
style=""attribute writes in a way that breaks strict CSP.Notes
From reading the v4 beta source, HTMX appears to temporarily copy old attributes onto matching new elements during settle, including
style, then restore them later.In our app, this specific path stopped when we set:
{ "morphIgnore": ["data-htmx-powered", "style"] }That suggests HTMX already has a partial escape hatch, but the default behavior still triggers CSP violations and the intended CSP-safe configuration is not clear.
Questions
morphIgnore: ["style"]the intended workaround for strict CSP?Suggested fix
One of these would help: