Skip to content

Commit

Permalink
Prevent header injection through content type / encoding in NativeMai…
Browse files Browse the repository at this point in the history
…lerHandler, fixes swiftmailer#458, closes swiftmailer#448
  • Loading branch information
Seldaek committed Dec 28, 2014
1 parent 5bee6fe commit 515a096
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Monolog/Handler/NativeMailerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ public function getEncoding()
*/
public function setContentType($contentType)
{
if (strpos($contentType, "\n") !== false || strpos($contentType, "\r") !== false) {
throw new \InvalidArgumentException('The content type can not contain newline characters to prevent email header injection');
}

$this->contentType = $contentType;

return $this;
Expand All @@ -140,6 +144,10 @@ public function setContentType($contentType)
*/
public function setEncoding($encoding)
{
if (strpos($encoding, "\n") !== false || strpos($encoding, "\r") !== false) {
throw new \InvalidArgumentException('The content type can not contain newline characters to prevent email header injection');
}

$this->encoding = $encoding;

return $this;
Expand Down
18 changes: 18 additions & 0 deletions tests/Monolog/Handler/NativeMailerHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,22 @@ public function testSetterArrayHeaderInjection()
$mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
$mailer->addHeader(array("Content-Type: text/html\r\nFrom: faked@attacker.org"));
}

/**
* @expectedException InvalidArgumentException
*/
public function testSetterContentTypeInjection()
{
$mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
$mailer->setContentType("text/html\r\nFrom: faked@attacker.org");
}

/**
* @expectedException InvalidArgumentException
*/
public function testSetterEncodingInjection()
{
$mailer = new NativeMailerHandler('spammer@example.org', 'dear victim', 'receiver@example.org');
$mailer->setEncoding("utf-8\r\nFrom: faked@attacker.org");
}
}

0 comments on commit 515a096

Please sign in to comment.