Skip to content

Commit 5d3a54f

Browse files
authored
Merge pull request rails#54724 from francktrouillez/bugfix/nonce-false-removes-nonce-option
Make `nonce: false` remove the nonce attribute for `javascript_tag`, `javascript_include_tag` and `stylesheet_link_tag`
2 parents 257da89 + 71a41f8 commit 5d3a54f

File tree

5 files changed

+30
-0
lines changed

5 files changed

+30
-0
lines changed

actionview/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Make `nonce: false` remove the nonce attribute from `javascript_tag`, `javascript_include_tag`, and `stylesheet_link_tag`.
2+
3+
*francktrouillez*
4+
15
* Add `dom_target` helper to create `dom_id`-like strings from an unlimited
26
number of objects.
37

actionview/lib/action_view/helpers/asset_tag_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ def javascript_include_tag(*sources)
139139
}.merge!(options)
140140
if tag_options["nonce"] == true || (!tag_options.key?("nonce") && auto_include_nonce_for_scripts)
141141
tag_options["nonce"] = content_security_policy_nonce
142+
elsif tag_options["nonce"] == false
143+
tag_options.delete("nonce")
142144
end
143145
content_tag("script", "", tag_options)
144146
}.join("\n").html_safe
@@ -229,6 +231,8 @@ def stylesheet_link_tag(*sources)
229231
}.merge!(options)
230232
if tag_options["nonce"] == true || (!tag_options.key?("nonce") && auto_include_nonce_for_styles)
231233
tag_options["nonce"] = content_security_policy_nonce
234+
elsif tag_options["nonce"] == false
235+
tag_options.delete("nonce")
232236
end
233237

234238
if apply_stylesheet_media_default && tag_options["media"].blank?

actionview/lib/action_view/helpers/javascript_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ def javascript_tag(content_or_options_with_block = nil, html_options = {}, &bloc
8585

8686
if html_options[:nonce] == true || (!html_options.key?(:nonce) && auto_include_nonce)
8787
html_options[:nonce] = content_security_policy_nonce
88+
elsif html_options[:nonce] == false
89+
html_options.delete(:nonce)
8890
end
8991

9092
content_tag("script", javascript_cdata_section(content), html_options)

actionview/test/template/asset_tag_helper_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,10 @@ def test_javascript_include_tag_nonce_with_auto_nonce
564564
end
565565
end
566566

567+
def test_javascript_include_tag_nonce_false
568+
assert_dom_equal %(<script src="/javascripts/bank.js"></script>), javascript_include_tag("bank", nonce: false)
569+
end
570+
567571
def test_stylesheet_path
568572
StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
569573
end
@@ -594,6 +598,10 @@ def test_stylesheet_link_tag_nonce_with_auto_nonce
594598
end
595599
end
596600

601+
def test_stylesheet_link_tag_nonce_false
602+
assert_dom_equal %(<link rel="stylesheet" href="/stylesheets/foo.css"></link>), stylesheet_link_tag("foo.css", nonce: false)
603+
end
604+
597605
def test_stylesheet_link_tag_with_missing_source
598606
assert_nothing_raised {
599607
stylesheet_link_tag("missing_security_guard")

actionview/test/template/javascript_helper_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,16 @@ def test_javascript_tag_with_auto_nonce_for_content_security_policy
8787
assert_dom_equal "<script nonce=\"iyhD0Yc0W+c=\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
8888
javascript_tag("alert('hello')")
8989
end
90+
91+
def test_javascript_tag_nonce_true
92+
instance_eval { def content_security_policy_nonce = "iyhD0Yc0W+c=" }
93+
assert_dom_equal "<script nonce=\"iyhD0Yc0W+c=\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
94+
javascript_tag("alert('hello')", nonce: true)
95+
end
96+
97+
def test_javascript_tag_nonce_false
98+
instance_eval { def content_security_policy_nonce = "iyhD0Yc0W+c=" }
99+
assert_dom_equal "<script>\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
100+
javascript_tag("alert('hello')", nonce: false)
101+
end
90102
end

0 commit comments

Comments
 (0)