Review main Abandoned

65612[TASK] Streamline AjaxDataHandler

Oliver Hader CR 0 V 0 Hard +295 −112 updated 05/20/25, 12:17

Commit message

* Streamlines messages being sent in AjaxDataHandler

  The same event was emitted twice as message to BroadcastChannel and
  as CustomEvent to current document - now it's only broadcasted, which
  internall emits to currrent document (in case there's a subscription)

  Event names have been streamlined and now are
  * typo3:ajax-data-handler:process-failed (+ meta-data, response)
  * typo3:ajax-data-handler:process-successed (+ meta-data, response)

* Listens to incoming broadcast messages

  Using message type `typo3:ajax-data-handler:instruction@<token>`, the
  internal AjaxDataHandler processing can be triggered for knwon actions.
  `<token>` is an internal random value to ensure that each instance of
  AjaxDataHandler is only handling responses they actually initiated.

These changes allow https://github.com/FriendsOfTYPO3/sudo-mode to
intercept and handle failed requests to server-side DataHandler.

Review

  • Code review CR 0
  • Verified V 0

Issues 1

Files 10

Review log 1820

  1. Oliver Hader uploaded patch set 1 09/07/20, 07:51

  2. Oliver Hader voted on patch set 1 CR -1 09/07/20, 08:03

  3. TYPO3com voted on patch set 1 V +1 09/07/20, 08:06 Completed build in 14m 08s on Mon, 7 Sep, 08:06 AM Test Summary: 73227 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-5310

  4. Oliver Hader uploaded patch set 2 09/07/20, 18:45

  5. Richard Haeser voted on patch set 2 CR +1 09/07/20, 18:54

  6. TYPO3com voted on patch set 2 V +1 09/07/20, 19:17 Completed build in 28m 16s on Mon, 7 Sep, 07:17 PM Test Summary: 73227 passed Find logs and detail information at https://bamboo.typo3.com/browse/CORE-GTC-5347

  7. Benni Mack voted on patch set 2 V +1 CR +1 09/07/20, 19:47

  8. Benjamin Franzke commented on patch set 2 V -1 CR -1 09/09/20, 14:59

    I don't really get what the type AjaxDataHandlerProcessMessage

    Interfaces.ts:9

    Reading AjaxDataHandlerProcessMessage and AjaxDataHandlerProcessEventDict it looks like the intention is to "extend" from AjaxDataHandlerProcessEventDict (which itself is intended to extend from AjaxDataHandlerRecord and AjaxDataHandlerElement) and AjaxDataHandlerProcessEventDict. As "|" works as a union, the properties of either of the types/interfaces may be available, but only those that are listed in all types are "required"/guaranteed, that means: There are no guarantees/static-type-checks, that for example hasErrors or processToken will be available in AjaxDataHandlerProcessMessage. So I think you rather want an intersection here, which means to replace "|" with "&". That would "mimic" the classical "extends" behaviour. Now, if we change that (replacing all "|" with "&") for these merged types, typescript will show, that elementIdentifier (as defined in interface AjaxDataHandlerElement) is currently missing in: https://github.com/TYPO3/TYPO3.CMS/blob/71cca94c29a044017d8755fe2e3f2d7656875445/Build/Sources/TypeScript/backend/Resources/Public/TypeScript/ContextMenuActions.ts#L234-L235 Using intersection that'd result in the following typescript message: Argument of type '{ component: string; action: string; table: string; uid: number; }' is not assignable to parameter of type 'AjaxDataHandlerProcessEventDict'. Property 'elementIdentifier' is missing in type '{ component: string; action: string; table: string; uid: number; }' but required in type 'AjaxDataHandlerElement'. Now, as Build/Sources/TypeScript/backend/Resources/Public/TypeScript/AjaxDataHandler.ts uses the interface AjaxDataHandlerInstruction (which uses extends and therefore marks elementIdentifier to be required), my guess is this is really not intended, as we got an inconsistency between union-behaviour of AjaxDataHandlerProcessMessage and extends-behaviour of AjaxDataHandlerInstruction. To summarize in form of example questions: Is elementIdentifier intended to be required for AjaxDataHandlerProcessMessage or optional? Is "component" intended to be required for AjaxDataHandlerProcessMessage or optional? PS: I actually tried to find whether there might be problems due to the mising elementIdentifier, and I could produce a javascript error deleting a be_group using the ContextMenu delete-entry with https://github.com/FriendsOfTYPO3/sudo-mode/pull/5 – I guess that is because ContextMenuAction is missing to add the elementIdentifier. Unfortunately I wasn't able to get a deep backtrace, as the error happens inside the promise that is started by AjaxDataHandler.process in ContextMenuActions.ts: Uncaught (in promise) s.AjaxResponse {response: Response, resolvedBody: {…}} (Note: deletion of be_groups in sudo mode using list-toolbar-button worked)
  9. Oliver Hader commented on patch set 2 CR -1 09/10/20, 10:50

    Interfaces.ts:9 Reply

    Actually shall be a union - elementIdentifier is only applicable if some $anchorElement is known (which is for instance not the case in context menus). The main problem in AjaxDataHandler is, that it mixes view dependencies (of the list module) with actual process flow being sent to the server (I did not want to touch/change that completely). The "component" property also has ambiguous meaning - one for the "calling component" and another for the "event emitting component", it seems to be mandatory, however I'm not sure whether all other parts calling AjaxDataHandler.process are considering that fact. I'll check the error you reported - which however makes me wonder, since it's one of the tests I recorded in the screencast for TYPO3 v9.5 attached to the forge ticket.
  10. Benjamin Franzke commented on patch set 2 09/10/20, 12:38

    AjaxDataHandler.ts:141

    As written in the other thread, I think you don't want to re-throw unconditionally here. The 9.5 variant of this patch also doesn't do that: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65696/1/typo3/sysext/backend/Resources/Private/TypeScript/AjaxDataHandler.ts#119 Or you add a catch() block to AjaxDataHandler.process in ContextMenuAction as AjaxDataHandler will do with this patch in processDeleteRecord(): https://review.typo3.org/c/Packages/TYPO3.CMS/+/65612/2/Build/Sources/TypeScript/backend/Resources/Public/TypeScript/AjaxDataHandler.ts#387 But the empty catch block shows that catching that thing isn't really helpful, if you do not know, whether the failure has been handled gracefully (due to broadcasting), or not. I think broadcasting show have a result, and if broadcast says ("error is handled"), then that should be available in the thrown object.

    Interfaces.ts:9 Reply Unresolved

    This made be suspicious, and I check again: It points out the "uncaught" is explicitly thrown by AjaxDataHandler.process when broadcastProcessFailed has been dispatched: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65612/2/Build/Sources/TypeScript/backend/Resources/Public/TypeScript/AjaxDataHandler.ts#141 So the catch handler does two things, broadcasting the failure *and* (re)throwing the response as an error. Maybe it was intended to not re-throw(, or it was intended that all users of AjaxDataHandler.process() always use .catch())? In the 9.5 variant you don't throw, the code just broadcasts the failure: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65696/1/typo3/sysext/backend/Resources/Private/TypeScript/AjaxDataHandler.ts#119 That explains why the 9.5 variant worked for you.
  11. Oliver Hader commented on patch set 2 09/10/20, 13:16

    AjaxDataHandler.ts:141 Reply

    Prior to this patch none of the failures were handled, so basically re-throwing is the same behavior as without having this patch. Of course most AJAX request respond with a 200 status code currently - in order to reproduce my statement (without this patch) you'd have to "cause" e.g. 500 HTTP status response server-side. From my POV the correct scenario would be that every caller (e.g. also ContextMenuAction) handles/catches these failures. Broadcasting is just sending a message, that's one-way. Introducing another promise here (in order to make sure a failure has been handled in this async context) makes the whole process even more complex. I need to think about that a bit...
  12. Oliver Hader commented on patch set 2 09/10/20, 13:17

    Besides that, I can take away all the types I introduced and we can continue with the wrong types or any...
  13. Benjamin Franzke commented on patch set 2 09/12/20, 18:45

    AjaxDataHandler.ts:141 Reply

    Yea, in general I'm perfectly fine with re-throwing to keep BC, just ContextMenuAction has to handle this case in a meaninful way then (like in generall all ajax errors should be handled, both 4xx and 5xx). BUT: I don't see a way how ContextMenuAction/AjaxDataHandler could handle that situation with the current interface in a *proper* way and *without* conflicting with an extension like sudo_mode. Reasons: When a 403 response is returned by the server (due to EXT:sudo_mode), then ContextMenuAction/AjaxDataHandler would need to show a Notification to inform the user about that error – currently those errors are silently ignored (as you said), yes, but that is not good. And we can/should not introduce an interface (error broadcasting for extensions like sudo_mode), that then depends on the current misbehaviour that errors are handled silently. Or in other words: A proper core Notiication when a 4xx or 5xx response is returned, would conflict with the things that sudo_mode wants to do. With broadcasting the core couldn't *wait* for a response, it has to happen – but when an error is handled to the AjaxDataHandler, it *has* to present that to the user. So this shows that broadcasting the error may simply be an inappropriate way for EXT:sudo_mode to be useful at all. We should rather have something that is more synchronous, but of course implemented asynchronously (basically UDP vs TCP). Now, that would be a perfect fit for clients middlewares (as PSR-15 middlewares but for client-side, so basically as in guzzle client). Such middleware could intercept he 403 response (that is was sudo_mode would do), issue a new request, handle all the privilege escalation stuff and then *re-issue* the original request and transmit that response to the original handler of the TYPO3 core AjaxRequest/AjaxDataHandler. This would allow sudo_mode to be transparent and the AjaxDataHandler wouldn't see an "error" (403) at all, as the 403 response would be intercepted. I've actually started to experiment in order to not only talk theoretic stuff in here and it turns out to be quite a less intrusive motification to the core than this change. So, I've added a simple POC implementation for middlewares to the ajax handler of TYPO3. (A middleware around window.fetch() – so using the browser Request/Response objects) It's basically like our PSR-15 middleware dispatcher (using a stack of closures that execute each other in order): https://github.com/bnf/TYPO3.CMS/commit/ajax-middleware-v1 (Implemented on top of this patch for now, as I'd wanted to adapt sudo_mode, and therefore used this as starting point) Here are the changes how sudo_mode would work using a middleware: https://github.com/bnf/sudo-mode/commit/ajax-middleware-v1 (This POC is based on https://github.com/FriendsOfTYPO3/sudo-mode/pull/5) I only tested this against TYPO3 master for now, but both, with and without rsaauth. Therefore a master-compatible rsaauth is needed: https://github.com/FriendsOfTYPO3/rsaauth/pull/2 (open pull request for v10 LTS/master support) Actually I figured it's not ideal to broadcast the errors from the inner frame to the outer frame to handle the sudo mode at all. A module might not always have access to an outer frame, therefore (and because it would be needed for middlewares anyway) the POC attaches to the AjaxDataHandler *within* the frame where the context menu actions are executed (of course, modal has than to be executed on the outer frame, if there is one). It uses a PageRenderer hook to only attach sudo_mode BackendEventListener if a core module is loaded that uses the AjaxDataHandler. To summarize: IMO middlewares allow for the same functionality, they are less intrusive and easier to maintain than this broadcasting thing. What we need to decide: You want to support v9/v10 in sudo_mode, that's of course a problem for a new feature like middleware (event if they are quite transparent and very unintrusive, but still…) But, to be fair: I actually think that these changes to add failure broadcasting and broadcast-responses (instructions) are also a bit more than just streamlining, they smell like new features (yea, broadcasts already exist, middlewares do not – but then we're probably talking about breaking changes in here).
  14. Benjamin Franzke commented on patch set 2 09/12/20, 22:25

    AjaxDataHandler.ts:141 Reply Unresolved

    Cleaned up the POC for AJAX middlewares a bit, rebased on master and moved to gerrit: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65708
  15. Benni Mack commented on patch set 2 11/04/21, 12:58

    how to continue here? If nothing happens I'm for abandoning this change.
  16. Oliver Hader marked it work in progress 11/09/21, 08:40

  17. Oliver Hader commented on patch set 2 11/09/21, 08:42

    Set as WIP - this is still a requirement for sudo-mode (either as extension or as core-integration). Sooner or later this topic will come back...
  18. Benni Mack wrote 11/29/21, 10:14

    Change destination moved from master to main
  19. Benni Mack updated the commit message in patch set 3 11/29/21, 13:39

  20. Benjamin Franzke abandoned the change 05/20/25, 12:17

    Implemented via fetch-middleware in https://review.typo3.org/c/Packages/TYPO3.CMS/+/89467