Mail review stage 3: email-to-ticket reads the receiving server's Authentication-Results and treats an email failing DMARC for a domain that asks for reject or quarantine, or SPF with nothing else passing, as from a stranger - a reply is an internal note that reopens nothing, a new email still makes a ticket with a note but copies nobody in and gets no answer, and it never makes a contact; someone who isn't a contact is answered without their own subject; 26.12.3 changelog

This commit is contained in:
johnnyq
2026-11-30 16:50:51 -05:00
parent 899f2259fe
commit 17d2611b60
5 changed files with 135 additions and 23 deletions

View File

@@ -737,6 +737,7 @@ function ticketEmailPreview(array $email) {
* (emailQueue()), for ticketReplyEmailed()
* answering the Message-ID of the email it answers, when it answers one
* auto replied or generated - one of ITFlow's own automatic emails (emailQueue()'s auto)
* subject in place of the ticket's own (ticketEmailSubject()) - with its number, so a reply finds it
*/
function ticketMailSend($ticket_id, array $email, array $opts = []) {
global $mysqli;
@@ -786,7 +787,7 @@ function ticketMailSend($ticket_id, array $email, array $opts = []) {
return (($b['group'] ?? '') === 'contact') <=> (($a['group'] ?? '') === 'contact');
});
$to_contact = ($recipients[0]['group'] ?? '') === 'contact';
return count(emailQueue($recipients, ticketEmailSubject($row), $email, ['from' => ticketEmailFrom(), 'attachments' => $opts['attachments'] ?? [], 'cal_str' => $opts['cal_str'] ?? '',
return count(emailQueue($recipients, (string) ($opts['subject'] ?? ticketEmailSubject($row)), $email, ['from' => ticketEmailFrom(), 'attachments' => $opts['attachments'] ?? [], 'cal_str' => $opts['cal_str'] ?? '',
'ticket_reply_id' => intval($opts['ticket_reply_id'] ?? 0), 'ticket_id' => $ticket_id,
'together' => ticketMailSettings()['copy'],
'together_footnote' => $to_contact
@@ -876,14 +877,16 @@ function ticketMailColleaguesNew($ticket_id) {
* unless $to_ccs is false; or to $to (ticketMailSend()'s) in their
* place. $how agent, email or scheduled (ticketEmailContent()).
*/
function ticketMailCreated($ticket_id, $attachments = [], $to_ccs = true, $how = 'agent', ?array $to = null) {
function ticketMailCreated($ticket_id, $attachments = [], $to_ccs = true, $how = 'agent', ?array $to = null, ?string $subject = null) {
$row = ticketRow($ticket_id);
if (!$row) {
return 0;
}
// Emailed in, it is an automatic answer to that email - which says so, so the other side doesn't answer it (functions/ticket_mail_auto.php)
// Emailed in, it is an automatic answer to that email - which says so, so the other side doesn't answer it (functions/ticket_mail_auto.php).
// $subject in place of the ticket's - an emailed ticket from someone who isn't a contact is answered without their own words
return ticketMailSend($row['ticket_id'], ticketEmailContent($row, 'created', ['how' => $how]),
['attachments' => $attachments, 'to_ccs' => $to_ccs, 'kind' => 'created'] + ($to !== null ? ['to' => $to] : []) + ($how === 'email' ? ['auto' => 'replied'] : []));
['attachments' => $attachments, 'to_ccs' => $to_ccs, 'kind' => 'created'] + ($to !== null ? ['to' => $to] : []) + ($how === 'email' ? ['auto' => 'replied'] : [])
+ ($subject !== null ? ['subject' => $subject] : []));
}
/*

View File

@@ -44,6 +44,50 @@ function ticketMailHeaderMap($message) {
return $map;
}
/*
* Whether the mail server that took an email found it forged, by its Authentication-Results header -
* the topmost, which that server adds last: a sender's own is below it, and ITFlow only acts on a
* failure, so one saying "pass" gains them nothing. Failed is:
* dmarc=fail, from a domain that asks for its forged email to be rejected or quarantined (p=reject or
* quarantine, or Microsoft 365's action reject, oreject or quarantine) - one with p=none hasn't
* asked, and many such domains' own email fails too
* spf=fail (not softfail), with no DKIM signature passing and no DMARC result
* and never when the server vouched for it anyway - arc=pass, or Microsoft 365's compauth=pass.
* '' when it didn't fail - or no server checked - else why, in words ("DMARC failed for example.com,
* which asks for forged email to be rejected").
*/
function ticketMailSenderCheck(array $headers) {
$results = strtolower(preg_replace('/\s+/', ' ', (string) ($headers['authentication-results'] ?? '')));
if ($results === '') {
return '';
}
$verdicts = [];
$dmarc_part = '';
foreach (explode(';', $results) as $part) {
if (preg_match('/^\s*(dkim|spf|dmarc|arc|compauth)\s*=\s*([a-z]+)/', $part, $m)) {
$verdicts[$m[1]][] = $m[2];
if ($m[1] === 'dmarc') {
$dmarc_part = $part;
}
}
}
$said = function ($method, $result) use ($verdicts) {
return in_array($result, $verdicts[$method] ?? [], true);
};
if ($said('arc', 'pass') || $said('compauth', 'pass')) {
return '';
}
$domain = preg_match('/header\.from=([a-z0-9.-]+)/', $results, $m) ? $m[1] : '';
if ($said('dmarc', 'fail') && preg_match('/\b(p|policy)=(reject|quarantine)\b|\baction=(oreject|reject|quarantine|pct\.reject|pct\.quarantine)\b/', $dmarc_part, $policy)) {
$asks = ($policy[2] ?? '') !== '' ? $policy[2] : str_replace(['pct.', 'oreject'], ['', 'reject'], $policy[3]);
return 'DMARC failed' . ($domain !== '' ? " for $domain" : '') . ", which asks for forged email to be " . ($asks === 'quarantine' ? 'quarantined' : 'rejected');
}
if ($said('spf', 'fail') && !$said('dkim', 'pass') && !isset($verdicts['dmarc'])) {
return 'SPF failed - it came from a server ' . ($domain !== '' ? $domain : 'its domain') . ' says doesn\'t send its email - and no DKIM signature passed';
}
return '';
}
/*
* Whether an email is a mail server's report that an email couldn't be delivered: a delivery
* status report (multipart/report; report-type=delivery-status), an X-Failed-Recipients header

View File

@@ -283,6 +283,27 @@ function ticketMailFilesLeftOut($ticket_id, array $dropped) {
return $note_id;
}
/*
* Say on a ticket made from an email that the email failed its sender checks - $why (ticketMailSenderCheck()),
* $from the address it says it is from, $copied the people it copied in, who weren't added. An internal note,
* as the ticket is made all the same, so nothing is lost. The note's id.
*/
function ticketMailForgedNote($ticket_id, $why, $from, array $copied = []) {
global $mysqli;
$ticket_id = intval($ticket_id);
if (!$ticket_id) {
return 0;
}
$note = '<b>This email failed its sender checks</b> - ' . escapeHtml($why) . '. It may not be from ' . escapeHtml($from) . '.<br>'
. "<i>It was made a ticket so nothing is lost, but its sender wasn't answered" . ($copied ? ' and the people it copied in (' . escapeHtml(implode(', ', $copied)) . ') weren\'t added' : '')
. '. Check with the client before acting on it.</i>';
mysqli_query($mysqli, "INSERT INTO ticket_replies SET ticket_reply = '" . mysqli_real_escape_string($mysqli, $note) . "', ticket_reply_type = 'Internal',
ticket_reply_time_worked = '00:00:00', ticket_reply_by = 0, ticket_reply_ticket_id = $ticket_id");
$note_id = intval(mysqli_insert_id($mysqli));
logTicketHistory($ticket_id, escapeSql("The email it came from failed its sender checks - it may not be from $from"));
return $note_id;
}
/*
* A client's reply - by email, text or in the portal - passed on to the people an agent's reply goes to
* (the ticket's To line, ticketReplyRecipients()) who didn't have it: not whoever wrote it, nor