diff --git a/lib/Controller/ExAppProxyController.php b/lib/Controller/ExAppProxyController.php index 5798dc3c..428ac798 100644 --- a/lib/Controller/ExAppProxyController.php +++ b/lib/Controller/ExAppProxyController.php @@ -87,6 +87,7 @@ private function createProxyResponse(string $path, IResponse $response, bool $is $proxyResponse = new ProxyResponse($response->getStatusCode(), $responseHeaders, $content); if ($cache && !$isHTML && empty($response->getHeader('cache-control')) + && !in_array($response->getStatusCode(), [301, 302, 303, 307, 308], true) && $response->getHeader('Content-Type') !== 'application/json' && $response->getHeader('Content-Type') !== 'application/x-tar') { $proxyResponse->cacheFor(3600); @@ -113,6 +114,7 @@ public function ExAppGet(string $appId, string $other): Response { RequestOptions::COOKIES => $this->buildProxyCookiesJar($_COOKIE, $this->service->getExAppDomain($exApp)), RequestOptions::HEADERS => $this->buildHeadersWithExclude($route, getallheaders()), RequestOptions::TIMEOUT => 0, + 'allow_redirects' => false, // the browser follows redirects, so each target passes the route check ], request: $this->request, ); @@ -142,6 +144,7 @@ public function ExAppPost(string $appId, string $other): Response { RequestOptions::COOKIES => $this->buildProxyCookiesJar($_COOKIE, $this->service->getExAppDomain($exApp)), RequestOptions::HEADERS => $this->buildHeadersWithExclude($route, getallheaders()), RequestOptions::TIMEOUT => 0, + 'allow_redirects' => false, ]; if (str_starts_with($this->request->getHeader('content-type'), 'multipart/form-data') || count($_FILES) > 0) { unset($options['headers']['Content-Type']); @@ -187,6 +190,7 @@ public function ExAppPut(string $appId, string $other): Response { RequestOptions::BODY => $stream, RequestOptions::HEADERS => $this->buildHeadersWithExclude($route, getallheaders()), RequestOptions::TIMEOUT => 0, + 'allow_redirects' => false, ]; $response = $this->service->requestToExApp2( $exApp, '/' . $other, $this->userId, 'PUT', @@ -221,6 +225,7 @@ public function ExAppDelete(string $appId, string $other): Response { RequestOptions::BODY => $stream, RequestOptions::HEADERS => $this->buildHeadersWithExclude($route, getallheaders()), RequestOptions::TIMEOUT => 0, + 'allow_redirects' => false, ]; $response = $this->service->requestToExApp2( $exApp, '/' . $other, $this->userId, 'DELETE', diff --git a/tests/php/Controller/ExAppProxyControllerTest.php b/tests/php/Controller/ExAppProxyControllerTest.php index db14eebe..91b98cf2 100644 --- a/tests/php/Controller/ExAppProxyControllerTest.php +++ b/tests/php/Controller/ExAppProxyControllerTest.php @@ -15,6 +15,7 @@ use OCA\AppAPI\Service\AppAPIService; use OCA\AppAPI\Service\ExAppService; use OCP\Files\IMimeTypeDetector; +use OCP\Http\Client\IResponse; use OCP\IGroupManager; use OCP\IRequest; use OCP\Security\Bruteforce\IThrottler; @@ -30,7 +31,7 @@ class ExAppProxyControllerTest extends TestCase { ['url' => '^/settings$', 'verb' => 'POST', 'access_level' => 2], ]; - private function controller(string $method, ?string $userId = null, bool $isAdmin = false, ?ExApp $exApp = null): ExAppProxyController { + private function controller(string $method, ?string $userId = null, bool $isAdmin = false, ?ExApp $exApp = null, ?AppAPIService $service = null): ExAppProxyController { $request = $this->createMock(IRequest::class); $request->method('getMethod')->willReturn($method); $exAppService = $this->createMock(ExAppService::class); @@ -39,7 +40,7 @@ private function controller(string $method, ?string $userId = null, bool $isAdmi $groupManager->method('isAdmin')->willReturn($isAdmin); return new ExAppProxyController( $request, - $this->createMock(AppAPIService::class), + $service ?? $this->createMock(AppAPIService::class), $exAppService, $this->createMock(IMimeTypeDetector::class), $this->createMock(ContentSecurityPolicyNonceManager::class), @@ -218,4 +219,38 @@ public static function forwardedPathProvider(): array { public function testPathIsForwarded(string $path): void { self::assertTrue($this->isForwarded($path)); } + + public static function proxyActionProvider(): array { + return [ + 'GET' => ['ExAppGet', 'GET'], + 'POST' => ['ExAppPost', 'POST'], + 'PUT' => ['ExAppPut', 'PUT'], + 'DELETE' => ['ExAppDelete', 'DELETE'], + 'GET of a cached file, not modified' => ['ExAppGet', 'GET', 304], + ]; + } + + #[DataProvider('proxyActionProvider')] + public function testRedirectIsReturnedNotFollowed(string $action, string $method, int $status = 302): void { + // The browser follows an ExApp redirect itself, so the target passes the route check. + $exAppResponse = $this->createMock(IResponse::class); + $exAppResponse->method('getStatusCode')->willReturn($status); + $exAppResponse->method('getHeaders')->willReturn(['Location' => ['/admin']]); + $sentOptions = []; + $service = $this->createMock(AppAPIService::class); + $service->method('requestToExApp2')->willReturnCallback( + function (ExApp $exApp, string $route, ?string $userId, string $method, array $queryParams, array $bodyParams, array $options) use (&$sentOptions, $exAppResponse) { + $sentOptions = $options; + return $exAppResponse; + } + ); + $exApp = new ExApp(['appid' => 'test-proxy', 'enabled' => 1, 'routes' => [['url' => '.*', 'verb' => $method, 'access_level' => 0]]]); + $response = $this->controller($method, exApp: $exApp, service: $service)->$action('test-proxy', 'login'); + + self::assertFalse($sentOptions['allow_redirects']); + self::assertSame($status, $response->getStatus()); + self::assertSame('/admin', $response->getHeaders()['Location']); + // Only a 304 keeps the cache time, as it refreshes a cached file. + self::assertSame($status === 304, str_contains($response->getHeaders()['Cache-Control'], 'max-age')); + } }