php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68776 mail() does not have mail header injection prevention for additional headers
Submitted: 2015-01-09 09:59 UTC Modified: 2015-06-25 04:24 UTC
From: [email protected] Assigned: yohgaki (profile)
Status: Closed Package: Mail related
PHP Version: Irrelevant OS: any
Private report: No CVE-ID: None
 [2015-01-09 09:59 UTC] [email protected]
Description:
------------
mb_send_mail() parses additional headers and stores into hash. During the parse process, invalid headers are discarded.

However, mail() simply check \0 and strip trailing \r\n. Therefore, mail() is vulnerable to mail header injections via additional header parameter.



Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-17 18:40 UTC] cmbecker69 at gmx dot de
When passing unvalidated and unsanitized input as
$additional_headers argument, both functions are vulnerable to
email header injection. For instance:

  // $_POST['from'] == "[email protected]\r\nBcc: [email protected]"
  $from = $_POST['from'];
  mb_send_mail(
    '[email protected]', 'foo', 'bar', "From: $from"
  );
  
It seems to me that this is more of an documentation issue.
 [2015-01-25 02:57 UTC] [email protected]
-Assigned To: +Assigned To: yohgaki
 [2015-01-25 03:00 UTC] [email protected]
@cmbecker69 Thanks. Sloppy reading the code.

I'll fix it anyway.
 [2015-06-01 01:12 UTC] [email protected]
Please see my comments in git.
 [2015-06-10 04:35 UTC] [email protected]
-Status: Assigned +Status: Closed
 [2015-06-23 09:45 UTC] chaos at isocity dot de
Now it has issues with:

mail('', $subject,'',imap_mail_compose($envelope, $body)));

Also this version of code:

function validateMail($str){
 return str_replace(array('\r\r','\r\0','\r\n\r\n','\n\n','\n\0'),'',$str);
}
mail('', $subject,'',validateMail(imap_mail_compose($envelope, $body))));
 [2015-06-23 11:49 UTC] [email protected]
We are aware of that.
I'm going to handle it.

https://bugs.php.net/bug.php?id=69791
 [2015-06-23 20:03 UTC] [email protected]
Yasuo, I assume that fixing bug #69791 will not make it possible
to pass the result of imap_mail_compose() as $additional_headers
parameter of mail(). Actually, I consider passing the body of a
mail via $additional_headers as more than doubtful.

However, the current documentation doesn't explicitly state that
this could not be done, and apparently it worked before the fix of
this bug had been applied. So this is a BC, albeit likely a very
minor one; the documentation should better be updated accordingly,
nonetheless.
 [2015-06-23 21:23 UTC] [email protected]
@cmb

I agree. Documentation must be improved. 
I'll update the doc.
 [2015-06-24 15:10 UTC] chaos at isocity dot de
Thx for response.

A short feedback here:

<?php

//Example mail with HMTL body on additional_header
$uid = md5(rand());

$to = "[email protected]";
$subject = "My subject";

$headers = 

"From: [email protected]" 				. "\r\n" .

"MIME-Version: 1.0" 					. "\r\n" .
"Content-Type: multipart/mixed; boundary=\"".$uid."\""	. "\r\n" .
"This is a multi-part message in MIME format." 		. "\r\n" .
"--".$uid						. "\r\n" .
"Content-Type: TEXT/html; CHARSET=iso-8859-1" 		. "\r\n" .
"Content-Transfer-Encoding: BASE64" 			. "\r\n" .
"Content-Description: htmlpart" 			. "\r\n" .
"" 							. "\r\n" .
"=?UTF-8?B?PHN0cm9uZz50ZXN0PC9zdHJvbmc+?=" 		. "\r\n" .
"--".$uid;

mail($to,$subject,'',$headers);

?>

Warning: mail(): Multiple or malformed newlines found in additional_header in /

It seems like it doesn't matter how to perform $additional_headers if something like attachments or html-body-parts are set up.

Addition: If this line: "". "\r\n" . is been removed, the mail()-error doesn't apply. But unfortunately either no email is sent this way on one hosting plattform or the email is sent without body on the other.
 [2015-06-25 04:21 UTC] [email protected]
@chaos

We are planning to eliminate injection by this
https://bugs.php.net/bug.php?id=69791
 [2015-06-25 04:24 UTC] [email protected]
@chaos

To send multipart MIME message, users should use header and body correctly. RFC 2822 defines CRLF+CRLF as start of body. So if users are misusing $additional_headers, they have to fix their code.
 [2015-09-02 13:18 UTC] merijn at web2all dot nl
@yohgaki

regarding your last comment @chaos; 
The documentation states 'String to be inserted at the end of the email header' and it could (and has) been used to send a MIME message. 
So this injection prevention fix breaks code which worked for over 10 years. And its also in a pretty important part, the sending of mail.

I think this should not be fixed in minor release without any mention of a serious backwards compatibility break....
 [2018-08-14 05:25 UTC] calltrichymap at gmail dot com
hai , this is test message
 [2019-04-10 14:11 UTC] real dot rongginn at gmail dot com
hello
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 29 03:00:02 2025 UTC