From 0972774a2005c712e1df94d24acd746917cac789 Mon Sep 17 00:00:00 2001 From: Jesper Noordsij Date: Mon, 5 May 2025 12:48:24 +0200 Subject: [PATCH] [Mailer] [Transport] Send clone of `RawMessage` instance in `RoundRobinTransport` --- .../Transport/RoundRobinTransportTest.php | 23 +++++++++++++++++++ .../Mailer/Transport/RoundRobinTransport.php | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Mailer/Tests/Transport/RoundRobinTransportTest.php b/src/Symfony/Component/Mailer/Tests/Transport/RoundRobinTransportTest.php index a1b2befcce846..cc5656e1e9a56 100644 --- a/src/Symfony/Component/Mailer/Tests/Transport/RoundRobinTransportTest.php +++ b/src/Symfony/Component/Mailer/Tests/Transport/RoundRobinTransportTest.php @@ -16,6 +16,8 @@ use Symfony\Component\Mailer\Exception\TransportExceptionInterface; use Symfony\Component\Mailer\Transport\RoundRobinTransport; use Symfony\Component\Mailer\Transport\TransportInterface; +use Symfony\Component\Mime\Header\Headers; +use Symfony\Component\Mime\Message; use Symfony\Component\Mime\RawMessage; /** @@ -143,6 +145,27 @@ public function testSendOneDeadAndRecoveryWithinRetryPeriod() $this->assertTransports($t, 1, []); } + public function testSendOneDeadMessageAlterationsDoNotPersist() + { + $t1 = $this->createMock(TransportInterface::class); + $t1->expects($this->once())->method('send') + ->willReturnCallback(function (Message $message) { + $message->getHeaders()->addTextHeader('X-Transport-1', 'value'); + throw new TransportException(); + }); + $t2 = $this->createMock(TransportInterface::class); + $t2->expects($this->once())->method('send'); + $t = new RoundRobinTransport([$t1, $t2]); + $p = new \ReflectionProperty($t, 'cursor'); + $p->setValue($t, 0); + $headers = new Headers(); + $headers->addTextHeader('X-Shared', 'value'); + $message = new Message($headers); + $t->send($message); + $this->assertSame($message->getHeaders()->get('X-Shared')->getBody(), 'value'); + $this->assertFalse($message->getHeaders()->has('X-Transport-1')); + } + public function testFailureDebugInformation() { $t1 = $this->createMock(TransportInterface::class); diff --git a/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php b/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php index ac9709bf7b6c4..a88662d623ef9 100644 --- a/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php +++ b/src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php @@ -52,7 +52,7 @@ public function send(RawMessage $message, ?Envelope $envelope = null): ?SentMess while ($transport = $this->getNextTransport()) { try { - return $transport->send($message, $envelope); + return $transport->send(clone $message, $envelope); } catch (TransportExceptionInterface $e) { $exception ??= new TransportException('All transports failed.'); $exception->appendDebug(sprintf("Transport \"%s\": %s\n", $transport, $e->getDebug()));