2017-11-16 19:51:44 +01:00
|
|
|
# System documented in https://zulip.readthedocs.io/en/latest/subsystems/logging.html
|
2020-06-11 00:54:34 +02:00
|
|
|
import logging
|
2015-11-23 17:29:37 +01:00
|
|
|
|
2016-06-04 21:47:59 +02:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
2018-04-11 05:50:08 +02:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.views.decorators.http import require_POST
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2023-05-03 17:35:09 +02:00
|
|
|
from zerver.lib.request import REQ, has_request_variables
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.response import json_success
|
2022-01-11 09:37:41 +01:00
|
|
|
from zerver.lib.validator import (
|
|
|
|
WildValue,
|
|
|
|
check_string,
|
|
|
|
to_wild_value,
|
|
|
|
)
|
2015-11-23 17:29:37 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-04-11 05:50:08 +02:00
|
|
|
@csrf_exempt
|
|
|
|
@require_POST
|
|
|
|
@has_request_variables
|
2021-02-12 08:19:30 +01:00
|
|
|
def report_csp_violations(
|
2022-01-11 09:37:41 +01:00
|
|
|
request: HttpRequest,
|
|
|
|
csp_report: WildValue = REQ(argument_type="body", converter=to_wild_value),
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> HttpResponse:
|
2018-04-11 05:50:08 +02:00
|
|
|
def get_attr(csp_report_attr: str) -> str:
|
2022-01-11 09:37:41 +01:00
|
|
|
return csp_report.get(csp_report_attr, "").tame(check_string)
|
2018-04-11 05:50:08 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
logging.warning(
|
2021-05-10 07:02:14 +02:00
|
|
|
"CSP violation in document('%s'). "
|
|
|
|
"blocked URI('%s'), original policy('%s'), "
|
|
|
|
"violated directive('%s'), effective directive('%s'), "
|
|
|
|
"disposition('%s'), referrer('%s'), "
|
|
|
|
"status code('%s'), script sample('%s')",
|
2021-02-12 08:20:45 +01:00
|
|
|
get_attr("document-uri"),
|
|
|
|
get_attr("blocked-uri"),
|
|
|
|
get_attr("original-policy"),
|
|
|
|
get_attr("violated-directive"),
|
|
|
|
get_attr("effective-directive"),
|
|
|
|
get_attr("disposition"),
|
|
|
|
get_attr("referrer"),
|
|
|
|
get_attr("status-code"),
|
|
|
|
get_attr("script-sample"),
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2018-04-11 05:50:08 +02:00
|
|
|
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|