summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-07-19 16:16:43 +0200
committererdgeist <erdgeist@erdgeist.org>2026-07-19 16:16:43 +0200
commite0c2d7a066793683a911bee7f7210d80eb6b2055 (patch)
tree91967742cbe245806776d0dd470ad7f4ec72abe0
parent042d99ec0e9095e46642d6f20932754025fd8af5 (diff)
Log CSP violations to their own file instead of relying on Rails.logger
-rw-r--r--app/controllers/csp_reports_controller.rb4
-rw-r--r--config/initializers/csp_log.rb4
2 files changed, 6 insertions, 2 deletions
diff --git a/app/controllers/csp_reports_controller.rb b/app/controllers/csp_reports_controller.rb
index a8f8edb..5a3b55e 100644
--- a/app/controllers/csp_reports_controller.rb
+++ b/app/controllers/csp_reports_controller.rb
@@ -12,9 +12,9 @@ class CspReportsController < ApplicationController
12 if report 12 if report
13 directive = report["effective-directive"] || report["violated-directive"] 13 directive = report["effective-directive"] || report["violated-directive"]
14 at = (URI.parse(report["document-uri"]).path rescue "unparsed") 14 at = (URI.parse(report["document-uri"]).path rescue "unparsed")
15 Rails.logger.warn("CSP violation: #{directive} blocked=#{report['blocked-uri']} at=#{at}") 15 CSP_LOGGER.warn("CSP violation: #{directive} blocked=#{report['blocked-uri']} at=#{at}")
16 else 16 else
17 Rails.logger.warn("CSP violation: unparseable report (#{raw.to_s.bytesize} bytes)") 17 CSP_LOGGER.warn("CSP violation: unparseable report (#{raw.to_s.bytesize} bytes)")
18 end 18 end
19 19
20 head :no_content 20 head :no_content
diff --git a/config/initializers/csp_log.rb b/config/initializers/csp_log.rb
new file mode 100644
index 0000000..1dde9c4
--- /dev/null
+++ b/config/initializers/csp_log.rb
@@ -0,0 +1,4 @@
1# CSP violation reports get their own file, independent of config.logger.
2CSP_LOGGER = Rails.env.production? ?
3 ActiveSupport::Logger.new(Rails.root.join("log", "csp_violations.log")) :
4 Rails.logger