# Django Content Security Policy Reports
[![Build Status](https://travis-ci.org/adamalton/django-csp-reports.svg)](https://travis-ci.org/adamalton/django-csp-reports)
A [Django](https://www.djangoproject.com) 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://en.wikipedia.org/wiki/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](https://pypi.python.org/pypi/django_csp) ([Github](https://github.com/mozilla/django-csp)) 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.5 to 3.10 and Django 2.2 to 4.x (latest).
Python 2.7 support is available in version 1.4 and/or 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_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:
```python
# your_app.model.py
from cspreports.models import CSPReportBase
class CustomCSPReport(CSPReportBase):
# Add your fields here
pass
```
```python
# 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.
Raw data
{
"_id": null,
"home_page": "https://github.com/adamalton/django-csp-reports",
"name": "django-csp-reports",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.4",
"maintainer_email": null,
"keywords": "django, csp, content security policy",
"author": "Adam Alton",
"author_email": "adamalton@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/0d/e7/c104673023f5eb2cb4efe1186c74c1302db660b3d07155dc4fd7b99558ed/django-csp-reports-1.9.1.tar.gz",
"platform": null,
"description": "# Django Content Security Policy Reports\n\n[![Build Status](https://travis-ci.org/adamalton/django-csp-reports.svg)](https://travis-ci.org/adamalton/django-csp-reports)\n\nA [Django](https://www.djangoproject.com) app for handling reports from web browsers of violations of your website's content security policy.\n\nThis app does not handle the setting of the [Content-Security-Policy](http://en.wikipedia.org/wiki/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.\n\nIt is recommended that you use an app such as [django-csp](https://pypi.python.org/pypi/django_csp) ([Github](https://github.com/mozilla/django-csp)) to set the `Content-Security-Policy` headers.\n\n### So What Does This Thing Do?\n\nIt receives the reports from the browser and does any/all of the following with them:\n\n* Logs them using the python `logging` module.\n* Sends them to you via email.\n* Saves them to the database via a Django model.\n* Runs any of your own custom functions on them.\n* Can generate a summary of a reports.\n\n\n### Supported Django Versions\n\nSupports Python 3.5 to 3.10 and Django 2.2 to 4.x (latest).\n\nPython 2.7 support is available in version 1.4 and/or the `python2.7-support` branch.\n\n\n### How Do I Use This Thing?\n\n1. Install this app into your Django project, e.g. `pip install django-csp-reports`.\n2. Add `'cspreports'` to your `INSTALLED_APPS`.\n3. Include `cspreports.urls` in your URL config somewhere, e.g. `urlpatterns = [path('csp/', include('cspreports.urls'))]`.\n4. 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).\n5. Set all/any of the following in settings.py as you so desire, hopefully they are self-explanatory:\n * `CSP_REPORTS_EMAIL_ADMINS` (`bool` defaults to `True`).\n * `CSP_REPORTS_LOG` (`bool`, whether or not to log the reporting using the python `logging` module, defaults to `True`).\n * `CSP_REPORTS_LOG_LEVEL` (`str`, one of the Python logging module's available log functions, defaults to `'warning'`).\n * `CSP_REPORTS_SAVE` (`bool` defaults to `True`). Determines whether the reports are saved to the database.\n * `CSP_REPORTS_ADDITIONAL_HANDLERS` (`iterable` defaults to `[]`).\n - Each value should be a dot-separated string path to a function which you want be called when a report is received.\n - Each function is passed the `HttpRequest` of the CSP report.\n * `CSP_REPORTS_FILTER_FUNCTION` (`str` of dotted path to a callable, defaults to `None`).\n - 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.\n - You may want to set this to `\"cspreports.filters.filter_browser_extensions\"` as a starting point.\n * `CSP_REPORTS_LOGGER_NAME` (`str` defaults to `CSP Reports`). Specifies the logger name that will be used for logging CSP reports, if enabled.\n * `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:\n\n ```python\n # your_app.model.py\n from cspreports.models import CSPReportBase\n\n class CustomCSPReport(CSPReportBase):\n # Add your fields here\n pass\n ```\n\n ```python\n # settings.py\n\n CSP_REPORTS_MODEL = \"your_app.CustomCSPReport\"\n ```\n6. Set a cron to generate summaries.\n7. Enjoy.\n\n\n### Commands\n\n#### `clean_cspreports`\nDeletes old reports from the database.\n\nOptions:\n\n* `--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.\n\n#### `make_csp_summary`\nGenerates a summary of CSP reports.\n\nBy default includes reports from yesterday (00:00:00 to midnight).\nThe summary shows the top 10 violation sources (i.e. pages from which violations were reported),\nthe top 10 blocked URIs (banned resources which the pages tried to load),\nand the top 10 invalid reports (which the browser provided an invalid CSP report).\n\nOptions:\n\n* `--since` - timestamp of the oldest reports to include. Accepts any string that can be parsed as a datetime.\n* `--to` - timestamp of the newest reports to include. Accepts any string that can be parsed as a datetime.\n* `--top` - limit of how many examples to show. Default is 10.\n",
"bugtrack_url": null,
"license": null,
"summary": "A Django app for handling reports from web browsers of violations of your website's HTTP Content Security Policy.",
"version": "1.9.1",
"project_urls": {
"Homepage": "https://github.com/adamalton/django-csp-reports"
},
"split_keywords": [
"django",
" csp",
" content security policy"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c55547244366866be195096d3442ffd67548c7e38ef242afbdafba18098107e9",
"md5": "2d8450d73cd01be2b2bbca7bf39123f8",
"sha256": "54e39cd73e4d190061085dec54049cec5b4428388400107b34b317cf307db462"
},
"downloads": -1,
"filename": "django_csp_reports-1.9.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2d8450d73cd01be2b2bbca7bf39123f8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.4",
"size": 25579,
"upload_time": "2024-08-22T11:42:27",
"upload_time_iso_8601": "2024-08-22T11:42:27.811983Z",
"url": "https://files.pythonhosted.org/packages/c5/55/47244366866be195096d3442ffd67548c7e38ef242afbdafba18098107e9/django_csp_reports-1.9.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0de7c104673023f5eb2cb4efe1186c74c1302db660b3d07155dc4fd7b99558ed",
"md5": "8ac7b72a694cd6e82199af524b44b667",
"sha256": "2fc35233bf3d479ccf233fd9cc74c2282603a793e0b4efb5e9c2aa5f0eab486b"
},
"downloads": -1,
"filename": "django-csp-reports-1.9.1.tar.gz",
"has_sig": false,
"md5_digest": "8ac7b72a694cd6e82199af524b44b667",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.4",
"size": 14312,
"upload_time": "2024-08-22T11:42:29",
"upload_time_iso_8601": "2024-08-22T11:42:29.189109Z",
"url": "https://files.pythonhosted.org/packages/0d/e7/c104673023f5eb2cb4efe1186c74c1302db660b3d07155dc4fd7b99558ed/django-csp-reports-1.9.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-22 11:42:29",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "adamalton",
"github_project": "django-csp-reports",
"travis_ci": true,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "django-csp-reports"
}