Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable2.2] fix: Check strict cookies for image proxy #8578

Merged
merged 1 commit into from
Jun 27, 2023
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
7 changes: 7 additions & 0 deletions lib/Controller/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use OCP\IURLGenerator;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Log\LoggerInterface;
use function file_get_contents;

class ProxyController extends Controller {
private IURLGenerator $urlGenerator;
Expand Down Expand Up @@ -105,6 +106,12 @@ public function proxy(string $src): ProxyDownloadResponse {
// close the session to allow parallel downloads
$this->session->close();

// If strict cookies are set it means we come from the same domain so no open redirect
if (!$this->request->passesStrictCookieCheck()) {
$content = file_get_contents(__DIR__ . '/../../img/blocked-image.png');
return new ProxyDownloadResponse($content, $src, 'application/octet-stream');
}

$client = $this->clientService->newClient();
try {
$response = $client->get($src);
Expand Down
36 changes: 34 additions & 2 deletions tests/Unit/Controller/ProxyControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* @author Christoph Wurst <[email protected]>
*
Expand Down Expand Up @@ -156,11 +158,41 @@ public function testRedirectInvalidUrl() {
$this->controller->redirect('ftps://example.com');
}

public function testProxy() {
public function testProxyWithoutCookies(): void {
$src = 'http://example.com';
$httpResponse = $this->createMock(IResponse::class);
$content = '🐵🐵🐵';
$this->session->expects($this->once())
->method('close');
$client = $this->getMockBuilder(IClient::class)->getMock();
$this->clientService->expects(self::never())
->method('newClient')
->willReturn($client);
$unexpected = new ProxyDownloadResponse(
$content,
$src,
'application/octet-stream'
);
$this->controller = new ProxyController(
$this->appName,
$this->request,
$this->urlGenerator,
$this->session,
$this->clientService,
$this->logger
);

$response = $this->controller->proxy($src);

$this->assertNotEquals($unexpected, $response);
}

public function testProxy(): void {
$src = 'http://example.com';
$httpResponse = $this->createMock(IResponse::class);
$content = '🐵🐵🐵';
$this->request->expects(self::once())
->method('passesStrictCookieCheck')
->willReturn(true);
$this->session->expects($this->once())
->method('close');
$client = $this->getMockBuilder(IClient::class)->getMock();
Expand Down