[PECL-CVS] [pecl-mail-mailparse] fix/gh20-content-id-parentheses: fixes #20
[email protected] (Rasmus Lerdorf) Sun, 5 Apr 2026 11:20:11 +0000
| Newsgroups | php.pecl.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: Rasmus Lerdorf (rlerdorf)
Date: 2026-04-05T07:19:55-04:00
Commit: https://github.com/php/pecl-mail-mailparse/commit/2b714b89e494ad89fd972d6a69eb270fd21489a8
Raw diff: https://github.com/php/pecl-mail-mailparse/commit/2b714b89e494ad89fd972d6a69eb270fd21489a8.diff
fixes #20
Changed paths:
A tests/gh20.phpt
M mailparse.c
Diff:
diff --git a/mailparse.c b/mailparse.c
index 5dac132..c7a810a 100644
--- a/mailparse.c
+++ b/mailparse.c
@@ -1510,15 +1510,19 @@ static int mailparse_get_part_data(php_mimepart *part, zval *return_value)
add_assoc_string(return_value, "content-boundary", part->boundary);
if ((tmpval = zend_hash_find(Z_ARRVAL_P(&headers), hash_key)) != NULL) {
- php_rfc822_tokenized_t *toks;
- php_rfc822_addresses_t *addrs;
-
- toks = php_mailparse_rfc822_tokenize(Z_STRVAL_P(tmpval), 1);
- addrs = php_rfc822_parse_address_tokens(toks);
- if (addrs->naddrs > 0)
- add_assoc_string(return_value, "content-id", addrs->addrs[0].address);
- php_rfc822_free_addresses(addrs);
- php_rfc822_tokenize_free(toks);
+ /* Extract content-id value directly instead of parsing it as an
+ * RFC 822 address, which incorrectly strips parenthesized text
+ * as comments (GH-20). Just strip angle brackets if present. */
+ char *id = Z_STRVAL_P(tmpval);
+ size_t len = Z_STRLEN_P(tmpval);
+
+ while (len > 0 && (id[0] == ' ' || id[0] == '\t')) { id++; len--; }
+ while (len > 0 && (id[len-1] == ' ' || id[len-1] == '\t')) { len--; }
+ if (len >= 2 && id[0] == '<' && id[len-1] == '>') {
+ add_assoc_stringl(return_value, "content-id", id + 1, len - 2);
+ } else {
+ add_assoc_stringl(return_value, "content-id", id, len);
+ }
}
zend_string_release(hash_key);
diff --git a/tests/gh20.phpt b/tests/gh20.phpt
new file mode 100644
index 0000000..451f86f
--- /dev/null
+++ b/tests/gh20.phpt
@@ -0,0 +1,58 @@
+--TEST--
+GH issue #20 (Unexpected parsed value of content-id with parentheses)
+--SKIPIF--
+<?php
+if (!extension_loaded("mailparse")) die("skip mailparse extension not available");
+?>
+--FILE--
+<?php
+$mime = "Content-Type: image/png\r\n" .
+ "Content-Transfer-Encoding: base64\r\n" .
+ "Content-ID: <Facebook_32x32(1)_aa284ba9-f148-4698-9c1f-c8e92bdb842e.png>\r\n" .
+ "\r\n" .
+ "iVBOR\r\n";
+
+$resource = mailparse_msg_create();
+mailparse_msg_parse($resource, $mime);
+
+$part = mailparse_msg_get_part($resource, 1);
+$data = mailparse_msg_get_part_data($part);
+
+echo "content-id: " . $data['content-id'] . "\n";
+
+/* Also test bare content-id without angle brackets */
+$mime2 = "Content-Type: image/png\r\n" .
+ "Content-Transfer-Encoding: base64\r\n" .
+ "Content-ID: Facebook_32x32(1)_test.png\r\n" .
+ "\r\n" .
+ "iVBOR\r\n";
+
+$resource2 = mailparse_msg_create();
+mailparse_msg_parse($resource2, $mime2);
+
+$part2 = mailparse_msg_get_part($resource2, 1);
+$data2 = mailparse_msg_get_part_data($part2);
+
+echo "content-id bare: " . $data2['content-id'] . "\n";
+
+/* Standard RFC-compliant content-id */
+$mime3 = "Content-Type: image/png\r\n" .
+ "Content-ID: <[email protected]>\r\n" .
+ "\r\n" .
+ "iVBOR\r\n";
+
+$resource3 = mailparse_msg_create();
+mailparse_msg_parse($resource3, $mime3);
+
+$part3 = mailparse_msg_get_part($resource3, 1);
+$data3 = mailparse_msg_get_part_data($part3);
+
+echo "content-id rfc: " . $data3['content-id'] . "\n";
+
+echo "ok\n";
+?>
+--EXPECT--
+content-id: Facebook_32x32(1)_aa284ba9-f148-4698-9c1f-c8e92bdb842e.png
+content-id bare: Facebook_32x32(1)_test.png
+content-id rfc: [email protected]
+ok