Refused to execute inline script because it violates the following Content Security Policy
I am using "Turbolinks 5.1.0".
#Gemfile
gem 'turbolinks', '~> 5.1'
#Layout header
<%= javascript_include_tag "application", nonce: true %>
#config/initializers/content_security_policy.rb
Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }
#Google chrome console error VM32 application-ae291f799496478302742f713e72f20a7958b7077387b87e18ab98c51ec979c4.js:243
[Report Only] Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' https: 'unsafe-inline' 'nonce-UiVx2CiP0HHN9jOOSEG43g=='". Note that 'unsafe-inline' is ignored if either a hash or nonce value is present in the source list.
n.assignNewBody @ VM32 application-ae291f799496478302742f713e72f20a7958b7077387b87e18ab98c51ec979c4.js:243 n.replaceBody @ VM32 application-ae291f799496478302742f713e72f20a7958b7077387b87e18ab98c51ec979c4.js:243 (anonymous) @ VM32 application-ae291f799496478302742f713e72f20a7958b7077387b87e18ab98c51ec979c4.js:243 t.renderView @ VM32 application-ae291f799496478302742f713e72f20a7958b7077387b87e18ab98c51ec979c4.js:243 n.render @ VM32 application-ae291f799496478302742f713e72f20a7958b7077387b87e18ab98c51ec979c4.js:243 t.render @ VM32 application-ae291f799496478302742f713e72f20a7958b7077387b87e18ab98c51ec979c4.js:243 e.renderSnapshot @ VM32 application-ae291f799496478302742f713e72f20a7958b7077387b87e18ab98c51ec979c4.js:243 e.render @ VM32 application-ae291f799496478302742f713e72f20a7958b7077387b87e18ab98c51ec979c4.js:243 t.render @ VM32 application-ae291f799496478302742f713e72f20a7958b7077387b87e18ab98c51ec979c4.js:243 (anonymous) @ VM32 application-ae291f799496478302742f713e72f20a7958b7077387b87e18ab98c51ec979c4.js:243 (anonymous) @ VM32 application-ae291f799496478302742f713e72f20a7958b7077387b87e18ab98c51ec979c4.js:243
To solve this problem,
1) Use the data-turbolinks-track: reload <%= javascript_include_tag "application", 'data-turbolinks-track': :reload, nonce: true %>
OR
2) Reuse the same nonce using session-store for turbolink requests, is this correct way of solving this problem?
Rails.application.config.content_security_policy_nonce_generator = -> request do
# use the same csp nonce for turbolinks requests
if request.env["HTTP_TURBOLINKS_REFERRER"].present? && request.session["mykey"].present?
request.session["mykey"]
else
request.session["mykey"] = SecureRandom.base64(16)
end
endPlease suggest the correct solution here!
Source: turbolinks/turbolinks