Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3a02917

Browse files
committedOct 2, 2024·
Add csp_meta_tag_nonce_attribute actionview configuration
Adds a configuration to allow the csp helper attribute name to be renamed. It is the backwards compatible `:content` value by default until the relevant JS libraries are updated to the new attribute name and Rails can ship with a new `:nonce` attribute name without breaking existing apps. Fixes rails#51580
1 parent 5fc266a commit 3a02917

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed
 

‎actionview/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
* Add a configuration `csp_meta_tag_nonce_attribute` to allow renaming the csp_meta_tag helper nonce attribute name.
2+
This allows to rename the `content` attribute to `nonce` to avoid certain kinds of value exfiltration attacks.
3+
4+
```
5+
app.config.action_view.csp_meta_tag_nonce_attribute = :nonce
6+
<%= csp_meta_tag %>
7+
# renders
8+
<meta name="csp-nonce" nonce="..." />
9+
10+
app.config.action_view.csp_meta_tag_nonce_attribute = :content (current default)
11+
# renders
12+
<meta name="csp-nonce" content="..." />
13+
```
14+
15+
*Niklas Häusele*
16+
117
## Rails 8.0.0.beta1 (September 26, 2024) ##
218
319
* Enable DependencyTracker to evaluate renders with trailing interpolation.

‎actionview/lib/action_view/helpers/csp_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module ActionView
44
module Helpers # :nodoc:
55
# = Action View CSP \Helpers
66
module CspHelper
7+
mattr_accessor :csp_meta_tag_nonce_attribute, default: :content
8+
79
# Returns a meta tag "csp-nonce" with the per-session nonce value
810
# for allowing inline <script> tags.
911
#
@@ -17,7 +19,7 @@ module CspHelper
1719
def csp_meta_tag(**options)
1820
if content_security_policy?
1921
options[:name] = "csp-nonce"
20-
options[:content] = content_security_policy_nonce
22+
options[csp_meta_tag_nonce_attribute] = content_security_policy_nonce
2123
tag("meta", options)
2224
end
2325
end

‎actionview/lib/action_view/railtie.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@ class Railtie < Rails::Engine # :nodoc:
1414
config.action_view.image_decoding = nil
1515
config.action_view.apply_stylesheet_media_default = true
1616
config.action_view.prepend_content_exfiltration_prevention = false
17+
config.action_view.csp_meta_tag_nonce_attribute = :content
1718

1819
config.eager_load_namespaces << ActionView
1920

21+
config.after_initialize do |app|
22+
ActionView::Helpers::CspHelper.csp_meta_tag_nonce_attribute =
23+
app.config.action_view.delete(:csp_meta_tag_nonce_attribute)
24+
end
25+
2026
config.after_initialize do |app|
2127
ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms =
2228
app.config.action_view.delete(:embed_authenticity_token_in_remote_forms)

‎actionview/test/template/csp_helper_test.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "abstract_unit"
4+
require "active_support/core_ext/object/with"
45

56
class CspHelperWithCspEnabledTest < ActionView::TestCase
67
tests ActionView::Helpers::CspHelper
@@ -13,7 +14,19 @@ def content_security_policy?
1314
true
1415
end
1516

16-
def test_csp_meta_tag
17+
def test_csp_meta_tag_uses_nonce_attribute_with_helper_nonce_attribute_name_nonce
18+
ActionView::Helpers::CspHelper.with(csp_meta_tag_nonce_attribute: :nonce) do
19+
assert_equal "<meta name=\"csp-nonce\" nonce=\"iyhD0Yc0W+c=\" />", csp_meta_tag
20+
end
21+
end
22+
23+
def test_csp_meta_tag_uses_nonce_attribute_with_helper_nonce_attribute_name_content
24+
ActionView::Helpers::CspHelper.with(csp_meta_tag_nonce_attribute: :content) do
25+
assert_equal "<meta name=\"csp-nonce\" content=\"iyhD0Yc0W+c=\" />", csp_meta_tag
26+
end
27+
end
28+
29+
def test_csp_meta_tag_with_helper_nonce_attribute_default_setting
1730
assert_equal "<meta name=\"csp-nonce\" content=\"iyhD0Yc0W+c=\" />", csp_meta_tag
1831
end
1932

0 commit comments

Comments
 (0)
Please sign in to comment.