From 4e539faf647098b0290398bf7be1acc83b48c844 Mon Sep 17 00:00:00 2001 From: Ralev93 Date: Mon, 13 Jun 2022 17:58:20 +0300 Subject: [PATCH] Attachments with longs names are not being processed Currently, Laminas\Mail successfully identifies filenames longer than 78 chars (RFC 2822) and successfully splits them into multiple variables in \Laminas\Mail\Header\ContentDisposition::getFieldValue(). However, the multiple variables do not get recognized in Laminas\Mime\Decode::splitHeaderField() due to this check strcasecmp($name, $wantedPart) i.e. it compares "filename*0" with "filename". To reproduce, try to process an email with an attachment with long name, for example "This_______________________________________________________is____________________long__________________________________name.txt" Signed-off-by: Ralev93 --- src/Decode.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/Decode.php b/src/Decode.php index 5afdeab..4580bda 100644 --- a/src/Decode.php +++ b/src/Decode.php @@ -199,17 +199,26 @@ public static function splitHeaderField($field, $wantedPart = null, $firstName = throw new Exception\RuntimeException('not a valid header field'); } + $fullField = []; if ($wantedPart) { foreach ($matches[1] as $key => $name) { - if (strcasecmp($name, $wantedPart)) { + + if (!preg_match('/' . preg_quote($wantedPart) . "(\*[0-9])*" . '/', $name)) { //support multiname continue; } - if ($matches[2][$key][0] !== '"') { - return $matches[2][$key]; + + $val = $matches[2][$key][0] != '"' ? $matches[2][$key] : substr($matches[2][$key], 1, -1); + + // if name and wantedPart doees not match fully(they have matched above in the regex), + // it means that we have multiple values + if (strcasecmp($name, $wantedPart) && gettype($val) === 'string') { + $fullField[$wantedPart] .= $val; + } else {// name and wantedPart match + return $val; } - return substr($matches[2][$key], 1, -1); + } - return; + return $fullField[$wantedPart] ?: null; } $split = [];