Opened 19 minutes ago
#37569 new Cleanup/optimization
Support {% csp_nonce_attr %} and remove inline onclick handlers in built-in debug templates (follow-up to #33180 and #36825)
| Reported by: | VCoder101 | Owned by: | |
|---|---|---|---|
| Component: | Error reporting | Version: | dev |
| Severity: | Low | Keywords: | CSP |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
### Background and relation to #33180 / #36825
In #33180 (closed fixed on 2025-09-02), Django resolved broken debug pages under strict CSP policies by decorating technical_500_response, technical_404_response, and default_urlconf in django/views/debug.py (lines 64-65, 629-630, 693-694) with:
`python
@csp_override({})
@csp_report_only_override({})
`
Moving CSS/JS to external static files was intentionally avoided in #33180 to prevent chained failures when static file handling causes the error.
Since then, #36825 introduced nonce-based CSP support across contrib.admin, and django/views/templates/csrf_403.html (line 8) already uses <style {% csp_nonce_attr %}> with csp(request) merged into its fallback Context (django/views/csrf.py:68).
A full scan of django/**/*.html on main (5b0c26e7) shows that the only 4 remaining <style> / <script> elements without {% csp_nonce_attr %} in Django are in the three debug templates:
django/views/templates/technical_500.html:<style>(line 9) and<script>(line 83)django/views/templates/technical_404.html:<style>(line 8)django/views/templates/default_urlconf.html:<style>(line 10)
Additionally, django/views/templates/technical_500.html contains 4 inline onclick= attributes (lines 228, 257, 262, 267):
- Line 228:
onclick="return switchPastebinFriendly(this);" - Lines 257, 262, 267:
onclick="toggle('pre{{ frame.id }}', 'post{{ frame.id }}')"
### Motivation
Even though debug pages are for DEBUG=True only and @csp_override({}) already suppresses Django's own ContentSecurityPolicyMiddleware header:
- As noted in #33180 comment:11,
@csp_override({})completely strips the app's CSP protection (instead of preserving a strict nonce-based policy) and does not help when a project wants nonce support in a custom debug/error reporter or inspects CSP headers consistently across all views in development/staging. - Keeping inline
<style>/<script>with{% csp_nonce_attr %}and moving the 4onclickhandlers intoaddEventListenerinside the existing inline<script>preserves full self-containment (no dependency onstaticfiles, no chained failures, andis_email=Truecontinues to emit plain<style>without a nonce when norequestnonce is present). - It finishes aligning the last 3 built-in Django HTML templates with
csrf_403.htmlandcontrib.admin.
### Note on trade-offs / existing behavior
- What if a project configures a strict CSP without
CSP.NONCE(e.g.,script-src: 'self',style-src: 'self')? Removing@csp_override({})unconditionally would break styling/JS on debug pages in that configuration. Two possible options to discuss:- Option A (lowest risk / backwards compatible): Keep
@csp_override({})/@csp_report_only_override({})on the views (or disable CSP only whenCSP.NONCEis not active), while still adding{% csp_nonce_attr %}(mergingcsp(request)whenrequestis available, matchingviews/csrf.py:68) and migrating the 4onclick=attributes toaddEventListener. - Option B: Use nonce-based headers whenever
CSP.NONCEis configured forscript-src/style-src, falling back to empty override otherwise.
- Option A (lowest risk / backwards compatible): Keep
I understand #33180 considered the current @csp_override({}) workaround sufficient, so I am opening this ticket for design feedback before submitting a PR. If accepted, I am happy to provide a patch and tests following admin_views/test_csp.py and view_tests/tests/test_debug.py.
*AI assistance disclosure: Arena.ai Agent Mode was used to scan Django's templates, check ticket history (#33180, #36825), and assist with drafting this ticket description. All file paths, line numbers, and baseline CSP test runs (73/73 OK) were verified locally and reviewed manually.*