Skip to content

Commit 35a505d

Browse files
committed
Support multiple sources in asset tag helpers
1 parent ba57531 commit 35a505d

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

lib/secure_headers/view_helper.rb

Lines changed: 23 additions & 7 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.
@@ -34,24 +36,30 @@ def nonced_javascript_tag(content_or_options = {}, &block)
3436
# Instructs secure_headers to append a nonce to script-src directive.
3537
#
3638
# Returns an html-safe script tag with the nonce attribute.
37-
def nonced_javascript_include_tag(*args, **kwargs, &block)
38-
javascript_include_tag(*args, kwargs.merge(nonce: content_security_policy_nonce(:script)), &block)
39+
def nonced_javascript_include_tag(*args, &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.
45-
def nonced_javascript_pack_tag(*args, **kwargs, &block)
46-
javascript_pack_tag(*args, kwargs.merge(nonce: content_security_policy_nonce(:script)), &block)
49+
def nonced_javascript_pack_tag(*args, &block)
50+
opts = extract_options(args).merge(nonce: content_security_policy_nonce(:script))
51+
52+
javascript_pack_tag(*args, opts, &block)
4753
end
4854

4955
# Public: create a stylesheet Webpacker link tag using the content security policy nonce.
5056
# Instructs secure_headers to append a nonce to style-src directive.
5157
#
5258
# Returns an html-safe link tag with the nonce attribute.
53-
def nonced_stylesheet_pack_tag(*args, **kwargs, &block)
54-
stylesheet_pack_tag(*args, kwargs.merge(nonce: content_security_policy_nonce(:style)), &block)
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)
5563
end
5664

5765
# Public: use the content security policy nonce for this request directly.
@@ -146,6 +154,14 @@ def nonced_tag(type, content_or_options, block)
146154
end
147155
content_tag type, content, options.merge(nonce: content_security_policy_nonce(type))
148156
end
157+
158+
def extract_options(args)
159+
if args.last.is_a? Hash
160+
args.pop
161+
else
162+
{}
163+
end
164+
end
149165
end
150166
end
151167

spec/lib/secure_headers/view_helpers_spec.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +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", defer: true %>
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 %>
4747
48-
<%= nonced_stylesheet_pack_tag "pack.css", media: :all %>
48+
<%= nonced_stylesheet_pack_tag "pack.css", "otherpack.css", media: :all %>
4949
5050
TEMPLATE
5151
end
@@ -72,14 +72,18 @@ def content_tag(type, content = nil, options = nil, &block)
7272
"<#{type}#{options}>#{content}</#{type}>"
7373
end
7474

75-
def javascript_include_tag(source, options = {})
76-
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
7779
end
7880

7981
alias_method :javascript_pack_tag, :javascript_include_tag
8082

81-
def stylesheet_link_tag(source, options = {})
82-
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
8387
end
8488

8589
alias_method :stylesheet_pack_tag, :stylesheet_link_tag

0 commit comments

Comments
 (0)