Subresource Integrity for Django
Project description
Django SRI
Subresource Integrity for Django.
Installation
pip install django-sri
And add sri
to your INSTALLED_APPS
.
Usage
Template Tags
Note: By default, integrity hashes are not output when DEBUG
is True
, as static files change a lot during local development. To override this, set USE_SRI
to True
.
django-sri
is designed to primarily be used through template tags:
{% load sri %}
{% sri_static "index.js" %} <!-- Will output "<script src='/static/index.js' integrity='sha256-...'></script>" -->
{% sri_static "index.css" %} <!-- Will output "<link rel='stylesheet' href='/static/index.css' integrity='sha256-...'/>" -->
For performance, the hashes of files are caches in Django's caching framework. It will attempt to use the "sri" cache, but fall back to "default" if it doesn't exist. The cache keys are the hash of the file path in the specified algorithm in hex. Caches are stored for as long as DEFAULT_TIMEOUT
is set to.
Algorithms
The SRI standard supports 3 algorithms: sha256, sha384 and sha512. By default, SHA256 is used. To override this, supply an additional algorithm
argument to the sri
template tag (or the specific ones):
{% load sri %}
{% sri_static "index.js" algorithm="sha512" %} <!-- Will output "<script src='/static/index.js' integrity='sha512-...'></script>" -->
The default algorithm can be changed by setting SRI_ALGORITHM
to the required algorithm.
Additional attributes
To add additional attributes to the output tag (such as async
/ defer
), specify them as additional arguments to the template tag:
{% load sri %}
{% sri_static "index.js" 'defer' 'async'%}
{% sri_static "index.woff2" preload as="font" %}
Just the integrity value
To retrieve just the integrity hash (the contents of the integrity
attribute), you can use the {% sri_integrity_static %}
tag, which supports the same arguments as the other tags.
{% load sri %}
{% sri_integrity_static "index.js" "sha512" %} <!-- Will output "sha512-..." -->
Supported Files
For automatic tag output, the following files are supported:
.js
.css
Unknown extensions will emit a link
tag with the URL as the href
attribute.
sri_integrity_static
is unaffected by this limitation.
API
from pathlib import Path
from sri import calculate_integrity, calculate_integrity_of_static, Algorithm
calculate_integrity(Path("/path/to/myfile.txt")) # "sha256-..."
calculate_integrity_of_static("index.js") # "sha256-..."
calculate_integrity_of_static("index.js", Algorithm.SHA512) # "sha512-..."
"Does this work with whitenoise or alike?"
Yes. django-sri
outputs the static file URL in the same way the builtin static
template tag does. This means the correct cachebusted URLs are output.
When using a manifest STATICFILES_STORAGE
, django-sri
will automatically retrieve the hashed and post-processed file as opposed to the original.
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
File details
Details for the file django-sri-0.7.0.tar.gz
.
File metadata
- Download URL: django-sri-0.7.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d77b4404988ccf1c40809da8eec5462c70384c84c6247485866a169a55d91cc6 |
|
MD5 | 7a51697506b90fbee77ea4e770402de5 |
|
BLAKE2b-256 | c7152bfda4cbc2ea12599b76d76318b76968a8fbcf8e78acb676a1564860240b |
File details
Details for the file django_sri-0.7.0-py3-none-any.whl
.
File metadata
- Download URL: django_sri-0.7.0-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.18
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b33b6d738f710e1c39a72c16dc0fdcfe8971e34395da446e0fede9d17df6857f |
|
MD5 | 391440195e22369e523ea67ec33932ec |
|
BLAKE2b-256 | 5151ffb78981c38408c759e91128bc777b60a2a819a696dc7dc7cdd3fb11c958 |