Skip to content
Open
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
8 changes: 6 additions & 2 deletions lib/Controller/ProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

namespace OCA\Mail\Controller;

use OCA\Mail\Exception\ClientException;
use OCA\Mail\Html\ProxyHmacGenerator;
use OCA\Mail\Http\ProxyDownloadResponse;
use OCA\Mail\Service\DelegationService;
use OCA\Mail\Service\MailManager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Db\DoesNotExistException;
Expand Down Expand Up @@ -44,6 +46,7 @@ public function __construct(
private ProxyHmacGenerator $hmacGenerator,
private LoggerInterface $logger,
private MailManager $mailManager,
private DelegationService $delegationService,
private ?string $userId,
) {
parent::__construct($appName, $request);
Expand Down Expand Up @@ -82,8 +85,9 @@ public function proxy(string $src, ?int $id, ?string $hmac): Response {
return new Response(Http::STATUS_BAD_REQUEST);
}
try {
$this->mailManager->getMessage($this->userId, $id);
} catch (DoesNotExistException $e) {
$effectiveUserId = $this->delegationService->resolveMessageUserId($id, $this->userId);
$this->mailManager->getMessage($effectiveUserId, $id);
} catch (DoesNotExistException|ClientException $e) {
return new Response(Http::STATUS_BAD_REQUEST);
}
if (!hash_equals($this->hmacGenerator->generate($id, $src), $hmac)) {
Expand Down
68 changes: 68 additions & 0 deletions tests/Unit/Controller/ProxyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCA\Mail\Controller\ProxyController;
use OCA\Mail\Html\ProxyHmacGenerator;
use OCA\Mail\Http\ProxyDownloadResponse;
use OCA\Mail\Service\DelegationService;
use OCA\Mail\Service\MailManager;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
Expand Down Expand Up @@ -53,6 +54,8 @@ class ProxyControllerTest extends TestCase {
/** @var MailManager|MockObject */
private $mailManager;

private DelegationService&MockObject $delegationService;

private string $userId = 'user';

/** @var ProxyController */
Expand All @@ -68,6 +71,7 @@ protected function setUp(): void {
$this->clientService = $this->createMock(IClientService::class);
$this->hmacGenerator = $this->createMock(ProxyHmacGenerator::class);
$this->mailManager = $this->createMock(MailManager::class);
$this->delegationService = $this->createMock(DelegationService::class);
$this->logger = new NullLogger();
}

Expand All @@ -89,6 +93,7 @@ public function testProxyWithoutCookies(): void {
$this->hmacGenerator,
$this->logger,
$this->mailManager,
$this->delegationService,
$this->userId,
);

Expand All @@ -112,6 +117,10 @@ public function testProxy(): void {
->method('generate')
->with($id, $src)
->willReturn($validHmac);
$this->delegationService->expects($this->once())
->method('resolveMessageUserId')
->with($id, $this->userId)
->willReturn($this->userId);
$this->mailManager->expects($this->once())
->method('getMessage')
->with($this->userId, $id);
Expand All @@ -135,6 +144,54 @@ public function testProxy(): void {
$this->hmacGenerator,
$this->logger,
$this->mailManager,
$this->delegationService,
$this->userId,
);

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

$this->assertInstanceOf(ProxyDownloadResponse::class, $response);
}

public function testProxyForDelegatedMessage(): void {
$src = 'http://example.com';
$id = 1;
$validHmac = 'valid-hmac-hash';
$content = 'mock image data';
$ownerUserId = 'owner';
$httpResponse = $this->createMock(IResponse::class);
$this->request->method('passesStrictCookieCheck')->willReturn(true);
$this->hmacGenerator->method('generate')
->with($id, $src)
->willReturn($validHmac);
$this->delegationService->expects($this->once())
->method('resolveMessageUserId')
->with($id, $this->userId)
->willReturn($ownerUserId);
$this->mailManager->expects($this->once())
->method('getMessage')
->with($ownerUserId, $id);
$client = $this->getMockBuilder(IClient::class)->getMock();
$this->clientService->expects($this->once())
->method('newClient')
->willReturn($client);
$client->expects($this->once())
->method('get')
->with($src)
->willReturn($httpResponse);
$httpResponse->expects($this->once())
->method('getBody')
->willReturn($content);
$this->controller = new ProxyController(
$this->appName,
$this->request,
$this->urlGenerator,
$this->session,
$this->clientService,
$this->hmacGenerator,
$this->logger,
$this->mailManager,
$this->delegationService,
$this->userId,
);

Expand All @@ -157,6 +214,10 @@ public function testProxyWithInvalidHmac(): void {
->method('generate')
->with($id, $src)
->willReturn($expectedHmac);
$this->delegationService->expects($this->once())
->method('resolveMessageUserId')
->with($id, $this->userId)
->willReturn($this->userId);
$this->mailManager->expects($this->once())
->method('getMessage')
->with($this->userId, $id);
Expand All @@ -171,6 +232,7 @@ public function testProxyWithInvalidHmac(): void {
$this->hmacGenerator,
$this->logger,
$this->mailManager,
$this->delegationService,
$this->userId,
);

Expand Down Expand Up @@ -198,6 +260,7 @@ public function testProxyWithMissingHmacParameters(): void {
$this->hmacGenerator,
$this->logger,
$this->mailManager,
$this->delegationService,
$this->userId,
);

Expand All @@ -215,6 +278,10 @@ public function testProxyWithMessageNotOwnedByUser(): void {
->willReturn(true);
$this->session->expects($this->once())
->method('close');
$this->delegationService->expects($this->once())
->method('resolveMessageUserId')
->with($id, $this->userId)
->willReturn($this->userId);
$this->mailManager->expects($this->once())
->method('getMessage')
->with($this->userId, $id)
Expand All @@ -230,6 +297,7 @@ public function testProxyWithMessageNotOwnedByUser(): void {
$this->hmacGenerator,
$this->logger,
$this->mailManager,
$this->delegationService,
$this->userId,
);

Expand Down