From 5da4a0c2491cf3fddc4cb347154b6bf8fe86c897 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Thu, 22 Mar 2018 16:02:50 +0100 Subject: [PATCH] base64_decode may return false in PHP < 7.1 (#324) Closes #284 --- src/Message/AbstractPart.php | 4 +++ tests/MessageTest.php | 17 ++++++++++++ tests/fixtures/boolean_decoded_content.eml | 31 ++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 tests/fixtures/boolean_decoded_content.eml diff --git a/src/Message/AbstractPart.php b/src/Message/AbstractPart.php index 3f87434e..f5ba2cc1 100644 --- a/src/Message/AbstractPart.php +++ b/src/Message/AbstractPart.php @@ -336,6 +336,10 @@ final public function getDecodedContent(): string $content = \quoted_printable_decode($content); } + if (false === $content) { + throw new UnexpectedEncodingException('Cannot decode content'); + } + // If this part is a text part, convert its charset to UTF-8. // We don't want to decode an attachment's charset. if (!$this instanceof Attachment && null !== $this->getCharset() && self::TYPE_TEXT === $this->getType()) { diff --git a/tests/MessageTest.php b/tests/MessageTest.php index b658235c..8986531a 100644 --- a/tests/MessageTest.php +++ b/tests/MessageTest.php @@ -903,6 +903,23 @@ public function test_imap_mime_header_decode_returns_false() $this->assertSame('=?UTF-8?B?nnDusSNdG92w6Fuw61fMjAxOF8wMy0xMzMyNTMzMTkzLnBkZg==?=', $message->getSubject()); } + public function testBooleanDecodedContent() + { + if (PHP_VERSION_ID >= 70100) { + $this->markTestSkipped('Requires PHP < 7.1'); + } + + $this->mailbox->addMessage($this->getFixture('boolean_decoded_content')); + + $message = $this->mailbox->getMessage(1); + $attachments = $message->getAttachments(); + $attachment = \current($attachments); + + $this->expectException(UnexpectedEncodingException::class); + + $attachment->getDecodedContent(); + } + private function resetAttachmentCharset(MessageInterface $message) { // Mimic GMAIL behaviour that correctly doesn't report charset diff --git a/tests/fixtures/boolean_decoded_content.eml b/tests/fixtures/boolean_decoded_content.eml new file mode 100644 index 00000000..72fb64a7 --- /dev/null +++ b/tests/fixtures/boolean_decoded_content.eml @@ -0,0 +1,31 @@ +From: from@there.com +To: to@here.com +Subject: Nuu +Date: Wed, 13 Sep 2017 13:05:45 +0200 +Content-Type: multipart/mixed; boundary="=-vyqYb0SSRwuGFKv/Trdf" + +--=-vyqYb0SSRwuGFKv/Trdf +Content-Type: multipart/alternative; boundary="=-ewUvwipK68Y6itClYNpy" + +--=-ewUvwipK68Y6itClYNpy +Content-Type: text/plain; charset="us-ascii" + +Here is the problem mail + +Body text +--=-ewUvwipK68Y6itClYNpy +Content-Type: text/html; charset="us-ascii" + +Here is the problem mail + +Body text +--=-ewUvwipK68Y6itClYNpy-- + +--=-vyqYb0SSRwuGFKv/Trdf +Content-Type: application/pdf; name="Example Domain.pdf" +Content-Disposition: attachment; filename="Example Domain.pdf" +Content-Transfer-Encoding: base64 + +nnDusSNdG92w6Fuw61fMjAxOF8wMy0xMzMyNTMzMTkzLnBkZg==?= + +--=-vyqYb0SSRwuGFKv/Trdf--