Skip to content

Commit 9f02c84

Browse files
committed
Prevent fetching singular rfc partials from running indefinitely Webklex#407
1 parent 50b4ea1 commit 9f02c84

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
99
- Legacy protocol support fixed (object to array conversion) #411
1010
- Header value decoding improved #410
1111
- Protocol exception handling improved (bad response message added) #408
12+
- Prevent fetching singular rfc partials from running indefinitely #407
1213

1314
### Added
1415
- NaN

src/Connection/Protocols/ImapProtocol.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,22 @@ public function readResponse(Response $response, string $tag, bool $dontParse =
294294
$lines[] = $tokens;
295295
} while (!$readAll);
296296

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

303+
$original = is_array($original)?$original : [$original];
304+
302305
// last line has response code
303306
if ($tokens[0] == 'OK') {
304307
return $lines ?: [true];
305308
} elseif ($tokens[0] == 'NO' || $tokens[0] == 'BAD' || $tokens[0] == 'BYE') {
306-
throw new ImapServerErrorException(implode("\n", $tokens));
309+
throw new ImapServerErrorException(implode("\n", $original));
307310
}
308311

309-
throw new ImapBadRequestException(implode("\n", $tokens));
312+
throw new ImapBadRequestException(implode("\n", $original));
310313
}
311314

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

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

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

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

776779
/**

tests/issues/Issue407Test.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/*
3+
* File: Issue407Test.php
4+
* Category: Test
5+
* Author: M.Goldenbaum
6+
* Created: 23.06.23 21:40
7+
* Updated: -
8+
*
9+
* Description:
10+
* -
11+
*/
12+
13+
namespace Tests\issues;
14+
15+
use PHPUnit\Framework\TestCase;
16+
use Tests\live\LiveMailboxTestCase;
17+
use Webklex\PHPIMAP\Folder;
18+
use Webklex\PHPIMAP\IMAP;
19+
use Webklex\PHPIMAP\Message;
20+
21+
class Issue407Test extends LiveMailboxTestCase {
22+
23+
/**
24+
* @return void
25+
* @throws \Webklex\PHPIMAP\Exceptions\AuthFailedException
26+
* @throws \Webklex\PHPIMAP\Exceptions\ConnectionFailedException
27+
* @throws \Webklex\PHPIMAP\Exceptions\EventNotFoundException
28+
* @throws \Webklex\PHPIMAP\Exceptions\FolderFetchingException
29+
* @throws \Webklex\PHPIMAP\Exceptions\ImapBadRequestException
30+
* @throws \Webklex\PHPIMAP\Exceptions\ImapServerErrorException
31+
* @throws \Webklex\PHPIMAP\Exceptions\InvalidMessageDateException
32+
* @throws \Webklex\PHPIMAP\Exceptions\MaskNotFoundException
33+
* @throws \Webklex\PHPIMAP\Exceptions\MessageContentFetchingException
34+
* @throws \Webklex\PHPIMAP\Exceptions\MessageFlagException
35+
* @throws \Webklex\PHPIMAP\Exceptions\MessageHeaderFetchingException
36+
* @throws \Webklex\PHPIMAP\Exceptions\ResponseException
37+
* @throws \Webklex\PHPIMAP\Exceptions\RuntimeException
38+
*/
39+
public function testIssue() {
40+
$folder = $this->getFolder('INBOX');
41+
self::assertInstanceOf(Folder::class, $folder);
42+
43+
$message = $this->appendMessageTemplate($folder, "plain.eml");
44+
self::assertInstanceOf(Message::class, $message);
45+
46+
$message->setFlag("Seen");
47+
48+
$flags = $this->getClient()->getConnection()->flags($message->uid, IMAP::ST_UID)->validatedData();
49+
50+
self::assertIsArray($flags);
51+
self::assertSame(1, count($flags));
52+
self::assertSame("\\Seen", $flags[$message->uid][0]);
53+
54+
$message->delete();
55+
}
56+
57+
}

0 commit comments

Comments
 (0)