2021-02-16 16:14:43 +01:00
|
|
|
# Be sure to restart your server when you modify this file.
|
|
|
|
|
|
|
|
# Define an application-wide content security policy
|
|
|
|
# For further information see the following documentation
|
|
|
|
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
|
|
|
|
|
2019-05-03 15:25:51 +02:00
|
|
|
Rails.application.config.content_security_policy do |policy|
|
2022-02-02 12:32:49 +01:00
|
|
|
images_whitelist = ["*.openstreetmap.org", "*.cloud.ovh.net", "*"]
|
2022-02-09 12:03:46 +01:00
|
|
|
images_whitelist << URI(DS_PROXY_URL).host if DS_PROXY_URL.present?
|
2022-02-02 12:32:49 +01:00
|
|
|
images_whitelist << URI(MATOMO_IFRAME_URL).host if MATOMO_IFRAME_URL.present?
|
|
|
|
policy.img_src(:self, :data, :blob, *images_whitelist)
|
|
|
|
|
2022-02-22 17:17:55 +01:00
|
|
|
# Javascript: allow us, SendInBlue and Matomo.
|
|
|
|
# We need unsafe_inline because miniprofiler and us have some inline buttons :(
|
2022-05-05 16:54:30 +02:00
|
|
|
scripts_whitelist = ["*.crisp.chat", "crisp.chat", "cdn.jsdelivr.net", "maxcdn.bootstrapcdn.com", "code.jquery.com"]
|
2022-02-02 12:32:49 +01:00
|
|
|
scripts_whitelist << URI(MATOMO_IFRAME_URL).host if MATOMO_IFRAME_URL.present?
|
|
|
|
policy.script_src(:self, :unsafe_eval, :unsafe_inline, :blob, *scripts_whitelist)
|
|
|
|
|
2022-02-22 17:17:55 +01:00
|
|
|
# CSS: We have a lot of inline style, and some <style> tags.
|
|
|
|
# It's too complicated to be fixed right now (and it wouldn't add value: this is hardcoded in views, so not subject to injections)
|
|
|
|
policy.style_src(:self, :unsafe_inline, "*.crisp.chat", "crisp.chat", 'cdn.jsdelivr.net', 'maxcdn.bootstrapcdn.com')
|
2022-02-02 12:32:49 +01:00
|
|
|
|
2022-05-05 16:54:30 +02:00
|
|
|
connect_whitelist = ["wss://*.crisp.chat", "*.crisp.chat", "app.franceconnect.gouv.fr", "sentry.io", "openmaptiles.geo.data.gouv.fr", "openmaptiles.github.io", "tiles.geo.api.gouv.fr", "wxs.ign.fr"]
|
2022-02-09 12:03:46 +01:00
|
|
|
connect_whitelist << ENV.fetch('APP_HOST')
|
|
|
|
connect_whitelist << URI(DS_PROXY_URL).host if DS_PROXY_URL.present?
|
2022-02-02 12:32:49 +01:00
|
|
|
connect_whitelist << URI(API_ADRESSE_URL).host if API_ADRESSE_URL.present?
|
|
|
|
connect_whitelist << URI(API_EDUCATION_URL).host if API_EDUCATION_URL.present?
|
|
|
|
connect_whitelist << URI(API_GEO_URL).host if API_GEO_URL.present?
|
2022-02-15 15:20:59 +01:00
|
|
|
connect_whitelist << Rails.application.secrets.matomo[:host] if Rails.application.secrets.matomo[:enabled]
|
2022-02-02 12:32:49 +01:00
|
|
|
policy.connect_src(:self, *connect_whitelist)
|
|
|
|
|
2022-02-22 17:17:55 +01:00
|
|
|
# Frames: allow Matomo's iframe on the /suivi page
|
2022-02-15 15:31:31 +01:00
|
|
|
frame_whitelist = []
|
|
|
|
frame_whitelist << URI(MATOMO_IFRAME_URL).host if Rails.application.secrets.matomo[:enabled]
|
|
|
|
policy.frame_src(:self, *frame_whitelist)
|
|
|
|
|
2022-02-22 17:17:55 +01:00
|
|
|
# Everything else: allow us
|
|
|
|
# Add the error source in the violation notification
|
2022-02-02 12:32:49 +01:00
|
|
|
default_whitelist = ["fonts.gstatic.com", "in-automate.sendinblue.com", "player.vimeo.com", "app.franceconnect.gouv.fr", "sentry.io", "*.crisp.chat", "crisp.chat", "*.crisp.help", "*.sibautomation.com", "sibautomation.com", "data"]
|
2022-02-09 12:03:46 +01:00
|
|
|
default_whitelist << URI(DS_PROXY_URL).host if DS_PROXY_URL.present?
|
2022-02-02 12:32:49 +01:00
|
|
|
policy.default_src(:self, :data, :blob, :report_sample, *default_whitelist)
|
|
|
|
|
2020-03-17 17:41:58 +01:00
|
|
|
if Rails.env.development?
|
2022-02-22 17:24:12 +01:00
|
|
|
# Allow LiveReload requests
|
|
|
|
policy.connect_src(*policy.connect_src, "ws://localhost:3035", "http://localhost:3035")
|
|
|
|
|
2022-02-22 17:17:55 +01:00
|
|
|
# CSP are not enforced in development (see content_security_policy_report_only in development.rb)
|
|
|
|
# However we notify a random local URL, to see breakage in the DevTools when adding a new external resource.
|
2022-02-02 12:32:49 +01:00
|
|
|
policy.report_uri "http://#{ENV.fetch('APP_HOST')}/csp/"
|
2022-02-22 17:24:12 +01:00
|
|
|
|
|
|
|
elsif Rails.env.test?
|
|
|
|
# Disallow all connections to external domains during tests
|
|
|
|
policy.img_src(:self, :data, :blob)
|
|
|
|
policy.script_src(:self, :unsafe_eval, :unsafe_inline, :blob)
|
2022-03-31 12:07:52 +02:00
|
|
|
policy.style_src(:self)
|
2022-02-22 17:24:12 +01:00
|
|
|
policy.connect_src(:self)
|
|
|
|
policy.frame_src(:self)
|
|
|
|
policy.default_src(:self, :data, :blob)
|
|
|
|
|
2022-02-09 15:44:46 +01:00
|
|
|
else
|
|
|
|
policy.report_uri CSP_REPORT_URI if CSP_REPORT_URI.present?
|
2020-03-17 17:41:58 +01:00
|
|
|
end
|
2019-05-03 16:12:24 +02:00
|
|
|
end
|
2021-02-16 16:14:43 +01:00
|
|
|
|
|
|
|
# If you are using UJS then enable automatic nonce generation
|
|
|
|
# Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) }
|
|
|
|
|
|
|
|
# Set the nonce only to specific directives
|
|
|
|
# Rails.application.config.content_security_policy_nonce_directives = %w(script-src)
|
|
|
|
|
|
|
|
# Report CSP violations to a specified URI
|
|
|
|
# For further information see the following documentation:
|
|
|
|
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only
|
|
|
|
# Rails.application.config.content_security_policy_report_only = true
|