2018-11-30 08:51:45 -06:00
|
|
|
# frozen_string_literal: true
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe ContentSecurityPolicy do
|
2023-01-09 05:18:21 -06:00
|
|
|
after { DiscoursePluginRegistry.reset! }
|
2019-09-20 07:32:43 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "report-uri" do
|
|
|
|
it "is enabled by SiteSetting" do
|
2018-10-22 12:22:23 -05:00
|
|
|
SiteSetting.content_security_policy_collect_reports = true
|
2023-01-09 05:18:21 -06:00
|
|
|
report_uri = parse(policy)["report-uri"].first
|
|
|
|
expect(report_uri).to eq("http://test.localhost/csp_reports")
|
2018-10-22 12:22:23 -05:00
|
|
|
|
|
|
|
SiteSetting.content_security_policy_collect_reports = false
|
2023-01-09 05:18:21 -06:00
|
|
|
report_uri = parse(policy)["report-uri"]
|
2018-10-22 12:22:23 -05:00
|
|
|
expect(report_uri).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "base-uri" do
|
|
|
|
it "is set to self" do
|
|
|
|
base_uri = parse(policy)["base-uri"]
|
2021-07-07 08:43:48 -05:00
|
|
|
expect(base_uri).to eq(["'self'"])
|
2019-01-09 14:04:50 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "object-src" do
|
|
|
|
it "is set to none" do
|
|
|
|
object_srcs = parse(policy)["object-src"]
|
2019-01-09 14:04:50 -06:00
|
|
|
expect(object_srcs).to eq(["'none'"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "upgrade-insecure-requests" do
|
|
|
|
it "is not included when force_https is off" do
|
2021-06-09 19:53:10 -05:00
|
|
|
SiteSetting.force_https = false
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(parse(policy)["upgrade-insecure-requests"]).to eq(nil)
|
2021-06-09 19:53:10 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "is included when force_https is on" do
|
2021-06-09 19:53:10 -05:00
|
|
|
SiteSetting.force_https = true
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(parse(policy)["upgrade-insecure-requests"]).to eq([])
|
2021-06-09 19:53:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-07 09:20:31 -06:00
|
|
|
describe "strict-dynamic script-src and worker-src" do
|
|
|
|
it "includes strict-dynamic keyword" do
|
|
|
|
script_srcs = parse(policy)["script-src"]
|
|
|
|
expect(script_srcs).to include("'strict-dynamic'")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not set worker-src" do
|
|
|
|
worker_src = parse(policy)["worker-src"]
|
|
|
|
expect(worker_src).to eq(nil)
|
|
|
|
end
|
2024-06-18 03:40:53 -05:00
|
|
|
|
|
|
|
it 'includes "report-sample" when report collection is enabled' do
|
|
|
|
SiteSetting.content_security_policy_collect_reports = true
|
|
|
|
script_srcs = parse(policy)["script-src"]
|
|
|
|
expect(script_srcs).to include("'report-sample'")
|
|
|
|
end
|
2024-03-07 09:20:31 -06:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "manifest-src" do
|
|
|
|
it "is set to self" do
|
|
|
|
expect(parse(policy)["manifest-src"]).to eq(["'self'"])
|
2021-06-08 08:32:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "frame-ancestors" do
|
|
|
|
context "with content_security_policy_frame_ancestors enabled" do
|
2021-03-22 14:00:25 -05:00
|
|
|
before do
|
|
|
|
SiteSetting.content_security_policy_frame_ancestors = true
|
2023-01-09 05:18:21 -06:00
|
|
|
Fabricate(:embeddable_host, host: "https://a.org")
|
|
|
|
Fabricate(:embeddable_host, host: "https://b.org")
|
2021-03-22 14:00:25 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "always has self" do
|
|
|
|
frame_ancestors = parse(policy)["frame-ancestors"]
|
2021-03-22 14:00:25 -05:00
|
|
|
expect(frame_ancestors).to include("'self'")
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "includes all EmbeddableHost" do
|
|
|
|
frame_ancestors = parse(policy)["frame-ancestors"]
|
2021-03-22 14:00:25 -05:00
|
|
|
expect(frame_ancestors).to include("https://a.org")
|
|
|
|
expect(frame_ancestors).to include("https://b.org")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
context "with content_security_policy_frame_ancestors disabled" do
|
|
|
|
before { SiteSetting.content_security_policy_frame_ancestors = false }
|
2021-03-22 14:00:25 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "does not set frame-ancestors" do
|
|
|
|
frame_ancestors = parse(policy)["frame-ancestors"]
|
2021-03-22 14:00:25 -05:00
|
|
|
expect(frame_ancestors).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
context "with a plugin" do
|
2021-06-07 13:59:15 -05:00
|
|
|
let(:plugin_class) do
|
|
|
|
Class.new(Plugin::Instance) do
|
|
|
|
attr_accessor :enabled
|
|
|
|
def enabled?
|
|
|
|
@enabled
|
|
|
|
end
|
2018-12-03 21:48:13 -06:00
|
|
|
end
|
2021-06-07 13:59:15 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "can extend frame_ancestors" do
|
2021-06-07 13:59:15 -05:00
|
|
|
SiteSetting.content_security_policy_frame_ancestors = true
|
|
|
|
plugin = plugin_class.new(nil, "#{Rails.root}/spec/fixtures/plugins/csp_extension/plugin.rb")
|
2018-11-30 08:51:45 -06:00
|
|
|
|
2021-06-07 13:59:15 -05:00
|
|
|
plugin.activate!
|
|
|
|
Discourse.plugins << plugin
|
|
|
|
|
|
|
|
plugin.enabled = true
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(parse(policy)["frame-ancestors"]).to include("'self'")
|
|
|
|
expect(parse(policy)["frame-ancestors"]).to include("https://frame-ancestors-plugin.ext")
|
2021-06-07 13:59:15 -05:00
|
|
|
|
|
|
|
plugin.enabled = false
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(parse(policy)["frame-ancestors"]).to_not include("https://frame-ancestors-plugin.ext")
|
2021-06-07 13:59:15 -05:00
|
|
|
|
|
|
|
Discourse.plugins.delete plugin
|
2021-10-25 03:24:21 -05:00
|
|
|
DiscoursePluginRegistry.reset!
|
2021-06-07 13:59:15 -05:00
|
|
|
end
|
2018-11-30 08:51:45 -06:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "can be extended by site setting" do
|
2024-03-07 09:20:31 -06:00
|
|
|
SiteSetting.content_security_policy_script_src = "'unsafe-eval'"
|
2018-11-30 08:51:45 -06:00
|
|
|
|
2024-03-07 09:20:31 -06:00
|
|
|
expect(parse(policy)["script-src"]).to include("'unsafe-eval'")
|
2018-10-22 12:22:23 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def parse(csp_string)
|
2023-01-09 05:18:21 -06:00
|
|
|
csp_string
|
|
|
|
.split(";")
|
|
|
|
.map do |policy|
|
|
|
|
directive, *sources = policy.split
|
|
|
|
[directive, sources]
|
|
|
|
end
|
|
|
|
.to_h
|
2018-10-22 12:22:23 -05:00
|
|
|
end
|
2018-11-30 08:51:45 -06:00
|
|
|
|
2021-06-17 21:16:26 -05:00
|
|
|
def policy(theme_id = nil, path_info: "/")
|
|
|
|
ContentSecurityPolicy.policy(theme_id, path_info: path_info)
|
2018-11-30 08:51:45 -06:00
|
|
|
end
|
2018-10-22 12:22:23 -05:00
|
|
|
end
|