|
| 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") |
0 commit comments