FIX: correctly strip unneeded csp directives under strict-dynamic (#26180)

This commit is contained in:
David Taylor 2024-03-14 18:50:09 +00:00 committed by GitHub
parent b16f212c47
commit 2546817d07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 9 deletions

View File

@ -76,7 +76,7 @@ class ContentSecurityPolicy
sources = Array(sources).map { |s| normalize_source(s) }
if SiteSetting.content_security_policy_strict_dynamic &&
%w[script-src worker-src].include?(directive.to_s)
%w[script_src worker_src].include?(directive.to_s)
# Strip any sources which are ignored under strict-dynamic
# If/when we make strict-dynamic the only option, we could print deprecation warnings
# asking plugin/theme authors to remove the unnecessary config

View File

@ -5,16 +5,16 @@ RSpec.describe ContentSecurityPolicy::Builder do
describe "#<<" do
it "normalizes directive name" do
builder << {
:script_src => ["symbol_underscore"],
:"script-src" => ["symbol_dash"],
"script_src" => ["string_underscore"],
"script-src" => ["string_dash"],
:script_src => ["'symbol_underscore'"],
:"script-src" => ["'symbol_dash'"],
"script_src" => ["'string_underscore'"],
"script-src" => ["'string_dash'"],
}
script_srcs = parse(builder.build)["script-src"]
expect(script_srcs).to include(
*%w[symbol_underscore symbol_dash string_underscore symbol_underscore],
*%w['symbol_underscore' 'symbol_dash' 'string_underscore' 'symbol_underscore'],
)
end

View File

@ -396,6 +396,16 @@ RSpec.describe ContentSecurityPolicy do
expect(parse(policy)["script-src"]).to include("'unsafe-eval'")
end
it "strips unsupported values from setting" do
SiteSetting.content_security_policy_script_src =
"'unsafe-eval'|blob:|https://example.com/script.js"
script_src = parse(policy)["script-src"]
expect(script_src).to include("'unsafe-eval'")
expect(script_src).not_to include("blob:")
expect(script_src).not_to include("https://example.com/script.js")
end
def parse(csp_string)
csp_string
.split(";")

View File

@ -647,14 +647,14 @@ RSpec.describe ApplicationController do
get "/"
script_src = parse(response.headers["Content-Security-Policy"])["script-src"]
expect(script_src).to_not include("'unsafe-inline'")
expect(script_src).to_not include("'unsafe-eval'")
SiteSetting.content_security_policy_script_src = "'unsafe-inline'"
SiteSetting.content_security_policy_script_src = "'unsafe-eval'"
get "/"
script_src = parse(response.headers["Content-Security-Policy"])["script-src"]
expect(script_src).to include("'unsafe-inline'")
expect(script_src).to include("'unsafe-eval'")
end
it "does not set CSP when responding to non-HTML" do