Review main Merged

90814[BUGFIX] Avoid applying CSP nonce sources when not required

Oliver Hader CR +2 V +2 Hard +802 −82 updated 10/30/25, 18:13

Commit message

This change aims to avoid applying Content-Security-Policy nonce
source when they are not actually required.

The `ConsumableNonce` class was refactored and does not extend
`ConsumableString` anymore. As a result the invocation counters
are now split into `consumeInline` (required usage of the nonce)
and `consumeStatic` (optional usage of the nonce) and are tracking
the different usage aspects, like their directives.

$nonce = new ConsumableNonce();
$nonce->consumeInline(Directive::ScriptSrcElem); // required usage
$nonce->consumeStatic(Directive::StyleSrcElem); // optional usage

Nonce sources are removed from the CSP policy in case
- the response content is readable and is empty (0 bytes); or
- the nonce consumption counter (all usages) is zero; or
- the consumed nonce sources for a particular directive can be
  avoided due to the existence of a source keyword exception
  (like using `'unsafe-inline'`); or
- the new `PolicyPreparedEvent` is used to programatically adjust
  the corresponding behavior to deny or allow  nonce sources

In case the nonce sources are supposed to be denied, both
`ContentSecurityPolicyHeaders` middlewares in the frontend
and backend scope are removing the corresponding nonce-related
literals from the generated HTML content of the response object.

Review

  • Code review CR +2 Done
    • Oliver Hader +2
    • Christian Kuhn +1
    • Benni Mack +1
  • Verified V +2 Done
    • Oliver Hader +2
    • Christian Kuhn +1
    • Benni Mack +1
    • core-ci +1 CI

Issues 1

Files 24

Show 4 more files

Review log 5181

Show 3356 earlier messages
  1. Oliver Hader uploaded patch set 1 09/26/25, 15:44

  2. Oliver Hader uploaded patch set 2 09/26/25, 15:56

  3. core-ci voted on patch set 1 V -1 09/26/25, 15:57 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/85616

  4. core-ci voted on patch set 2 V +1 09/26/25, 16:03 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/85619

  5. Oliver Hader uploaded patch set 3 V +1 09/28/25, 18:34

  6. core-ci voted on patch set 3 V -1 09/28/25, 18:40 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/85666

  7. Oliver Hader rebased onto patch set 4 V -1 09/29/25, 07:45

  8. core-ci voted on patch set 4 V +1 09/29/25, 07:53 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/85683

  9. Oliver Hader uploaded patch set 5 V +1 10/06/25, 16:43

  10. Oliver Hader commented on patch set 5 10/06/25, 16:44

    NonceValueSubstitution.php:23

  11. core-ci voted on patch set 5 V +1 10/06/25, 16:53 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86087

  12. Oliver Hader uploaded patch set 6 V +1 10/07/25, 19:16

  13. core-ci voted on patch set 6 V -1 10/07/25, 19:37 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86181

  14. Oliver Hader uploaded patch set 7 V -1 10/07/25, 19:41

  15. core-ci voted on patch set 7 V -1 10/07/25, 19:54 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86183

  16. Oliver Hader uploaded patch set 8 V -1 10/07/25, 20:00

  17. core-ci voted on patch set 8 V -1 10/07/25, 20:11 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86184

  18. Oliver Hader uploaded patch set 9 V -1 10/07/25, 20:49

  19. core-ci voted on patch set 9 V +1 10/07/25, 20:56 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86188

  20. Oliver Hader uploaded patch set 10 V +1 10/07/25, 21:17

  21. core-ci voted on patch set 10 V +1 10/07/25, 21:24 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86190

  22. Oliver Hader uploaded patch set 11 V +1 10/07/25, 22:04

  23. Oliver Hader marked it ready for review 10/07/25, 22:04

  24. core-ci voted on patch set 11 V +1 10/07/25, 22:11 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86191

  25. Garvin Hicking commented on patch set 11 10/08/25, 04:54

    Important-107062-AvoidApplyingContent-Security-PolicyNonceSourcesWhenNotRequired.rst:87

    Could you describe more distinction of "allow" and "avoid"? I don't understand what would trigger a nonce in "default" that would not get triggered in "avoid". the example of "strict-dynamic" would trigger an nonce in both allow and avoid, so a "negative" example would help for distinction?

    Important-107062-AvoidApplyingContent-Security-PolicyNonceSourcesWhenNotRequired.rst:90

    Could you also mention in which situations a dropped nonce even though required would be useful? I wonder how a site would work then on a client. Is this about being "lax" to violations, so if you only use nonce protected assets like CSS/JS that is optional and ok to not be shown? Or is this about things like edge caches / proxies which should ignore them?
  26. Garvin Hicking commented on patch set 11 10/08/25, 05:09

    Important-107062-AvoidApplyingContent-Security-PolicyNonceSourcesWhenNotRequired.rst:15

    Do you mean performance by allowing reverse proxies and such? or client rendering performance? Maybe we should drop this "nonce are nasty for caching (client-side and for proxies" keyword?
  27. Oliver Hader commented on patch set 11 10/08/25, 08:21

    Important-107062-AvoidApplyingContent-Security-PolicyNonceSourcesWhenNotRequired.rst:15 Reply

    Yepp. When nonce sources are used, a page is not fully cached in TYPO3 (it behaves like when using `USER_INT` or `COA_INT` in TypoScript), plus HTTP cache headers are set to private. This has a negative impact on proxies, and e.g. for extensions like `staticfilecache`.

    Important-107062-AvoidApplyingContent-Security-PolicyNonceSourcesWhenNotRequired.rst:87 Reply

    (see below) - `allow` as the default case represents the current behavior (without this patch) - it also could be named `auto` or just `null` (in terms of undefined)

    Important-107062-AvoidApplyingContent-Security-PolicyNonceSourcesWhenNotRequired.rst:90 Reply

    `drop` focusses on fully cached pages and sending proper cache-headers (which is not possible when nonce sources are used). These enum cases are a preparation for introducing hash sources (as alternative to nonce values). Maybe the values should be: * `true` - use it (without enforcing) * `null` - let the system detect & decide * `false` - drop it (incl. constraints like for strict-dynamic) I also could make the event `@internal`, since it's not useful yet, without having the possibility to use hash sources.
  28. Oliver Hader uploaded patch set 12 V +1 10/08/25, 08:55

  29. Oliver Hader commented on patch set 12 10/08/25, 08:57

    Important-107062-AvoidApplyingContent-Security-PolicyNonceSourcesWhenNotRequired.rst:15 Reply

    I've adjusted this section to mention HTTP cache headers, as well as the performance topic.

    Important-107062-AvoidApplyingContent-Security-PolicyNonceSourcesWhenNotRequired.rst:90 Reply

    I've now adjusted the whole section and removed the `UseType` enum from the code.
  30. core-ci voted on patch set 12 V +1 10/08/25, 09:02 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86212

  31. Oliver Hader commented on patch set 11 10/08/25, 09:14

  32. Garvin Hicking commented on patch set 12 10/08/25, 12:07

    ConsumableNonce.php:28

    This is a small goose chase ;), maybe directly refer to consumeInline/consumeStatic here?

    Important-107062-AvoidApplyingContent-Security-PolicyNonceSourcesWhenNotRequired.rst:15 Reply

    Much better to understand, thanks!

    Important-107062-AvoidApplyingContent-Security-PolicyNonceSourcesWhenNotRequired.rst:19

    ```suggestion This change aims for having fully cached pages and tries to avoid nonce sources ```

    Important-107062-AvoidApplyingContent-Security-PolicyNonceSourcesWhenNotRequired.rst:87 Reply

    Acknowledged

    Important-107062-AvoidApplyingContent-Security-PolicyNonceSourcesWhenNotRequired.rst:90 Reply

    I also think this makes it better readable and has less overhead.
  33. Oliver Hader uploaded patch set 13 V +1 10/08/25, 13:53

  34. Oliver Hader commented on patch set 13 10/08/25, 13:53

    Stream.php:229 Reply

    Done

    ConsumableNonce.php:28 Reply

    Done

    Important-107062-AvoidApplyingContent-Security-PolicyNonceSourcesWhenNotRequired.rst:19 Reply

    Done
  35. core-ci voted on patch set 13 V +1 10/08/25, 14:06 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86236

  36. Oliver Hader uploaded patch set 14 V +1 10/08/25, 23:19

  37. core-ci voted on patch set 14 V +1 10/08/25, 23:26 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86257

  38. Oliver Hader rebased onto patch set 15 V +1 10/09/25, 18:28

  39. core-ci voted on patch set 15 V +1 10/09/25, 18:35 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86336

  40. Oliver Hader commented on patch set 16 V +1 10/13/25, 07:33

    Published edit on patch set 15.
  41. core-ci voted on patch set 16 V +1 10/13/25, 07:42 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86519

  42. Oliver Hader rebased onto patch set 17 V +1 10/14/25, 11:56

  43. core-ci voted on patch set 17 V +1 10/14/25, 12:04 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86622

  44. Oliver Hader rebased onto patch set 18 V +1 10/14/25, 20:52

  45. core-ci voted on patch set 18 V -1 10/14/25, 21:00 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86661

  46. core-ci commented on patch set 18 10/14/25, 21:10 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86661

  47. Oliver Hader uploaded patch set 19 V -1 10/15/25, 09:41

  48. core-ci voted on patch set 19 V +1 10/15/25, 09:50 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/86707

  49. Oliver Hader commented on patch set 19 CR -1 10/19/25, 09:09

  50. Oliver Hader uploaded patch set 20 CR -1 V +1 10/19/25, 17:15

  51. core-ci voted on patch set 20 V +1 10/19/25, 17:25 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87019

  52. Oliver Hader rebased onto patch set 21 V +1 10/23/25, 11:25

  53. Oliver Hader commented on patch set 21 10/23/25, 11:25

    Reply

    Done
  54. core-ci voted on patch set 21 V +1 10/23/25, 11:34 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87412

  55. Oliver Hader uploaded patch set 22 V +1 10/27/25, 16:06

  56. core-ci voted on patch set 22 V +1 10/27/25, 16:15 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87679

  1. Oliver Hader uploaded patch set 23 V +1 10/27/25, 16:32

  2. core-ci voted on patch set 23 V +1 10/27/25, 16:39 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87681

  3. Oliver Hader uploaded patch set 24 V +1 10/27/25, 16:40

  4. core-ci voted on patch set 24 V +1 10/27/25, 16:50 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87684

  5. Christian Kuhn uploaded patch set 25 V +1 10/28/25, 13:20

  6. core-ci voted on patch set 25 V -1 10/28/25, 13:28 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87825

  7. Christian Kuhn commented on patch set 25 10/28/25, 13:39

    PolicyProvider.php:45

    the final is removed here only for unit RequestHandlerTest to mock it. i'll have a look at that test case now, it needs a refactoring anyway.
  8. Christian Kuhn uploaded patch set 26 V -1 10/28/25, 13:41

  9. core-ci voted on patch set 26 V +1 10/28/25, 13:48 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87826

  10. Oliver Hader uploaded patch set 27 V +1 10/28/25, 19:49

  11. Oliver Hader rebased onto patch set 28 10/28/25, 19:52

  12. core-ci voted on patch set 27 V -1 10/28/25, 19:52 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87876

  13. Oliver Hader commented on patch set 29 10/28/25, 19:53

    Published edit on patch set 28.
  14. Oliver Hader commented on patch set 29 10/28/25, 19:55

    PolicyProvider.php:45 Reply

    Awesome, thx! I re-added `final` (and kept the `readonly` change for that `@internal` class).
  15. core-ci voted on patch set 29 V +1 10/28/25, 20:03 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/87878

  16. Christian Kuhn commented on patch set 29 10/30/25, 13:09

    RequestHandler.php:137

    skip. frontend.page.parts must be set at this point, and if someone managed to make it something different than PageParts, it could please just fatal. maybe just expect if you insist on testing for it here. PrepareTypoScriptFrontendRendering tests for some attributes and excepts as well already.

    RequestHandler.php:179

    let's do the bugfix, but i think we should model this differently and more explicit in main afterwards, to separate it from casual "int" handling.
  17. Christian Kuhn uploaded patch set 30 V +1 10/30/25, 13:12

  18. Christian Kuhn commented on patch set 29 10/30/25, 13:12

    RequestHandler.php:137 Reply

    Done

    RequestHandler.php:179 Reply

    Done
  19. Christian Kuhn commented on patch set 30 CR +1 V +1 10/30/25, 13:20

    RequestHandler.php:180

    the main issue is that registering the pseudo INT makes core never send client cache headers, right? it stems from the mix up with "casual" INT objects here, if i understand this correctly and leads to issues with tsfe->getCacheHeaders() (i'm rewriting the method in https://review.typo3.org/c/Packages/TYPO3.CMS/+/91332 and will move it soon). so we could potentially simplify this area if we separate int/non-cached element and nonce handling in a better way?
  20. core-ci voted on patch set 30 V +1 10/30/25, 13:21 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/88141

  21. Benni Mack voted on patch set 30 CR +1 V +1 10/30/25, 14:22

  22. Oliver Hader voted on patch set 30 CR +2 V +2 10/30/25, 15:52

  23. Oliver Hader merged patch set 31 10/30/25, 16:20

  24. Oliver Hader commented on patch set 31 10/30/25, 16:24

    RequestHandler.php:180 Reply

    Correct, nonces must not be cached ("number used *once*"), that's why it was added as pseudo `INT` object here. When rewriting this (CSP) part, it would require an indicator, that the nonce needs to be regenerated in the next request and the corresponding post-processing.
  25. Christian Kuhn commented on patch set 31 10/30/25, 18:13

    RequestHandler.php:180 Reply

    i see. thx. i'll keep that in mind with current refactoring loops and may come back to it.