Skip to content

Commit 2afa399

Browse files
Add nonce to style tag
1 parent a8fde08 commit 2afa399

File tree

4 files changed

+7
-8
lines changed

4 files changed

+7
-8
lines changed

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ end
4141
namespace :style do
4242
desc "Build main.css from the SASS sources"
4343
task :build do
44-
css = BetterErrors::ErrorPageStyle.compiled_style(true)
44+
css = BetterErrors::ErrorPageStyle.compiled_css(true)
4545
File.open(File.expand_path("lib/better_errors/templates/main.css", File.dirname(__FILE__)), "w") do |f|
4646
f.write(css)
4747
end

lib/better_errors/error_page_style.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module BetterErrors
44
# @private
55
module ErrorPageStyle
6-
def self.compiled_style(for_deployment = false)
6+
def self.compiled_css(for_deployment = false)
77
style_dir = File.expand_path("style", File.dirname(__FILE__))
88
style_file = "#{style_dir}/main.scss"
99

@@ -17,14 +17,14 @@ def self.compiled_style(for_deployment = false)
1717
engine.render
1818
end
1919

20-
def self.style_tag
20+
def self.style_tag(csp_nonce)
2121
style_file = File.expand_path("templates/main.css", File.dirname(__FILE__))
2222
css = if File.exist?(style_file)
2323
File.open(style_file).read
2424
else
25-
compiled_style(false)
25+
compiled_css(false)
2626
end
27-
"<style type='text/css'>\n#{css}\n</style>"
27+
"<style type='text/css' nonce='#{csp_nonce}'>\n#{css}\n</style>"
2828
end
2929
end
3030
end

lib/better_errors/middleware.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ def show_error_page(env, exception=nil)
119119
# for older browsers without nonce support.
120120
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src
121121
"script-src 'self' 'nonce-#{csp_nonce}' 'unsafe-inline'",
122-
# Inline style is required by the syntax highlighter.
123-
"style-src 'self' 'unsafe-inline'",
122+
"style-src 'self' 'nonce-#{csp_nonce}' 'unsafe-inline'",
124123
"connect-src 'self'",
125124
"navigate-to 'self' #{BetterErrors.editor.scheme}",
126125
].join('; '),

lib/better_errors/templates/main.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</head>
66
<body class="better-errors-javascript-not-loaded">
77
<%# Stylesheets are placed in the <body> for Turbolinks compatibility. %>
8-
<%== ErrorPageStyle.style_tag %>
8+
<%== ErrorPageStyle.style_tag(csp_nonce) %>
99

1010
<%# IE8 compatibility crap %>
1111
<script nonce="<%= csp_nonce %>">

0 commit comments

Comments
 (0)