diff --git a/plugins/filter/README.md b/plugins/filter/README.md
index 7312a1d39a..ccea48bcfe 100644
--- a/plugins/filter/README.md
+++ b/plugins/filter/README.md
@@ -33,13 +33,14 @@ flowchart TD
grant["csp_filters.py:354
logout grant"]
wl["csp_filters.py:123
get_csp_whitelist"]
inj["sys-front-inj-dashboard,
-javascript, -matomo, -logout
add_csp_hash"]
- gate["csp_filters.py:365
hash gate"]
+ gate["csp_filters.py:366
hash gate"]
build["csp_filters.py:203
build_csp_header"]
header["set $csp
nginx vhost"]
decl -->|"1. flags read by"| flags
flags -->|"2. tokens"| build
svc -->|"3. read by"| grant
+ decl -->|"3a. unsafe-inline: false
on -elem or -attr vetoes"| grant
grant -->|"4. adds 'unsafe-inline'
to script-src-elem and -attr"| build
decl -->|"5. whitelist read by"| wl
wl -->|"6. tokens"| build
@@ -52,7 +53,7 @@ flowchart TD
grant -.->|"TRIP-WIRE: absent from the
smart-defaults docstring, which
names logout only for frame-ancestors"| build
```
-Styles carry `'unsafe-inline'` by default; scripts do not. The logout grant is the only path that puts it into a script directive.
+Styles carry `'unsafe-inline'` by default; scripts do not. The logout grant is the only implicit path that puts it into a script directive. An application keeps it out of `script-src-elem` or `script-src-attr` by setting `unsafe-inline: false` on that directive in its `meta/csp.yml`.
## Further Reading
diff --git a/plugins/filter/csp_filters.py b/plugins/filter/csp_filters.py
index 3ff788bb42..aa1111722d 100644
--- a/plugins/filter/csp_filters.py
+++ b/plugins/filter/csp_filters.py
@@ -351,10 +351,11 @@ class FilterModule:
)
)
- if directive in (
- "script-src-attr",
- "script-src-elem",
- ) and self.is_feature_enabled(applications, "logout", application_id):
+ if (
+ directive in ("script-src-attr", "script-src-elem")
+ and self.is_feature_enabled(applications, "logout", application_id)
+ and explicit_flags.get("unsafe-inline") is not False
+ ):
tokens.append("'unsafe-inline'")
tokens += self.get_csp_whitelist(
diff --git a/roles/web-app-dashboard/files/playwright/test-csp-canonical-domain.js b/roles/web-app-dashboard/files/playwright/test-csp-canonical-domain.js
index 3edfdebcaf..63f1448081 100644
--- a/roles/web-app-dashboard/files/playwright/test-csp-canonical-domain.js
+++ b/roles/web-app-dashboard/files/playwright/test-csp-canonical-domain.js
@@ -23,6 +23,12 @@ exports.register = function (shared) {
const directives = assertCspResponseHeader(response, "dashboard landing");
await assertCspMetaParity(page, directives, "dashboard landing");
+ for (const directive of ["script-src", "script-src-elem"]) {
+ expect(
+ directives[directive],
+ `dashboard landing: ${directive} MUST NOT carry 'unsafe-inline', inline scripts run by hash`
+ ).not.toContain("'unsafe-inline'");
+ }
const documentHtml = await response.text();
const documentUrl = response.url();
diff --git a/roles/web-app-dashboard/meta/csp.yml b/roles/web-app-dashboard/meta/csp.yml
index 71a188495c..a93a209457 100644
--- a/roles/web-app-dashboard/meta/csp.yml
+++ b/roles/web-app-dashboard/meta/csp.yml
@@ -15,5 +15,9 @@ whitelist:
frame-src:
- "*.{{ DOMAIN_PRIMARY }}"
flags:
+ script-src:
+ unsafe-inline: false
+ script-src-elem:
+ unsafe-inline: false
script-src-attr:
- unsafe-inline: true # nocheck: csp-unsafe Reason: historical, TODO: re-verify it is really required and does not open attack surface
+ unsafe-inline: true # nocheck: csp-unsafe Reason: port-ui 2.2.2 renders inline event handlers (menu onclick entries, openDynamicPopup, closeAllModals, card image onerror)
diff --git a/tests/unit/python/plugins/filter/test_csp_filters/test_toggles_desktop_logout.py b/tests/unit/python/plugins/filter/test_csp_filters/test_toggles_desktop_logout.py
index 87563c8582..7d3ed0923d 100644
--- a/tests/unit/python/plugins/filter/test_csp_filters/test_toggles_desktop_logout.py
+++ b/tests/unit/python/plugins/filter/test_csp_filters/test_toggles_desktop_logout.py
@@ -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", {})