aboutsummaryrefslogtreecommitdiffstats
path: root/actionview
diff options
context:
space:
mode:
authoryuuji.yaginuma <yuuji.yaginuma@gmail.com>2019-02-14 14:15:55 +0900
committeryuuji.yaginuma <yuuji.yaginuma@gmail.com>2019-02-16 09:36:37 +0900
commit96937335d105c684bd271f8fd427ee20f2b3aaf6 (patch)
tree13e3d8176abae7ab33fbdc6c955f82cbd162f13e /actionview
parent3f186e30452b700483c42c8d07f095c7b5c031b8 (diff)
downloadrails-96937335d105c684bd271f8fd427ee20f2b3aaf6.tar.gz
rails-96937335d105c684bd271f8fd427ee20f2b3aaf6.tar.bz2
rails-96937335d105c684bd271f8fd427ee20f2b3aaf6.zip
Allow to pass options to `csp_meta_tag`
Currently `csp_meta_tag` generates `name` attribute only. However, in libraries like `Material-UI` and `JSS`, expect that the meta tag that contains the nonce with `property` attribute. https://material-ui.com/css-in-js/advanced/#how-does-one-implement-csp https://github.com/cssinjs/jss/blob/master/docs/csp.md This patch allows `csp_meta_tag` to specify arbitrary options and allows `nonce` to be passed to those libraries.
Diffstat (limited to 'actionview')
-rw-r--r--actionview/lib/action_view/helpers/csp_helper.rb6
-rw-r--r--actionview/test/template/csp_helper_test.rb4
2 files changed, 8 insertions, 2 deletions
diff --git a/actionview/lib/action_view/helpers/csp_helper.rb b/actionview/lib/action_view/helpers/csp_helper.rb
index e2e065c218..4415018845 100644
--- a/actionview/lib/action_view/helpers/csp_helper.rb
+++ b/actionview/lib/action_view/helpers/csp_helper.rb
@@ -14,9 +14,11 @@ module ActionView
# This is used by the Rails UJS helper to create dynamically
# loaded inline <script> elements.
#
- def csp_meta_tag
+ def csp_meta_tag(**options)
if content_security_policy?
- tag("meta", name: "csp-nonce", content: content_security_policy_nonce)
+ options[:name] = "csp-nonce"
+ options[:content] = content_security_policy_nonce
+ tag("meta", options)
end
end
end
diff --git a/actionview/test/template/csp_helper_test.rb b/actionview/test/template/csp_helper_test.rb
index 8bad25ba7d..1b7fd4665f 100644
--- a/actionview/test/template/csp_helper_test.rb
+++ b/actionview/test/template/csp_helper_test.rb
@@ -16,6 +16,10 @@ class CspHelperWithCspEnabledTest < ActionView::TestCase
def test_csp_meta_tag
assert_equal "<meta name=\"csp-nonce\" content=\"iyhD0Yc0W+c=\" />", csp_meta_tag
end
+
+ def test_csp_meta_tag_with_options
+ assert_equal "<meta property=\"csp-nonce\" name=\"csp-nonce\" content=\"iyhD0Yc0W+c=\" />", csp_meta_tag(property: "csp-nonce")
+ end
end
class CspHelperWithCspDisabledTest < ActionView::TestCase