Reduce CSP configuration changes with upstream (#2536)
parent
7b7b6d234b
commit
19e14654cc
|
@ -10,11 +10,15 @@ class ContentSecurityPolicy
|
||||||
end
|
end
|
||||||
|
|
||||||
def media_hosts
|
def media_hosts
|
||||||
[assets_host, cdn_host_value].compact
|
[assets_host, cdn_host_value].concat(extra_data_hosts).compact
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def extra_data_hosts
|
||||||
|
ENV.fetch('EXTRA_DATA_HOSTS', '').split('|')
|
||||||
|
end
|
||||||
|
|
||||||
def url_from_configured_asset_host
|
def url_from_configured_asset_host
|
||||||
Rails.configuration.action_controller.asset_host
|
Rails.configuration.action_controller.asset_host
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,13 +6,11 @@
|
||||||
# See the Securing Rails Applications Guide for more information:
|
# See the Securing Rails Applications Guide for more information:
|
||||||
# https://guides.rubyonrails.org/security.html#content-security-policy-header
|
# https://guides.rubyonrails.org/security.html#content-security-policy-header
|
||||||
|
|
||||||
def host_to_url(str)
|
require_relative '../../app/lib/content_security_policy'
|
||||||
return if str.blank?
|
|
||||||
|
|
||||||
uri = Addressable::URI.parse("http#{Rails.configuration.x.use_https ? 's' : ''}://#{str}")
|
policy = ContentSecurityPolicy.new
|
||||||
uri.path += '/' unless uri.path.blank? || uri.path.end_with?('/')
|
assets_host = policy.assets_host
|
||||||
uri.to_s
|
media_hosts = policy.media_hosts
|
||||||
end
|
|
||||||
|
|
||||||
def sso_host
|
def sso_host
|
||||||
return unless ENV['ONE_CLICK_SSO_LOGIN'] == 'true'
|
return unless ENV['ONE_CLICK_SSO_LOGIN'] == 'true'
|
||||||
|
@ -32,51 +30,35 @@ def sso_host
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
unless Rails.env.development?
|
Rails.application.config.content_security_policy do |p|
|
||||||
assets_host = Rails.configuration.action_controller.asset_host || "https://#{ENV['WEB_DOMAIN'] || Rails.configuration.x.local_domain}"
|
p.base_uri :none
|
||||||
data_hosts = [assets_host]
|
p.default_src :none
|
||||||
|
p.frame_ancestors :none
|
||||||
|
p.font_src :self, assets_host
|
||||||
|
p.img_src :self, :data, :blob, *media_hosts
|
||||||
|
p.style_src :self, assets_host
|
||||||
|
p.media_src :self, :data, *media_hosts
|
||||||
|
p.frame_src :self, :https
|
||||||
|
p.manifest_src :self, assets_host
|
||||||
|
|
||||||
if ENV['S3_ENABLED'] == 'true' || ENV['AZURE_ENABLED'] == 'true'
|
if sso_host.present?
|
||||||
attachments_host = host_to_url(ENV['S3_ALIAS_HOST'] || ENV['S3_CLOUDFRONT_HOST'] || ENV['AZURE_ALIAS_HOST'] || ENV['S3_HOSTNAME'] || "s3-#{ENV['S3_REGION'] || 'us-east-1'}.amazonaws.com")
|
p.form_action :self, sso_host
|
||||||
elsif ENV['SWIFT_ENABLED'] == 'true'
|
|
||||||
attachments_host = ENV.fetch('SWIFT_OBJECT_URL')
|
|
||||||
attachments_host = "https://#{Addressable::URI.parse(attachments_host).host}"
|
|
||||||
else
|
else
|
||||||
attachments_host = nil
|
p.form_action :self
|
||||||
end
|
end
|
||||||
|
|
||||||
data_hosts << attachments_host unless attachments_host.nil?
|
p.child_src :self, :blob, assets_host
|
||||||
|
p.worker_src :self, :blob, assets_host
|
||||||
|
|
||||||
if ENV['PAPERCLIP_ROOT_URL']
|
if Rails.env.development?
|
||||||
url = Addressable::URI.parse(assets_host) + ENV['PAPERCLIP_ROOT_URL']
|
webpacker_public_host = ENV.fetch('WEBPACKER_DEV_SERVER_PUBLIC', Webpacker.config.dev_server[:public])
|
||||||
data_hosts << "https://#{url.host}"
|
webpacker_urls = %w(ws http).map { |protocol| "#{protocol}#{Webpacker.dev_server.https? ? 's' : ''}://#{webpacker_public_host}" }
|
||||||
end
|
|
||||||
|
|
||||||
data_hosts.concat(ENV['EXTRA_DATA_HOSTS'].split('|')) if ENV['EXTRA_DATA_HOSTS']
|
p.connect_src :self, :data, :blob, *media_hosts, Rails.configuration.x.streaming_api_base_url, *webpacker_urls
|
||||||
|
p.script_src :self, :unsafe_inline, :unsafe_eval, assets_host
|
||||||
data_hosts.uniq!
|
else
|
||||||
|
p.connect_src :self, :data, :blob, *media_hosts, Rails.configuration.x.streaming_api_base_url
|
||||||
Rails.application.config.content_security_policy do |p|
|
p.script_src :self, assets_host, "'wasm-unsafe-eval'"
|
||||||
p.base_uri :none
|
|
||||||
p.default_src :none
|
|
||||||
p.frame_ancestors :none
|
|
||||||
p.font_src :self, assets_host
|
|
||||||
p.img_src :self, :data, :blob, *data_hosts
|
|
||||||
p.style_src :self, assets_host
|
|
||||||
p.media_src :self, :data, *data_hosts
|
|
||||||
p.frame_src :self, :https
|
|
||||||
p.manifest_src :self, assets_host
|
|
||||||
|
|
||||||
if sso_host.present?
|
|
||||||
p.form_action :self, sso_host
|
|
||||||
else
|
|
||||||
p.form_action :self
|
|
||||||
end
|
|
||||||
|
|
||||||
p.child_src :self, :blob, assets_host
|
|
||||||
p.worker_src :self, :blob, assets_host
|
|
||||||
p.connect_src :self, :blob, :data, Rails.configuration.x.streaming_api_base_url, *data_hosts
|
|
||||||
p.script_src :self, assets_host, "'wasm-unsafe-eval'"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ describe 'Content-Security-Policy' do
|
||||||
"form-action 'self'",
|
"form-action 'self'",
|
||||||
"child-src 'self' blob: https://cb6e6126.ngrok.io",
|
"child-src 'self' blob: https://cb6e6126.ngrok.io",
|
||||||
"worker-src 'self' blob: https://cb6e6126.ngrok.io",
|
"worker-src 'self' blob: https://cb6e6126.ngrok.io",
|
||||||
"connect-src 'self' blob: data: ws://localhost:4000 https://cb6e6126.ngrok.io",
|
"connect-src 'self' data: blob: https://cb6e6126.ngrok.io ws://localhost:4000",
|
||||||
"script-src 'self' https://cb6e6126.ngrok.io 'wasm-unsafe-eval'"
|
"script-src 'self' https://cb6e6126.ngrok.io 'wasm-unsafe-eval'"
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue