Skip to content

Fix GH-7902: mb_send_mail may delimit headers with LF only #7907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions ext/mbstring/mbstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -3270,6 +3270,8 @@ PHP_FUNCTION(mb_decode_numericentity)
continue; \
}

#define CRLF "\r\n"

static int _php_mbstr_parse_mail_headers(HashTable *ht, const char *str, size_t str_len)
{
const char *ps;
Expand Down Expand Up @@ -3601,7 +3603,7 @@ PHP_FUNCTION(mb_send_mail)
|| orig_str.encoding->no_encoding == mbfl_no_encoding_pass) {
orig_str.encoding = mbfl_identify_encoding(&orig_str, MBSTRG(current_detect_order_list), MBSTRG(current_detect_order_list_size), MBSTRG(strict_detection));
}
pstr = mbfl_mime_header_encode(&orig_str, &conv_str, tran_cs, head_enc, "\n", sizeof("Subject: [PHP-jp nnnnnnnn]"));
pstr = mbfl_mime_header_encode(&orig_str, &conv_str, tran_cs, head_enc, CRLF, sizeof("Subject: [PHP-jp nnnnnnnn]" CRLF) - 1);
if (pstr != NULL) {
subject_buf = subject = (char *)pstr->val;
}
Expand Down Expand Up @@ -3640,14 +3642,14 @@ PHP_FUNCTION(mb_send_mail)
n = ZSTR_LEN(str_headers);
mbfl_memory_device_strncat(&device, p, n);
if (n > 0 && p[n - 1] != '\n') {
mbfl_memory_device_strncat(&device, "\n", 1);
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
}
zend_string_release_ex(str_headers, 0);
}

if (!zend_hash_str_exists(&ht_headers, "MIME-VERSION", sizeof("MIME-VERSION") - 1)) {
mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER1, sizeof(PHP_MBSTR_MAIL_MIME_HEADER1) - 1);
mbfl_memory_device_strncat(&device, "\n", 1);
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
}

if (!suppressed_hdrs.cnt_type) {
Expand All @@ -3658,7 +3660,7 @@ PHP_FUNCTION(mb_send_mail)
mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER3, sizeof(PHP_MBSTR_MAIL_MIME_HEADER3) - 1);
mbfl_memory_device_strcat(&device, p);
}
mbfl_memory_device_strncat(&device, "\n", 1);
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
}
if (!suppressed_hdrs.cnt_trans_enc) {
mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER4, sizeof(PHP_MBSTR_MAIL_MIME_HEADER4) - 1);
Expand All @@ -3667,9 +3669,10 @@ PHP_FUNCTION(mb_send_mail)
p = "7bit";
}
mbfl_memory_device_strcat(&device, p);
mbfl_memory_device_strncat(&device, "\n", 1);
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
}

mbfl_memory_device_unput(&device);
mbfl_memory_device_unput(&device);
mbfl_memory_device_output('\0', &device);
str_headers = zend_string_init((char *)device.buffer, strlen((char *)device.buffer), 0);
Expand Down Expand Up @@ -3707,6 +3710,7 @@ PHP_FUNCTION(mb_send_mail)
}

#undef SKIP_LONG_HEADER_SEP_MBSTRING
#undef CRLF
#undef MAIL_ASCIIZ_CHECK_MBSTRING
#undef PHP_MBSTR_MAIL_MIME_HEADER1
#undef PHP_MBSTR_MAIL_MIME_HEADER2
Expand Down
32 changes: 32 additions & 0 deletions ext/mbstring/tests/gh7902.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
--TEST--
GH-7902 (mb_send_mail may delimit headers with LF only)
--SKIPIF--
<?php
if (!extension_loaded("mbstring")) die("skip mbstring extension not available");
?>
--INI--
sendmail_path={MAIL:{PWD}/gh7902.eml}
--FILE--
<?php
mb_internal_encoding("UTF-8");
mb_language("uni");
$to = "omittedvalidaddress@example.com";
$subject = "test mail";
$message = "body of testing php mail";
$header["Mime-Version"] = "1.0";
$header["Content-Type"] = "text/html; charset=UTF-8";
$header["From"] = "omittedvalidaddress2@example.com";
$header["X-Mailer"] = "PHP/" . phpversion();
mb_send_mail($to, $subject, $message, $header);

$stream = fopen(__DIR__ . "/gh7902.eml", "rb");
$eml = stream_get_contents($stream);
fclose($stream);
var_dump(preg_match_all('/(?<!\r)\n/', $eml));
?>
--CLEAN--
<?php
@unlink(__DIR__ . "/gh7902.eml");
?>
--EXPECT--
int(0)