From: Yasuo Ohgaki Date: Wed, 13 May 2015 00:18:46 +0000 (+0900) Subject: Fixed bug #68776 X-Git-Tag: php-5.4.42^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9d168b863e007c4e15ebe4d2eecabdf8b0582e30;p=php Fixed bug #68776 --- diff --git a/ext/standard/mail.c b/ext/standard/mail.c index 1ebc8fecb7..448013a472 100644 --- a/ext/standard/mail.c +++ b/ext/standard/mail.c @@ -221,6 +221,44 @@ void php_mail_log_to_file(char *filename, char *message, size_t message_size TSR } +static int php_mail_detect_multiple_crlf(char *hdr) { + /* This function detects multiple/malformed multiple newlines. */ + size_t len; + + if (!hdr) { + return 0; + } + + /* Should not have any newlines at the beginning. */ + /* RFC 2822 2.2. Header Fields */ + if (*hdr < 33 || *hdr > 126 || *hdr == ':') { + return 1; + } + + while(*hdr) { + if (*hdr == '\r') { + if (*(hdr+1) == '\0' || *(hdr+1) == '\r' || (*(hdr+1) == '\n' && (*(hdr+2) == '\0' || *(hdr+2) == '\n' || *(hdr+2) == '\r'))) { + /* Malformed or multiple newlines. */ + return 1; + } else { + hdr += 2; + } + } else if (*hdr == '\n') { + if (*(hdr+1) == '\0' || *(hdr+1) == '\r' || *(hdr+1) == '\n') { + /* Malformed or multiple newlines. */ + return 1; + } else { + hdr += 2; + } + } else { + hdr++; + } + } + + return 0; +} + + /* {{{ php_mail */ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char *extra_cmd TSRMLS_DC) @@ -266,6 +304,7 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char efree(tmp); } + if (PG(mail_x_header)) { const char *tmp = zend_get_executed_filename(TSRMLS_C); char *f; @@ -281,6 +320,11 @@ PHPAPI int php_mail(char *to, char *subject, char *message, char *headers, char efree(f); } + if (hdr && php_mail_detect_multiple_crlf(hdr)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Multiple or malformed newlines found in additional_header"); + MAIL_RET(0); + } + if (!sendmail_path) { #if (defined PHP_WIN32 || defined NETWARE) /* handle old style win smtp sending */ diff --git a/ext/standard/tests/mail/mail_basic6.phpt b/ext/standard/tests/mail/mail_basic6.phpt new file mode 100644 index 0000000000..d0d45b78f3 --- /dev/null +++ b/ext/standard/tests/mail/mail_basic6.phpt @@ -0,0 +1,329 @@ +--TEST-- +Test mail() function : basic functionality +--INI-- +sendmail_path=tee mailBasic.out >/dev/null +mail.add_x_header = Off +--SKIPIF-- + +--FILE-- + +===DONE=== +--EXPECTF-- +*** Testing mail() : basic functionality *** +-- Valid Header -- +bool(true) +To: user@example.com +Subject: Test Subject +HEAD1: a +HEAD2: b + +A Message +-- Valid Header -- +bool(true) +To: user@example.com +Subject: Test Subject +HEAD1: a +HEAD2: b + +A Message +-- Valid Header -- +bool(true) +To: user@example.com +Subject: Test Subject +HEAD1: a HEAD2: b + +A Message +-- Invalid Header - preceeding newline-- + +Warning: mail(): Multiple or malformed newlines found in additional_header in %s/mail_basic6.php on line %d +bool(false) +-- Invalid Header - preceeding newline-- + +Warning: mail(): Multiple or malformed newlines found in additional_header in %s/mail_basic6.php on line %d +bool(false) +-- Invalid Header - preceeding newline-- + +Warning: mail(): Multiple or malformed newlines found in additional_header in %s/mail_basic6.php on line %d +bool(false) +-- Invalid Header - preceeding newline-- + +Warning: mail(): Multiple or malformed newlines found in additional_header in %s/mail_basic6.php on line %d +bool(false) +-- Invalid Header - preceeding newline-- + +Warning: mail(): Multiple or malformed newlines found in additional_header in %s/mail_basic6.php on line %d +bool(false) +-- Invalid Header - preceeding newline-- + +Warning: mail(): Multiple or malformed newlines found in additional_header in %s/mail_basic6.php on line %d +bool(false) +-- Invalid Header - multiple newlines in the middle -- + +Warning: mail(): Multiple or malformed newlines found in additional_header in %s/mail_basic6.php on line %d +bool(false) +-- Invalid Header - multiple newlines in the middle -- + +Warning: mail(): Multiple or malformed newlines found in additional_header in %s/mail_basic6.php on line %d +bool(false) +-- Invalid Header - multiple newlines in the middle -- + +Warning: mail(): Multiple or malformed newlines found in additional_header in %s/mail_basic6.php on line %d +bool(false) +-- Invalid Header - multiple newlines in the middle -- + +Warning: mail(): Multiple or malformed newlines found in additional_header in %s/mail_basic6.php on line %d +bool(false) +-- Invalid Header - multiple newlines in the middle -- + +Warning: mail(): Multiple or malformed newlines found in additional_header in %s/mail_basic6.php on line %d +bool(false) +-- Invalid Header - multiple newlines in the middle -- + +Warning: mail(): Multiple or malformed newlines found in additional_header in %s/mail_basic6.php on line %d +bool(false) +-- Invalid Header - trailing newlines -- +bool(true) +To: user@example.com +Subject: Test Subject +HEAD1: a +HEAD2: b + +A Message +-- Invalid Header - trailing newlines -- +bool(true) +To: user@example.com +Subject: Test Subject +HEAD1: a +HEAD2: b + +A Message +-- Invalid Header - trailing newlines -- +bool(true) +To: user@example.com +Subject: Test Subject +HEAD1: a +HEAD2: b + +A Message +-- Invalid Header - trailing newlines -- +bool(true) +To: user@example.com +Subject: Test Subject +HEAD1: a +HEAD2: b + +A Message +===DONE===