Skip to content

Commit

Permalink
Merge branch 'stable2.2' into backport/8383/stable2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
GretaD authored Jun 28, 2023
2 parents 43a876a + 797b584 commit fb4abf1
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 125 deletions.
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
8 changes: 6 additions & 2 deletions lib/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ class Folder {
/** @var string[] */
private $specialUse;

public function __construct(int $accountId, Horde_Imap_Client_Mailbox $mailbox, array $attributes, ?string $delimiter) {
public function __construct(int $accountId,
Horde_Imap_Client_Mailbox $mailbox,
array $attributes,
?string $delimiter,
array $status) {
$this->accountId = $accountId;
$this->mailbox = $mailbox;
$this->attributes = $attributes;
$this->delimiter = $delimiter;
$this->status = [];
$this->status = $status;
$this->specialUse = [];
}

Expand Down
52 changes: 12 additions & 40 deletions lib/IMAP/FolderMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,21 @@ public function getFolders(Account $account, Horde_Imap_Client_Socket $client,
'delimiter' => true,
'attributes' => true,
'special_use' => true,
'status' => Horde_Imap_Client::STATUS_ALL,
]);

return array_filter(array_map(function (array $mailbox) use ($account, $client) {
return array_filter(array_map(static function (array $mailbox) use ($account) {
if (in_array($mailbox['mailbox']->utf8, self::DOVECOT_SIEVE_FOLDERS, true)) {
// This is a special folder that must not be shown
return null;
}

try {
$client->status($mailbox["mailbox"]);
} catch (Horde_Imap_Client_Exception $e) {
// ignore folders which cause errors on access
// (i.e. server-side system I/O errors)
if (in_array($e->getCode(), [
Horde_Imap_Client_Exception::UNSPECIFIED,
], true)) {
return null;
}
}

return new Folder(
$account->getId(),
$mailbox['mailbox'],
$mailbox['attributes'],
$mailbox['delimiter']
$mailbox['delimiter'],
$mailbox['status'],
);
}, $mailboxes));
}
Expand All @@ -97,39 +87,21 @@ public function createFolder(Horde_Imap_Client_Socket $client,
'delimiter' => true,
'attributes' => true,
'special_use' => true,
'status' => Horde_Imap_Client::STATUS_ALL,
]);
$mb = reset($list);

if ($mb === null) {
throw new ServiceException("Created mailbox does not exist");
}

return new Folder($account->getId(), $mb['mailbox'], $mb['attributes'], $mb['delimiter']);
}

/**
* @param Folder[] $folders
* @param Horde_Imap_Client_Socket $client
*
* @throws Horde_Imap_Client_Exception
*
* @return void
*/
public function getFoldersStatus(array $folders,
Horde_Imap_Client_Socket $client): void {
$mailboxes = array_map(function (Folder $folder) {
return $folder->getMailbox();
}, array_filter($folders, function (Folder $folder) {
return !in_array('\noselect', $folder->getAttributes());
}));

$status = $client->status($mailboxes);

foreach ($folders as $folder) {
if (isset($status[$folder->getMailbox()])) {
$folder->setStatus($status[$folder->getMailbox()]);
}
}
return new Folder(
$account->getId(),
$mb['mailbox'],
$mb['attributes'],
$mb['delimiter'],
$mb['status'],
);
}

/**
Expand Down
1 change: 0 additions & 1 deletion lib/IMAP/MailboxSync.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ public function sync(Account $account,

try {
$folders = $this->folderMapper->getFolders($account, $client);
$this->folderMapper->getFoldersStatus($folders, $client);
} catch (Horde_Imap_Client_Exception $e) {
throw new ServiceException(
sprintf("IMAP error synchronizing account %d: %s", $account->getId(), $e->getMessage()),
Expand Down
1 change: 0 additions & 1 deletion lib/Service/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ public function createMailbox(Account $account, string $name): Mailbox {
$client = $this->imapClientFactory->getClient($account);
try {
$folder = $this->folderMapper->createFolder($client, $account, $name);
$this->folderMapper->getFoldersStatus([$folder], $client);
} catch (Horde_Imap_Client_Exception $e) {
throw new ServiceException(
"Could not get mailbox status: " .
Expand Down
2 changes: 1 addition & 1 deletion tests/FolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private function mockFolder(array $attributes = [], $delimiter = '.') {
$this->accountId = 15;
$this->mailbox = $this->createMock(Horde_Imap_Client_Mailbox::class);

$this->folder = new Folder($this->accountId, $this->mailbox, $attributes, $delimiter);
$this->folder = new Folder($this->accountId, $this->mailbox, $attributes, $delimiter, []);
}

public function testGetMailbox() {
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
93 changes: 18 additions & 75 deletions tests/Unit/IMAP/FolderMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function setUp(): void {
$this->mapper = new FolderMapper();
}

public function testGetFoldersEmtpyAccount() {
public function testGetFoldersEmtpyAccount(): void {
$account = $this->createMock(Account::class);
$client = $this->createMock(Horde_Imap_Client_Socket::class);
$client->expects($this->once())
Expand All @@ -52,6 +52,7 @@ public function testGetFoldersEmtpyAccount() {
'delimiter' => true,
'attributes' => true,
'special_use' => true,
'status' => Horde_Imap_Client::STATUS_ALL,
]))
->willReturn([]);

Expand All @@ -60,7 +61,7 @@ public function testGetFoldersEmtpyAccount() {
$this->assertEquals([], $folders);
}

public function testGetFolders() {
public function testGetFolders(): void {
$account = $this->createMock(Account::class);
$account->method('getId')->willReturn(27);
$client = $this->createMock(Horde_Imap_Client_Socket::class);
Expand All @@ -71,32 +72,39 @@ public function testGetFolders() {
'delimiter' => true,
'attributes' => true,
'special_use' => true,
'status' => Horde_Imap_Client::STATUS_ALL,
]))
->willReturn([
[
'mailbox' => new Horde_Imap_Client_Mailbox('INBOX'),
'attributes' => [],
'delimiter' => '.',
'status' => [
'unseen' => 0,
],
],
[
'mailbox' => new Horde_Imap_Client_Mailbox('Sent'),
'attributes' => [
'\sent',
],
'delimiter' => '.',
'status' => [
'unseen' => 1,
],
],
]);
$expected = [
new Folder(27, new Horde_Imap_Client_Mailbox('INBOX'), [], '.'),
new Folder(27, new Horde_Imap_Client_Mailbox('Sent'), ['\sent'], '.'),
new Folder(27, new Horde_Imap_Client_Mailbox('INBOX'), [], '.', ['unseen' => 0]),
new Folder(27, new Horde_Imap_Client_Mailbox('Sent'), ['\sent'], '.', ['unseen' => 1]),
];

$folders = $this->mapper->getFolders($account, $client);

$this->assertEquals($expected, $folders);
}

public function testCreateFolder() {
public function testCreateFolder(): void {
$account = $this->createMock(Account::class);
$account->method('getId')->willReturn(42);
$client = $this->createMock(Horde_Imap_Client_Socket::class);
Expand All @@ -110,90 +118,25 @@ public function testCreateFolder() {
'delimiter' => true,
'attributes' => true,
'special_use' => true,
'status' => Horde_Imap_Client::STATUS_ALL,
]))
->willReturn([
[
'mailbox' => new Horde_Imap_Client_Mailbox('new'),
'attributes' => [],
'delimiter' => '.',
'status' => [
'unseen' => 0,
],
],
]);

$created = $this->mapper->createFolder($client, $account, 'new');

$expected = new Folder(42, new Horde_Imap_Client_Mailbox('new'), [], '.');
$expected = new Folder(42, new Horde_Imap_Client_Mailbox('new'), [], '.', ['unseen' => 0]);
$this->assertEquals($expected, $created);
}

public function testGetFoldersStatus() {
$folders = [
$this->createMock(Folder::class),
];
$client = $this->createMock(Horde_Imap_Client_Socket::class);
$folders[0]->expects($this->any())
->method('getMailbox')
->willReturn('folder1');
$folders[0]->expects($this->once())
->method('getAttributes')
->willReturn([]);
$client->expects($this->once())
->method('status')
->with($this->equalTo(['folder1']))
->willReturn([
'folder1' => [
'total' => 123
],
]);
$folders[0]->expects($this->once())
->method('setStatus');

$this->mapper->getFoldersStatus($folders, $client);
}

public function testGetFoldersStatusNoStatusReported() {
$folders = [
$this->createMock(Folder::class),
];
$client = $this->createMock(Horde_Imap_Client_Socket::class);
$folders[0]->expects($this->any())
->method('getMailbox')
->willReturn('folder1');
$folders[0]->expects($this->once())
->method('getAttributes')
->willReturn([]);
$client->expects($this->once())
->method('status')
->with($this->equalTo(['folder1']))
->willReturn([
// Nothing reported for this folder
]);
$folders[0]->expects($this->never())
->method('setStatus');

$this->mapper->getFoldersStatus($folders, $client);
}

public function testGetFoldersStatusNotSearchable() {
$folders = [
$this->createMock(Folder::class),
];
$client = $this->createMock(Horde_Imap_Client_Socket::class);
$folders[0]->expects($this->any())
->method('getMailbox')
->willReturn('folder1');
$folders[0]->expects($this->once())
->method('getAttributes')
->willReturn(['\\noselect']);
$client->expects($this->once())
->method('status')
->with($this->equalTo([]))
->willReturn([]);
$folders[0]->expects($this->never())
->method('setStatus');

$this->mapper->getFoldersStatus($folders, $client);
}

public function testGetFoldersStatusAsObject() {
$client = $this->createMock(Horde_Imap_Client_Socket::class);
$client->expects($this->once())
Expand Down
3 changes: 0 additions & 3 deletions tests/Unit/Service/MailManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,6 @@ public function testCreateFolder() {
->method('createFolder')
->with($this->equalTo($client), $this->equalTo($account), $this->equalTo('new'))
->willReturn($folder);
$this->folderMapper->expects($this->once())
->method('getFoldersStatus')
->with($this->equalTo([$folder]));
$this->folderMapper->expects($this->once())
->method('detectFolderSpecialUse')
->with($this->equalTo([$folder]));
Expand Down

0 comments on commit fb4abf1

Please sign in to comment.