From a6c89632f113cab987301033050c454b3e09bad7 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 1 Jun 2024 08:42:59 +0200 Subject: [PATCH] [Mime] Fix TextPart using an unknown File --- src/Symfony/Component/Mime/Part/TextPart.php | 6 +++++- .../Component/Mime/Tests/Part/TextPartTest.php | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Mime/Part/TextPart.php b/src/Symfony/Component/Mime/Part/TextPart.php index 1f14e0caef07d..2a8dd5852068a 100644 --- a/src/Symfony/Component/Mime/Part/TextPart.php +++ b/src/Symfony/Component/Mime/Part/TextPart.php @@ -123,7 +123,11 @@ public function getName(): ?string public function getBody(): string { if ($this->body instanceof File) { - return file_get_contents($this->body->getPath()); + if (false === $ret = @file_get_contents($this->body->getPath())) { + throw new InvalidArgumentException(error_get_last()['message']); + } + + return $ret; } if (null === $this->seekable) { diff --git a/src/Symfony/Component/Mime/Tests/Part/TextPartTest.php b/src/Symfony/Component/Mime/Tests/Part/TextPartTest.php index 905349e670048..ae1a5921ecc1e 100644 --- a/src/Symfony/Component/Mime/Tests/Part/TextPartTest.php +++ b/src/Symfony/Component/Mime/Tests/Part/TextPartTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Mime\Tests\Part; use PHPUnit\Framework\TestCase; +use Symfony\Component\Mime\Exception\InvalidArgumentException; use Symfony\Component\Mime\Header\Headers; use Symfony\Component\Mime\Header\ParameterizedHeader; use Symfony\Component\Mime\Header\UnstructuredHeader; @@ -55,6 +56,16 @@ public function testConstructorWithFile() $this->assertSame('content', implode('', iterator_to_array($p->bodyToIterable()))); } + public function testConstructorWithUnknownFile() + { + $p = new TextPart(new File(\dirname(__DIR__).'/Fixtures/unknown.txt')); + + // Exception should be thrown only when the body is accessed + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessageMatches('{Failed to open stream}'); + $p->getBody(); + } + public function testConstructorWithNonStringOrResource() { $this->expectException(\TypeError::class); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy