1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-02 17:06:09 +00:00
webvirtcloud/webvirtcloud/tests/test_csp.py
catborise 443ba63154 feat(webvirtcloud): pages run only the scripts the panel serves
The Content-Security-Policy now also sends script-src 'self': a script
injected into a page, inline or as an event handler, does not run. The
templates have none of their own any more, and a test keeps it so.
worker-src 'self' blob: lets the XML editor (Ace) and the API docs
(Redoc) start their workers, which they make from blob: URLs. The
Swagger UI page is drf-spectacular's split view, which serves its start
script as a file instead of inline.
2026-11-02 10:08:09 +03:00

38 lines
1.7 KiB
Python

"""Every response carries the Content-Security-Policy, and no template has
what its script-src 'self' would block: an inline script, an inline event
handler or a javascript: URL."""
import re
from pathlib import Path
from django.conf import settings
from django.test import SimpleTestCase, TestCase
from django.urls import reverse
# a <script> without src that is not json_script data
INLINE_SCRIPT = re.compile(r"<script(?![^>]*\bsrc=)(?![^>]*application/json)[^>]*>")
INLINE_HANDLER = re.compile(r"\son[a-z]+\s*=", re.I)
class ContentSecurityPolicyTests(TestCase):
def test_pages_and_redirects_carry_the_policy(self):
for url in (reverse("accounts:login"), reverse("index")): # a page, and a redirect to the login
with self.subTest(url=url):
policy = self.client.get(url)["Content-Security-Policy"]
self.assertEqual(
policy,
"script-src 'self'; worker-src 'self' blob:; object-src 'none'; base-uri 'self'; form-action 'self'; frame-ancestors 'none'",
)
class TemplatesRunNoInlineScriptTests(SimpleTestCase):
def test_no_template_has_an_inline_script_handler_or_javascript_url(self):
base = Path(settings.BASE_DIR)
templates = [*base.glob("*/templates/**/*.html"), *base.glob("templates/**/*.html")]
self.assertTrue(templates)
for path in templates:
source = path.read_text()
with self.subTest(template=str(path.relative_to(base))):
self.assertIsNone(INLINE_SCRIPT.search(source))
self.assertIsNone(INLINE_HANDLER.search(source))
self.assertNotIn("javascript:", source)