Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- The ExApp proxy also refuses `.` path segments and `..` segments followed by `;`, `%3F` or `%23`. #1163
- The ExApp proxy refuses requests to the `/heartbeat`, `/init` and `/enabled` endpoints of ExApps, as HaRP does. #1168
- The ExApp proxy also applies a stricter route that matches the path as an ExApp may read it, as HaRP 0.4.8 does. #1182
- The ExApp proxy returns an ExApp's redirect to the browser instead of following it, as HaRP does. #1195

## [34.0.0]

Expand Down
5 changes: 5 additions & 0 deletions lib/Controller/ExAppProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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,
);
Expand Down Expand Up @@ -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,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
];
if (str_starts_with($this->request->getHeader('content-type'), 'multipart/form-data') || count($_FILES) > 0) {
unset($options['headers']['Content-Type']);
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
39 changes: 37 additions & 2 deletions tests/php/Controller/ExAppProxyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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),
Expand Down Expand Up @@ -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'));
}
}
Loading