Skip to content

Commit 58cbee1

Browse files
committed
Merge branch 'PHP-8.1'
* PHP-8.1: Fix GH-7902: mb_send_mail may delimit headers with LF only
2 parents 2c4b9e9 + 69f6b09 commit 58cbee1

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

ext/mbstring/mbstring.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3318,6 +3318,8 @@ PHP_FUNCTION(mb_decode_numericentity)
33183318
/* }}} */
33193319

33203320
/* {{{ Sends an email message with MIME scheme */
3321+
#define CRLF "\r\n"
3322+
33213323
static int _php_mbstr_parse_mail_headers(HashTable *ht, const char *str, size_t str_len)
33223324
{
33233325
const char *ps;
@@ -3649,7 +3651,7 @@ PHP_FUNCTION(mb_send_mail)
36493651
|| orig_str.encoding->no_encoding == mbfl_no_encoding_pass) {
36503652
orig_str.encoding = mbfl_identify_encoding(&orig_str, MBSTRG(current_detect_order_list), MBSTRG(current_detect_order_list_size), MBSTRG(strict_detection));
36513653
}
3652-
pstr = mbfl_mime_header_encode(&orig_str, &conv_str, tran_cs, head_enc, "\n", sizeof("Subject: [PHP-jp nnnnnnnn]"));
3654+
pstr = mbfl_mime_header_encode(&orig_str, &conv_str, tran_cs, head_enc, CRLF, sizeof("Subject: [PHP-jp nnnnnnnn]" CRLF) - 1);
36533655
if (pstr != NULL) {
36543656
subject_buf = subject = (char *)pstr->val;
36553657
}
@@ -3688,14 +3690,14 @@ PHP_FUNCTION(mb_send_mail)
36883690
n = ZSTR_LEN(str_headers);
36893691
mbfl_memory_device_strncat(&device, p, n);
36903692
if (n > 0 && p[n - 1] != '\n') {
3691-
mbfl_memory_device_strncat(&device, "\n", 1);
3693+
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
36923694
}
36933695
zend_string_release_ex(str_headers, 0);
36943696
}
36953697

36963698
if (!zend_hash_str_exists(&ht_headers, "mime-version", sizeof("mime-version") - 1)) {
36973699
mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER1, sizeof(PHP_MBSTR_MAIL_MIME_HEADER1) - 1);
3698-
mbfl_memory_device_strncat(&device, "\n", 1);
3700+
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
36993701
}
37003702

37013703
if (!suppressed_hdrs.cnt_type) {
@@ -3706,7 +3708,7 @@ PHP_FUNCTION(mb_send_mail)
37063708
mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER3, sizeof(PHP_MBSTR_MAIL_MIME_HEADER3) - 1);
37073709
mbfl_memory_device_strcat(&device, p);
37083710
}
3709-
mbfl_memory_device_strncat(&device, "\n", 1);
3711+
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
37103712
}
37113713
if (!suppressed_hdrs.cnt_trans_enc) {
37123714
mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER4, sizeof(PHP_MBSTR_MAIL_MIME_HEADER4) - 1);
@@ -3715,9 +3717,10 @@ PHP_FUNCTION(mb_send_mail)
37153717
p = "7bit";
37163718
}
37173719
mbfl_memory_device_strcat(&device, p);
3718-
mbfl_memory_device_strncat(&device, "\n", 1);
3720+
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
37193721
}
37203722

3723+
mbfl_memory_device_unput(&device);
37213724
mbfl_memory_device_unput(&device);
37223725
mbfl_memory_device_output('\0', &device);
37233726
str_headers = zend_string_init((char *)device.buffer, strlen((char *)device.buffer), 0);
@@ -3754,6 +3757,7 @@ PHP_FUNCTION(mb_send_mail)
37543757
}
37553758
}
37563759

3760+
#undef CRLF
37573761
#undef MAIL_ASCIIZ_CHECK_MBSTRING
37583762
#undef PHP_MBSTR_MAIL_MIME_HEADER1
37593763
#undef PHP_MBSTR_MAIL_MIME_HEADER2

ext/mbstring/tests/gh7902.phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
GH-7902 (mb_send_mail may delimit headers with LF only)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded("mbstring")) die("skip mbstring extension not available");
6+
?>
7+
--INI--
8+
sendmail_path={MAIL:{PWD}/gh7902.eml}
9+
--FILE--
10+
<?php
11+
mb_internal_encoding("UTF-8");
12+
mb_language("uni");
13+
14+
$subject = "test mail";
15+
$message = "body of testing php mail";
16+
$header["Mime-Version"] = "1.0";
17+
$header["Content-Type"] = "text/html; charset=UTF-8";
18+
$header["From"] = "[email protected]";
19+
$header["X-Mailer"] = "PHP/" . phpversion();
20+
mb_send_mail($to, $subject, $message, $header);
21+
22+
$stream = fopen(__DIR__ . "/gh7902.eml", "rb");
23+
$eml = stream_get_contents($stream);
24+
fclose($stream);
25+
var_dump(preg_match_all('/(?<!\r)\n/', $eml));
26+
?>
27+
--CLEAN--
28+
<?php
29+
@unlink(__DIR__ . "/gh7902.eml");
30+
?>
31+
--EXPECT--
32+
int(0)

0 commit comments

Comments
 (0)