From 05a9368e6a87a8def56cfcd7f4ba37c68f26841d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= Date: Sat, 27 Jun 2020 10:56:51 +0300 Subject: [PATCH] Fix AddressList toString method to quote semicolon MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Elan Ruusamäe --- src/Header/AbstractAddressList.php | 6 ++++-- test/Storage/MessageTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Header/AbstractAddressList.php b/src/Header/AbstractAddressList.php index a6839517..511095ce 100644 --- a/src/Header/AbstractAddressList.php +++ b/src/Header/AbstractAddressList.php @@ -135,7 +135,9 @@ public function getFieldValue($format = HeaderInterface::FORMAT_RAW) $email = $address->getEmail(); $name = $address->getName(); - if (! empty($name) && false !== strstr($name, ',')) { + // quote $name if value requires so + if (! empty($name) && (false !== strpos($name, ',') || false !== strpos($name, ';'))) { + // FIXME: what if name contains double quote? $name = sprintf('"%s"', $name); } @@ -241,7 +243,7 @@ protected static function getComments($value) * Supposed to be private, protected as a workaround for PHP bug 68194 * * @param string $value - * @return void + * @return string */ protected static function stripComments($value) { diff --git a/test/Storage/MessageTest.php b/test/Storage/MessageTest.php index 21666381..75f55487 100644 --- a/test/Storage/MessageTest.php +++ b/test/Storage/MessageTest.php @@ -10,6 +10,7 @@ use Exception as GeneralException; use Laminas\Mail\Exception as MailException; +use Laminas\Mail\Headers; use Laminas\Mail\Storage; use Laminas\Mail\Storage\Exception; use Laminas\Mail\Storage\Message; @@ -433,6 +434,31 @@ public function testSpaceInFieldName() $this->assertEquals(Mime\Decode::splitHeaderField($header, 'baz'), 42); } + /** + * splitMessage with Headers as input fails to process AddressList with semicolons + * + * @see https://github.com/laminas/laminas-mail/pull/93 + */ + public function testHeadersLosesNameQuoting() + { + $headerList = [ + 'From: "Famous bearings |;" ', + 'Reply-To: "Famous bearings |:" ', + ]; + + // create Headers object from array + Mime\Decode::splitMessage(implode("\r\n", $headerList), $headers1, $body); + $this->assertInstanceOf(Headers::class, $headers1); + // create Headers object from Headers object + Mime\Decode::splitMessage($headers1, $headers2, $body); + $this->assertInstanceOf(Headers::class, $headers2); + + // test that same problem does not happen with Storage\Message internally + $message = new Message(['headers' => $headers2, 'content' => (string)$body]); + $this->assertEquals('"Famous bearings |;" ', $message->from); + $this->assertEquals('Famous bearings |: ', $message->replyTo); + } + /** * @group Laminas-372 */