2018-10-23 01:22:23 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
class CspReportsController < ApplicationController
|
|
|
|
skip_before_action :check_xhr, :preload_json, :verify_authenticity_token, only: [:create]
|
|
|
|
|
|
|
|
def create
|
|
|
|
raise Discourse::NotFound unless report_collection_enabled?
|
|
|
|
|
2022-06-20 14:57:46 +08:00
|
|
|
report = parse_report
|
2018-10-23 01:22:23 +08:00
|
|
|
|
2022-06-20 14:57:46 +08:00
|
|
|
if report.blank?
|
|
|
|
render_json_error("empty CSP report", status: 422)
|
|
|
|
else
|
|
|
|
Logster.add_to_env(request.env, 'CSP Report', report)
|
|
|
|
Rails.logger.warn("CSP Violation: '#{report['blocked-uri']}' \n\n#{report['script-sample']}")
|
|
|
|
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
rescue JSON::ParserError
|
|
|
|
render_json_error("invalid CSP report", status: 422)
|
2018-10-23 01:22:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-06-20 14:57:46 +08:00
|
|
|
def parse_report
|
|
|
|
obj = JSON.parse(request.body.read)
|
|
|
|
if Hash === obj
|
|
|
|
obj = obj['csp-report']
|
|
|
|
if Hash === obj
|
|
|
|
obj.slice(
|
|
|
|
'blocked-uri',
|
|
|
|
'disposition',
|
|
|
|
'document-uri',
|
|
|
|
'effective-directive',
|
|
|
|
'original-policy',
|
|
|
|
'referrer',
|
|
|
|
'script-sample',
|
|
|
|
'status-code',
|
|
|
|
'violated-directive',
|
|
|
|
'line-number',
|
|
|
|
'source-file'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2018-10-23 01:22:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def report_collection_enabled?
|
2018-11-15 05:23:29 +08:00
|
|
|
SiteSetting.content_security_policy_collect_reports
|
2018-10-23 01:22:23 +08:00
|
|
|
end
|
|
|
|
end
|