Skip to content

Commit 69f6b09

Browse files
committed
Merge branch 'PHP-8.0' into PHP-8.1
* PHP-8.0: Fix GH-7902: mb_send_mail may delimit headers with LF only
2 parents 8d2ed19 + 03816fb commit 69f6b09

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ PHP NEWS
1414
- FPM:
1515
. Fixed memory leak on invalid port. (David Carlier)
1616

17+
- MBString:
18+
. Fixed bug GH-7902 (mb_send_mail may delimit headers with LF only). (cmb)
19+
1720
- pcntl:
1821
. Fixed pcntl_rfork build for DragonFlyBSD. (David Carlier)
1922

ext/mbstring/mbstring.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3294,6 +3294,8 @@ PHP_FUNCTION(mb_decode_numericentity)
32943294
continue; \
32953295
}
32963296

3297+
#define CRLF "\r\n"
3298+
32973299
static int _php_mbstr_parse_mail_headers(HashTable *ht, const char *str, size_t str_len)
32983300
{
32993301
const char *ps;
@@ -3620,7 +3622,7 @@ PHP_FUNCTION(mb_send_mail)
36203622
|| orig_str.encoding->no_encoding == mbfl_no_encoding_pass) {
36213623
orig_str.encoding = mbfl_identify_encoding(&orig_str, MBSTRG(current_detect_order_list), MBSTRG(current_detect_order_list_size), MBSTRG(strict_detection));
36223624
}
3623-
pstr = mbfl_mime_header_encode(&orig_str, &conv_str, tran_cs, head_enc, "\n", sizeof("Subject: [PHP-jp nnnnnnnn]"));
3625+
pstr = mbfl_mime_header_encode(&orig_str, &conv_str, tran_cs, head_enc, CRLF, sizeof("Subject: [PHP-jp nnnnnnnn]" CRLF) - 1);
36243626
if (pstr != NULL) {
36253627
subject_buf = subject = (char *)pstr->val;
36263628
}
@@ -3659,14 +3661,14 @@ PHP_FUNCTION(mb_send_mail)
36593661
n = ZSTR_LEN(str_headers);
36603662
mbfl_memory_device_strncat(&device, p, n);
36613663
if (n > 0 && p[n - 1] != '\n') {
3662-
mbfl_memory_device_strncat(&device, "\n", 1);
3664+
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
36633665
}
36643666
zend_string_release_ex(str_headers, 0);
36653667
}
36663668

36673669
if (!zend_hash_str_exists(&ht_headers, "mime-version", sizeof("mime-version") - 1)) {
36683670
mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER1, sizeof(PHP_MBSTR_MAIL_MIME_HEADER1) - 1);
3669-
mbfl_memory_device_strncat(&device, "\n", 1);
3671+
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
36703672
}
36713673

36723674
if (!suppressed_hdrs.cnt_type) {
@@ -3677,7 +3679,7 @@ PHP_FUNCTION(mb_send_mail)
36773679
mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER3, sizeof(PHP_MBSTR_MAIL_MIME_HEADER3) - 1);
36783680
mbfl_memory_device_strcat(&device, p);
36793681
}
3680-
mbfl_memory_device_strncat(&device, "\n", 1);
3682+
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
36813683
}
36823684
if (!suppressed_hdrs.cnt_trans_enc) {
36833685
mbfl_memory_device_strncat(&device, PHP_MBSTR_MAIL_MIME_HEADER4, sizeof(PHP_MBSTR_MAIL_MIME_HEADER4) - 1);
@@ -3686,9 +3688,10 @@ PHP_FUNCTION(mb_send_mail)
36863688
p = "7bit";
36873689
}
36883690
mbfl_memory_device_strcat(&device, p);
3689-
mbfl_memory_device_strncat(&device, "\n", 1);
3691+
mbfl_memory_device_strncat(&device, CRLF, sizeof(CRLF)-1);
36903692
}
36913693

3694+
mbfl_memory_device_unput(&device);
36923695
mbfl_memory_device_unput(&device);
36933696
mbfl_memory_device_output('\0', &device);
36943697
str_headers = zend_string_init((char *)device.buffer, strlen((char *)device.buffer), 0);
@@ -3726,6 +3729,7 @@ PHP_FUNCTION(mb_send_mail)
37263729
}
37273730

37283731
#undef SKIP_LONG_HEADER_SEP_MBSTRING
3732+
#undef CRLF
37293733
#undef MAIL_ASCIIZ_CHECK_MBSTRING
37303734
#undef PHP_MBSTR_MAIL_MIME_HEADER1
37313735
#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)