﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
37569	Support {% csp_nonce_attr %} and remove inline onclick handlers in built-in debug templates (follow-up to #33180 and #36825)	VCoder101		"### 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:

1. 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.
2. Keeping inline `<style>` / `<script>` with `{% csp_nonce_attr %}` and moving the 4 `onclick` handlers into `addEventListener` inside the existing inline `<script>` preserves full self-containment (no dependency on `staticfiles`, no chained failures, and `is_email=True` continues to emit plain `<style>` without a nonce when no `request` nonce is present).
3. It finishes aligning the last 3 built-in Django HTML templates with `csrf_403.html` and `contrib.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 when `CSP.NONCE` is not active), while still adding `{% csp_nonce_attr %}` (merging `csp(request)` when `request` is available, matching `views/csrf.py:68`) and migrating the 4 `onclick=` attributes to `addEventListener`.
  - **Option B:** Use nonce-based headers whenever `CSP.NONCE` is configured for `script-src`/`style-src`, falling back to empty override otherwise.

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.*"	Cleanup/optimization	new	Error reporting	dev	Low		CSP		Unreviewed	0	0	0	0	0	0
