Private
Public Access
Fix CSP blocking the service worker registration and tag colors
script-src 'self' blocks any inline <script> block outright, which would have silently broken the service-worker registration snippet in base.html in production — the exact feature this whole project exists to support. Moved it to static/js/register-sw.js, loaded via <script src>, so script-src can stay strict with no 'unsafe-inline' exception. style-src needed 'unsafe-inline' regardless, since tag pills set a per-tag color via a dynamic inline style attribute. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3f6474ce94
commit
4c20558525
4 files changed
+13
-9
No files matched your search
+1
-1
@@ -59,5 +59,5 @@ def test_base_template_links_manifest_and_registers_service_worker(client, djang
|
|||||||
content = response.content.decode()
|
content = response.content.decode()
|
||||||
|
|
||||||
assert 'rel="manifest"' in content
|
assert 'rel="manifest"' in content
|
||||||
assert "serviceWorker" in content
|
assert "register-sw.js" in content
|
||||||
assert "apple-touch-icon" in content
|
assert "apple-touch-icon" in content
|
||||||
@@ -35,7 +35,12 @@ CONTENT_SECURITY_POLICY = {
|
|||||||
"DIRECTIVES": {
|
"DIRECTIVES": {
|
||||||
"default-src": ["'self'"],
|
"default-src": ["'self'"],
|
||||||
"img-src": ["'self'", "https:", "data:"],
|
"img-src": ["'self'", "https:", "data:"],
|
||||||
"style-src": ["'self'"],
|
# Tag pills render a per-tag color via a dynamic inline style="--tag-
|
||||||
|
# color: ..." attribute (templates/articles/_article_tags.html etc.),
|
||||||
|
# so style-src needs 'unsafe-inline'. script-src stays strict with no
|
||||||
|
# exception — every <script> in the app is loaded from a static file
|
||||||
|
# (see templates/base.html), none inline.
|
||||||
|
"style-src": ["'self'", "'unsafe-inline'"],
|
||||||
"script-src": ["'self'"],
|
"script-src": ["'self'"],
|
||||||
"frame-ancestors": ["'none'"],
|
"frame-ancestors": ["'none'"],
|
||||||
"base-uri": ["'self'"],
|
"base-uri": ["'self'"],
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
if ("serviceWorker" in navigator) {
|
||||||
|
window.addEventListener("load", () => {
|
||||||
|
navigator.serviceWorker.register("/sw.js");
|
||||||
|
});
|
||||||
|
}
|
||||||
+1
-7
@@ -27,12 +27,6 @@
|
|||||||
<main>
|
<main>
|
||||||
{% block content %}{% endblock %}
|
{% block content %}{% endblock %}
|
||||||
</main>
|
</main>
|
||||||
<script>
|
<script src="{% static 'js/register-sw.js' %}" defer></script>
|
||||||
if ("serviceWorker" in navigator) {
|
|
||||||
window.addEventListener("load", () => {
|
|
||||||
navigator.serviceWorker.register("{% url 'pwa:service_worker' %}");
|
|
||||||
});
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in new issue
Block a user