Merge pull request #6983 from betagouv/csp-in-tests
Blocage des connexions vers des services externes pendant les tests
This commit is contained in:
commit
70fe0b2fd4
2 changed files with 25 additions and 15 deletions
|
@ -66,8 +66,8 @@ Rails.application.configure do
|
||||||
protocol: :http
|
protocol: :http
|
||||||
}
|
}
|
||||||
|
|
||||||
# Use Content-Security-Policy-Report-Only headers
|
# Disallow all connections to external domains during tests
|
||||||
config.content_security_policy_report_only = true
|
config.content_security_policy_report_only = false
|
||||||
|
|
||||||
config.active_job.queue_adapter = :test
|
config.active_job.queue_adapter = :test
|
||||||
config.active_storage.service = :test
|
config.active_storage.service = :test
|
||||||
|
|
|
@ -5,22 +5,20 @@
|
||||||
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
|
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
|
||||||
|
|
||||||
Rails.application.config.content_security_policy do |policy|
|
Rails.application.config.content_security_policy do |policy|
|
||||||
# Whitelist image
|
|
||||||
images_whitelist = ["*.openstreetmap.org", "*.cloud.ovh.net", "*"]
|
images_whitelist = ["*.openstreetmap.org", "*.cloud.ovh.net", "*"]
|
||||||
images_whitelist << URI(DS_PROXY_URL).host if DS_PROXY_URL.present?
|
images_whitelist << URI(DS_PROXY_URL).host if DS_PROXY_URL.present?
|
||||||
images_whitelist << URI(MATOMO_IFRAME_URL).host if MATOMO_IFRAME_URL.present?
|
images_whitelist << URI(MATOMO_IFRAME_URL).host if MATOMO_IFRAME_URL.present?
|
||||||
policy.img_src(:self, :data, :blob, *images_whitelist)
|
policy.img_src(:self, :data, :blob, *images_whitelist)
|
||||||
|
|
||||||
# Whitelist JS: nous, sendinblue et matomo
|
# Javascript: allow us, SendInBlue and Matomo.
|
||||||
# miniprofiler et nous avons quelques boutons inline :(
|
# We need unsafe_inline because miniprofiler and us have some inline buttons :(
|
||||||
scripts_whitelist = ["*.sendinblue.com", "*.crisp.chat", "crisp.chat", "*.sibautomation.com", "sibautomation.com", "cdn.jsdelivr.net", "maxcdn.bootstrapcdn.com", "code.jquery.com"]
|
scripts_whitelist = ["*.sendinblue.com", "*.crisp.chat", "crisp.chat", "*.sibautomation.com", "sibautomation.com", "cdn.jsdelivr.net", "maxcdn.bootstrapcdn.com", "code.jquery.com"]
|
||||||
scripts_whitelist << URI(MATOMO_IFRAME_URL).host if MATOMO_IFRAME_URL.present?
|
scripts_whitelist << URI(MATOMO_IFRAME_URL).host if MATOMO_IFRAME_URL.present?
|
||||||
policy.script_src(:self, :unsafe_eval, :unsafe_inline, :blob, *scripts_whitelist)
|
policy.script_src(:self, :unsafe_eval, :unsafe_inline, :blob, *scripts_whitelist)
|
||||||
|
|
||||||
# Pour les CSS, on a beaucoup de style inline et quelques balises <style>
|
# CSS: We have a lot of inline style, and some <style> tags.
|
||||||
# c'est trop compliqué pour être rectifié immédiatement (et sans valeur ajoutée:
|
# 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)
|
||||||
# c'est hardcodé dans les vues, donc pas injectable).
|
policy.style_src(:self, :unsafe_inline, "*.crisp.chat", "crisp.chat", 'cdn.jsdelivr.net', 'maxcdn.bootstrapcdn.com')
|
||||||
policy.style_src(:self, "*.crisp.chat", "crisp.chat", 'cdn.jsdelivr.net', 'maxcdn.bootstrapcdn.com', :unsafe_inline)
|
|
||||||
|
|
||||||
connect_whitelist = ["wss://*.crisp.chat", "*.crisp.chat", "in-automate.sendinblue.com", "app.franceconnect.gouv.fr", "sentry.io", "openmaptiles.geo.data.gouv.fr", "openmaptiles.github.io", "tiles.geo.api.gouv.fr", "wxs.ign.fr"]
|
connect_whitelist = ["wss://*.crisp.chat", "*.crisp.chat", "in-automate.sendinblue.com", "app.franceconnect.gouv.fr", "sentry.io", "openmaptiles.geo.data.gouv.fr", "openmaptiles.github.io", "tiles.geo.api.gouv.fr", "wxs.ign.fr"]
|
||||||
connect_whitelist << ENV.fetch('APP_HOST')
|
connect_whitelist << ENV.fetch('APP_HOST')
|
||||||
|
@ -31,22 +29,34 @@ Rails.application.config.content_security_policy do |policy|
|
||||||
connect_whitelist << Rails.application.secrets.matomo[:host] if Rails.application.secrets.matomo[:enabled]
|
connect_whitelist << Rails.application.secrets.matomo[:host] if Rails.application.secrets.matomo[:enabled]
|
||||||
policy.connect_src(:self, *connect_whitelist)
|
policy.connect_src(:self, *connect_whitelist)
|
||||||
|
|
||||||
|
# Frames: allow Matomo's iframe on the /suivi page
|
||||||
frame_whitelist = []
|
frame_whitelist = []
|
||||||
frame_whitelist << URI(MATOMO_IFRAME_URL).host if Rails.application.secrets.matomo[:enabled]
|
frame_whitelist << URI(MATOMO_IFRAME_URL).host if Rails.application.secrets.matomo[:enabled]
|
||||||
policy.frame_src(:self, *frame_whitelist)
|
policy.frame_src(:self, *frame_whitelist)
|
||||||
|
|
||||||
# Pour tout le reste, par défaut on accepte uniquement ce qui vient de chez nous
|
# Everything else: allow us
|
||||||
# et dans la notification on inclue la source de l'erreur
|
# Add the error source in the violation notification
|
||||||
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"]
|
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"]
|
||||||
default_whitelist << URI(DS_PROXY_URL).host if DS_PROXY_URL.present?
|
default_whitelist << URI(DS_PROXY_URL).host if DS_PROXY_URL.present?
|
||||||
policy.default_src(:self, :data, :blob, :report_sample, *default_whitelist)
|
policy.default_src(:self, :data, :blob, :report_sample, *default_whitelist)
|
||||||
|
|
||||||
if Rails.env.development?
|
if Rails.env.development?
|
||||||
# Les CSP ne sont pas appliquées en dev: on notifie cependant une url quelconque de la violation
|
# Allow LiveReload requests
|
||||||
# pour détecter les erreurs lors de l'ajout d'une nouvelle brique externe durant le développement
|
|
||||||
policy.report_uri "http://#{ENV.fetch('APP_HOST')}/csp/"
|
|
||||||
# En développement, quand bin/webpack-dev-server est utilisé, on autorise les requêtes faites par le live-reload
|
|
||||||
policy.connect_src(*policy.connect_src, "ws://localhost:3035", "http://localhost:3035")
|
policy.connect_src(*policy.connect_src, "ws://localhost:3035", "http://localhost:3035")
|
||||||
|
|
||||||
|
# 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.
|
||||||
|
policy.report_uri "http://#{ENV.fetch('APP_HOST')}/csp/"
|
||||||
|
|
||||||
|
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)
|
||||||
|
policy.style_src(:self)
|
||||||
|
policy.connect_src(:self)
|
||||||
|
policy.frame_src(:self)
|
||||||
|
policy.default_src(:self, :data, :blob)
|
||||||
|
|
||||||
else
|
else
|
||||||
policy.report_uri CSP_REPORT_URI if CSP_REPORT_URI.present?
|
policy.report_uri CSP_REPORT_URI if CSP_REPORT_URI.present?
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue