Skip to content

Commit

Permalink
Prevent fetching singular rfc partials from running indefinitely #407
Browse files Browse the repository at this point in the history
  • Loading branch information
Webklex committed Jun 23, 2023
1 parent 50b4ea1 commit 9f02c84
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- Legacy protocol support fixed (object to array conversion) #411
- Header value decoding improved #410
- Protocol exception handling improved (bad response message added) #408
- Prevent fetching singular rfc partials from running indefinitely #407

### Added
- NaN
Expand Down
15 changes: 9 additions & 6 deletions src/Connection/Protocols/ImapProtocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,19 +294,22 @@ public function readResponse(Response $response, string $tag, bool $dontParse =
$lines[] = $tokens;
} while (!$readAll);

$original = $tokens;
if ($dontParse) {
// First two chars are still needed for the response code
$tokens = [substr($tokens, 0, 2)];
}

$original = is_array($original)?$original : [$original];

// last line has response code
if ($tokens[0] == 'OK') {
return $lines ?: [true];
} elseif ($tokens[0] == 'NO' || $tokens[0] == 'BAD' || $tokens[0] == 'BYE') {
throw new ImapServerErrorException(implode("\n", $tokens));
throw new ImapServerErrorException(implode("\n", $original));
}

throw new ImapBadRequestException(implode("\n", $tokens));
throw new ImapBadRequestException(implode("\n", $original));
}

/**
Expand Down Expand Up @@ -730,7 +733,7 @@ public function fetch(array|string $items, array|int $from, mixed $to = null, in
* @throws RuntimeException
*/
public function content(int|array $uids, string $rfc = "RFC822", int|string $uid = IMAP::ST_UID): Response {
return $this->fetch(["$rfc.TEXT"], $uids, null, $uid);
return $this->fetch(["$rfc.TEXT"], is_array($uids)?$uids:[$uids], null, $uid);
}

/**
Expand All @@ -744,7 +747,7 @@ public function content(int|array $uids, string $rfc = "RFC822", int|string $uid
* @throws RuntimeException
*/
public function headers(int|array $uids, string $rfc = "RFC822", int|string $uid = IMAP::ST_UID): Response {
return $this->fetch(["$rfc.HEADER"], $uids, null, $uid);
return $this->fetch(["$rfc.HEADER"], is_array($uids)?$uids:[$uids], null, $uid);
}

/**
Expand All @@ -757,7 +760,7 @@ public function headers(int|array $uids, string $rfc = "RFC822", int|string $uid
* @throws RuntimeException
*/
public function flags(int|array $uids, int|string $uid = IMAP::ST_UID): Response {
return $this->fetch(["FLAGS"], $uids, null, $uid);
return $this->fetch(["FLAGS"], is_array($uids)?$uids:[$uids], null, $uid);
}

/**
Expand All @@ -770,7 +773,7 @@ public function flags(int|array $uids, int|string $uid = IMAP::ST_UID): Response
* @throws RuntimeException
*/
public function sizes(int|array $uids, int|string $uid = IMAP::ST_UID): Response {
return $this->fetch(["RFC822.SIZE"], $uids, null, $uid);
return $this->fetch(["RFC822.SIZE"], is_array($uids)?$uids:[$uids], null, $uid);
}

/**
Expand Down
57 changes: 57 additions & 0 deletions tests/issues/Issue407Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/*
* File: Issue407Test.php
* Category: Test
* Author: M.Goldenbaum
* Created: 23.06.23 21:40
* Updated: -
*
* Description:
* -
*/

namespace Tests\issues;

use PHPUnit\Framework\TestCase;
use Tests\live\LiveMailboxTestCase;
use Webklex\PHPIMAP\Folder;
use Webklex\PHPIMAP\IMAP;
use Webklex\PHPIMAP\Message;

class Issue407Test extends LiveMailboxTestCase {

/**
* @return void
* @throws \Webklex\PHPIMAP\Exceptions\AuthFailedException
* @throws \Webklex\PHPIMAP\Exceptions\ConnectionFailedException
* @throws \Webklex\PHPIMAP\Exceptions\EventNotFoundException
* @throws \Webklex\PHPIMAP\Exceptions\FolderFetchingException
* @throws \Webklex\PHPIMAP\Exceptions\ImapBadRequestException
* @throws \Webklex\PHPIMAP\Exceptions\ImapServerErrorException
* @throws \Webklex\PHPIMAP\Exceptions\InvalidMessageDateException
* @throws \Webklex\PHPIMAP\Exceptions\MaskNotFoundException
* @throws \Webklex\PHPIMAP\Exceptions\MessageContentFetchingException
* @throws \Webklex\PHPIMAP\Exceptions\MessageFlagException
* @throws \Webklex\PHPIMAP\Exceptions\MessageHeaderFetchingException
* @throws \Webklex\PHPIMAP\Exceptions\ResponseException
* @throws \Webklex\PHPIMAP\Exceptions\RuntimeException
*/
public function testIssue() {
$folder = $this->getFolder('INBOX');
self::assertInstanceOf(Folder::class, $folder);

$message = $this->appendMessageTemplate($folder, "plain.eml");
self::assertInstanceOf(Message::class, $message);

$message->setFlag("Seen");

$flags = $this->getClient()->getConnection()->flags($message->uid, IMAP::ST_UID)->validatedData();

self::assertIsArray($flags);
self::assertSame(1, count($flags));
self::assertSame("\\Seen", $flags[$message->uid][0]);

$message->delete();
}

}

0 comments on commit 9f02c84

Please sign in to comment.