Set the Integrity-Policy HTTP header on your Django app.
Project description
Set the Integrity-Policy HTTP header on your Django app.
Work smarter and faster with my book Boost Your Django DX which covers many ways to improve your development experience.
Requirements
Python 3.10 to 3.14 supported.
Django 4.2 to 6.0 supported.
Installation
Install with pip:
python -m pip install django-integrity-policy
2. Add the middleware in your MIDDLEWARE setting. It’s best to add it after Django’s SecurityMiddleware, so it adds the header at the same point in your stack:
MIDDLEWARE = [
...,
"django.middleware.security.SecurityMiddleware",
"django_integrity_policy.IntegrityPolicyMiddleware",
...,
]
Add an INTEGRITY_POLICY or INTEGRITY_POLICY_REPORT_ONLY setting to your settings file. Here’s an example that blocks scripts and stylesheets that lack integrity metadata:
INTEGRITY_POLICY = { "blocked-destinations": ["script", "style"], }See below for more information on the settings.
Settings
The integrity policy for your page is configured with two settings:
INTEGRITY_POLICY - sets the Integrity-Policy header, which defines the policy that the browser enforces.
INTEGRITY_POLICY_REPORT_ONLY - sets the Integrity-Policy-Report-Only header, which defines a policy that the browser simulates but does not enforce.
In both cases, any violations are reported to the console and optionally to a reporting endpoint defined by the Reporting-Endpoints header. The report-only header is useful for testing a new policy before enforcing it.
Each setting should be a dictionary with the following keys:
blocked-destinations (required) - a list of request destinations that must include valid integrity metadata. Allowed values are 'script' and 'style'.
sources (optional) - a list of integrity metadata sources. The only allowed value is 'inline', which is also the default when sources is omitted.
endpoints (optional) - a list of reporting endpoint names to send violation reports to. The named endpoints must be defined in a Reporting-Endpoints response header.
If the keys or values are invalid, ImproperlyConfigured will be raised at instantiation time.
Examples
Block scripts and styles that lack integrity metadata:
INTEGRITY_POLICY = {
"blocked-destinations": ["script", "style"],
}
Block scripts and report violations to a named endpoint:
INTEGRITY_POLICY = {
"blocked-destinations": ["script"],
"endpoints": ["integrity-endpoint"],
}
Test the effect of blocking scripts without enforcing it:
INTEGRITY_POLICY_REPORT_ONLY = {
"blocked-destinations": ["script"],
"endpoints": ["integrity-endpoint"],
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_integrity_policy-1.0.0.tar.gz.
File metadata
- Download URL: django_integrity_policy-1.0.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a31e82ec5094862cdecdd55fb3415a29914d42eeceadfb26b7032a7add0c9c04
|
|
| MD5 |
f362dc9bc0be371a1e457e5a0e443842
|
|
| BLAKE2b-256 |
166e96f264805cd0e6e40897da9edbf862b7150c2f41d2e2ae3624fcd145bfdd
|
Provenance
The following attestation bundles were made for django_integrity_policy-1.0.0.tar.gz:
Publisher:
main.yml on adamchainz/django-integrity-policy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_integrity_policy-1.0.0.tar.gz -
Subject digest:
a31e82ec5094862cdecdd55fb3415a29914d42eeceadfb26b7032a7add0c9c04 - Sigstore transparency entry: 1656479064
- Sigstore integration time:
-
Permalink:
adamchainz/django-integrity-policy@783ed2d69bce97454538f2151b4c7763db2c5420 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/adamchainz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@783ed2d69bce97454538f2151b4c7763db2c5420 -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_integrity_policy-1.0.0-py3-none-any.whl.
File metadata
- Download URL: django_integrity_policy-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7ab666dcbf28fef7f5d95ffbb6977c4b4a0a74830f82fe1d4688c2826f8cc1a
|
|
| MD5 |
535442c39f0043c441c69d5ed75cf65a
|
|
| BLAKE2b-256 |
c948bbfa8a7bf7b43fc5c3a2b952ec90b136c8d61142074aed6410af046f3d29
|
Provenance
The following attestation bundles were made for django_integrity_policy-1.0.0-py3-none-any.whl:
Publisher:
main.yml on adamchainz/django-integrity-policy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_integrity_policy-1.0.0-py3-none-any.whl -
Subject digest:
b7ab666dcbf28fef7f5d95ffbb6977c4b4a0a74830f82fe1d4688c2826f8cc1a - Sigstore transparency entry: 1656479199
- Sigstore integration time:
-
Permalink:
adamchainz/django-integrity-policy@783ed2d69bce97454538f2151b4c7763db2c5420 -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/adamchainz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@783ed2d69bce97454538f2151b4c7763db2c5420 -
Trigger Event:
push
-
Statement type: