From 2d27c110610a848d30fe150c58bbd60ebf6fab7c Mon Sep 17 00:00:00 2001 From: ThibG Date: Thu, 11 Oct 2018 20:35:46 +0200 Subject: Set Content-Security-Policy rules through RoR's config (#8957) * Set CSP rules in RoR's configuration * Override CSP setting in the embed controller to allow frames --- config/initializers/1_hosts.rb | 29 ++++++++++++++++++++++++++ config/initializers/content_security_policy.rb | 25 ++++++++++++---------- config/initializers/ostatus.rb | 29 -------------------------- 3 files changed, 43 insertions(+), 40 deletions(-) create mode 100644 config/initializers/1_hosts.rb delete mode 100644 config/initializers/ostatus.rb (limited to 'config') diff --git a/config/initializers/1_hosts.rb b/config/initializers/1_hosts.rb new file mode 100644 index 000000000..757f1f735 --- /dev/null +++ b/config/initializers/1_hosts.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +port = ENV.fetch('PORT') { 3000 } +host = ENV.fetch('LOCAL_DOMAIN') { "localhost:#{port}" } +web_host = ENV.fetch('WEB_DOMAIN') { host } + +alternate_domains = ENV.fetch('ALTERNATE_DOMAINS') { '' } + +Rails.application.configure do + https = Rails.env.production? || ENV['LOCAL_HTTPS'] == 'true' + + config.x.local_domain = host + config.x.web_domain = web_host + config.x.use_https = https + config.x.use_s3 = ENV['S3_ENABLED'] == 'true' + config.x.use_swift = ENV['SWIFT_ENABLED'] == 'true' + + config.x.alternate_domains = alternate_domains.split(/\s*,\s*/) + + config.action_mailer.default_url_options = { host: web_host, protocol: https ? 'https://' : 'http://', trailing_slash: false } + + config.x.streaming_api_base_url = ENV.fetch('STREAMING_API_BASE_URL') do + if Rails.env.production? + "ws#{https ? 's' : ''}://#{web_host}" + else + "ws://#{ENV['REMOTE_DEV'] == 'true' ? host.split(':').first : 'localhost'}:4000" + end + end +end diff --git a/config/initializers/content_security_policy.rb b/config/initializers/content_security_policy.rb index 37f2c0d45..f84116d73 100644 --- a/config/initializers/content_security_policy.rb +++ b/config/initializers/content_security_policy.rb @@ -2,17 +2,20 @@ # For further information see the following documentation # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy -# Rails.application.config.content_security_policy do |p| -# p.default_src :self, :https -# p.font_src :self, :https, :data -# p.img_src :self, :https, :data -# p.object_src :none -# p.script_src :self, :https -# p.style_src :self, :https, :unsafe_inline -# -# # Specify URI for violation reports -# # p.report_uri "/csp-violation-report-endpoint" -# end +assets_host = Rails.configuration.action_controller.asset_host || "https://#{ENV['WEB_DOMAIN'] || ENV['LOCAL_DOMAIN']}" + +Rails.application.config.content_security_policy do |p| + p.base_uri :none + p.default_src :none + p.frame_ancestors :none + p.script_src :self, assets_host + p.font_src :self, assets_host + p.img_src :self, :https, :data, :blob + p.style_src :self, :unsafe_inline, assets_host + p.media_src :self, :data, assets_host + p.frame_src :self, :https + p.connect_src :self, assets_host, Rails.configuration.x.streaming_api_base_url +end # Report CSP violations to a specified URI # For further information see the following documentation: diff --git a/config/initializers/ostatus.rb b/config/initializers/ostatus.rb deleted file mode 100644 index 757f1f735..000000000 --- a/config/initializers/ostatus.rb +++ /dev/null @@ -1,29 +0,0 @@ -# frozen_string_literal: true - -port = ENV.fetch('PORT') { 3000 } -host = ENV.fetch('LOCAL_DOMAIN') { "localhost:#{port}" } -web_host = ENV.fetch('WEB_DOMAIN') { host } - -alternate_domains = ENV.fetch('ALTERNATE_DOMAINS') { '' } - -Rails.application.configure do - https = Rails.env.production? || ENV['LOCAL_HTTPS'] == 'true' - - config.x.local_domain = host - config.x.web_domain = web_host - config.x.use_https = https - config.x.use_s3 = ENV['S3_ENABLED'] == 'true' - config.x.use_swift = ENV['SWIFT_ENABLED'] == 'true' - - config.x.alternate_domains = alternate_domains.split(/\s*,\s*/) - - config.action_mailer.default_url_options = { host: web_host, protocol: https ? 'https://' : 'http://', trailing_slash: false } - - config.x.streaming_api_base_url = ENV.fetch('STREAMING_API_BASE_URL') do - if Rails.env.production? - "ws#{https ? 's' : ''}://#{web_host}" - else - "ws://#{ENV['REMOTE_DEV'] == 'true' ? host.split(':').first : 'localhost'}:4000" - end - end -end -- cgit