Skip to content

Add support for the Trusted Types CSP directives - #394

Open
ousamabenyounes wants to merge 1 commit into
nelmio:masterfrom
ousamabenyounes:fix/issue-233
Open

ousamabenyounes wants to merge 1 commit into
nelmio:masterfrom
ousamabenyounes:fix/issue-233

Conversation

@ousamabenyounes

Copy link
Copy Markdown

Adds support for the Trusted Types CSP
directives require-trusted-types-for and trusted-types (Fix #233).

Trusted Types help mitigate DOM-based XSS by locking down dangerous DOM sinks.
This lets applications configure the two directives through the bundle's usual
CSP configuration.

What

  • Register require-trusted-types-for and trusted-types in
    DirectiveSet::$directiveNames under a dedicated TYPE_TRUSTED_TYPES_LIST
    type. Like other non-source-list directives they are always emitted and never
    deduplicated against default-src.
  • Advertise them in PolicyManager::getChromeDirectives() only. Trusted Types
    is currently shipped by Chromium-based browsers, so (with browser_adaptive
    enabled) they are emitted for Chrome/Opera and omitted for Firefox/Safari,
    consistent with how the bundle already gates browser-specific directives.
  • Quote the script and allow-duplicates keywords in
    ContentSecurityPolicyParser so require-trusted-types-for: ['script']
    renders as require-trusted-types-for 'script' and the 'allow-duplicates'
    trusted-types keyword is quoted, while custom policy names are left untouched.
  • Config wiring and DI are automatic (the config tree is built from
    DirectiveSet::getNames()).
  • Document the new directives in Resources/doc/index.rst.

Purely additive, opt-in, no new dependency and no BC break. This revives the
approach from the (now stale) PR #235.

Test verification (RED → GREEN)

New cases in ContentSecurityPolicyParserTest and
DirectiveSetTest::testTrustedTypesDirectives.

RED (unmodified branch, only the tests applied):

2) ...testQuotesKeywords
-''allow-duplicates''
+'allow-duplicates'
3) ...testTrustedTypesDirectives with data set "chrome emits trusted types directives"
-'default-src 'self'; require-trusted-types-for 'script'; trusted-types my-policy 'allow-duplicates''
+'default-src 'self''
Tests: 18, Assertions: 18, Failures: 3

GREEN (with the fix):

..................                                                18 / 18 (100%)
OK (Tests: 18, Assertions: 18)

Full suite: 433 tests green (was 428 + 5 new). PHP-CS-Fixer and
composer-normalize clean; PHPStan reports no new errors.

Add the require-trusted-types-for and trusted-types Content Security Policy
directives. They are registered as a dedicated policies-list type (always
emitted, never fallbacked by default-src) and advertised for Chromium-based
browsers only, which are currently the ones shipping Trusted Types. The
'script' and 'allow-duplicates' keywords are quoted by the CSP parser.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Contribution] Extending CSP to include Trusted-Types headers

1 participant