Skip to main content

Set the Integrity-Policy HTTP header on your Django app.

Project description

https://img.shields.io/github/actions/workflow/status/adamchainz/django-integrity-policy/main.yml.svg?branch=main&style=for-the-badge https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/django-integrity-policy.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

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

  1. 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",
    ...,
]
  1. 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:

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_integrity_policy-1.0.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_integrity_policy-1.0.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

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

Hashes for django_integrity_policy-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a31e82ec5094862cdecdd55fb3415a29914d42eeceadfb26b7032a7add0c9c04
MD5 f362dc9bc0be371a1e457e5a0e443842
BLAKE2b-256 166e96f264805cd0e6e40897da9edbf862b7150c2f41d2e2ae3624fcd145bfdd

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_integrity_policy-1.0.0.tar.gz:

Publisher: main.yml on adamchainz/django-integrity-policy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_integrity_policy-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_integrity_policy-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b7ab666dcbf28fef7f5d95ffbb6977c4b4a0a74830f82fe1d4688c2826f8cc1a
MD5 535442c39f0043c441c69d5ed75cf65a
BLAKE2b-256 c948bbfa8a7bf7b43fc5c3a2b952ec90b136c8d61142074aed6410af046f3d29

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page