File tree 4 files changed +39
-2
lines changed
4 files changed +39
-2
lines changed Original file line number Diff line number Diff line change
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
+
1
15
* Add queries count to template rendering instrumentation
2
16
3
17
```
Original file line number Diff line number Diff line change @@ -4,6 +4,9 @@ module ActionView
4
4
module Helpers # :nodoc:
5
5
# = Action View CSP \Helpers
6
6
module CspHelper
7
+ mattr_accessor :rename_csp_helper_nonce_attribute
8
+ self . rename_csp_helper_nonce_attribute = nil
9
+
7
10
# Returns a meta tag "csp-nonce" with the per-session nonce value
8
11
# for allowing inline <script> tags.
9
12
#
@@ -17,7 +20,8 @@ module CspHelper
17
20
def csp_meta_tag ( **options )
18
21
if content_security_policy?
19
22
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
21
25
tag ( "meta" , options )
22
26
end
23
27
end
Original file line number Diff line number Diff line change @@ -14,9 +14,15 @@ class Railtie < Rails::Engine # :nodoc:
14
14
config . action_view . image_decoding = nil
15
15
config . action_view . apply_stylesheet_media_default = true
16
16
config . action_view . prepend_content_exfiltration_prevention = false
17
+ config . action_view . rename_csp_helper_nonce_attribute = false
17
18
18
19
config . eager_load_namespaces << ActionView
19
20
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
+
20
26
config . after_initialize do |app |
21
27
ActionView ::Helpers ::FormTagHelper . embed_authenticity_token_in_remote_forms =
22
28
app . config . action_view . delete ( :embed_authenticity_token_in_remote_forms )
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
3
require "abstract_unit"
4
+ require "active_support/core_ext/object/with"
4
5
5
6
class CspHelperWithCspEnabledTest < ActionView ::TestCase
6
7
tests ActionView ::Helpers ::CspHelper
@@ -13,7 +14,19 @@ def content_security_policy?
13
14
true
14
15
end
15
16
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
17
30
assert_equal "<meta name=\" csp-nonce\" content=\" iyhD0Yc0W+c=\" />" , csp_meta_tag
18
31
end
19
32
You can’t perform that action at this time.
0 commit comments