Skip to main content

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

Project description

https://img.shields.io/github/actions/workflow/status/adamchainz/django-permissions-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-permissions-policy.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

Set the Permissions-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.9 to 3.14 supported.

Django 4.2 to 6.0 supported.

Installation

  1. Install with pip:

python -m pip install django-permissions-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_permissions_policy.PermissionsPolicyMiddleware",
    ...,
]
  1. Add the PERMISSIONS_POLICY setting to your settings, naming at least one feature. Here’s an example that sets a strict policy to disable many potentially privacy-invading and annoying features for all scripts:

    PERMISSIONS_POLICY = {
        "accelerometer": [],
        "ambient-light-sensor": [],
        "autoplay": [],
        "camera": [],
        "display-capture": [],
        "encrypted-media": [],
        "fullscreen": [],
        "geolocation": [],
        "gyroscope": [],
        "interest-cohort": [],
        "magnetometer": [],
        "microphone": [],
        "midi": [],
        "payment": [],
        "usb": [],
    }

    See below for more information on the setting.

Setting

Change the PERMISSIONS_POLICY setting to configure the contents of the header.

The setting should be a dictionary laid out with:

  • Keys as the names of browser features - a full list is available on the W3 Spec repository. The MDN article is also worth reading.

  • Values as lists of strings, where each string is either an origin, e.g. 'https://example.com', or of the special values 'self' or '*'. If there is just one value, no containing list is necessary. To represent no origins being allowed, use an empty list.

    Note that in the header, domains are wrapped in double quotes - do not include these quotes within your Python string, as they will be added by the middleware.

If the keys or values are invalid, ImproperlyConfigured will be raised at instantiation time, or when processing a response. The current feature list is pulled from the JavaScript API with document.featurePolicy.allowedFeatures() on Chrome and Firefox. Browsers don’t always recognize all features, depending on the version and configuration. You may see warnings in the console for unavailable features in the header - these are normally safe to ignore, since django-permissions-policy already validates that you don’t have completely unknown names.

For backwards compatibility with old configuration, the value 'none' is supported in lists, but ignored - it’s preferable to use the empty list instead. It doesn’t make sense to specify 'none' alongside other values.

Examples

Disable geolocation entirely, for the current origin and any iframes:

PERMISSIONS_POLICY = {
    "geolocation": [],
}

Allow autoplay from only the current origin and iframes from https://archive.org:

PERMISSIONS_POLICY = {
    "autoplay": ["self", "https://archive.org"],
}

Allow autoplay from all origins:

PERMISSIONS_POLICY = {
    "autoplay": "*",
}

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_permissions_policy-4.28.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

django_permissions_policy-4.28.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file django_permissions_policy-4.28.0.tar.gz.

File metadata

File hashes

Hashes for django_permissions_policy-4.28.0.tar.gz
Algorithm Hash digest
SHA256 956ce8b08b82efb11ebabe3c5b11a6eb380f44eb543b3a2130adaec5d7fec651
MD5 853370b03867c15ac5ebdb8b29707f17
BLAKE2b-256 cead6445c82d70bcac2b9e2b3e42b59553a195c799c63b7187c310a02a775dc2

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_permissions_policy-4.28.0.tar.gz:

Publisher: main.yml on adamchainz/django-permissions-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_permissions_policy-4.28.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_permissions_policy-4.28.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bea079be235a7e10325d4eb84cfb97f44e1ee8a1786da0e3f9122c0a0b1e66ec
MD5 ceacd6a7269ae76f42cfee6efa40a19d
BLAKE2b-256 ac7c395dff3ce4a299de2b0f069c2572bc8f20c149066b6c754b2a08486dacf9

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_permissions_policy-4.28.0-py3-none-any.whl:

Publisher: main.yml on adamchainz/django-permissions-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