Skip to content

handle nonce re-writing for hx-head extension - #4220

Open
MichaelWest22 wants to merge 1 commit into
bigskysoftware:four-devfrom
MichaelWest22:hx-head-csp
Open

MichaelWest22 wants to merge 1 commit into
bigskysoftware:four-devfrom
MichaelWest22:hx-head-csp

Conversation

@MichaelWest22

Copy link
Copy Markdown
Collaborator

Description

hx-head does not handle nonces in scripts well as they need to be processed with the original page nonce to work. So we should probably use the existing inlineScriptNonce config in hx-head to set the correct nonce on the inserted script tags. Also we can make hx-csp use htmx:before:response event for its nonce rewriting so it can work well with hx-head which can process the head data after it. This does mean the hx-csp extension needs to be loaded first.

Corresponding issue:

Testing

Please explain how you tested this change manually, and, if applicable, what new tests you added. If
you're making a change to just the website, you can omit this section.

Checklist

  • I have read the contribution guidelines
  • I have targeted this PR against the correct branch (master for website changes, dev for
    source changes)
  • This is either a bugfix, a documentation update, or a new feature that has been explicitly
    approved via an issue
  • I ran the test suite locally (npm run test) and verified that it succeeded

@scrhartley

scrhartley commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Won't setting the nonce in addNode in hx-head mean that outerHTML comparisons will have already detected an element as different, when only their nonce is different?

@scrhartley

Copy link
Copy Markdown
Contributor

Other observations:

  • When hx-csp is also in use, a TrustedTypes policy being in effect (require-trusted-types-for 'script') means that hx-head is doing things it's not allowed to.
  • Since I'm using <meta http-equiv="Content-Security-Policy", when I include a new one for hx-csp, then hx-head replacing it causes all sorts of errors.

@scrhartley

Copy link
Copy Markdown
Contributor

A limitation of supporting htmx.config.inlineScriptNonce is that of course it doesn't handle stylesheets. In htmx v4, to avoid needing htmx.config.inlineStyleNonce it uses adoptedStyleSheets, which is something hx-head doesn't support.

@scrhartley

Copy link
Copy Markdown
Contributor

I've now authored the following modifications to this PR: MichaelWest22/htmx#4 and MichaelWest22/htmx#5

For my last point about TrustedTypes support in hx-head, there seems to currently be no way for hx-head to use the policy registered by hx-csp. Should it just define it's own policy (I assume named something different so that 'allow-duplicates' is not required), or should there be a mechanism within htmx to allow sharing its policy?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants