Skip to content

Add granular script-src/style-src CSP directives - #393

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

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

Conversation

@ousamabenyounes

Copy link
Copy Markdown

Adds support for the CSP level 3 granular source directives:
script-src-attr, script-src-elem, style-src-attr, style-src-elem.

Why

These directives let applications control inline event handlers and
<script>/<style> elements separately from the generic script-src/
style-src directives (see the CSP3 spec).
They have been requested for a while (Fix #200).

What

  • Register the four directives in DirectiveSet::$directiveNames as
    TYPE_SRC_LIST_NOFB. They fall back to script-src/style-src (not
    default-src), so they must always be emitted as configured rather than
    being deduplicated against default-src.
  • Advertise them as available for CSP level 3 browsers in PolicyManager
    (getLevel3()), so they are emitted for Chrome/Firefox and omitted for
    Safari (CSP level 1), consistent with the other level 3 directives.
  • Config wiring (Configuration.php) and DI are automatic: the config tree is
    built from DirectiveSet::getNames().
  • Document the new directives in Resources/doc/index.rst.

No new dependency, no BC break — the directives are purely additive and opt-in.

Test verification (RED → GREEN)

New data-driven cases in DirectiveSetTest::testGranularSrcDirectives.

RED (on the unmodified branch, only the test applied):

1) ...testGranularSrcDirectives with data set "chrome emits granular directives"
--- Expected
+++ Actual
@@ @@
-'default-src 'self'; script-src-attr 'unsafe-inline'; script-src-elem script-elem.example.org 'self'; style-src-attr 'unsafe-inline'; style-src-elem style-elem.example.org 'self''
+'default-src 'self''
Tests: 3, Assertions: 3, Failures: 2

GREEN (with the fix):

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

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

Add support for the CSP level 3 script-src-attr, script-src-elem,
style-src-attr and style-src-elem directives. They are registered as
no-default-fallback source lists (they fall back to script-src/style-src,
not default-src) and advertised as available for CSP level 3 browsers.
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.

New script src directives

1 participant