Skip to content

Commit 87fca56

Browse files
authored
Merge pull request #150 from DirectoryTree/escape-id
Escape ID command parameters to prevent injection
2 parents b3f0ac3 + d666fb6 commit 87fca56

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Connection/ImapConnection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ public function id(?array $ids = null): UntaggedResponse
539539
$token = '(';
540540

541541
foreach ($ids as $id) {
542-
$token .= '"'.$id.'" ';
542+
$token .= '"'.Str::escape($id).'" ';
543543
}
544544

545545
$token = rtrim($token).')';

tests/Unit/Connection/ImapConnectionTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,28 @@
648648
expect($response->type()->is('ID'))->toBeTrue();
649649
});
650650

651+
test('id escapes special characters to prevent command injection', function () {
652+
$stream = new FakeStream;
653+
$stream->open();
654+
655+
$stream->feed([
656+
'* OK Welcome to IMAP',
657+
'* ID NIL',
658+
'TAG1 OK ID completed',
659+
]);
660+
661+
$connection = new ImapConnection($stream);
662+
$connection->connect('imap.example.com');
663+
664+
$connection->id([
665+
'name' => 'Evil"Client',
666+
'version' => "1.0\r\nLOGOUT",
667+
'vendor' => 'Test\\Vendor',
668+
]);
669+
670+
$stream->assertWritten('TAG1 ID ("Evil\\"Client" "1.0LOGOUT" "Test\\\\Vendor")');
671+
});
672+
651673
test('expunge', function () {
652674
$stream = new FakeStream;
653675
$stream->open();

0 commit comments

Comments
 (0)