|
17 | 17 | use PHPUnit\Framework\MockObject\Exception; |
18 | 18 | use PHPUnit\Framework\MockObject\MockObject; |
19 | 19 | use PHPUnit\Framework\TestCase; |
| 20 | +use ReflectionClass; |
20 | 21 | use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport; |
21 | 22 | use Symfony\Component\Mailer\Transport\TransportInterface; |
| 23 | +use Symfony\Component\Mime\Message; |
| 24 | +use Symfony\Component\Mime\Part\AbstractPart; |
22 | 25 | use Symfony\Component\Mime\Part\DataPart; |
23 | 26 | use Symfony\Component\Mime\Part\Multipart\MixedPart; |
24 | 27 | use Symfony\Component\Mime\Part\TextPart; |
@@ -292,4 +295,136 @@ public function testAttachFilesNoNestedMixedParts(): void |
292 | 295 | $this->assertNotInstanceOf(MixedPart::class, $part); |
293 | 296 | } |
294 | 297 | } |
| 298 | + |
| 299 | + /** |
| 300 | + * Found an attachment leak where DataParts |
| 301 | + * embedded into Message::$body by attachFiles() survived a failed send |
| 302 | + * and were wrapped into the next email. The fix must live in dot-mail: |
| 303 | + * after send() (success OR failure) the underlying Message::$body must |
| 304 | + * be reset so the next send rebuilds the body from scratch. |
| 305 | + */ |
| 306 | + public function testMessageBodyIsResetAfterFailedSendToPreventAttachmentLeak(): void |
| 307 | + { |
| 308 | + $this->mailService->setSubject('First'); |
| 309 | + $this->message->html('First body'); |
| 310 | + $this->mailService->addAttachment( |
| 311 | + $this->fileSystem->url() . '/data/mail/attachments/testPdfAttachment.pdf' |
| 312 | + ); |
| 313 | + |
| 314 | + $this->transportInterface |
| 315 | + ->method('send') |
| 316 | + ->willThrowException(new RuntimeException('SMTP failure')); |
| 317 | + |
| 318 | + try { |
| 319 | + $this->mailService->send(); |
| 320 | + $this->fail('Expected MailException was not thrown'); |
| 321 | + } catch (MailException) { |
| 322 | + // expected |
| 323 | + } |
| 324 | + |
| 325 | + $this->assertNull( |
| 326 | + $this->readPrivateMessageBody($this->message), |
| 327 | + 'After a failed send the Symfony Message::$body must be reset; ' |
| 328 | + . 'otherwise attachFiles() will wrap the leaked MixedPart into the next email.' |
| 329 | + ); |
| 330 | + } |
| 331 | + |
| 332 | + /** |
| 333 | + * End-to-end regression for the same leak: after a failed send with |
| 334 | + * attachments, configure a fresh email with no attachments, and verify |
| 335 | + * the message handed to the transport contains no DataPart from the |
| 336 | + * previous send. |
| 337 | + */ |
| 338 | + public function testAttachmentsDoNotLeakIntoSubsequentSendAfterFailure(): void |
| 339 | + { |
| 340 | + $this->mailService->setSubject('First'); |
| 341 | + $this->message->html('First email'); |
| 342 | + $this->mailService->addAttachment( |
| 343 | + $this->fileSystem->url() . '/data/mail/attachments/testPdfAttachment.pdf' |
| 344 | + ); |
| 345 | + |
| 346 | + $capturedSecondBody = null; |
| 347 | + $callCount = 0; |
| 348 | + $this->transportInterface |
| 349 | + ->method('send') |
| 350 | + ->willReturnCallback(function ($email) use (&$capturedSecondBody, &$callCount) { |
| 351 | + $callCount++; |
| 352 | + if ($callCount === 1) { |
| 353 | + throw new RuntimeException('first send fails'); |
| 354 | + } |
| 355 | + $capturedSecondBody = $email->getBody(); |
| 356 | + return null; |
| 357 | + }); |
| 358 | + |
| 359 | + try { |
| 360 | + $this->mailService->send(); |
| 361 | + } catch (MailException) { |
| 362 | + // expected |
| 363 | + } |
| 364 | + |
| 365 | + // Reconfigure: brand new email with NO attachments. |
| 366 | + $this->mailService->setAttachments([]); |
| 367 | + $this->mailService->setSubject('Second'); |
| 368 | + $this->message->html('Second email - must not carry first email\'s attachment'); |
| 369 | + |
| 370 | + $this->mailService->send(); |
| 371 | + |
| 372 | + $this->assertInstanceOf( |
| 373 | + AbstractPart::class, |
| 374 | + $capturedSecondBody, |
| 375 | + 'Transport did not receive a second message body' |
| 376 | + ); |
| 377 | + $this->assertFalse( |
| 378 | + $this->bodyContainsDataPart($capturedSecondBody), |
| 379 | + 'Second email leaked a DataPart from the failed first send' |
| 380 | + ); |
| 381 | + } |
| 382 | + |
| 383 | + /** |
| 384 | + * Confirms the success path also resets the body, so a future refactor |
| 385 | + * does not silently regress the success case. |
| 386 | + */ |
| 387 | + public function testMessageBodyIsResetAfterSuccessfulSend(): void |
| 388 | + { |
| 389 | + $this->mailService->setSubject('Hello'); |
| 390 | + $this->message->html('Body'); |
| 391 | + $this->mailService->addAttachment( |
| 392 | + $this->fileSystem->url() . '/data/mail/attachments/testPdfAttachment.pdf' |
| 393 | + ); |
| 394 | + |
| 395 | + $this->transportInterface->expects($this->once())->method('send'); |
| 396 | + |
| 397 | + $this->mailService->send(); |
| 398 | + |
| 399 | + $this->assertNull( |
| 400 | + $this->readPrivateMessageBody($this->message), |
| 401 | + 'Message body should be null after a successful send so the next email starts clean.' |
| 402 | + ); |
| 403 | + } |
| 404 | + |
| 405 | + private function readPrivateMessageBody(Message $message): ?AbstractPart |
| 406 | + { |
| 407 | + $reflection = new ReflectionClass(Message::class); |
| 408 | + $property = $reflection->getProperty('body'); |
| 409 | + $property->setAccessible(true); |
| 410 | + |
| 411 | + return $property->getValue($message); |
| 412 | + } |
| 413 | + |
| 414 | + private function bodyContainsDataPart(AbstractPart $part): bool |
| 415 | + { |
| 416 | + if ($part instanceof DataPart) { |
| 417 | + return true; |
| 418 | + } |
| 419 | + |
| 420 | + if ($part instanceof MixedPart) { |
| 421 | + foreach ($part->getParts() as $child) { |
| 422 | + if ($this->bodyContainsDataPart($child)) { |
| 423 | + return true; |
| 424 | + } |
| 425 | + } |
| 426 | + } |
| 427 | + |
| 428 | + return false; |
| 429 | + } |
295 | 430 | } |
0 commit comments