fix(mail-ingest): enhance header processing to prioritize X-Forwarded-To for IMAP

This commit is contained in:
Luffy
2026-12-04 12:33:17 +08:00
committed by Maurice
parent a4bd40cb0a
commit b15cd49833
5 changed files with 79 additions and 11 deletions
+2
View File
@@ -45,6 +45,7 @@ interface MailEnvelope {
messageId: string | null;
deliveredTo: string | null;
xOriginalTo: string | null;
xForwardedTo: string | null;
listUnsubscribe: string | null;
precedence: string | null;
autoSubmitted: string | null;
@@ -85,6 +86,7 @@ const ENVELOPE_HEADERS: [keyof MailEnvelope, string][] = [
['messageId', 'message-id'],
['deliveredTo', 'delivered-to'],
['xOriginalTo', 'x-original-to'],
['xForwardedTo', 'x-forwarded-to'],
['listUnsubscribe', 'list-unsubscribe'],
['precedence', 'precedence'],
['autoSubmitted', 'auto-submitted'],
@@ -7,6 +7,7 @@ import { dbNow } from '../../db/types';
import { UnitOfWork } from '../database/unit-of-work';
import { MailReadService } from '../llm-parse/mail-read.service';
import type { WalkedMail } from '../llm-parse/mail-read.types';
import { addressOf } from './mail-classify';
import { MailIngestAddressService } from './mail-ingest-address.service';
import { MailIngestSettingsService } from './mail-ingest-settings.service';
import { MAIL_INGEST_LIMITS } from './mail-ingest.constants';
@@ -82,7 +83,7 @@ export class MailIngestIntakeService {
const addresses =
envelopeRecipients.length > 0
? await this.addresses.resolveAll(envelopeRecipients)
: await this.headerAddresses(mail.envelope);
: await this.headerAddresses(mail.envelope, transport === 'imap');
if (addresses.length === 0) return { status: 'refused', reason: 'unknown_address' };
const results = await addresses.reduce<Promise<MailIntakeResult[]>>(async (previous, address) => {
const done = await previous;
@@ -111,10 +112,21 @@ export class MailIngestIntakeService {
* writes, then To. The first that names one decides. A delivery header
* without a token names none: Stalwart writes the box itself there, its
* sub-address dropped, and the token then stands only in To.
*
* Gmail automatic forwarding can put a bare address such as
* trek+{token}@example.com in X-Forwarded-To. IMAP tries the topmost value
* of that header when none of the headers above names a live address.
*/
private async headerAddresses(envelope: WalkedMail['envelope']): Promise<MailIngestAddressRow[]> {
private async headerAddresses(
envelope: WalkedMail['envelope'],
useForwardedTo: boolean,
): Promise<MailIngestAddressRow[]> {
const named = await Promise.all(headerRecipients(envelope).map((list) => this.addresses.resolveAll(list)));
return named.find((addresses) => addresses.length > 0) ?? [];
const matched = named.find((addresses) => addresses.length > 0);
if (matched) return matched;
const forwardedTo = useForwardedTo ? addressOf(envelope.xForwardedTo) : null;
return forwardedTo && /^[^\s@]+@[^\s@]+$/.test(forwardedTo) ? this.addresses.resolveAll([forwardedTo]) : [];
}
private async precheck(raw: Buffer): Promise<MailIntakeRefusal | null> {