Skip to content

Add optional nonce parameter to HotReload.script()#41

Open
davidpoblador wants to merge 1 commit intoflorimondmanca:masterfrom
davidpoblador:feat/script-nonce
Open

Add optional nonce parameter to HotReload.script()#41
davidpoblador wants to merge 1 commit intoflorimondmanca:masterfrom
davidpoblador:feat/script-nonce

Conversation

@davidpoblador
Copy link
Copy Markdown
Contributor

@davidpoblador davidpoblador commented Feb 24, 2026

Summary

Applications using Content Security Policy with nonce-based script-src directives currently can't use arel without either weakening their CSP or disabling hot-reload entirely. Every inline <script> tag needs to carry the per-request nonce, and HotReload.script() doesn't support that.

This adds an optional nonce parameter:

hotreload.script(url_for("hot-reload"), nonce=request.state.csp_nonce)

When provided, the generated tag becomes <script nonce="...">. When omitted, behavior is identical to today — fully backwards compatible, no changes needed for existing users.

This feature was suggested by @dlopcol

Test plan

  • Added test_script_nonce covering both default (no nonce) and nonce cases
  • All existing tests pass, 100% coverage maintained

Applications that enforce Content Security Policy with nonce-based
script-src directives need every inline script tag to carry the
request nonce. Without this, the hot-reload script gets blocked by
CSP or logged in report-only mode.

The new optional nonce parameter on script() causes the generated
tag to include a nonce attribute when a value is provided. Passing
no argument preserves the existing behavior, so this is fully
backwards compatible.
@florimondmanca
Copy link
Copy Markdown
Owner

@davidpoblador Looking perfect, thanks!

Is there any chance you could address the isort check failure? I'm away from a laptop right now.

I suppose running 'make format' after a fresh local 'make install' should fix it.

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