aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/app/assets
diff options
context:
space:
mode:
authorAndrew White <pixeltrix@users.noreply.github.com>2018-02-22 15:32:23 +0000
committerGitHub <noreply@github.com>2018-02-22 15:32:23 +0000
commite20742f12b362676e8f69fe68c3193ad80a90172 (patch)
tree6e8a3c3b0206673cc7a27d67750af0f053acc96d /actionview/app/assets
parent1c36aa71bd352e3704f424991f77c780853b3ac4 (diff)
parent31abee0341cb9d19f0234da7b42dddbabfcd1d4a (diff)
downloadrails-e20742f12b362676e8f69fe68c3193ad80a90172.tar.gz
rails-e20742f12b362676e8f69fe68c3193ad80a90172.tar.bz2
rails-e20742f12b362676e8f69fe68c3193ad80a90172.zip
Merge pull request #32018 from rails/add-nonce-support-to-csp
Add support for automatic nonce generation for Rails UJS
Diffstat (limited to 'actionview/app/assets')
-rw-r--r--actionview/app/assets/javascripts/rails-ujs/utils/ajax.coffee4
-rw-r--r--actionview/app/assets/javascripts/rails-ujs/utils/csp.coffee4
2 files changed, 7 insertions, 1 deletions
diff --git a/actionview/app/assets/javascripts/rails-ujs/utils/ajax.coffee b/actionview/app/assets/javascripts/rails-ujs/utils/ajax.coffee
index cc0e037428..2a8f5659e3 100644
--- a/actionview/app/assets/javascripts/rails-ujs/utils/ajax.coffee
+++ b/actionview/app/assets/javascripts/rails-ujs/utils/ajax.coffee
@@ -1,7 +1,8 @@
+#= require ./csp
#= require ./csrf
#= require ./event
-{ CSRFProtection, fire } = Rails
+{ cspNonce, CSRFProtection, fire } = Rails
AcceptHeaders =
'*': '*/*'
@@ -65,6 +66,7 @@ processResponse = (response, type) ->
try response = JSON.parse(response)
else if type.match(/\b(?:java|ecma)script\b/)
script = document.createElement('script')
+ script.nonce = cspNonce()
script.text = response
document.head.appendChild(script).parentNode.removeChild(script)
else if type.match(/\b(xml|html|svg)\b/)
diff --git a/actionview/app/assets/javascripts/rails-ujs/utils/csp.coffee b/actionview/app/assets/javascripts/rails-ujs/utils/csp.coffee
new file mode 100644
index 0000000000..8d2d6ce447
--- /dev/null
+++ b/actionview/app/assets/javascripts/rails-ujs/utils/csp.coffee
@@ -0,0 +1,4 @@
+# Content-Security-Policy nonce for inline scripts
+cspNonce = Rails.cspNonce = ->
+ meta = document.querySelector('meta[name=csp-nonce]')
+ meta and meta.content