Skip to content

Commit 7801462

Browse files
authored
Merge pull request #292 from twitter/view_helper
add content_security_policy_script/style_nonce to view helper
2 parents a85b80c + 6f0c453 commit 7801462

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/secure_headers/view_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ def content_security_policy_nonce(type)
3434
end
3535
end
3636

37+
def content_security_policy_script_nonce
38+
content_security_policy_nonce(:script)
39+
end
40+
41+
def content_security_policy_style_nonce
42+
content_security_policy_nonce(:style)
43+
end
44+
3745
##
3846
# Checks to see if the hashed code is expected and adds the hash source
3947
# value to the current CSP.

spec/lib/secure_headers/view_helpers_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ def self.template
2727
background-color: black;
2828
}
2929
<% end %>
30+
31+
<script nonce="<%= content_security_policy_script_nonce %>">
32+
alert(1)
33+
</script>
34+
35+
<style nonce="<%= content_security_policy_style_nonce %>">
36+
body {
37+
background-color: black;
38+
}
39+
</style>
3040
<%= @name %>
3141
3242
TEMPLATE

0 commit comments

Comments
 (0)