From a48b84fb1088702af623fb5ac23cad277783e0a2 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Tue, 28 Nov 2023 05:57:30 +0100 Subject: [PATCH] fix(API): Add ParameterOutOfRange middleware Signed-off-by: Joas Schilling --- lib/AppInfo/Application.php | 2 + .../ParameterOutOfRangeMiddleware.php | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 lib/Middleware/ParameterOutOfRangeMiddleware.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 637242c998d..7287ad0f8fc 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -103,6 +103,7 @@ use OCA\Talk\Maps\MapsPluginLoader; use OCA\Talk\Middleware\CanUseTalkMiddleware; use OCA\Talk\Middleware\InjectionMiddleware; +use OCA\Talk\Middleware\ParameterOutOfRangeMiddleware; use OCA\Talk\Notification\Listener as NotificationListener; use OCA\Talk\Notification\Notifier; use OCA\Talk\OCP\TalkBackend; @@ -165,6 +166,7 @@ public function __construct(array $urlParams = []) { public function register(IRegistrationContext $context): void { $context->registerMiddleWare(CanUseTalkMiddleware::class); $context->registerMiddleWare(InjectionMiddleware::class); + $context->registerMiddleWare(ParameterOutOfRangeMiddleware::class); $context->registerCapability(Capabilities::class); // Listeners to load the UI and integrate it into other apps diff --git a/lib/Middleware/ParameterOutOfRangeMiddleware.php b/lib/Middleware/ParameterOutOfRangeMiddleware.php new file mode 100644 index 00000000000..a5cc3bcbe56 --- /dev/null +++ b/lib/Middleware/ParameterOutOfRangeMiddleware.php @@ -0,0 +1,50 @@ + + * + * @author Joas Schilling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Talk\Middleware; + +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\AppFramework\Http\ParameterOutOfRangeException; +use OCP\AppFramework\Http\Response; +use OCP\AppFramework\Middleware; +use OCP\AppFramework\OCSController; + +class ParameterOutOfRangeMiddleware extends Middleware { + /** + * @throws \Exception + */ + public function afterException(Controller $controller, string $methodName, \Exception $exception): Response { + if ($exception instanceof ParameterOutOfRangeException + && $controller instanceof OCSController) { + return new DataResponse([ + 'error' => $exception->getParameterName(), + ], Http::STATUS_BAD_REQUEST); + } + + throw $exception; + } +}