|
2 | 2 |
|
3 | 3 | use DirectoryTree\ImapEngine\Collections\ResponseCollection; |
4 | 4 | use DirectoryTree\ImapEngine\Connection\ImapConnection; |
| 5 | +use DirectoryTree\ImapEngine\Connection\ImapQueryBuilder; |
5 | 6 | use DirectoryTree\ImapEngine\Connection\Streams\FakeStream; |
6 | 7 | use DirectoryTree\ImapEngine\Enums\ImapFetchIdentifier; |
7 | 8 | use DirectoryTree\ImapEngine\Exceptions\ImapCommandException; |
8 | 9 | use DirectoryTree\ImapEngine\Exceptions\ImapConnectionException; |
9 | 10 | use DirectoryTree\ImapEngine\Exceptions\ImapConnectionFailedException; |
10 | 11 | use DirectoryTree\ImapEngine\Support\Str; |
11 | 12 |
|
| 13 | +test('store rejects injected flags before writing to the stream', function () { |
| 14 | + $stream = new FakeStream; |
| 15 | + $stream->open(); |
| 16 | + |
| 17 | + $connection = new ImapConnection($stream); |
| 18 | + |
| 19 | + expect(fn () => $connection->store(["Seen)\r\nX1 CREATE Injected\r\nX2 NOOP ("], 1)) |
| 20 | + ->toThrow(InvalidArgumentException::class); |
| 21 | + |
| 22 | + $stream->assertNotWritten('TAG1'); |
| 23 | +}); |
| 24 | + |
| 25 | +test('append rejects injected flags before writing to the stream', function () { |
| 26 | + $stream = new FakeStream; |
| 27 | + $stream->open(); |
| 28 | + |
| 29 | + $connection = new ImapConnection($stream); |
| 30 | + |
| 31 | + expect(fn () => $connection->append('INBOX', "Subject: Test\r\n\r\nBody", ["Seen)\r\nX1 NOOP ("])) |
| 32 | + ->toThrow(InvalidArgumentException::class); |
| 33 | + |
| 34 | + $stream->assertNotWritten('TAG1'); |
| 35 | +}); |
| 36 | + |
| 37 | +test('send validates all tokens before writing a literal command', function () { |
| 38 | + $stream = new FakeStream; |
| 39 | + $stream->open(); |
| 40 | + |
| 41 | + $connection = new ImapConnection($stream); |
| 42 | + |
| 43 | + expect(fn () => $connection->send('APPEND', [['{5}', 'hello'], "\r\nX1 NOOP"])) |
| 44 | + ->toThrow(InvalidArgumentException::class); |
| 45 | + |
| 46 | + $stream->assertNotWritten('TAG1'); |
| 47 | +}); |
| 48 | + |
| 49 | +test('store accepts system flags and custom keywords', function () { |
| 50 | + $stream = new FakeStream; |
| 51 | + $stream->open(); |
| 52 | + $stream->feed(['* OK Welcome to IMAP', 'TAG1 OK STORE completed']); |
| 53 | + |
| 54 | + $connection = new ImapConnection($stream); |
| 55 | + $connection->connect('imap.example.com'); |
| 56 | + $connection->store(['\\Seen', 'Important', '$Junk'], 1); |
| 57 | + |
| 58 | + $stream->assertWritten('TAG1 UID STORE 1 +FLAGS.SILENT (\\Seen Important $Junk)'); |
| 59 | +}); |
| 60 | + |
| 61 | +test('raw command arguments reject injected commands before writing', function (string $method, array $arguments) { |
| 62 | + $stream = new FakeStream; |
| 63 | + $stream->open(); |
| 64 | + |
| 65 | + $connection = new ImapConnection($stream); |
| 66 | + |
| 67 | + expect(fn () => $connection->{$method}(...$arguments))->toThrow(InvalidArgumentException::class); |
| 68 | + |
| 69 | + $stream->assertNotWritten('TAG1'); |
| 70 | +})->with([ |
| 71 | + ['status', ['INBOX', ["MESSAGES)\r\nX1 NOOP"]]], |
| 72 | + ['bodyPart', ["1]\r\nX1 NOOP", 1]], |
| 73 | + ['fetch', [['FLAGS'], ["1\r\nX1 NOOP"]]], |
| 74 | + ['copy', ['Archive', ["1\r\nX1 NOOP"]]], |
| 75 | + ['move', ['Archive', ["1\r\nX1 NOOP"]]], |
| 76 | + ['store', [['\\Seen'], ["1\r\nX1 NOOP"]]], |
| 77 | + ['store', [['\\Seen'], 1, null, '+', true, "FLAGS\r\nX1 NOOP"]], |
| 78 | +]); |
| 79 | + |
| 80 | +test('header names reject injected commands before writing', function () { |
| 81 | + $stream = new FakeStream; |
| 82 | + $stream->open(); |
| 83 | + |
| 84 | + $connection = new ImapConnection($stream); |
| 85 | + $query = (new ImapQueryBuilder)->header("Subject\r\nX1 NOOP", 'test'); |
| 86 | + |
| 87 | + expect(fn () => $connection->search([$query->toImap()]))->toThrow(InvalidArgumentException::class); |
| 88 | + |
| 89 | + $stream->assertNotWritten('TAG1'); |
| 90 | +}); |
| 91 | + |
12 | 92 | test('connect success', function () { |
13 | 93 | $stream = new FakeStream; |
14 | 94 |
|
|
0 commit comments