Skip to content

Commit

Permalink
Correctly process invalid mail Reply-To headers - closes #4158
Browse files Browse the repository at this point in the history
  • Loading branch information
freescout-help-desk committed Aug 2, 2024
1 parent 2dc8d06 commit 02e5fa8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/Console/Commands/ParseEml.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public function handle()
$this->info($message->getHeader()->raw);
$this->line('From: ');
$this->info(json_encode($message->getFrom()[0] ?? [], JSON_UNESCAPED_UNICODE));
$this->line('Reply-To: ');
$this->info(json_encode($message->getReplyTo()[0] ?? [], JSON_UNESCAPED_UNICODE));
$this->line('In-Reply-To: ');
$this->info($message->getInReplyTo());
$this->line('References: ');
Expand Down
32 changes: 30 additions & 2 deletions overrides/webklex/php-imap/src/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ public function rfc822_parse_headers($raw_headers) {
$raw_imap_headers = (array)\imap_rfc822_parse_headers($raw_headers);
foreach ($raw_imap_headers as $key => $values) {
$key = str_replace("-", "_", $key);
$imap_headers[$key] = $values;
$values = $this->sanitizeHeaderValue($values);
if (!is_array($values) || (is_array($values) && count($values))) {
$imap_headers[$key] = $values;
}
}
}
$lines = explode("\r\n", preg_replace("/\r\n\s/", ' ', $raw_headers));
Expand Down Expand Up @@ -322,12 +325,37 @@ public function rfc822_parse_headers($raw_headers) {
}
break;
}
$headers[$key] = $value;
$value = $this->sanitizeHeaderValue($value);
if (!is_array($value) || (is_array($value) && count($value))) {
$headers[$key] = $value;
} elseif (is_array($value) && !count($value) && isset($headers[$key])) {
unset($headers[$key]);
}
}

return (object)array_merge($headers, $imap_headers);
}

// https://github.com/freescout-help-desk/freescout/issues/4158
public function sanitizeHeaderValue($value)
{
if (is_array($value)) {
foreach ($value as $i => $v) {
if (is_object($v)
&& isset($v->mailbox)
&& ($v->mailbox == '>' || $v->mailbox == 'INVALID_ADDRESS')
&& ((isset($v->host) && ($v->host == '.SYNTAX-ERROR.' || $v->host === null)) || !isset($v->host))
) {
echo 'unset: '.$v->mailbox;

unset($value[$i]);
}
}
}

return $value;
}

/**
* Decode MIME header elements
* @link https://php.net/manual/en/function.imap-mime-header-decode.php
Expand Down

0 comments on commit 02e5fa8

Please sign in to comment.