v4 beta: settle/history restore copies style attributes and triggers CSP violations
### 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:
```js
element.setAttribute("style", ...)
```
Under a strict CSP, navigation still works, but the browser logs repeated CSP violations.
Version
- `htmx 4.0.0-beta1`
**CSP**
Reproduced with a policy like:
```css
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:
1. HTMX navigation swaps in content with `hx-swap="innerHTML"`
2. Old and new content contain matching element `id`s
3. Use the browser back button
4. 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:
```json
{
"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
关闭于 2026-04-19 4 条评论