Review main Merged

85044[TASK] Cache frontend Content-Security-Policy dynamic mutations

Oliver Hader CR +2 V +2 Normal +86 −2 updated 10/29/24, 13:28

Commit message

The Content-Security-Policy model can be manipulated by several
PSR-14 events during runtime. This works fine unless the generated
response document would be cached (e.g. during frontend rendering).

This change enhances the CSP frontend scope by handling caches and
fetching or persisting those dynamic mutations, which would be lost
in case they were not cached consequently.

Review

  • Code review CR +2 Done
    • Oliver Hader +2
    • Benjamin Franzke +1
    • Andreas Kienast +1
  • Verified V +2 Done
    • Oliver Hader +2
    • Benjamin Franzke +1
    • Andreas Kienast +1
    • core-ci +1 CI

Issues 1

Files 3

Review log 3852

Show 1927 earlier messages
  1. Oliver Hader uploaded patch set 1 07/01/24, 08:27

  2. core-ci voted on patch set 1 V +1 07/01/24, 08:40 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/60738

  3. Oliver Hader wrote 07/01/24, 08:55

    Topic set to csp
  4. Patrick Schriner commented on patch set 1 07/01/24, 09:02

    I really like this approach, will remove some of my headaches
  5. Christian Kuhn commented on patch set 1 CR -1 07/01/24, 11:27

    RequestHandler.php:122

    couldn't we just always register the listener and let it return early in case the request is not an FE application request, or similar? This would avoid the entire 'dynamic listener' approach and is probably easier to grasp as well?
  6. Oliver Hader commented on patch set 1 07/01/24, 15:00

    RequestHandler.php:122 Reply

    That would create new cross-dependencies between separate components: * `PolicyProvider` is preparing the CSP headers for the response * `RequestHandler` knows about the current cache vs uncached state & actually triggers the "page generation" * `TypoScriptFrontendController` defines the partition used for the cache identifiers (hash-base and page-ids) By defining a static event handler in a separate class - this event handler would have runtime dependencies - as it should not be executed in the backend context. Just a side-reference to Symfony and how events are connected there: https://symfony.com/doc/current/components/event_dispatcher.html#connecting-listeners
  7. Christian Kuhn commented on patch set 1 07/02/24, 01:32

    RequestHandler.php:122 Reply

    the dependency to tsfe should/will fall: cache details should be modeled as request attributes which voids that dependency. modeling cache stuff in a good way should be the very next thing when tsfe-removal is being picked up again - i wouldn't worry about that dependency too much at the moment. i thought about this: * do not have this 'addRuntimeListener' call. * make handleContentSecurityPolicyDynamicMutations() public and register this as listener using the casual attribute-based registration. let it return early if the request is not a fe application request. note we have similar listeners in middlewares already to have middleware code called back (look for AsEventListener): https://github.com/TYPO3/typo3/blob/main/typo3/sysext/frontend/Classes/Middleware/TimeTrackerInitialization.php https://github.com/TYPO3/typo3/blob/main/typo3/sysext/workspaces/Classes/Middleware/WorkspacePreview.php what do you think?
  8. Oliver Hader uploaded patch set 2 CR -1 V +1 07/02/24, 18:03

  9. Oliver Hader uploaded patch set 3 07/02/24, 18:05

  10. Oliver Hader commented on patch set 3 07/02/24, 18:19

    RequestHandler.php:122 Reply

    I've adjusted the patch accordingly - my focus is on fixing the behavior for content-security-policy. The provided examples show implementations that act as PSR-15 middlewares and PSR-14 event handler at the same time - and they modify internal state. This only works if the process flow stays exactly like that - if the processing order of the event handler and middleware changes (for whatever reason), it might cause undetected side effects...
  11. core-ci voted on patch set 2 V -1 07/02/24, 18:20 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/60860

  12. Oliver Hader commented on patch set 3 07/02/24, 18:20

    RequestHandler.php:122 Reply

    Anyways, please have a look to the adjusted patch - which is now called for any request in the backend or frontend - that's the consequence of having a global event handler.
  13. core-ci voted on patch set 3 V +1 07/02/24, 18:21 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/60862

  14. Oliver Hader uploaded patch set 4 V +1 07/03/24, 12:04

  15. core-ci voted on patch set 4 V +1 07/03/24, 12:14 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/60905

  16. Oliver Hader uploaded patch set 5 V +1 09/03/24, 08:31

  17. Markus Klein commented on patch set 6 09/03/24, 08:35

    Published edit on patch set 5.
  18. core-ci voted on patch set 6 V +1 09/03/24, 08:41 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/64652

  19. core-ci voted on patch set 5 V +1 09/03/24, 08:47 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/64650

  20. Oliver Hader uploaded patch set 7 V +1 10/05/24, 10:44

  21. core-ci voted on patch set 7 V +1 10/05/24, 11:01 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/67149

  22. Guido Schmechel commented on patch set 7 CR +1 V +1 10/05/24, 12:36

    Basically, the whole thing worked. But what I would like to see is another RST for the new event. What do you think? I got it working with this example: ``` #[AsEventListener( identifier: 'vendor/extension/identifier' )] final class ContentSecurityPolicyCacheEventListener { public function __invoke(BeforePolicyMutationEvent $event): void { $mutations = $event->getPersistedMutations(); $mutations[] = new MutationCollection( new Mutation( MutationMode::Extend, Directive::FrameSrc, SourceScheme::data, new UriValue('*.example.com'), ), ); $event->setPersistedMutations($mutations); } } ``` But I'm not sure if this is best practice. Disclaimer: I am not yet fully into the CSP topic
  23. Oliver Hader commented on patch set 7 CR -1 10/05/24, 17:02

    Reply

    Thanks for your feedback. Having the new event documented in an RST file makes sense. I also realized, that `setPersistedMutations()` should be dropped from the event. Modifications to persisted mutations are not cached individually, since it is supposed to be retrieved from the database for a given csp-scope.
  24. Oliver Hader uploaded patch set 8 CR +1 V +1 10/11/24, 16:37

  25. Oliver Hader commented on patch set 8 10/11/24, 16:39

    Reply

    I dropped the idea of having an RST file for the new event as it is mainly supposed to be an internal functionality. I made this clear by annotating the new event class with `@internal`.
  26. core-ci voted on patch set 8 V +1 10/11/24, 16:43 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/67610

  27. Guido Schmechel voted on patch set 8 CR +1 V +1 10/11/24, 20:42

  1. Christian Kuhn commented on patch set 8 CR -1 10/12/24, 11:45

    RequestHandler.php:204

    request is within the event and does not need to be fetched from globals here, if i read correctly. we should avoid new code using $GLOBALS['TYPO3_REQUEST'].

    RequestHandler.php:211

    any reason to not DI this?

    RequestHandler.php:242

    any reason to not DI this?
  2. Christian Kuhn commented on patch set 8 10/12/24, 11:49

    RequestHandler.php:221

    Catching JsonException should be sufficient here, I think. Catching PHP errors (\Throwable, the mother of both exception and errors) could be avoided as far as I see, and if not, should at least have a comment why it's needed.
  3. Oliver Hader updated the commit message in patch set 9 CR +1 V +1 10/18/24, 13:56

  4. core-ci voted on patch set 9 V +1 10/18/24, 19:48 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/68091

  5. Oliver Hader commented on patch set 9 10/20/24, 09:13

    RequestHandler.php:204 Reply

    Wrong. The request from the event is optional/nullable. see https://review.typo3.org/c/Packages/TYPO3.CMS/+/84952

    RequestHandler.php:211 Reply

    I would have considered this a breaking change - especially since it's targeted for v12

    RequestHandler.php:242 Reply

    I would have considered this a breaking change - especially since it's targeted for v12
  6. Oliver Hader uploaded patch set 10 V +1 10/22/24, 06:54

  7. Oliver Hader commented on patch set 10 10/22/24, 06:54

    RequestHandler.php:204 Reply

    Done

    RequestHandler.php:211 Reply

    Done

    RequestHandler.php:242 Reply

    Done
  8. Oliver Hader uploaded patch set 11 10/22/24, 06:56

  9. Oliver Hader commented on patch set 11 10/22/24, 06:56

    RequestHandler.php:221 Reply

    Done
  10. core-ci voted on patch set 10 V -1 10/22/24, 07:03 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/68354

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

  12. Oliver Hader uploaded patch set 12 V -1 10/22/24, 07:28

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

  14. Oliver Hader commented on patch set 12 10/22/24, 10:20

    @lolli@schwarzbu.ch Is this fine for you now?
  15. Oliver Hader uploaded patch set 13 V +1 10/22/24, 15:59

  16. core-ci voted on patch set 13 V +1 10/22/24, 16:09 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/68379

  17. Christian Kuhn commented on patch set 13 10/22/24, 16:10

    Reply

    on my list for next week (latest).
  18. Oliver Hader commented on patch set 13 CR -1 10/28/24, 09:58

    I'm reworking the change again...
  19. Oliver Hader uploaded patch set 14 CR -1 V +1 10/28/24, 21:41

  20. core-ci voted on patch set 14 V +1 10/28/24, 21:46 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/68739

  21. Benjamin Franzke commented on patch set 14 CR +1 V +1 10/29/24, 12:14

    MetaDataState.php:44

    I'd allow to get/update state from the request. But I'm fine to do that in v14 only and have the current approach as-is for the backports. I've prepared that for v14 as an additional patch in: https://review.typo3.org/c/Packages/TYPO3.CMS/+/86821/1

    MetaDataState.php:66

    I'd love exceptions here, but we've discussed in personal that we don't want to introduce that for v12 anymore.
  22. Andreas Kienast commented on patch set 14 CR +1 V +1 10/29/24, 12:55

    MetaDataState.php:61

    Nitpick: `JSON_THROW_ON_ERROR` is registered on a global level, no need for the namespace here.
  23. Oliver Hader voted on patch set 14 CR +2 V +2 10/29/24, 13:07

  24. Oliver Hader merged patch set 15 10/29/24, 13:08

  25. Oliver Hader commented on patch set 15 10/29/24, 13:28

    Reply

    Done

    MetaDataState.php:61 Reply

    I kept it like this...