Emit csp-nonce meta tag so Turbo-injected styles/scripts work under a strict CSP - #5
Open
infused wants to merge 1 commit into
Open
Emit csp-nonce meta tag so Turbo-injected styles/scripts work under a strict CSP#5infused wants to merge 1 commit into
infused wants to merge 1 commit into
Conversation
The bundled Turbo injects its progress-bar <style> (and an async-import <script> shim) at runtime and reads the nonce from a <meta name="csp-nonce"> tag. The layout never rendered that tag, so under a strict, nonce-based CSP (no 'unsafe-inline') those injected elements were nonce-less and blocked. Render Rails' csp_meta_tag in the layout head. It emits the meta only when a CSP is configured, so it is a no-op for hosts without one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4.
Under a strict, nonce-based CSP the dashboard's Turbo-injected progress-bar
<style>(and async-import<script>shim) are blocked, because the layout never provides thecsp-noncemeta that the bundled Turbo already reads (getMetaContent("csp-nonce")→ assigns.nonceon the elements it injects).Change
<%= csp_meta_tag %>aftercsrf_meta_tagsin the engine layout head. Rails'csp_meta_tagrenders the meta only when a CSP is present (if content_security_policy?), so it's a complete no-op for apps without CSP — no behavior change for existing users.Test
test/integration/csp_nonce_test.rbloads the dashboard under a nonce-based CSP and asserts the layout emits<meta name="csp-nonce">carrying the same nonce as the response'sstyle-srcdirective, plus a no-op case asserting no meta is emitted when the host has no CSP.bin/testis green on both the full-host and API-only legs.Changelog
Fixedentry under[Unreleased].