Skip to content

Commit 1e81a0a

Browse files
committed
Add nonced versions of Rails link/include tags
Adds support for nonced versions of Rails' CSS, JavaScript and Webpacker link/include tags. * nonced_stylesheet_link_tag * nonced_javascript_include_tag * nonced_javascript_pack_tag
1 parent d34fa27 commit 1e81a0a

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

docs/per_action_configuration.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ body {
6666
background-color: black;
6767
}
6868
<% end %>
69+
70+
<%= nonced_javascript_include_tag "include.js" %>
71+
72+
<%= nonced_javascript_pack_tag "pack.js" %>
73+
74+
<%= nonced_stylesheet_link_tag "link.css" %>
6975
```
7076

7177
becomes:

lib/secure_headers/view_helper.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,45 @@ module ViewHelpers
77
class UnexpectedHashedScriptException < StandardError; end
88

99
# Public: create a style tag using the content security policy nonce.
10-
# Instructs secure_headers to append a nonce to style/script-src directives.
10+
# Instructs secure_headers to append a nonce to style-src directive.
1111
#
1212
# Returns an html-safe style tag with the nonce attribute.
1313
def nonced_style_tag(content_or_options = {}, &block)
1414
nonced_tag(:style, content_or_options, block)
1515
end
1616

17+
# Public: create a stylesheet link tag using the content security policy nonce.
18+
# Instructs secure_headers to append a nonce to style-src directive.
19+
#
20+
# Returns an html-safe link tag with the nonce attribute.
21+
def nonced_stylesheet_link_tag(*args, &block)
22+
stylesheet_link_tag(*args, nonce: content_security_policy_nonce(:style), &block)
23+
end
24+
1725
# Public: create a script tag using the content security policy nonce.
18-
# Instructs secure_headers to append a nonce to style/script-src directives.
26+
# Instructs secure_headers to append a nonce to script-src directive.
1927
#
2028
# Returns an html-safe script tag with the nonce attribute.
2129
def nonced_javascript_tag(content_or_options = {}, &block)
2230
nonced_tag(:script, content_or_options, block)
2331
end
2432

33+
# Public: create a script src tag using the content security policy nonce.
34+
# Instructs secure_headers to append a nonce to script-src directive.
35+
#
36+
# Returns an html-safe script tag with the nonce attribute.
37+
def nonced_javascript_include_tag(*args, &block)
38+
javascript_include_tag(*args, nonce: content_security_policy_nonce(:script), &block)
39+
end
40+
41+
# Public: create a script Webpacker pack tag using the content security policy nonce.
42+
# Instructs secure_headers to append a nonce to script-src directive.
43+
#
44+
# Returns an html-safe script tag with the nonce attribute.
45+
def nonced_javascript_pack_tag(*args, &block)
46+
javascript_pack_tag(*args, nonce: content_security_policy_nonce(:script), &block)
47+
end
48+
2549
# Public: use the content security policy nonce for this request directly.
2650
# Instructs secure_headers to append a nonce to style/script-src directives.
2751
#

spec/lib/secure_headers/view_helpers_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def self.template
3939
}
4040
</style>
4141
42+
<%= nonced_javascript_include_tag "include.js" %>
43+
44+
<%= nonced_javascript_pack_tag "pack.js" %>
45+
46+
<%= nonced_stylesheet_link_tag "link.css" %>
47+
4248
TEMPLATE
4349
end
4450

@@ -64,6 +70,16 @@ def content_tag(type, content = nil, options = nil, &block)
6470
"<#{type}#{options}>#{content}</#{type}>"
6571
end
6672

73+
def javascript_include_tag(source, options = {})
74+
content_tag(:script, nil, options.merge(src: source))
75+
end
76+
77+
alias_method :javascript_pack_tag, :javascript_include_tag
78+
79+
def stylesheet_link_tag(source, options = {})
80+
content_tag(:link, nil, options.merge(href: source, rel: "stylesheet", media: "screen"))
81+
end
82+
6783
def result
6884
super(binding)
6985
end

0 commit comments

Comments
 (0)