Skip to content

Commit 52db6d7

Browse files
fix(csp): no nonce for a directive the app opted into 'unsafe-inline' (ADR-0100, #330) (#253)
Signed-off-by: Andre van Zuydam <andrevanzuydam@gmail.com> Co-authored-by: Andre van Zuydam <andrevanzuydam@gmail.com>
1 parent a593556 commit 52db6d7

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright (c) 2026 Code Infinity
2+
# SPDX-License-Identifier: MPL-2.0
3+
# This Source Code Form is subject to the terms of the Mozilla Public
4+
# License, v. 2.0. If a copy of the MPL was not distributed with this
5+
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
6+
7+
"""CSP nonce respects a user's 'unsafe-inline' (ADR-0100, refines ADR-0088).
8+
9+
A browser ignores 'unsafe-inline' in any directive that also lists a nonce
10+
(CSP Level 2+). Since 3.13.143 the framework appended its per-response nonce to
11+
script-src/style-src unconditionally - including an app's own TINA4_CSP - so an
12+
app that deliberately allowed inline through 'unsafe-inline' had it silently
13+
blocked (tina4-php#330). The rule now: a directive (present, or derived from
14+
default-src) that already carries 'unsafe-inline' gets NO nonce; the app's
15+
explicit choice wins. The strict default policy is unchanged and still nonced.
16+
17+
The answer key is tina4-documentation/plan/v3/fixtures/securityheaders_contract.json,
18+
invariant csp-nonce-respects-unsafe-inline. The same cases run in all four
19+
frameworks on the real inject function - no mocks, a pure string transform.
20+
"""
21+
from tina4_python.core.csp import inject_nonce_into_csp
22+
23+
NONCE = "abc123"
24+
TOKEN = "'nonce-abc123'"
25+
26+
27+
def _directive(csp: str, name: str) -> str:
28+
for part in csp.split(";"):
29+
part = part.strip()
30+
if part.split(" ", 1)[0].lower() == name:
31+
return part
32+
return ""
33+
34+
35+
def test_explicit_unsafe_inline_script_src_keeps_unsafe_inline_and_gets_no_nonce():
36+
out = inject_nonce_into_csp(
37+
"default-src 'self'; script-src 'self' 'unsafe-inline'", NONCE
38+
)
39+
assert "'unsafe-inline'" in _directive(out, "script-src")
40+
assert TOKEN not in _directive(out, "script-src")
41+
42+
43+
def test_explicit_unsafe_inline_style_src_keeps_unsafe_inline_and_gets_no_nonce():
44+
out = inject_nonce_into_csp(
45+
"default-src 'self'; style-src 'self' 'unsafe-inline'", NONCE
46+
)
47+
assert "'unsafe-inline'" in _directive(out, "style-src")
48+
assert TOKEN not in _directive(out, "style-src")
49+
50+
51+
def test_unsafe_inline_default_src_does_not_spawn_a_nonced_directive():
52+
out = inject_nonce_into_csp("default-src 'self' 'unsafe-inline'", NONCE)
53+
# No derived script-src/style-src is added - they fall back to default-src,
54+
# so the app's inline keeps working; the nonce is nowhere.
55+
assert TOKEN not in out
56+
assert _directive(out, "script-src") == ""
57+
assert _directive(out, "style-src") == ""
58+
59+
60+
def test_mixed_unsafe_inline_script_but_strict_style_nonces_only_style():
61+
out = inject_nonce_into_csp(
62+
"default-src 'self'; script-src 'self' 'unsafe-inline'", NONCE
63+
)
64+
# script-src opted into inline -> untouched; style-src derived from the
65+
# strict default -> gets the nonce so framework inline styles still run.
66+
assert TOKEN not in _directive(out, "script-src")
67+
assert TOKEN in _directive(out, "style-src")
68+
69+
70+
def test_strict_default_policy_still_gets_the_nonce():
71+
out = inject_nonce_into_csp("default-src 'self'", NONCE)
72+
assert TOKEN in _directive(out, "script-src")
73+
assert TOKEN in _directive(out, "style-src")
74+
75+
76+
def test_explicit_non_unsafe_inline_directive_still_gets_the_nonce():
77+
out = inject_nonce_into_csp(
78+
"default-src 'self'; script-src 'self' https://cdn.example", NONCE
79+
)
80+
assert TOKEN in _directive(out, "script-src")

‎tina4_python/core/csp.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,23 @@ def inject_nonce_into_csp(csp: str, nonce: str) -> str:
100100
for directive in ("style-src", "script-src"):
101101
existing = next((d for d in directives if d[0] == directive), None)
102102
if existing is not None:
103+
# ADR-0100: the app opted this directive into inline via
104+
# 'unsafe-inline'. A browser ignores 'unsafe-inline' the moment a
105+
# nonce joins the same directive (CSP Level 2+), so adding the nonce
106+
# would silently block the inline the app asked for. The app's
107+
# explicit choice wins; its own inline runs, framework inline rides
108+
# the same 'unsafe-inline'.
109+
if "'unsafe-inline'" in existing[1].split():
110+
continue
103111
if token not in existing[1].split():
104112
existing[1] = (existing[1] + " " + token).strip()
105113
else:
106114
base = default_value if default_value else "'self'"
115+
# ADR-0100: a directive derived from an 'unsafe-inline' default-src
116+
# must not be spawned with a nonce - it would cancel the inline the
117+
# app allowed through default-src. Leave it to fall back.
118+
if "'unsafe-inline'" in base.split():
119+
continue
107120
directives.append([directive, (base + " " + token).strip()])
108121

109122
return "; ".join(

0 commit comments

Comments
 (0)