From 9473f47c0df37494aa3e214c86456a733eac14da Mon Sep 17 00:00:00 2001 From: provokateurin Date: Wed, 20 Aug 2025 13:35:17 +0200 Subject: [PATCH] fix(Dispatcher): Catch TypeErrors and turn them into bad request responses Signed-off-by: provokateurin --- lib/private/AppFramework/Http/Dispatcher.php | 13 ++++++++++++- .../Utility/ControllerMethodReflector.php | 13 +++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/private/AppFramework/Http/Dispatcher.php b/lib/private/AppFramework/Http/Dispatcher.php index 2c5f12764a687..ee20dee6a4541 100644 --- a/lib/private/AppFramework/Http/Dispatcher.php +++ b/lib/private/AppFramework/Http/Dispatcher.php @@ -201,7 +201,18 @@ private function executeController(Controller $controller, string $methodName): } $this->eventLogger->start('controller:' . get_class($controller) . '::' . $methodName, 'App framework controller execution'); - $response = \call_user_func_array([$controller, $methodName], $arguments); + try { + $response = \call_user_func_array([$controller, $methodName], $arguments); + } catch (\TypeError $e) { + // Only intercept TypeErrors occuring on the first line, meaning that the invocation of the controller method failed. + // Any other TypeError happens inside the controller method logic and should be logged as normal. + if ($e->getFile() === $this->reflector->getFile() && $e->getLine() === $this->reflector->getStartLine()) { + $this->logger->debug('Failed to call controller method: ' . $e->getMessage(), ['exception' => $e]); + return new Response(Http::STATUS_BAD_REQUEST); + } + + throw $e; + } $this->eventLogger->end('controller:' . get_class($controller) . '::' . $methodName); if (!($response instanceof Response)) { diff --git a/lib/private/AppFramework/Utility/ControllerMethodReflector.php b/lib/private/AppFramework/Utility/ControllerMethodReflector.php index 679e1788004c5..66ef1ae22f1d4 100644 --- a/lib/private/AppFramework/Utility/ControllerMethodReflector.php +++ b/lib/private/AppFramework/Utility/ControllerMethodReflector.php @@ -18,6 +18,8 @@ class ControllerMethodReflector implements IControllerMethodReflector { private $types = []; private $parameters = []; private array $ranges = []; + private int $startLine = 0; + private string $file = ''; /** * @param object $object an object or classname @@ -25,6 +27,9 @@ class ControllerMethodReflector implements IControllerMethodReflector { */ public function reflect($object, string $method) { $reflection = new \ReflectionMethod($object, $method); + $this->startLine = $reflection->getStartLine(); + $this->file = $reflection->getFileName(); + $docs = $reflection->getDocComment(); if ($docs !== false) { @@ -134,4 +139,12 @@ public function getAnnotationParameter(string $name, string $key): string { return ''; } + + public function getStartLine(): int { + return $this->startLine; + } + + public function getFile(): string { + return $this->file; + } }