Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions src/ext/hx-csp.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,26 +170,36 @@
htmx_before_on_init: checkNonce,

// Rewrites response nonces to pageNonce in raw HTML before fragment parsing.
// Always scrubs stolen pageNonce. Only promotes response nonce for verified same-origin.
htmx_after_request: (elt, detail) => {
// Wraps ctx.response.raw.text so the rewrite happens before hx-head's head merge,
// which also wraps raw.text in htmx_before_response. hx-csp loads first so its
// wrapper is outermost and runs first.
htmx_before_response: (elt, detail) => {
if (!pageNonce) return false;
let ctx = detail.ctx;
let realText = ctx.response.raw.text.bind(ctx.response.raw);
ctx.response.raw.text = async () => {
let text = await realText();

// Always scrub stolen pageNonce from any response. The server cannot know the
// page nonce, so its presence indicates a stolen-nonce injection attempt.
text = rewriteNoncesInText(text, pageNonce, '');

// Only promote response nonce for verified same-origin responses
let responseURL = ctx?.response?.raw?.url;
if (responseURL) {
try {
if (new URL(responseURL).origin === location.origin) {
let responseNonce = extractNonceFromCSP(ctx?.response?.headers?.get('Content-Security-Policy'))
?? extractNonceFromMetaTag(text);
if (responseNonce && responseNonce !== pageNonce) {
text = rewriteNoncesInText(text, responseNonce);
}
}
} catch (_) {}
}

// Always scrub stolen pageNonce from any response. The server cannot know the
// page nonce, so its presence indicates a stolen-nonce injection attempt.
ctx.text = rewriteNoncesInText(ctx.text, pageNonce, '');

// Only promote response nonce for verified same-origin responses
let responseURL = ctx?.response?.raw?.url;
if (!responseURL) return; // can't verify origin; scrub only, no promotion
try { if (new URL(responseURL).origin !== location.origin) return; }
catch (_) { return; }

let responseNonce = extractNonceFromCSP(ctx?.response?.headers?.get('Content-Security-Policy'))
?? extractNonceFromMetaTag(ctx?.text);
if (responseNonce && responseNonce !== pageNonce) {
ctx.text = rewriteNoncesInText(ctx.text, responseNonce);
}
return text;
};
},

// Blocks boosted form submissions where an unnonced submitter overrides formaction.
Expand Down
1 change: 1 addition & 0 deletions src/ext/hx-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
function appendNode(newNode) {
let newElt = document.createElement(newNode.tagName)
for (const attr of newNode.attributes) newElt.setAttribute(attr.name, attr.value)
if (newNode.tagName === 'SCRIPT' && htmx.config.inlineScriptNonce) newElt.nonce = htmx.config.inlineScriptNonce
newElt.textContent = newNode.textContent

// stylesheet: await CSSOM or content will flash unstyled
Expand Down
11 changes: 11 additions & 0 deletions www/src/content/extensions/04-hx-head.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,14 @@ RC1 namespaces `hx-head` lifecycle events:
</head>
<div>Saved</div>
```

## Content Security Policy (CSP)

When pages use CSP nonces, scripts appended by hx-head must carry the live page
nonce or the browser will block them. Set [`inlineScriptNonce`](/reference/configuration)
to the page nonce on first load — hx-head stamps it onto every script it appends

Alternativly for full CSP enforcement (nonce-gating, TrustedTypes, safe eval), load
[`hx-csp`](/extensions/hx-csp) **before** hx-head. hx-csp rewrites response
nonces to the live page nonce in the raw HTML before hx-head parses it — if
loaded after, hx-head sees the original nonces first and the rewrite is too late
Loading