|
| 1 | +import { |
| 2 | + PHP, |
| 3 | + setPhpIniEntries, |
| 4 | + SupportedPHPVersions, |
| 5 | +} from '@php-wasm/universal'; |
| 6 | +import { |
| 7 | + SENDMAIL_CAPTURE_MAX_SIZE, |
| 8 | + sendmailSpawnHandler, |
| 9 | +} from '@php-wasm/util'; |
| 10 | +import type { PHPSendmailSpawnedEvent } from '@php-wasm/util'; |
| 11 | +import { loadNodeRuntime } from '../lib'; |
| 12 | + |
| 13 | +const phpVersions = |
| 14 | + 'PHP' in process.env ? [process.env['PHP']!] : SupportedPHPVersions; |
| 15 | + |
| 16 | +describe.each(phpVersions)('PHP %s - sendmail capture', (phpVersion) => { |
| 17 | + let php: PHP; |
| 18 | + let events: PHPSendmailSpawnedEvent[]; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + events = []; |
| 22 | + php = new PHP(await loadNodeRuntime(phpVersion as any)); |
| 23 | + php.setCommandSpawnHandler('sendmail', sendmailSpawnHandler(php)); |
| 24 | + php.addEventListener('sendmail.spawned', (event) => { |
| 25 | + events.push(event as PHPSendmailSpawnedEvent); |
| 26 | + }); |
| 27 | + await setPhpIniEntries(php, { |
| 28 | + disable_functions: '', |
| 29 | + }); |
| 30 | + }, 30_000); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + php?.exit(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('captures message sent to sendmail stdin via proc_open()', async () => { |
| 37 | + const result = await php.run({ |
| 38 | + code: `<?php |
| 39 | + error_reporting(E_ALL); |
| 40 | + $email = "From: sender@test.com\\r\\n" |
| 41 | + . "To: recipient@test.com\\r\\n" |
| 42 | + . "Subject: Raw proc_open\\r\\n" |
| 43 | + . "\\r\\n" |
| 44 | + . "Raw proc_open body."; |
| 45 | + $proc = proc_open( |
| 46 | + '/usr/sbin/sendmail -t -i -fsender@test.com', |
| 47 | + [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], |
| 48 | + $pipes |
| 49 | + ); |
| 50 | + if (!is_resource($proc)) { |
| 51 | + echo 'PROC_OPEN_FAILED'; |
| 52 | + exit; |
| 53 | + } |
| 54 | + fwrite($pipes[0], $email); |
| 55 | + fclose($pipes[0]); |
| 56 | + $exit = proc_close($proc); |
| 57 | + echo $exit === 0 ? 'SENT' : 'EXIT_' . $exit; |
| 58 | + `, |
| 59 | + }); |
| 60 | + |
| 61 | + expect(result.text).toBe('SENT'); |
| 62 | + expect(events).toHaveLength(1); |
| 63 | + const message = await decodeStream(events[0].stdin); |
| 64 | + expect(message).toContain('Subject: Raw proc_open'); |
| 65 | + expect(message).toContain('Raw proc_open body.'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('waits for delayed sendmail stdin bytes', async () => { |
| 69 | + const result = await php.run({ |
| 70 | + code: `<?php |
| 71 | + error_reporting(E_ALL); |
| 72 | + $proc = proc_open( |
| 73 | + '/usr/sbin/sendmail -t', |
| 74 | + [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], |
| 75 | + $pipes |
| 76 | + ); |
| 77 | + if (!is_resource($proc)) { |
| 78 | + echo 'PROC_OPEN_FAILED'; |
| 79 | + exit; |
| 80 | + } |
| 81 | + usleep(500000); |
| 82 | + fwrite($pipes[0], "Subject: Delayed proc_open\r\n\r\nDelayed body."); |
| 83 | + fclose($pipes[0]); |
| 84 | + $exit = proc_close($proc); |
| 85 | + echo $exit === 0 ? 'SENT' : 'EXIT_' . $exit; |
| 86 | + `, |
| 87 | + }); |
| 88 | + |
| 89 | + expect(result.text).toBe('SENT'); |
| 90 | + expect(events).toHaveLength(1); |
| 91 | + const message = await decodeStream(events[0].stdin); |
| 92 | + expect(message).toContain('Subject: Delayed proc_open'); |
| 93 | + expect(message).toContain('Delayed body.'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('exits 75 when sendmail receives no stdin bytes', async () => { |
| 97 | + const result = await php.run({ |
| 98 | + code: `<?php |
| 99 | + error_reporting(E_ALL); |
| 100 | + $proc = proc_open( |
| 101 | + '/usr/sbin/sendmail -t', |
| 102 | + [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], |
| 103 | + $pipes |
| 104 | + ); |
| 105 | + if (!is_resource($proc)) { |
| 106 | + echo 'PROC_OPEN_FAILED'; |
| 107 | + exit; |
| 108 | + } |
| 109 | + fclose($pipes[0]); |
| 110 | + $stderr = stream_get_contents($pipes[2]); |
| 111 | + $exit = proc_close($proc); |
| 112 | + echo json_encode(['exit' => $exit, 'stderr' => $stderr]); |
| 113 | + `, |
| 114 | + }); |
| 115 | + |
| 116 | + const data = result.json; |
| 117 | + expect(data).toEqual({ |
| 118 | + exit: 75, |
| 119 | + stderr: 'sendmail: fatal: No message bytes received over stdin\n', |
| 120 | + }); |
| 121 | + expect(events).toHaveLength(0); |
| 122 | + }); |
| 123 | + |
| 124 | + it('rejects messages larger than the fixed capture limit', async () => { |
| 125 | + const result = await php.run({ |
| 126 | + code: `<?php |
| 127 | + error_reporting(E_ALL); |
| 128 | + $proc = proc_open( |
| 129 | + '/usr/sbin/sendmail -t', |
| 130 | + [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], |
| 131 | + $pipes |
| 132 | + ); |
| 133 | + if (!is_resource($proc)) { |
| 134 | + echo 'PROC_OPEN_FAILED'; |
| 135 | + exit; |
| 136 | + } |
| 137 | + fwrite($pipes[0], str_repeat('a', ${SENDMAIL_CAPTURE_MAX_SIZE + 1})); |
| 138 | + fclose($pipes[0]); |
| 139 | + $stderr = stream_get_contents($pipes[2]); |
| 140 | + $exit = proc_close($proc); |
| 141 | + echo json_encode(['exit' => $exit, 'stderr' => $stderr]); |
| 142 | + `, |
| 143 | + }); |
| 144 | + |
| 145 | + const data = result.json; |
| 146 | + expect(data).toEqual({ |
| 147 | + exit: 1, |
| 148 | + stderr: `sendmail: Message size exceeds fixed maximum message size (${SENDMAIL_CAPTURE_MAX_SIZE})\n`, |
| 149 | + }); |
| 150 | + expect(events).toHaveLength(1); |
| 151 | + await expect(decodeStream(events[0].stdin)).rejects.toThrow( |
| 152 | + `sendmail: Message size exceeds fixed maximum message size (${SENDMAIL_CAPTURE_MAX_SIZE})` |
| 153 | + ); |
| 154 | + }, 30_000); |
| 155 | + |
| 156 | + it('should capture email after runtime rotation', async () => { |
| 157 | + await php.hotSwapPHPRuntime(await loadNodeRuntime(phpVersion as any)); |
| 158 | + await setPhpIniEntries(php, { |
| 159 | + disable_functions: '', |
| 160 | + }); |
| 161 | + |
| 162 | + const result = await php.run({ |
| 163 | + code: `<?php |
| 164 | + error_reporting(E_ALL); |
| 165 | + $result = mail( |
| 166 | + 'recipient@test.com', |
| 167 | + 'Raw subject', |
| 168 | + 'Raw body.', |
| 169 | + 'From: sender@test.com' |
| 170 | + ); |
| 171 | + echo $result ? 'SENT' : 'FAILED'; |
| 172 | + `, |
| 173 | + }); |
| 174 | + |
| 175 | + expect(result.text).toBe('SENT'); |
| 176 | + expect(events).toHaveLength(1); |
| 177 | + const message = await decodeStream(events[0].stdin); |
| 178 | + expect(message).toContain('Subject: Raw subject'); |
| 179 | + expect(message).toContain('Raw body.'); |
| 180 | + }); |
| 181 | +}); |
| 182 | + |
| 183 | +async function decodeStream(stream: ReadableStream<Uint8Array>) { |
| 184 | + const reader = stream.getReader(); |
| 185 | + const decoder = new TextDecoder(); |
| 186 | + let text = ''; |
| 187 | + while (true) { |
| 188 | + const { value, done } = await reader.read(); |
| 189 | + if (done) { |
| 190 | + return text; |
| 191 | + } |
| 192 | + text += decoder.decode(value, { stream: true }); |
| 193 | + } |
| 194 | +} |
0 commit comments