Skip to main content

django-pipeline-csp

CSP nonce support for django-pipeline script tags.

Under a nonce-based Content Security Policy with 'strict-dynamic', browsers ignore host allowlists, so every <script> needs the request's nonce. django-pipeline renders its tags without one (jazzband/django-pipeline#771). django-pipeline-csp provides a drop-in {% javascript %} tag that adds it.

Installation

pip install django-pipeline-csp
INSTALLED_APPS = [
    # ...
    "pipeline",
    "pipeline_csp",
]

Usage

Replace {% load pipeline %} with {% load pipeline_csp %}:

{% load pipeline_csp %}
{% stylesheet "base" %}
{% javascript "polyfills" %}

Every <script> rendered by {% javascript %} gets nonce="..." — individual source files (PIPELINE_ENABLED = False), the compressed bundle and inline JavaScript templates alike. {% stylesheet %} is passed through unchanged, so one load is enough.

Do not load both libraries in one template: the library loaded last wins, and with {% load pipeline_csp pipeline %} the nonce is silently missing.

Form and widget media (class Media)

Django renders {{ form.media }} itself, so those <script> tags have no nonce either — including PipelineFormMedia with js_packages. There are two ways to add it.

Everywhere: MediaNonceMiddleware

Add the middleware after your CSP middleware:

MIDDLEWARE = [
    # ...
    "csp.middleware.CSPMiddleware",  # or django.middleware.csp.ContentSecurityPolicyMiddleware
    "pipeline_csp.middleware.MediaNonceMiddleware",
]

Every <script> rendered by a Media object during the request gets the nonce: your templates, templates of third-party apps and the Django admin alike, without changing any template. When the middleware is loaded it wraps django.forms.Media.render_js once; outside a request (or without a nonce) the output is unchanged. Only tags generated by Media from Python code are touched, never the response as a whole, so injected HTML never receives the nonce. <link> tags are left as they are.

Per template: csp_nonce filter

{% load pipeline_csp %}
{{ form.media|csp_nonce:request }}
{{ form.media.js|csp_nonce:request }}

Renders like {{ form.media }}, with the nonce on every <script>. Values that are not Media, request=None or a missing nonce leave the output unchanged. request must exist in the context: Django resolves filter arguments strictly, so a missing variable raises VariableDoesNotExist. Keep this in mind for templates that are rendered without a request, e.g. by Django's default server_error view for 500.html.

Filter and middleware can be combined; the nonce is never added twice.

Nonce sources

The nonce is taken from, in this order:

  1. Django's built-in CSP (Django >= 6.0): django.middleware.csp.ContentSecurityPolicyMiddleware
  2. django-csp >= 4.0: csp.middleware.CSPMiddleware (pip install django-pipeline-csp[django-csp])

Your policy must include the nonce in script-src, e.g. CSP.NONCE (Django) or csp.constants.NONCE (django-csp). Without an active middleware the output is identical to django-pipeline's.

request must be in the template context (django.template.context_processors.request).

The tag must render before the CSP middleware writes the response header — normal template rendering already satisfies this. Accessing the nonce after the header was written raises CSPNonceError with django-csp; with Django's built-in CSP a late nonce is simply not included in the header and the scripts it was meant to allow are blocked. This matters mainly for streaming responses, where content can be produced after the headers are sent.

Not covered

  • Jinja2 templates
  • Nonces on <link rel="stylesheet">
  • Scripts injected via document.write ('strict-dynamic' does not trust parser-inserted scripts)
  • Inline event handlers such as onclick="..."

Compatibility

Python 3.10–3.14, Django 4.2 / 5.0 / 5.1 / 5.2 / 6.0 / 6.1, django-pipeline >= 4.1.

License

MIT

Release files for django-pipeline-csp 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-pipeline-csp 0.2.0
File Size Uploaded
django_pipeline_csp-0.2.0.tar.gz 11.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-pipeline-csp 0.2.0
File Interpreter ABI Platform
django_pipeline_csp-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.6 kB

Release files / django_pipeline_csp-0.2.0.tar.gz

Download URL django_pipeline_csp-0.2.0.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
c72adb58ba6264b96ce869133796bb14dde67af9afa7ce48a0934a7c8eab99b3
BLAKE2b-256 checksum
How to use checksums
10241f2ff186e3759312748dfbf229f30141cf59ebf56a875b8ecccfbb9f3748
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release files / django_pipeline_csp-0.2.0-py3-none-any.whl

Download URL django_pipeline_csp-0.2.0-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fd6961c5b6ca4e9a12458c29b3aa8852cdfbc8eba3fd34766eef27243225aa66
BLAKE2b-256 checksum
How to use checksums
7a5303eeb06126af9a99e47536ecb82897023c19ed7b095495244c9c1ce11e2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 25, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page