Review main Abandoned

75613[TASK] Directly respond to invalid HTTP server requests

Oliver Hader CR 0 V 0 Easy +37 −1 updated 10/04/24, 10:55

Commit message

Instead of throwing exceptions on invalid HTTP server
requests, the application now responds directly with a
corresponding HTTP status code.

This way, malformed requests are not polluting error
logs anymore and result in a proper HTTP status code.

Review

  • Code review CR 0
  • Verified V 0

Issues 1

Files 2

Review log 3236

Show 1011 earlier messages
  1. Oliver Hader uploaded patch set 1 09/06/22, 09:51

  2. Oliver Hader commented on patch set 1 09/06/22, 09:53

    This change is ready for review.
  3. Oliver Hader commented on patch set 1 09/06/22, 09:54

    I'm not really happy yet with the new static map - using another exception instead (which would be capable of transporting the status-code) might be an alternative as well, e.g. `InvalidServerRequestException`... Looking forward to receiving your feedback/tests on this one.
  4. core-ci voted on patch set 1 V +1 09/06/22, 09:59 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/30645

  5. Andreas Kienast commented on patch set 1 09/06/22, 10:30

    I kinda dislike the suggested

    Reply

    "InvalidServerRequestException" doesn't sound correct for status codes >= 500, IMO. In Symfony, there's a generic HttpException, where most error codes have their own respective exception class, maybe it's worth a look for inspiration.
  6. Andreas Kienast commented on patch set 1 09/06/22, 10:30

    Reply

    aaaargh, Gerrit...
  7. Oliver Klee commented on patch set 1 09/06/22, 11:45

    ServerRequestFactory.php:35

    /** * @var array<positive-int, positive-int> */

    ServerRequestFactory.php:35

    From the naming, it's not directly clear that these are HTTP status codes. Proposed name: EXCEPTION_CODES_TO_HTTP_STATUS_CODES (which also makes clear that this is a mapping)

    ServerRequestFactory.php:36

    For the HTTP status codes, do we have some names constants we can use?
  8. Stephan Großberndt commented on patch set 1 09/06/22, 11:50

    exception
  9. Susi Moog commented on patch set 1 09/06/22, 15:24

    Reply

    Yep, I like the symfony solution (which is afaics also used by laravel btw) - https://github.com/symfony/symfony/tree/6.2/src/Symfony/Component/HttpKernel/Exception providing both a generic HttpException and "speaking" ones, which would also have the advantage that the calling method could denote its' intention better (so Request::validateMethod could throw a NotImplementedHttpException)
  10. Oliver Hader commented on patch set 1 09/06/22, 18:56

    Thanks for your feedback. For a bugfix that can be back-ported to TYPO3 v10 I tried to avoid introducing new transport exceptions and was aiming for a simple mapping instead. However, it seems that a general `HttpException` (which still extends `\InvalidArgumentException`, for BW-compatibility) would be the way to go. This still can be extended with meaningful exception names.
  11. Oliver Hader commented on patch set 1 09/06/22, 19:59

    Reply

    Done

    ServerRequestFactory.php:35 Reply

    Obsolete

    ServerRequestFactory.php:35 Reply

    Obsolete
  1. Oliver Hader uploaded patch set 2 09/06/22, 20:01

  2. Oliver Klee voted on patch set 2 CR +1 09/06/22, 20:06

  3. core-ci voted on patch set 2 V -1 09/06/22, 20:16 Core CI is not happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/30683

  4. Oliver Hader commented on patch set 2 CR -1 09/07/22, 07:52

    HttpException.php:23

    wrong
  5. Oliver Hader uploaded patch set 3 09/07/22, 07:58

  6. core-ci voted on patch set 3 V +1 09/07/22, 08:05 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/30705

  7. Susi Moog voted on patch set 3 V +1 CR +1 09/07/22, 08:36

  8. Susi Moog commented on patch set 3 09/07/22, 08:37

    HttpException.php:23 Reply

    Done
  9. Markus Klein voted on patch set 3 CR +1 09/07/22, 11:04

  10. Markus Klein commented on patch set 3 09/07/22, 11:08

    HttpException.php:35 Unresolved

    IMO this is not needed

    Request.php:234 Unresolved

    @throws?
  11. Benjamin Franzke commented on patch set 3 CR -1 09/08/22, 16:36

    So this is basically like ImmediateResponseException (or the older ImmediateResponseException), both which are there for cases where current API doesn't allow to directly return responses. So we could rather create responses and use PropagateResponseException without intdroducing new API. Also: The same remarks as with ImmediateResponseException vs ImmediateResponseException would apply here: HttpException bypasses the middleware stack, that's not good and it would be better (for proper cleanup) to walk through the middleware stack. For those two reasons: Please use PropagateResponseException instead.
  12. Benjamin Franzke commented on patch set 3 09/08/22, 16:37

    Reply

    I meant "like PropagateResponseException (or the older ImmediateResponseException".
  13. Benjamin Franzke commented on patch set 3 CR 0 09/08/22, 16:48

    Hm.. and an additional note:

    Reply

    Well, ok, I understand that PropagateResponseException is no option, as we're inside our PSR-7 implementation in here and PSR-7 requires thrown exceptions to be \InvalidArgumentException. Taking my CR-1 away, as I don't really seem to understand the reasoning of this change. But it looks like we're doing implicit client input validation/refusal here, which should be out-of-scope for the PSR-7 implementation.
  14. Benjamin Franzke commented on patch set 3 09/08/22, 16:51

    Reply Unresolved

    replied too early: Question was: Is the exception message excluded from the response on (security) purpose?
  15. Benjamin Franzke commented on patch set 3 CR -1 09/08/22, 16:55

    Reply

    Sorry for the back and forth. Readding CR-1. Mixing 400er and 500er codes makes no sense, as it implies from which context (user input or server-side generate subrequest) the methods are used. That's out of scope for PSR-7 and should be handled by the usages of the respective methods (catching \InvalidArgumentException).
  16. Oliver Hader commented on patch set 3 V -1 12/06/22, 14:21

    Reply Unresolved

    Ack & thanks for the explanation. Invocation context is missing here...
  17. Oliver Hader marked it work in progress 12/06/22, 14:22

  18. Oliver Hader commented on patch set 3 06/26/23, 09:01

    Just stumbled over this again... since the HTTP status code in the response depends on the context, it only would be possible to implement an "interpretation" of the exception codes and map it to corresponding HTTP status codes in `AbstractApplication` (`CommandApplication` for CLI of course should not used HTTP responses).
  19. Oliver Hader uploaded patch set 4 CR +1 V +1 06/26/23, 09:39

  20. Oliver Hader commented on patch set 4 06/26/23, 09:41

    Without the patch: ``` curl -Ik -XWRONG https://ip12.anyhost.it/ HTTP/1.1 500 Internal Server Error Date: Mon, 26 Jun 2023 09:40:51 GMT Server: Apache/2.4.57 (Unix) OpenSSL/3.1.1 mod_fcgid/2.3.9 X-Powered-By: PHP/8.1.18 Connection: close Transfer-Encoding: chunked Content-Type: text/html; charset=UTF-8 ``` With the patch: ``` curl -Ik https://ip12.anyhost.it/ -XWRONG HTTP/1.1 400 Bad Request Date: Mon, 26 Jun 2023 09:38:49 GMT Server: Apache/2.4.57 (Unix) OpenSSL/3.1.1 mod_fcgid/2.3.9 X-Powered-By: PHP/8.1.18 Content-Length: 0 Connection: close Content-Type: text/html; charset=UTF-8 ```
  21. core-ci voted on patch set 4 V +1 06/26/23, 09:47 Core CI is happy: https://git.typo3.org/typo3/CI/cms/-/pipelines/42982

  22. Markus Klein voted on patch set 4 CR +1 06/26/23, 11:41

  23. Markus Klein commented on patch set 4 10/04/24, 09:45

    According to https://forge.typo3.org/issues/98264#note-7 this should be fixed. So we can abandon this one?
  24. Oliver Hader commented on patch set 4 10/04/24, 10:55

    Reply

    It still responds with an HTTP 500 exception, but is not logged anymore. Let's close this one here...
  25. Oliver Hader abandoned the change 10/04/24, 10:55