From e2318f2087c739bf5f14de06f86f490754d0bdf0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 27 Nov 2024 09:26:26 +0100 Subject: [PATCH] fix(controller): Fix false booleans in multipart/form-data Signed-off-by: Joas Schilling --- lib/private/AppFramework/Http/Dispatcher.php | 12 +++++-- .../lib/AppFramework/Http/DispatcherTest.php | 35 +++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/private/AppFramework/Http/Dispatcher.php b/lib/private/AppFramework/Http/Dispatcher.php index e2750e30fa91a..e6dcc7bb0559e 100644 --- a/lib/private/AppFramework/Http/Dispatcher.php +++ b/lib/private/AppFramework/Http/Dispatcher.php @@ -188,9 +188,15 @@ private function executeController(Controller $controller, string $methodName): if (($type === 'bool' || $type === 'boolean') && $value === 'false' && ( - $this->request->method === 'GET' || - str_contains($this->request->getHeader('Content-Type'), - 'application/x-www-form-urlencoded') + $this->request->method === 'GET' + || str_contains( + $this->request->getHeader('Content-Type'), + 'application/x-www-form-urlencoded', + ) + || str_contains( + $this->request->getHeader('Content-Type'), + 'multipart/form-data', + ) ) ) { $value = false; diff --git a/tests/lib/AppFramework/Http/DispatcherTest.php b/tests/lib/AppFramework/Http/DispatcherTest.php index 94bcfcc4af2f8..9620cbecf336f 100644 --- a/tests/lib/AppFramework/Http/DispatcherTest.php +++ b/tests/lib/AppFramework/Http/DispatcherTest.php @@ -471,6 +471,41 @@ public function testResponseTransformedByAcceptHeader(): void { $this->assertEquals('{"text":[3,false,4,1]}', $response[3]); } + public function testResponseTransformedBySendingMultipartFormData(): void { + $this->request = new Request( + [ + 'post' => [ + 'int' => '3', + 'bool' => 'false', + 'double' => 1.2, + ], + 'server' => [ + 'HTTP_ACCEPT' => 'application/text, test', + 'HTTP_CONTENT_TYPE' => 'multipart/form-data' + ], + 'method' => 'POST' + ], + $this->createMock(IRequestId::class), + $this->createMock(IConfig::class) + ); + $this->dispatcher = new Dispatcher( + $this->http, $this->middlewareDispatcher, $this->reflector, + $this->request, + $this->config, + \OC::$server->getDatabaseConnection(), + $this->logger, + $this->eventLogger, + $this->container + ); + $controller = new TestController('app', $this->request); + + // reflector is supposed to be called once + $this->dispatcherPassthrough(); + $response = $this->dispatcher->dispatch($controller, 'exec'); + + $this->assertEquals('{"text":[3,false,4,1]}', $response[3]); + } + public function testResponsePrimarilyTransformedByParameterFormat(): void { $this->request = new Request(