Skip to content

Commit fe3b34b

Browse files
committed
Add rename_csp_helper_nonce_attribute actionview configuration
Adds a configuration to rename the csp helper attribute name. It's disabled by default currently until the JS libraries are updated to the new attribute name and Rails can ship with a new default attribute name. Fixes rails#51580
1 parent d65fec4 commit fe3b34b

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

actionview/CHANGELOG.md

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
* Add a disabled configuration `rename_csp_helper_nonce_attribute` to rename the csp_meta_tag helper nonce attribute name
2+
If enabled, it renames the `content` attribute to `nonce` to avoid certain kinds of value exfiltration attacks.
3+
4+
```
5+
app.config.action_view.rename_csp_helper_nonce_attribute = true
6+
<%= csp_meta_tag %>
7+
# renders
8+
<meta name="csp-nonce" nonce="..." />
9+
# instead of
10+
<meta name="csp-nonce" content="..." />
11+
```
12+
13+
*Niklas Häusele*
14+
115
* Add queries count to template rendering instrumentation
216
317
```

actionview/lib/action_view/helpers/csp_helper.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ module ActionView
44
module Helpers # :nodoc:
55
# = Action View CSP \Helpers
66
module CspHelper
7+
mattr_accessor :rename_csp_helper_nonce_attribute
8+
self.rename_csp_helper_nonce_attribute = nil
9+
710
# Returns a meta tag "csp-nonce" with the per-session nonce value
811
# for allowing inline <script> tags.
912
#
@@ -17,7 +20,8 @@ module CspHelper
1720
def csp_meta_tag(**options)
1821
if content_security_policy?
1922
options[:name] = "csp-nonce"
20-
options[:content] = content_security_policy_nonce
23+
nonce_attribute_name = rename_csp_helper_nonce_attribute ? :nonce : :content
24+
options[nonce_attribute_name] = content_security_policy_nonce
2125
tag("meta", options)
2226
end
2327
end

actionview/lib/action_view/railtie.rb

+6
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.rename_csp_helper_nonce_attribute = false
1718

1819
config.eager_load_namespaces << ActionView
1920

21+
config.after_initialize do |app|
22+
ActionView::Helpers::CspHelper.rename_csp_helper_nonce_attribute =
23+
app.config.action_view.delete(:rename_csp_helper_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

+14-1
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_name_with_helper_nonce_attribute_enabled
18+
ActionView::Helpers::CspHelper.with(rename_csp_helper_nonce_attribute: true) 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_content_attribute_name_with_helper_nonce_attribute_disabled
24+
ActionView::Helpers::CspHelper.with(rename_csp_helper_nonce_attribute: false) 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)