Set the Permissions-Policy HTTP header on your Django app.
Project description
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.10 to 3.14 supported.
Django 4.2 to 6.0 supported.
Installation
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",
...,
]
Add a PERMISSIONS_POLICY or PERMISSIONS_POLICY_REPORT_ONLY setting to your settings file, 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 settings.
Settings
The permissions policy for your page is configured with two settings:
PERMISSIONS_POLICY - sets the Permissions-Policy header, which defines the policy that the browser enforces.
PERMISSIONS_POLICY_REPORT_ONLY - sets the Permissions-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 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": "*",
}
Disable geolocation entirely, and test the effect of allowing autoplay from certain domains:
PERMISSIONS_POLICY = {
"geolocation": [],
}
PERMISSIONS_POLICY_REPORT_ONLY = {
"autoplay": ["self", "https://archive.org"],
}
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_permissions_policy-4.29.0.tar.gz.
File metadata
- Download URL: django_permissions_policy-4.29.0.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9046457a4c3f74e077df00f2d90007ffb14f812e8402b04b020ee0ff9cc1f6c3
|
|
| MD5 |
c983c613c234feb31f8dea980094bd36
|
|
| BLAKE2b-256 |
ad58cd7f4cba102ef076a709bb8e86c56b5f419c42687623e8da6fb8fd357fb1
|
Provenance
The following attestation bundles were made for django_permissions_policy-4.29.0.tar.gz:
Publisher:
main.yml on adamchainz/django-permissions-policy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_permissions_policy-4.29.0.tar.gz -
Subject digest:
9046457a4c3f74e077df00f2d90007ffb14f812e8402b04b020ee0ff9cc1f6c3 - Sigstore transparency entry: 1189477678
- Sigstore integration time:
-
Permalink:
adamchainz/django-permissions-policy@d5b005782092a6a9f557060cd5393e059fa6984c -
Branch / Tag:
refs/tags/4.29.0 - Owner: https://github.com/adamchainz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@d5b005782092a6a9f557060cd5393e059fa6984c -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_permissions_policy-4.29.0-py3-none-any.whl.
File metadata
- Download URL: django_permissions_policy-4.29.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
42adb0a2dea9a2e9bec3aa2b644fbdaebbfd29cec02ffc279560fdd231db94cf
|
|
| MD5 |
cdde6c884945b11095f92a1d74c1969a
|
|
| BLAKE2b-256 |
f58666a7c20fd68b2029dc7b1fd189162a97c8080accbc5c3c6b27093651713d
|
Provenance
The following attestation bundles were made for django_permissions_policy-4.29.0-py3-none-any.whl:
Publisher:
main.yml on adamchainz/django-permissions-policy
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_permissions_policy-4.29.0-py3-none-any.whl -
Subject digest:
42adb0a2dea9a2e9bec3aa2b644fbdaebbfd29cec02ffc279560fdd231db94cf - Sigstore transparency entry: 1189477681
- Sigstore integration time:
-
Permalink:
adamchainz/django-permissions-policy@d5b005782092a6a9f557060cd5393e059fa6984c -
Branch / Tag:
refs/tags/4.29.0 - Owner: https://github.com/adamchainz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@d5b005782092a6a9f557060cd5393e059fa6984c -
Trigger Event:
push
-
Statement type: