Skip to content

Commit

Permalink
fetcher: Fail Safely on Email Parse Error
Browse files Browse the repository at this point in the history
This commit adds ability to log email parse errors. This can happen if the
system fails to fetch complete raw email or when the email content is malformed
somehow.
  • Loading branch information
protich committed Sep 28, 2022
1 parent e724ccb commit 459520e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
16 changes: 10 additions & 6 deletions include/api.tickets.php
Expand Up @@ -158,10 +158,14 @@ function createTicket($data) {

function processEmail($data=false) {

if (!$data)
$data = $this->getEmailRequest();
elseif (!is_array($data))
$data = $this->parseEmail($data);
try {
if (!$data)
$data = $this->getEmailRequest();
elseif (!is_array($data))
$data = $this->parseEmail($data);
} catch (Exception $ex) {
throw new EmailParseError($ex->getMessage());
}

$seen = false;
if (($entry = ThreadEntry::lookupByEmailHeaders($data, $seen))
Expand Down Expand Up @@ -246,7 +250,7 @@ static function process() {
}
}

class TicketDenied extends Exception {
class TicketDenied extends Exception {}
class EmailParseError extends Exception {}

}
?>
4 changes: 3 additions & 1 deletion include/class.api.php
Expand Up @@ -226,8 +226,10 @@ function parseRequest($stream, $format, $validate=true) {
return $this->exerr(415, __('Unsupported data format'));
}

if (!($data = $parser->parse($stream)))
if (!($data = $parser->parse($stream))) {
$this->exerr(400, $parser->lastError());
throw new Exception($parser->lastError());
}

//Validate structure of the request.
if ($validate && $data)
Expand Down
8 changes: 7 additions & 1 deletion include/class.mailfetch.php
Expand Up @@ -71,9 +71,15 @@ function createTicket(int $i) {
// If a ticket is denied we're going to report it as processed
// so it can be moved out of the inbox or deleted.
return true;
} catch (\EmailParseError $ex) {
// Log the parse error + headers as a warning
$this->log(sprintf("%s\n\n%s",
$ex->getMessage(),
$this->mbox->getRawHeader($i)));
} catch (\Throwable $t) {
return false;
//noop
}
return false;
}

function processEmails() {
Expand Down

0 comments on commit 459520e

Please sign in to comment.