Set the draft security HTTP header Permissions-Policy (previously Feature-Policy) on your Django app.
Project description
Set the draft security HTTP header Permissions-Policy (previously Feature-Policy) on your Django app.
Requirements
Python 3.6 to 3.10 supported.
Django 2.2 to 4.0 supported.
Are your tests slow? Check out my book Speed Up Your Django Tests which covers loads of best practices so you can write faster, more accurate tests.
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 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": [], "document-domain": [], "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.
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
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
Hashes for django-permissions-policy-4.5.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3167db2edb25a3230c3e0709e0cb3601e38fc2ab107731cde3b800dfd90c6055 |
|
MD5 | 1f2b22a54a84a5a7aeae624d67a03457 |
|
BLAKE2b-256 | 89a963370898df26ef80b09c3f76fc7121d139ba14a0674a95bb3f43ffe9b7e4 |
Hashes for django_permissions_policy-4.5.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b720850084235fe9f4cb203777e8b264c055f442de8c0efb4715d09ca9414b9b |
|
MD5 | 78e48fedf9b75aa2d7a4980654d6907e |
|
BLAKE2b-256 | 16b7ab28b4cacc961cab4fbbd940c91041e9701519a39f263f8206ad779ca115 |