提交 47013a71 编写于 作者: Y Yaroslav Markin

Add the `nonce: true` option for `javascript_include_tag` helper.

上级 15d7fb90
* Add the `nonce: true` option for `javascript_include_tag` helper to
support automatic nonce generation for Content Security Policy.
Works the same way as `javascript_tag nonce: true` does.
*Yaroslav Markin*
* Remove `ActionView::Helpers::RecordTagHelper`.
*Yoshiyuki Hirano*
......
......@@ -55,6 +55,8 @@ module AssetTagHelper
# that path.
# * <tt>:skip_pipeline</tt> - This option is used to bypass the asset pipeline
# when it is set to true.
# * <tt>:nonce<tt> - When set to true, adds an automatic nonce value if
# you have Content Security Policy enabled.
#
# ==== Examples
#
......@@ -79,6 +81,9 @@ module AssetTagHelper
#
# javascript_include_tag "http://www.example.com/xmlhr.js"
# # => <script src="http://www.example.com/xmlhr.js"></script>
#
# javascript_include_tag "http://www.example.com/xmlhr.js", nonce: true
# # => <script src="http://www.example.com/xmlhr.js" nonce="..."></script>
def javascript_include_tag(*sources)
options = sources.extract_options!.stringify_keys
path_options = options.extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys
......@@ -90,6 +95,9 @@ def javascript_include_tag(*sources)
tag_options = {
"src" => href
}.merge!(options)
if tag_options["nonce"] == true
tag_options["nonce"] = content_security_policy_nonce
end
content_tag("script".freeze, "", tag_options)
}.join("\n").html_safe
......
......@@ -29,6 +29,10 @@ def url_for(*args)
"http://www.example.com"
end
def content_security_policy_nonce
"iyhD0Yc0W+c="
end
AssetPathToTag = {
%(asset_path("")) => %(),
%(asset_path(" ")) => %(),
......@@ -421,6 +425,10 @@ def test_javascript_include_tag_default_protocol
assert_dom_equal %(<script src="//assets.example.com/javascripts/prototype.js"></script>), javascript_include_tag("prototype")
end
def test_javascript_include_tag_nonce
assert_dom_equal %(<script src="/javascripts/bank.js" nonce="iyhD0Yc0W+c="></script>), javascript_include_tag("bank", nonce: true)
end
def test_stylesheet_path
StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
end
......
......@@ -1182,6 +1182,12 @@ as part of `html_options`. Example:
<% end -%>
```
The same works with `javascript_include_tag`:
```html+erb
<%= javascript_include_tag "script", nonce: true %>
```
Use [`csp_meta_tag`](http://api.rubyonrails.org/classes/ActionView/Helpers/CspHelper.html#method-i-csp_meta_tag)
helper to create a meta tag "csp-nonce" with the per-session nonce value
for allowing inline `<script>` tags.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册