2018-11-30 22:51:45 +08:00
|
|
|
# frozen_string_literal: true
|
2022-03-21 22:28:52 +08:00
|
|
|
require "content_security_policy"
|
2018-11-30 22:51:45 +08:00
|
|
|
|
|
|
|
class ContentSecurityPolicy
|
|
|
|
class Default
|
|
|
|
attr_reader :directives
|
|
|
|
|
2023-07-28 19:53:44 +08:00
|
|
|
def initialize(base_url:)
|
2020-03-20 03:54:42 +08:00
|
|
|
@base_url = base_url
|
2018-11-30 22:51:45 +08:00
|
|
|
@directives =
|
|
|
|
{}.tap do |directives|
|
2021-06-10 08:53:10 +08:00
|
|
|
directives[:upgrade_insecure_requests] = [] if SiteSetting.force_https
|
2021-07-07 21:43:48 +08:00
|
|
|
directives[:base_uri] = [:self]
|
2019-01-10 04:04:50 +08:00
|
|
|
directives[:object_src] = [:none]
|
2018-11-30 22:51:45 +08:00
|
|
|
directives[:script_src] = script_src
|
|
|
|
directives[:worker_src] = worker_src
|
|
|
|
directives[
|
|
|
|
:report_uri
|
|
|
|
] = report_uri if SiteSetting.content_security_policy_collect_reports
|
2021-03-23 03:00:25 +08:00
|
|
|
directives[:frame_ancestors] = frame_ancestors if restrict_embed?
|
2021-06-08 21:32:31 +08:00
|
|
|
directives[:manifest_src] = ["'self'"]
|
2018-11-30 22:51:45 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-20 03:54:42 +08:00
|
|
|
def base_url
|
|
|
|
@base_url
|
|
|
|
end
|
2018-11-30 22:51:45 +08:00
|
|
|
|
|
|
|
SCRIPT_ASSET_DIRECTORIES = [
|
2020-08-14 19:30:39 +08:00
|
|
|
# [dir, can_use_s3_cdn, can_use_cdn, for_worker]
|
|
|
|
["/assets/", true, true, true],
|
|
|
|
["/extra-locales/", false, false, false],
|
|
|
|
["/highlight-js/", false, true, false],
|
|
|
|
["/javascripts/", false, true, true],
|
|
|
|
["/plugins/", false, true, true],
|
|
|
|
["/theme-javascripts/", false, true, false],
|
|
|
|
["/svg-sprite/", false, true, false],
|
2018-11-30 22:51:45 +08:00
|
|
|
]
|
|
|
|
|
2022-12-08 18:36:20 +08:00
|
|
|
def script_assets(
|
|
|
|
base = base_url,
|
|
|
|
s3_cdn = GlobalSetting.s3_asset_cdn_url.presence || GlobalSetting.s3_cdn_url,
|
|
|
|
cdn = GlobalSetting.cdn_url,
|
|
|
|
worker: false
|
|
|
|
)
|
2020-08-14 19:30:39 +08:00
|
|
|
SCRIPT_ASSET_DIRECTORIES
|
|
|
|
.map do |dir, can_use_s3_cdn, can_use_cdn, for_worker|
|
|
|
|
next if worker && !for_worker
|
2018-11-30 22:51:45 +08:00
|
|
|
if can_use_s3_cdn && s3_cdn
|
|
|
|
s3_cdn + dir
|
|
|
|
elsif can_use_cdn && cdn
|
2020-10-09 19:51:24 +08:00
|
|
|
cdn + Discourse.base_path + dir
|
2018-11-30 22:51:45 +08:00
|
|
|
else
|
|
|
|
base + dir
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2018-11-30 22:51:45 +08:00
|
|
|
end
|
2020-08-14 19:30:39 +08:00
|
|
|
.compact
|
2018-11-30 22:51:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def script_src
|
2024-02-16 19:16:54 +08:00
|
|
|
sources = []
|
|
|
|
|
|
|
|
if SiteSetting.content_security_policy_strict_dynamic
|
|
|
|
sources << "'strict-dynamic'"
|
|
|
|
else
|
|
|
|
sources.push(
|
|
|
|
"#{base_url}/logs/",
|
|
|
|
"#{base_url}/sidekiq/",
|
|
|
|
"#{base_url}/mini-profiler-resources/",
|
|
|
|
*script_assets,
|
|
|
|
)
|
2021-04-28 01:55:23 +08:00
|
|
|
|
|
|
|
# Support Ember CLI Live reload
|
|
|
|
if Rails.env.development?
|
|
|
|
sources << "#{base_url}/ember-cli-live-reload.js"
|
|
|
|
sources << "#{base_url}/_lr/"
|
|
|
|
end
|
|
|
|
|
2020-11-07 04:15:36 +08:00
|
|
|
# we need analytics.js still as gtag/js is a script wrapper for it
|
2020-10-14 01:20:41 +08:00
|
|
|
if SiteSetting.ga_universal_tracking_code.present?
|
|
|
|
sources << "https://www.google-analytics.com/analytics.js"
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2020-11-07 04:15:36 +08:00
|
|
|
if SiteSetting.ga_universal_tracking_code.present? && SiteSetting.ga_version == "v4_gtag"
|
|
|
|
sources << "https://www.googletagmanager.com/gtag/js"
|
2023-01-09 20:10:19 +08:00
|
|
|
end
|
2021-03-26 23:19:31 +08:00
|
|
|
if SiteSetting.gtm_container_id.present?
|
|
|
|
sources << "https://www.googletagmanager.com/gtm.js"
|
|
|
|
end
|
2018-11-30 22:51:45 +08:00
|
|
|
end
|
2024-02-16 19:16:54 +08:00
|
|
|
|
|
|
|
sources << :report_sample if SiteSetting.content_security_policy_collect_reports
|
|
|
|
|
|
|
|
sources
|
2018-11-30 22:51:45 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def worker_src
|
2024-02-16 19:16:54 +08:00
|
|
|
return [] if SiteSetting.content_security_policy_strict_dynamic
|
2018-11-30 22:51:45 +08:00
|
|
|
[
|
2020-08-15 01:15:30 +08:00
|
|
|
"'self'", # For service worker
|
2020-08-14 19:30:39 +08:00
|
|
|
*script_assets(worker: true),
|
2018-11-30 22:51:45 +08:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def report_uri
|
|
|
|
"#{base_url}/csp_reports"
|
|
|
|
end
|
2021-03-23 03:00:25 +08:00
|
|
|
|
|
|
|
def frame_ancestors
|
|
|
|
["'self'", *EmbeddableHost.pluck(:host).map { |host| "https://#{host}" }]
|
|
|
|
end
|
|
|
|
|
|
|
|
def restrict_embed?
|
|
|
|
SiteSetting.content_security_policy_frame_ancestors && !SiteSetting.embed_any_origin
|
|
|
|
end
|
2018-11-30 22:51:45 +08:00
|
|
|
end
|
|
|
|
end
|