Skip to content

A platform for community discussion. Free, open, simple.

FIX: Allow CDN asset host in worker-src CSP (#40871)

David Taylore7ce1e219e29
diff --git a/lib/content_security_policy/default.rb b/lib/content_security_policy/default.rb
index 47eb39118c0..c555de02f44 100644
--- a/lib/content_security_policy/default.rb
+++ b/lib/content_security_policy/default.rb
@@ -12,7 +12,7 @@ class ContentSecurityPolicy
directives[:base_uri] = [:self]
directives[:object_src] = [:none]
directives[:script_src] = script_src
- directives[:worker_src] = [:self, "blob:"]
+ directives[:worker_src] = worker_src
directives[:frame_ancestors] = frame_ancestors if restrict_embed?
directives[:manifest_src] = ["'self'"]
end
@@ -24,6 +24,21 @@ class ContentSecurityPolicy
%w['strict-dynamic' 'wasm-unsafe-eval']
end
+ def worker_src
+ [:self, "blob:", *worker_asset_host]
+ end
+
+ def worker_asset_host
+ if GlobalSetting.use_s3? && GlobalSetting.s3_cdn_url.present?
+ s3_cdn = GlobalSetting.s3_asset_cdn_url.presence || GlobalSetting.s3_cdn_url
+ ["#{s3_cdn}/assets/"]
+ elsif GlobalSetting.cdn_url.present?
+ ["#{GlobalSetting.cdn_url}#{Discourse.base_path}/assets/"]
+ else
+ []
+ end
+ end
+
def frame_ancestors
["'self'", *EmbeddableHost.pluck(:host).map { |host| "https://#{host}" }]
end
diff --git a/spec/lib/content_security_policy_spec.rb b/spec/lib/content_security_policy_spec.rb
index 45cae13536c..e3f5bc554e0 100644
--- a/spec/lib/content_security_policy_spec.rb
+++ b/spec/lib/content_security_policy_spec.rb
@@ -43,6 +43,23 @@ RSpec.describe ContentSecurityPolicy do
worker_src = parse(policy)["worker-src"]
expect(worker_src).to contain_exactly("'self'", "blob:")
end
+
+ it "includes the CDN asset host in worker-src so the worker chunk can be imported" do
+ set_cdn_url "https://cdn.example.com"
+ worker_src = parse(policy)["worker-src"]
+ expect(worker_src).to contain_exactly("'self'", "blob:", "https://cdn.example.com/assets/")
+ end
+
+ it "includes the s3 asset CDN host in worker-src" do
+ global_setting :s3_bucket, "test_bucket"
+ global_setting :s3_region, "ap-australia"
+ global_setting :s3_access_key_id, "123"
+ global_setting :s3_secret_access_key, "123"
+ global_setting :s3_cdn_url, "https://s3cdn.example.com"
+
+ worker_src = parse(policy)["worker-src"]
+ expect(worker_src).to contain_exactly("'self'", "blob:", "https://s3cdn.example.com/assets/")
+ end
end
describe "manifest-src" do