From b7f5f0ec101d3ba13e7490f6526af6fedd4fa0be Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 16 Aug 2019 01:54:36 +0200 Subject: [PATCH] Fix media host not being included in connect-src for OCR (#11577) --- config/initializers/content_security_policy.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/config/initializers/content_security_policy.rb b/config/initializers/content_security_policy.rb index 56e99793327..2dbc15a8da6 100644 --- a/config/initializers/content_security_policy.rb +++ b/config/initializers/content_security_policy.rb @@ -2,9 +2,19 @@ # For further information see the following documentation # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy -base_host = Rails.configuration.x.web_domain +def host_to_url(str) + "http#{Rails.configuration.x.use_https ? 's' : ''}://#{str}" unless str.blank? +end + +base_host = Rails.configuration.x.web_domain + assets_host = Rails.configuration.action_controller.asset_host -assets_host ||= "http#{Rails.configuration.x.use_https ? 's' : ''}://#{base_host}" +assets_host ||= host_to_url(base_host) + +media_host = host_to_url(ENV['S3_ALIAS_HOST']) +media_host ||= host_to_url(ENV['S3_CLOUDFRONT_HOST']) +media_host ||= host_to_url(ENV['S3_HOSTNAME']) if ENV['S3_ENABLED'] == 'true' +media_host ||= assets_host Rails.application.config.content_security_policy do |p| p.base_uri :none @@ -20,10 +30,10 @@ Rails.application.config.content_security_policy do |p| if Rails.env.development? webpacker_urls = %w(ws http).map { |protocol| "#{protocol}#{Webpacker.dev_server.https? ? 's' : ''}://#{Webpacker.dev_server.host_with_port}" } - p.connect_src :self, :data, :blob, assets_host, Rails.configuration.x.streaming_api_base_url, *webpacker_urls + p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url, *webpacker_urls p.script_src :self, :blob, :unsafe_inline, :unsafe_eval, assets_host else - p.connect_src :self, :data, :blob, assets_host, Rails.configuration.x.streaming_api_base_url + p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url p.script_src :self, :blob, assets_host end end