Skip to content

Commit 50c3882

Browse files
Gargronhiyuki2578
authored andcommitted
Fix media host not being included in connect-src for OCR (mastodon#11577)
1 parent 677e20c commit 50c3882

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

config/initializers/content_security_policy.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22
# For further information see the following documentation
33
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
44

5-
base_host = Rails.configuration.x.web_domain
5+
def host_to_url(str)
6+
"http#{Rails.configuration.x.use_https ? 's' : ''}://#{str}" unless str.blank?
7+
end
8+
9+
base_host = Rails.configuration.x.web_domain
10+
611
assets_host = Rails.configuration.action_controller.asset_host
7-
assets_host ||= "http#{Rails.configuration.x.use_https ? 's' : ''}://#{base_host}"
812
instance_ticker_host = "https://wee.jp"
13+
assets_host ||= host_to_url(base_host)
14+
15+
media_host = host_to_url(ENV['S3_ALIAS_HOST'])
16+
media_host ||= host_to_url(ENV['S3_CLOUDFRONT_HOST'])
17+
media_host ||= host_to_url(ENV['S3_HOSTNAME']) if ENV['S3_ENABLED'] == 'true'
18+
media_host ||= assets_host
919

1020
Rails.application.config.content_security_policy do |p|
1121
p.base_uri :none
@@ -21,10 +31,10 @@
2131
if Rails.env.development?
2232
webpacker_urls = %w(ws http).map { |protocol| "#{protocol}#{Webpacker.dev_server.https? ? 's' : ''}://#{Webpacker.dev_server.host_with_port}" }
2333

24-
p.connect_src :self, :data, :blob, assets_host, Rails.configuration.x.streaming_api_base_url, *webpacker_urls
34+
p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url, *webpacker_urls
2535
p.script_src :self, :blob, :unsafe_inline, :unsafe_eval, assets_host
2636
else
27-
p.connect_src :self, :data, :blob, assets_host, Rails.configuration.x.streaming_api_base_url
37+
p.connect_src :self, :data, :blob, assets_host, media_host, Rails.configuration.x.streaming_api_base_url
2838
p.script_src :self, :blob, assets_host
2939
end
3040
end

0 commit comments

Comments
 (0)