Skip to content

Commit 8c4af05

Browse files
authored
Merge pull request rails#50591 from akhilgkrishnan/add-nonce-stylesheet-link-tag
Add the nonce: true option for stylesheet_link_tag helper
2 parents 442d4b8 + 7fa6d15 commit 8c4af05

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

actionview/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
* Add the `nonce: true` option for `stylesheet_link_tag` helper to support automatic nonce generation for Content Security Policy.
2+
Works the same way as `javascript_include_tag nonce: true` does.
3+
4+
*Akhil G Krishnan*, *AJ Esler*
5+
16
* Parse `ActionView::TestCase#rendered` HTML content as `Nokogiri::XML::DocumentFragment` instead of `Nokogiri::XML::Document`
27

38
*Sean Doyle*

actionview/lib/action_view/helpers/asset_tag_helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ def javascript_include_tag(*sources)
190190
# stylesheet_link_tag "random.styles", "/css/stylish"
191191
# # => <link href="/assets/random.styles" rel="stylesheet" />
192192
# # <link href="/css/stylish.css" rel="stylesheet" />
193+
#
194+
# stylesheet_link_tag "style", nonce: true
195+
# # => <link href="/assets/style.css" rel="stylesheet" nonce="..." />
193196
def stylesheet_link_tag(*sources)
194197
options = sources.extract_options!.stringify_keys
195198
path_options = options.extract!("protocol", "extname", "host", "skip_pipeline").symbolize_keys
@@ -214,6 +217,9 @@ def stylesheet_link_tag(*sources)
214217
"crossorigin" => crossorigin,
215218
"href" => href
216219
}.merge!(options)
220+
if tag_options["nonce"] == true
221+
tag_options["nonce"] = content_security_policy_nonce
222+
end
217223

218224
if apply_stylesheet_media_default && tag_options["media"].blank?
219225
tag_options["media"] = "screen"

actionview/test/template/asset_tag_helper_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,10 @@ def test_stylesheet_link_tag
560560
StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
561561
end
562562

563+
def test_stylesheet_link_tag_nonce
564+
assert_dom_equal %(<link rel="stylesheet" href="/stylesheets/foo.css" nonce="iyhD0Yc0W+c="></link>), stylesheet_link_tag("foo.css", nonce: true)
565+
end
566+
563567
def test_stylesheet_link_tag_with_missing_source
564568
assert_nothing_raised {
565569
stylesheet_link_tag("missing_security_guard")

guides/source/security.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1283,10 +1283,11 @@ can be added to script tags by passing `nonce: true` as part of `html_options`:
12831283
<% end -%>
12841284
```
12851285

1286-
The same works with `javascript_include_tag`:
1286+
The same works with `javascript_include_tag` and the `stylesheet_link_tag`:
12871287

12881288
```html+erb
12891289
<%= javascript_include_tag "script", nonce: true %>
1290+
<%= stylesheet_link_tag "style.css", nonce: true %>
12901291
```
12911292

12921293
Use [`csp_meta_tag`](https://api.rubyonrails.org/classes/ActionView/Helpers/CspHelper.html#method-i-csp_meta_tag)

0 commit comments

Comments
 (0)