Skip to main content

Django Content Security Policy Reports

Build Status

A Django app for handling reports from web browsers of violations of your website's content security policy.

This app does not handle the setting of the Content-Security-Policy HTTP headers, but deals with handling the reports that web browsers may submit to your site (via the report-uri) when the stated content security policy is violated.

It is recommended that you use an app such as django-csp (Github) to set the Content-Security-Policy headers.

So What Does This Thing Do?

It receives the reports from the browser and does any/all of the following with them:

  • Logs them using the python logging module.
  • Sends them to you via email.
  • Saves them to the database via a Django model.
  • Runs any of your own custom functions on them.
  • Can generate a summary of a reports.

Supported Django Versions

Supports Python 3.9 to 3.15 and Django 4.2 to 6.x (latest).

The last version to support Python 2.7 was release 1.4 (or see the python2.7-support branch).

How Do I Use This Thing?

  1. Install this app into your Django project, e.g. pip install django-csp-reports.
  2. Add 'cspreports' to your INSTALLED_APPS.
  3. Include cspreports.urls in your URL config somewhere, e.g. urlpatterns = [path('csp/', include('cspreports.urls'))].
  4. In your Content-Security-Policy HTTP headers, set reverse('report_csp') as the report-uri. (Note, with django-csp, you will want to set CSP_REPORT_URI = reverse_lazy('report_csp') in settings.py).
  5. Set all/any of the following in settings.py as you so desire, hopefully they are self-explanatory:
    • CSP_REPORTS_EMAIL_ADMINS (bool defaults to True).

    • CSP_REPORTS_LOG (bool, whether or not to log the reporting using the python logging module, defaults to True).

    • CSP_REPORTS_LOG_LEVEL (str, one of the Python logging module's available log functions, defaults to 'warning').

    • CSP_REPORTS_SAVE (bool defaults to True). Determines whether the reports are saved to the database.

    • CSP_REPORTS_MAX_BODY_SIZE (int, maximum report body size in bytes, defaults to 65536). Reports larger than this are dropped before any handler runs; the view still answers 200.

    • CSP_REPORTS_ADDITIONAL_HANDLERS (iterable defaults to []).

      • Each value should be a dot-separated string path to a function which you want be called when a report is received.
      • Each function is passed the HttpRequest of the CSP report.
    • CSP_REPORTS_FILTER_FUNCTION (str of dotted path to a callable, defaults to None).

      • If set, the specificed function is passed each HttpRequest object of the CSP report before it's processed. Only requests for which the function returns True are processed.
      • You may want to set this to "cspreports.filters.filter_browser_extensions" as a starting point.
    • CSP_REPORTS_LOGGER_NAME (str defaults to CSP Reports). Specifies the logger name that will be used for logging CSP reports, if enabled.

    • CSP_REPORTS_MODEL (<app_label>.<model_name> defaults to "cspreports.CSPReport"). Specifies the model to be used for storing the CSP reports. You can easily extend the model by implementing the abstract base class cspreports.models.CSPReportBase and adding your additional fields to it:

      # your_app.model.py
      from cspreports.models import CSPReportBase
      
      class CustomCSPReport(CSPReportBase):
          # Add your fields here
          pass
      
      # settings.py
      
      CSP_REPORTS_MODEL = "your_app.CustomCSPReport"
      
  6. Set a cron to generate summaries.
  7. Enjoy.

Commands

clean_cspreports

Deletes old reports from the database.

Options:

  • --limit - timestamp that all reports created since will not be deleted. Defaults to 1 week. Accepts any string that can be parsed as a datetime.

make_csp_summary

Generates a summary of CSP reports.

By default includes reports from yesterday (00:00:00 to midnight). The summary shows the top 10 violation sources (i.e. pages from which violations were reported), the top 10 blocked URIs (banned resources which the pages tried to load), and the top 10 invalid reports (which the browser provided an invalid CSP report).

Options:

  • --since - timestamp of the oldest reports to include. Accepts any string that can be parsed as a datetime.
  • --to - timestamp of the newest reports to include. Accepts any string that can be parsed as a datetime.
  • --top - limit of how many examples to show. Default is 10.

Metadata

Release files for django-csp-reports 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-csp-reports 2.0.0
File Size Uploaded Provenance Actions
django_csp_reports-2.0.0.tar.gz 15.3 kB Without provenance Details for django_csp_reports-2.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-csp-reports 2.0.0
File Interpreter ABI Platform Actions
django_csp_reports-2.0.0-py3-none-any.whl Python 3 none any Details for django_csp_reports-2.0.0-py3-none-any.whl

Total release size: 41.9 kB

Release files / django_csp_reports-2.0.0.tar.gz

Download URL django_csp_reports-2.0.0.tar.gz
Size 15.3 kB
Tags Source
SHA-256 checksum
How to use checksums
fbbc2762c3325329b3986a4ba6f797fa9a9af8ad9f3cb06160f93aa600a47851
BLAKE2b-256 checksum
How to use checksums
bf096afaff1ed33db58f2bfd16880a5b14a4478540226a5f19846fbbce979e37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / django_csp_reports-2.0.0-py3-none-any.whl

Download URL django_csp_reports-2.0.0-py3-none-any.whl
Size 26.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3583957d8239b5de4c9e1313f5ce4ac0a7bde2e4cf72c5718c43d5cf2ed6255e
BLAKE2b-256 checksum
How to use checksums
de131b7e79701f8f0123be914b786031ed788f5acba67f223acb1acdfb541d99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for django-csp-reports 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page