-
-
Notifications
You must be signed in to change notification settings - Fork 632
Add CSP nonce to immediate hydration scripts #2398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Extract reusable `csp_nonce` method from `wrap_console_script_with_nonce` and use it in `generate_component_script` and `generate_store_script` so that immediate hydration inline scripts include the CSP nonce attribute when Content Security Policy is enabled. Co-Authored-By: Claude Opus 4.6 <[email protected]>
- `#csp_nonce` tests: keep singleton methods (necessary since `content_security_policy_nonce` doesn't exist on the test helper and RSpec partial double verification blocks mocking nonexistent methods) - `#generate_component_script`, `#generate_store_script`, and `#wrap_console_script_with_nonce` tests: mock `csp_nonce` directly instead of simulating the underlying Rails method Co-Authored-By: Claude Opus 4.6 <[email protected]>
Greptile OverviewGreptile SummaryThis PR fixes a Content Security Policy (CSP) compliance issue where immediate hydration inline scripts were missing the nonce attribute, causing browsers to block them under strict CSP. The fix extracts the CSP nonce retrieval logic into a reusable Key Changes:
The implementation follows the existing pattern from Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant Browser
participant RailsView
participant Helper
participant ProHelper
Browser->>RailsView: Request page with React component
RailsView->>ProHelper: generate_component_script(render_options)
ProHelper->>Helper: csp_nonce()
Helper->>Helper: Check respond_to?(:content_security_policy_nonce)
alt CSP enabled
Helper->>Helper: content_security_policy_nonce(:script)
Helper-->>ProHelper: Return nonce value
else CSP not enabled
Helper-->>ProHelper: Return nil
end
alt immediate_hydration enabled
ProHelper->>ProHelper: Create component spec JSON script
ProHelper->>ProHelper: Create immediate hydration script with nonce
ProHelper-->>RailsView: Component spec + immediate script
else immediate_hydration disabled
ProHelper->>ProHelper: Create component spec JSON script only
ProHelper-->>RailsView: Component spec only
end
RailsView-->>Browser: HTML with script tags
Browser->>Browser: Verify CSP nonce (if enabled)
Browser->>Browser: Execute immediate hydration script
|
- Fix Layout/MultilineMethodCallBraceLayout in generate_store_script - Replace double with instance_double for RSpec/VerifiedDoubles Co-Authored-By: Claude Opus 4.6 <[email protected]>
WalkthroughThis change introduces CSP nonce support to React on Rails by adding a public Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Fixes #2350
The immediate hydration inline
<script>tags inpro_helper.rb(generate_component_scriptandgenerate_store_script) were missing the CSP nonce attribute, causing browsers to block them when strict Content Security Policy is enabled.Changes
csp_noncemethod fromwrap_console_script_with_nonceinhelper.rbnonce: csp_nonceto both immediate hydrationcontent_tag(:script, ...)calls inpro_helper.rbcsp_nonce,generate_component_script, andgenerate_store_script🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Improvements
Tests