Skip to content

Commit

Permalink
Revise DKIM header canonicalisation, see #1525
Browse files Browse the repository at this point in the history
  • Loading branch information
Synchro committed Sep 2, 2019
1 parent 7c28398 commit a0ab65d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/PHPMailer.php
Expand Up @@ -4350,12 +4350,14 @@ public function DKIM_Sign($signHeader)
*/
public function DKIM_HeaderC($signHeader)
{
//Unfold all header continuation lines
//Also collapses folded whitespace.
//Note PCRE \s is too broad a definition of whitespace; RFC5322 defines it as `[ \t]`
//@see https://tools.ietf.org/html/rfc5322#section-2.2
//That means this may break if you do something daft like put vertical tabs in your headers.
$signHeader = preg_replace('/\r\n[ \t]+/', ' ', $signHeader);
//Unfold header lines
$signHeader = preg_replace('/\r\n[ \t]+/m', '', $signHeader);
//Collapse internal whitespace to a single space
// $signHeader = preg_replace('/[ \t]+/', ' ', $signHeader);
//Break headers out into an array
$lines = explode("\r\n", $signHeader);
foreach ($lines as $key => $line) {
//If the header is missing a :, skip it as it's invalid
Expand All @@ -4367,12 +4369,12 @@ public function DKIM_HeaderC($signHeader)
list($heading, $value) = explode(':', $line, 2);
//Lower-case header name
$heading = strtolower($heading);
//Collapse white space within the value
$value = preg_replace('/[ \t]{2,}/', ' ', $value);
//Collapse white space within the value, also convert WSP to space
$value = preg_replace('/[ \t]+/', ' ', $value);
//RFC6376 is slightly unclear here - it says to delete space at the *end* of each value
//But then says to delete space before and after the colon.
//Net result is the same as trimming both ends of the value.
//by elimination, the same applies to the field name
//By elimination, the same applies to the field name
$lines[$key] = trim($heading, " \t") . ':' . trim($value, " \t");
}

Expand Down
13 changes: 13 additions & 0 deletions test/PHPMailerTest.php
Expand Up @@ -2060,6 +2060,19 @@ public function testDKIMHeaderCanonicalization()
$this->Mail->DKIM_HeaderC($preheaders),
'DKIM header canonicalization incorrect'
);
//Check that long folded lines with runs of spaces are canonicalised properly
$preheaders = "Long-Header-1: <https://example.com/somescript.php?".
"id=1234567890&name=Abcdefghijklmnopquestuvwxyz&hash=\r\n abc1234".
"\r\nLong-Header-2: This is a long header value that contains runs of spaces and trailing ".
"\r\n and is folded onto 2 lines";
$postheaders = "long-header-1:<https://example.com/somescript.php?id=1234567890&".
"name=Abcdefghijklmnopquestuvwxyz&hash=abc1234\r\nlong-header-2:This is a long".
" header value that contains runs of spaces and trailing and is folded onto 2 lines";
$this->assertEquals(
$postheaders,
$this->Mail->DKIM_HeaderC($preheaders),
'DKIM header canonicalization of long lines incorrect'
);
}

/**
Expand Down

0 comments on commit a0ab65d

Please sign in to comment.