django-integrity-policy
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.15 supported.
Django 5.2 to 6.1 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.
Middleware arguments
IntegrityPolicyMiddleware also accepts two keyword-only arguments, which take precedence over the equivalent settings:
policy - a dictionary in the same format as the INTEGRITY_POLICY setting.
report_only_policy - a dictionary in the same format as the INTEGRITY_POLICY_REPORT_ONLY setting.
When an argument is None (the default), the corresponding setting is used, and changes to it (such as with override_settings() in tests) take effect. When an argument is given, the setting is ignored for that instance, and the header value is computed and validated at instantiation time. Note that an empty dictionary ({}) is a value meaning “send no header”, which is different to None meaning “use the setting”.
Django only ever instantiates middleware with get_response, so these arguments are for when you construct instances yourself, such as within another middleware that dispatches between several instances.
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"],
}
Decorators
Use the below decorators to override the integrity policies (live and report-only) on a per-view basis. The decorators fully replace the given policy set by the global settings for that view, rather than merging with it.
The examples below use function-based views. To decorate class-based views, use the @method_decorator per Django’s class-based view decoration documentation.
integrity_policy_override(config)
Overrides the Integrity-Policy header for the decorated view, using a dictionary in the same format as the INTEGRITY_POLICY setting.
If config is an empty mapping ({}), no Integrity-Policy header will be added to the response for that view.
For example, to block integrity-free scripts but not styles on a particular view:
from django.shortcuts import render
from django_integrity_policy.decorators import integrity_policy_override
@integrity_policy_override(
{
"blocked-destinations": ["script"],
}
)
def drawbridge_view(request):
return render(request, "castle/drawbridge.html")
…or, to not set an integrity policy at all on a particular view:
from django.shortcuts import render
from django_integrity_policy.decorators import integrity_policy_override
@integrity_policy_override({})
def dungeon_view(request):
return render(request, "castle/dungeon.html")
integrity_policy_report_only_override(config)
Overrides the Integrity-Policy-Report-Only header for the decorated view, using a dictionary in the same format as the INTEGRITY_POLICY_REPORT_ONLY setting.
If config is an empty mapping ({}), no Integrity-Policy-Report-Only header will be added to the response for that view.
For example, to test a policy that blocks integrity-free scripts and styles for a particular view:
from django.shortcuts import render
from django_integrity_policy.decorators import integrity_policy_report_only_override
@integrity_policy_report_only_override(
{
"blocked-destinations": ["script", "style"],
"endpoints": ["integrity-endpoint"],
}
)
def gatehouse_view(request):
return render(request, "castle/gatehouse.html")
…or, to not set a report-only integrity policy at all on a particular view:
from django.shortcuts import render
from django_integrity_policy.decorators import integrity_policy_report_only_override
@integrity_policy_report_only_override({})
def moat_view(request):
return render(request, "castle/moat.html")
Adding integrity attributes
Once Integrity-Policy is set, the browser will block any scripts or stylesheets (depending on configuration) that lack a valid integrity attribute, including your first-party resources. To generate integrity attributes for your project’s static files, you can use the django-sri package, which provides template tags to generate appropriately hashed HTML tags. For example:
{% load sri %}
{% sri_static "app.js" %}
{% sri_static "app.css" %}
…will output:
<script src="/static/app.js" integrity="sha256-..."></script>
<link rel="stylesheet" href="/static/app.css" integrity="sha256-..."/>
These tags would be allowed per a strict integrity policy.
For a full example project, see the example/ directory in this repository.
Metadata
Release files for django-integrity-policy 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| django_integrity_policy-1.2.0.tar.gz | 7.6 kB | With provenance | Details for django_integrity_policy-1.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| django_integrity_policy-1.2.0-py3-none-any.whl | Python 3 | none | any | Details for django_integrity_policy-1.2.0-py3-none-any.whl |
Total release size: 15.8 kB
Release files / django_integrity_policy-1.2.0.tar.gz
| Download URL | django_integrity_policy-1.2.0.tar.gz |
|---|---|
| Size | 7.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
48f9518aaa98e4498dd86121573f32214cef5d9452966e4795b0115e93218c86
|
|
BLAKE2b-256 checksum How to use checksums |
6fb765f47489dc697803f3f7df02a7e5ca33359748841d40f9be7e6bba1eacb1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 14, 2026.
Transparency logRelease files / django_integrity_policy-1.2.0-py3-none-any.whl
| Download URL | django_integrity_policy-1.2.0-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0f27a761d234a38a614733c78def17d6d2d842596fe05aef8323ee08c8542a0c
|
|
BLAKE2b-256 checksum How to use checksums |
9ca9b87e338ea39298e4d70086a845c2327a61435d7cdde3dcb52b94ab91cd61
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.13
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 14, 2026.
Transparency logSecurity for django-integrity-policy 1.2.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.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for django_integrity_policy-1.2.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 14, 2026.
Transparency logA 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.