Skip to content

Commit

Permalink
KAYAKOC-32113 | POP3 queues are throwing "Malformed header detected" (#3
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fpt-thanhdq2 authored Dec 2, 2022
1 parent 379b4b3 commit 55641ce
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/Header/AbstractAddressList.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function ($value) use (&$wasEncoded) {
},
$values
);

$addresses = array_filter($addresses);

$header = new static();
Expand Down Expand Up @@ -145,11 +146,11 @@ public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
$name = HeaderWrap::mimeEncodeValue($name, $encoding);
}

if (preg_match('/^(.*)\<(.+)@([^@]+)\>(.*)$/', $value, $matches)) {
if (preg_match('/^(.*)\<(.+)@([^@]+)\>(.*)$/', $email, $matches)) {
$localPart = $matches[2];
$hostname = $this->idnToAscii($matches[3]);
$email = sprintf('%s@%s', $localPart, $hostname);
} elseif (preg_match('/^(.+)@([^@]+)$/', $value, $matches)) {
} elseif (preg_match('/^(.+)@([^@]+)$/', $email, $matches)) {
$localPart = $matches[1];
$hostname = $this->idnToAscii($matches[2]);
$email = sprintf('%s@%s', $localPart, $hostname);
Expand Down
15 changes: 9 additions & 6 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,26 @@ public static function fromString($string, $EOL = self::EOL)
// iterate the header lines, some might be continuations
$lines = explode($EOL, $string);
$total = count($lines);

for ($i = 0; $i < $total; $i += 1) {
$line = $lines[$i];
$isEmptyLine = preg_match('/^\s*$/', $line);

// Empty line indicates end of headers
// EXCEPT if there are more lines, in which case, there's a possible error condition
if (preg_match('/^\s*$/', $line)) {
if ($isEmptyLine) {
$emptyLine += 1;
if ($emptyLine > 2) {
throw new Exception\RuntimeException('Malformed header detected');
}
continue;
}

if ($emptyLine > 1) {
// if some data follow by empty lines then indicate malform headers
if ($emptyLine > 1 && !$isEmptyLine) {
throw new Exception\RuntimeException('Malformed header detected');
}

if (!$isEmptyLine) {
$emptyLine = 0;
}

// check if a header name is present
if (preg_match('/^[\x21-\x39\x3B-\x7E]+:.*$/', $line)) {
if ($currentLine) {
Expand Down

0 comments on commit 55641ce

Please sign in to comment.