Skip to content

Commit 83eddbd

Browse files
authored
Merge pull request #373 from paulfri/webpacker-stylesheet-nonced-tag
Add missing `nonced_stylesheet_pack_tag`
2 parents 2d198d4 + 35a505d commit 83eddbd

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

docs/per_action_configuration.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ body {
7272
<%= nonced_javascript_pack_tag "pack.js" %>
7373
7474
<%= nonced_stylesheet_link_tag "link.css" %>
75+
76+
<%= nonced_stylesheet_pack_tag "pack.css" %>
7577
```
7678

7779
becomes:
@@ -136,4 +138,4 @@ end
136138
class SessionsController < ApplicationController
137139
after_action :clear_browser_cache, only: :destroy
138140
end
139-
```
141+
```

lib/secure_headers/view_helper.rb

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ def nonced_style_tag(content_or_options = {}, &block)
1919
#
2020
# Returns an html-safe link tag with the nonce attribute.
2121
def nonced_stylesheet_link_tag(*args, &block)
22-
stylesheet_link_tag(*args, nonce: content_security_policy_nonce(:style), &block)
22+
opts = extract_options(args).merge(nonce: content_security_policy_nonce(:style))
23+
24+
stylesheet_link_tag(*args, opts, &block)
2325
end
2426

2527
# Public: create a script tag using the content security policy nonce.
@@ -35,15 +37,29 @@ def nonced_javascript_tag(content_or_options = {}, &block)
3537
#
3638
# Returns an html-safe script tag with the nonce attribute.
3739
def nonced_javascript_include_tag(*args, &block)
38-
javascript_include_tag(*args, nonce: content_security_policy_nonce(:script), &block)
40+
opts = extract_options(args).merge(nonce: content_security_policy_nonce(:script))
41+
42+
javascript_include_tag(*args, opts, &block)
3943
end
4044

4145
# Public: create a script Webpacker pack tag using the content security policy nonce.
4246
# Instructs secure_headers to append a nonce to script-src directive.
4347
#
4448
# Returns an html-safe script tag with the nonce attribute.
4549
def nonced_javascript_pack_tag(*args, &block)
46-
javascript_pack_tag(*args, nonce: content_security_policy_nonce(:script), &block)
50+
opts = extract_options(args).merge(nonce: content_security_policy_nonce(:script))
51+
52+
javascript_pack_tag(*args, opts, &block)
53+
end
54+
55+
# Public: create a stylesheet Webpacker link tag using the content security policy nonce.
56+
# Instructs secure_headers to append a nonce to style-src directive.
57+
#
58+
# Returns an html-safe link tag with the nonce attribute.
59+
def nonced_stylesheet_pack_tag(*args, &block)
60+
opts = extract_options(args).merge(nonce: content_security_policy_nonce(:style))
61+
62+
stylesheet_pack_tag(*args, opts, &block)
4763
end
4864

4965
# Public: use the content security policy nonce for this request directly.
@@ -138,6 +154,14 @@ def nonced_tag(type, content_or_options, block)
138154
end
139155
content_tag type, content, options.merge(nonce: content_security_policy_nonce(type))
140156
end
157+
158+
def extract_options(args)
159+
if args.last.is_a? Hash
160+
args.pop
161+
else
162+
{}
163+
end
164+
end
141165
end
142166
end
143167

spec/lib/secure_headers/view_helpers_spec.rb

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ def self.template
3939
}
4040
</style>
4141
42-
<%= nonced_javascript_include_tag "include.js" %>
42+
<%= nonced_javascript_include_tag "include.js", defer: true %>
4343
44-
<%= nonced_javascript_pack_tag "pack.js" %>
44+
<%= nonced_javascript_pack_tag "pack.js", "otherpack.js", defer: true %>
4545
46-
<%= nonced_stylesheet_link_tag "link.css" %>
46+
<%= nonced_stylesheet_link_tag "link.css", media: :all %>
47+
48+
<%= nonced_stylesheet_pack_tag "pack.css", "otherpack.css", media: :all %>
4749
4850
TEMPLATE
4951
end
@@ -70,16 +72,22 @@ def content_tag(type, content = nil, options = nil, &block)
7072
"<#{type}#{options}>#{content}</#{type}>"
7173
end
7274

73-
def javascript_include_tag(source, options = {})
74-
content_tag(:script, nil, options.merge(src: source))
75+
def javascript_include_tag(*sources, **options)
76+
sources.map do |source|
77+
content_tag(:script, nil, options.merge(src: source))
78+
end
7579
end
7680

7781
alias_method :javascript_pack_tag, :javascript_include_tag
7882

79-
def stylesheet_link_tag(source, options = {})
80-
content_tag(:link, nil, options.merge(href: source, rel: "stylesheet", media: "screen"))
83+
def stylesheet_link_tag(*sources, **options)
84+
sources.map do |source|
85+
content_tag(:link, nil, options.merge(href: source, rel: "stylesheet", media: "screen"))
86+
end
8187
end
8288

89+
alias_method :stylesheet_pack_tag, :stylesheet_link_tag
90+
8391
def result
8492
super(binding)
8593
end

0 commit comments

Comments
 (0)