Skip to content

Commit

Permalink
Merge pull request #296 from mikelkew/csp-nonce
Browse files Browse the repository at this point in the history
Add CSP nonce to injected scripts and styles
  • Loading branch information
gsamokovarov committed Aug 19, 2020
2 parents e1ed9e6 + bc5d230 commit 28dfbc4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/web_console/templates/console.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ var promptBoxHtml = <%= render_inlined_string '_prompt_box_markup.html' %>;
var consoleStyleCss = <%= render_inlined_string 'style.css' %>;
// Insert a style element with the unique ID
var styleElementId = 'sr02459pvbvrmhco';
// Nonce to use for CSP
var styleElementNonce = '<%= @nonce %>';

// REPLConsole Constructor
function REPLConsole(config) {
Expand Down Expand Up @@ -441,6 +443,9 @@ REPLConsole.prototype.insertCss = function() {
style.type = 'text/css';
style.innerHTML = consoleStyleCss;
style.id = styleElementId;
if (styleElementNonce.length > 0) {
style.nonce = styleElementNonce;
}
document.getElementsByTagName('head')[0].appendChild(style);
};

Expand Down
2 changes: 1 addition & 1 deletion lib/web_console/templates/layouts/javascript.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<script type="text/javascript" data-template="<%= @template %>">
<script type="text/javascript" data-template="<%= @template %>" nonce="<%= @nonce %>">
(function() {
<%= yield %>
}).call(this);
Expand Down
1 change: 1 addition & 0 deletions lib/web_console/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def only_on_regular_page(*args)
# leaking globals, unless you explicitly want to.
def render_javascript(template)
assign(template: template)
assign(nonce: @env["action_dispatch.content_security_policy_nonce"])
render(template: template, layout: "layouts/javascript")
end

Expand Down

0 comments on commit 28dfbc4

Please sign in to comment.