diff options
| -rw-r--r-- | app/controllers/csp_reports_controller.rb | 4 | ||||
| -rw-r--r-- | config/initializers/csp_log.rb | 4 |
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. | ||
| 2 | CSP_LOGGER = Rails.env.production? ? | ||
| 3 | ActiveSupport::Logger.new(Rails.root.join("log", "csp_violations.log")) : | ||
| 4 | Rails.logger | ||
