Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

unfold before calling iconv_mime_decode #188

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Header/HeaderWrap.php
Expand Up @@ -103,6 +103,13 @@ public static function mimeEncodeValue($value, $encoding, $lineLength = 998)
*/
public static function mimeDecodeValue($value)
{
// unfold first, because iconv_mime_decode is discarding "\n" with no apparent reason
// making the resulting value no longer valid.

// see https://tools.ietf.org/html/rfc2822#section-2.2.3 about unfolding
$parts = explode(Headers::FOLDING, $value);
$value = implode(' ', $parts);

$decodedValue = iconv_mime_decode($value, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');

return $decodedValue;
Expand Down
21 changes: 21 additions & 0 deletions test/Header/HeaderWrapTest.php
Expand Up @@ -12,6 +12,7 @@
use PHPUnit\Framework\TestCase;
use Zend\Mail\Header\GenericHeader;
use Zend\Mail\Header\HeaderWrap;
use Zend\Mail\Storage;

/**
* @group Zend_Mail
Expand Down Expand Up @@ -74,6 +75,26 @@ public function testMimeDecoding()
$this->assertEquals($expected, $decoded);
}

/**
* Test that header lazy-loading doesn't break later header access
* because undocumented behavior in iconv_mime_decode()
* @see https://github.com/zendframework/zend-mail/pull/187
*/
public function testMimeDecodeBreakageBug()
{
$headerValue = "v=1; a=rsa-sha25; c=relaxed/simple; d=example.org; h=\r\n\tcontent-language:content-type:content-type:in-reply-to";
$headers = "DKIM-Signature: {$headerValue}";

$message = new Storage\Message(['headers' => $headers, 'content' => 'irrelevant']);
$headers = $message->getHeaders();
// calling toString will lazy load all headers
// and would break DKIM-Signature header access
$headers->toString();

$header = $headers->get('DKIM-Signature');
$this->assertEquals('v=1; a=rsa-sha25; c=relaxed/simple; d=example.org; h= content-language:content-type:content-type:in-reply-to', $header->getFieldValue());
}

/**
* Test that fails with HeaderWrap::canBeEncoded at lowest level:
* iconv_mime_encode(): Unknown error (7)
Expand Down