aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/lib/action_view/helpers/csp_helper.rb
diff options
context:
space:
mode:
authorAndrew White <andrew.white@unboxed.co>2018-02-16 13:21:48 +0000
committerAndrew White <andrew.white@unboxed.co>2018-02-19 15:59:34 +0000
commit31abee0341cb9d19f0234da7b42dddbabfcd1d4a (patch)
treee9c45382ed7528c341d85df52f1b0f381ce0fd47 /actionview/lib/action_view/helpers/csp_helper.rb
parentdc6185b462dc423e9e6fa89a64aa54427ff7660d (diff)
downloadrails-31abee0341cb9d19f0234da7b42dddbabfcd1d4a.tar.gz
rails-31abee0341cb9d19f0234da7b42dddbabfcd1d4a.tar.bz2
rails-31abee0341cb9d19f0234da7b42dddbabfcd1d4a.zip
Add support for automatic nonce generation for Rails UJS
Because the UJS library creates a script tag to process responses it normally requires the script-src attribute of the content security policy to include 'unsafe-inline'. To work around this we generate a per-request nonce value that is embedded in a meta tag in a similar fashion to how CSRF protection embeds its token in a meta tag. The UJS library can then read the nonce value and set it on the dynamically generated script tag to enable it to execute without needing 'unsafe-inline' enabled. Nonce generation isn't 100% safe - if your script tag is including user generated content in someway then it may be possible to exploit an XSS vulnerability which can take advantage of the nonce. It is however an improvement on a blanket permission for inline scripts. It is also possible to use the nonce within your own script tags by using `nonce: true` to set the nonce value on the tag, e.g <%= javascript_tag nonce: true do %> alert('Hello, World!'); <% end %> Fixes #31689.
Diffstat (limited to 'actionview/lib/action_view/helpers/csp_helper.rb')
-rw-r--r--actionview/lib/action_view/helpers/csp_helper.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/actionview/lib/action_view/helpers/csp_helper.rb b/actionview/lib/action_view/helpers/csp_helper.rb
new file mode 100644
index 0000000000..e2e065c218
--- /dev/null
+++ b/actionview/lib/action_view/helpers/csp_helper.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+module ActionView
+ # = Action View CSP Helper
+ module Helpers #:nodoc:
+ module CspHelper
+ # Returns a meta tag "csp-nonce" with the per-session nonce value
+ # for allowing inline <script> tags.
+ #
+ # <head>
+ # <%= csp_meta_tag %>
+ # </head>
+ #
+ # This is used by the Rails UJS helper to create dynamically
+ # loaded inline <script> elements.
+ #
+ def csp_meta_tag
+ if content_security_policy?
+ tag("meta", name: "csp-nonce", content: content_security_policy_nonce)
+ end
+ end
+ end
+ end
+end