fix(web-app-dashboard): run inline scripts by hash instead of 'unsafe-inline'

The dashboard sent script-src 'unsafe-inline' from two sources. Its own
meta/csp.yml granted it to script-src-attr, and build_csp_header adds it
to script-src-elem and script-src-attr for every app with
services.logout.enabled. Both reach script-src through the family union,
and the token in script-src-elem also closes the hash gate, so the
hashes that sys-front-inj-javascript, -dashboard, -logout and -matomo
register for their inline snippets were discarded.

build_csp_header now skips the logout grant on a directive whose
meta/csp.yml sets unsafe-inline: false. Only web-app-dashboard sets it:
on script-src and script-src-elem. Its injected snippets now run by hash
and the logout one-liner is hashed like the others. A render of
build_csp_header for every role, variant, logout on/off and tor on/off
(1712 headers) differs from origin/main only in the 8 web-app-dashboard
headers; the 86 other roles that declare a logout service render
byte-identical policies.

script-src-attr keeps 'unsafe-inline': port-ui 2.2.2 still renders
inline event handlers (menu onclick entries, openDynamicPopup,
closeAllModals, the card image onerror). It goes once the port-ui
release without inline handlers is pinned and header.yml.j2 moves from
onclick to action.

The dashboard Playwright spec asserts that script-src and script-src-elem
carry no 'unsafe-inline' on the landing page, next to its existing check
for CSP violations.

Refs #1122

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
kevinveenbirkenbachandClaude Opus 5.5 committed 2026-10-29 00:27:49 +01:00
1 parent e8cb250c51
commit d721e887a5
5 files changed
+60 -7

No files matched your search

@@ -89,6 +89,47 @@ class TestCspTogglesDashboardLogout(unittest.TestCase):
self.assertIn("'unsafe-inline'", attr)
self.assertIn("'unsafe-inline'", elem)
def test_logout_respects_explicit_disable_on_elem_directive(self):
apps = copy.deepcopy(self.apps)
apps["app1"]["csp"]["flags"]["script-src-elem"] = {"unsafe-inline": False}
self._set_service_enabled(apps, "logout", True)
header = self.filter.build_csp_header(
apps,
"app1",
self.domains,
"https",
extra_hashes={"script-src-elem": ["console.log('logout');"]},
)
elem = self._get_directive_tokens(header, "script-src-elem")
attr = self._get_directive_tokens(header, "script-src-attr")
expected_hash = self.filter.get_csp_hash("console.log('logout');")
self.assertNotIn("'unsafe-inline'", elem)
self.assertIn(expected_hash, elem)
self.assertIn("'unsafe-inline'", attr)
def test_logout_respects_explicit_disable_on_whole_script_family(self):
apps = copy.deepcopy(self.apps)
for directive in ("script-src", "script-src-elem", "script-src-attr"):
apps["app1"]["csp"]["flags"][directive] = {"unsafe-inline": False}
self._set_service_enabled(apps, "logout", True)
header = self.filter.build_csp_header(
apps,
"app1",
self.domains,
"https",
extra_hashes={"script-src-elem": ["console.log('logout');"]},
)
expected_hash = self.filter.get_csp_hash("console.log('logout');")
for directive in ("script-src", "script-src-elem", "script-src-attr"):
self.assertNotIn(
"'unsafe-inline'", self._get_directive_tokens(header, directive)
)
self.assertIn(expected_hash, self._get_directive_tokens(header, "script-src"))
def test_logout_propagates_to_base_when_not_explicitly_disabled(self):
apps = copy.deepcopy(self.apps)
apps["app1"].setdefault("csp", {}).setdefault("flags", {})