discourse/spec/system/content_security_policy_spec.rb
Kelv b751742573
FIX: invalid CSP directive sources should allow site to boot with valid CSP directives (#31256)
[Security
patch](5558e72f22)
(for this [CVE](https://nvd.nist.gov/vuln/detail/CVE-2024-54133)) from
rails actionpack was backported from [Rails
8.0.0.1](https://github.com/rails/rails/blob/v8.0.1/actionpack/CHANGELOG.md#rails-8001-december-10-2024)
to previous stable versions including `7-1-stable` / `7-2-stable`.

Any previous version of Discourse upgrading to v3.4.0.beta3 and above
would have observed their sites crashing if they had invalid sources in
their CSP directive extensions.

This fix removes such invalid sources during our build of the CSP, and
logs these at a warning level so devs are able to find out why their CSP
sources were filtered out of the extendable directives.
2025-02-10 20:38:36 +08:00

56 lines
1.5 KiB
Ruby

# frozen_string_literal: true
describe "Content security policy", type: :system do
let(:plugin_class) do
Class.new(Plugin::Instance) do
attr_accessor :enabled
def enabled?
@enabled
end
end
end
it "can boot the application in strict_dynamic mode even with invalid directives from CSP extensions" do
plugin = plugin_class.new(nil, "#{Rails.root}/spec/fixtures/plugins/csp_extension/plugin.rb")
plugin.activate!
Discourse.plugins << plugin
plugin.enabled = true
expect(SiteSetting.content_security_policy).to eq(true)
visit "/"
expect(page).to have_css("#site-logo")
get "/"
expect(response.headers["Content-Security-Policy"]).to include("'strict-dynamic'")
expect(response.headers["Content-Security-Policy"]).not_to include(
"'unsafe-eval' https://invalid.example.com'",
)
Discourse.plugins.delete plugin
DiscoursePluginRegistry.reset!
end
it "works for 'public exceptions' like RoutingError" do
expect(SiteSetting.content_security_policy).to eq(true)
SiteSetting.bootstrap_error_pages = true
get "/nonexistent"
expect(response.headers["Content-Security-Policy"]).to include("'strict-dynamic'")
visit "/nonexistent"
expect(page).not_to have_css("body.no-ember")
expect(page).to have_css("#site-logo")
end
it "can boot logster in strict_dynamic mode" do
expect(SiteSetting.content_security_policy).to eq(true)
sign_in Fabricate(:admin)
visit "/logs"
expect(page).to have_css("#log-table")
end
end