Skip to content

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

FEATURE: Allow custom CSP reporting URL to be configured (#41413)

David Taylorf07c551d28c9
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 6d302b80f78..6f7813e24ef 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2060,6 +2060,7 @@ en:
llms_txt: "Upload a text file to be served at /llms.txt for LLM crawlers. See <a href='https://llmstxt.org/' target='_blank'>llmstxt.org</a> for more information."
content_security_policy: "Enable Content-Security-Policy (CSP). CSP is an additional layer of security that helps to prevent certain types of attacks, including Cross Site Scripting (XSS) and data injection. It should only be disabled temporarily for debugging/testing purposes."
content_security_policy_report_only: "Enable Content-Security-Policy-Report-Only (CSP)"
+ content_security_policy_report_uri: "Send CSP violation reports to this URL via the CSP <code>report-uri</code> directive. Leave blank to disable reporting."
content_security_policy_frame_ancestors: "Restrict who can embed this site in iframes via CSP. Control allowed hosts on <a href='%{base_path}/admin/customize/embedding'>Embedding</a>"
content_security_policy_script_src: "Additional allowlisted script sources. The current host and CDN are included by default. See <a href='https://meta.discourse.org/t/mitigate-xss-attacks-with-content-security-policy/104243' target='_blank'>Mitigate XSS Attacks with Content Security Policy.</a> (CSP). Other host sources are ignored as strict-dynamic is enabled."
invalidate_inactive_admin_email_after_days: "Admin accounts that have not visited the site in this number of days will need to re-validate their email address before logging in. Set to 0 to disable."
@@ -2964,6 +2965,7 @@ en:
invalid_domain_hostname: "Must not include * or ? characters."
invalid_allowed_iframes_url: "Iframe urls must start with http:// or https:// and have at least one more additional '/'"
invalid_csp_script_src: "Value must be either 'unsafe-eval' or 'wasm-unsafe-eval', or in the form '<hash algorithm>-<base64 value>' where supported hash algorithms are sha256, sha384 or sha512. Ensure that your input is wrapped in single quotation marks."
+ invalid_csp_report_uri: "Value must be a single valid http:// or https:// URL and cannot contain spaces, semicolons or commas."
pop3_polling_host_is_empty: "You must set a 'pop3 polling host' before enabling POP3 polling."
pop3_polling_username_is_empty: "You must set a 'pop3 polling username' before enabling POP3 polling."
pop3_polling_password_is_empty: "You must set a 'pop3 polling password' before enabling POP3 polling."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index cb00deefb90..2a385a89a87 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -3025,6 +3025,10 @@ security:
requires_confirmation: "simple_on_disable"
content_security_policy_report_only:
default: false
+ content_security_policy_report_uri:
+ default: ""
+ regex: '\Ahttps?://[^\s;,]+\z'
+ regex_error: "site_settings.errors.invalid_csp_report_uri"
content_security_policy_frame_ancestors:
default: true
content_security_policy_script_src:
diff --git a/lib/content_security_policy/default.rb b/lib/content_security_policy/default.rb
index c555de02f44..e35319a1d0e 100644
--- a/lib/content_security_policy/default.rb
+++ b/lib/content_security_policy/default.rb
@@ -15,13 +15,20 @@ class ContentSecurityPolicy
directives[:worker_src] = worker_src
directives[:frame_ancestors] = frame_ancestors if restrict_embed?
directives[:manifest_src] = ["'self'"]
+ directives[:report_uri] = [report_uri] if report_uri.present?
end
end
private
def script_src
- %w['strict-dynamic' 'wasm-unsafe-eval']
+ sources = %w['strict-dynamic' 'wasm-unsafe-eval']
+ sources << "'report-sample'" if report_uri.present?
+ sources
+ end
+
+ def report_uri
+ SiteSetting.content_security_policy_report_uri.presence
end
def worker_src
diff --git a/spec/lib/content_security_policy_spec.rb b/spec/lib/content_security_policy_spec.rb
index e3f5bc554e0..977ba92e00f 100644
--- a/spec/lib/content_security_policy_spec.rb
+++ b/spec/lib/content_security_policy_spec.rb
@@ -68,6 +68,22 @@ RSpec.describe ContentSecurityPolicy do
end
end
+ describe "report-uri" do
+ it "is not included when content_security_policy_report_uri is blank" do
+ SiteSetting.content_security_policy_report_uri = ""
+
+ expect(parse(policy)["report-uri"]).to eq(nil)
+ expect(parse(policy)["script-src"]).to_not include("'report-sample'")
+ end
+
+ it "points to the configured endpoint and enables report-sample when set" do
+ SiteSetting.content_security_policy_report_uri = "https://csp.example.com/report"
+
+ expect(parse(policy)["report-uri"]).to eq(["https://csp.example.com/report"])
+ expect(parse(policy)["script-src"]).to include("'report-sample'")
+ end
+ end
+
describe "frame-ancestors" do
context "with content_security_policy_frame_ancestors enabled" do
before do