Skip to content

Fix CSP configuration for django-csp 4 - #4493

Merged
mathjazz merged 2 commits into
mozilla:mainfrom
mathjazz:4492-django-csp-4-settings
Sep 3, 2026
Merged

Fix CSP configuration for django-csp 4#4493
mathjazz merged 2 commits into
mozilla:mainfrom
mathjazz:4492-django-csp-4-settings

Conversation

@mathjazz

@mathjazz mathjazz commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

Fix #4492.

@mathjazz
mathjazz requested review from eemeli and flodolo September 3, 2026 18:28

@flodolo flodolo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Django deal with uppercase variable names… 😵

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One issue pointed out by LLMs: this test would fail locally for someone with SITE=https:// (couldn't verify on the device I'm currently in).

-def test_content_security_policy(rf):
+def test_content_security_policy(rf, settings):
     response = CSPMiddleware(lambda request: HttpResponse())(rf.get("/"))

+    # These directives get an http: fallback if the site isn't hosted on HTTPS
+    http = "" if settings.SITE_URL.startswith("https") else " http:"
+
     policy = response.headers["Content-Security-Policy"]
     assert "default-src 'self'" in policy
     assert "connect-src 'self'" in policy
     assert "font-src 'self'" in policy
-    assert "frame-src https: http:" in policy
+    assert f"frame-src https:{http}" in policy
     assert "img-src 'self'" in policy
     assert "script-src 'self' 'unsafe-eval'" in policy
     assert "style-src 'self' 'unsafe-inline'" in policy
-    assert "worker-src https: blob: http:" in policy
+    assert f"worker-src https: blob:{http}" in policy

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mathjazz
mathjazz merged commit 1d860eb into mozilla:main Sep 3, 2026
7 checks passed
@mathjazz
mathjazz deleted the 4492-django-csp-4-settings branch September 3, 2026 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CSP is broken

2 participants