diff --git a/server/src/nest/mail-ingest/mail-classify.ts b/server/src/nest/mail-ingest/mail-classify.ts index de7001432..9eabab939 100644 --- a/server/src/nest/mail-ingest/mail-classify.ts +++ b/server/src/nest/mail-ingest/mail-classify.ts @@ -51,17 +51,33 @@ export function classifyMail(mail: WalkedMail): MailClass { /** * Whether the mail came from the person themselves, signed: the From address - * is their account's and the receiving server saw a DKIM or DMARC pass for - * its domain. Only ever a hint shown with the mail (owner decision - * 13.11.2026), never a reason to stop. + * is their account's, and the receiving server's verdict holds a DKIM pass + * signed by its domain or a DMARC pass for it as the From domain. Only a mail + * read from the admin's own IMAP box carries such a verdict, as the topmost + * Authentication-Results, added above whatever the mail brought along. The + * TREK Cloud relay hands a mail on without its server's verdict, and a + * webhook sender or an uploaded file can write any header, so those never + * count. Only ever a hint shown with the mail (owner decision 13.11.2026), + * never a reason to stop. */ -export function senderVerified(mail: WalkedMail, accountEmail: string): boolean { +export function senderVerified(mail: WalkedMail, accountEmail: string, transport: string): boolean { + if (transport !== 'imap') return false; const from = addressOf(mail.envelope.from); if (!from || from !== accountEmail.trim().toLowerCase()) return false; const domain = from.split('@')[1] ?? ''; - const results = (mail.envelope.authenticationResults ?? '').toLowerCase(); - const dkim = new RegExp(`dkim=pass[^;]*header\\.(d|i)=@?${escapeRegExp(domain)}\\b`); - return dkim.test(results) || /dmarc=pass/.test(results); + // The part before the first `;` names the server that judged; each part after it is one method's result. + const results = (mail.envelope.authenticationResults ?? '').toLowerCase().split(';').slice(1); + return results.some((result) => passesFor(result.trim(), domain)); +} + +/** Whether one method's result is a pass that names the From domain itself. */ +function passesFor(result: string, domain: string): boolean { + const property = (name: string) => new RegExp(`(?:^|\\s)${name}=@?([^\\s;]+)`).exec(result)?.[1] ?? null; + if (/^dkim=pass\b/.test(result)) { + return property('header\\.d') === domain || property('header\\.i')?.split('@').pop() === domain; + } + if (/^dmarc=pass\b/.test(result)) return property('header\\.from') === domain; + return false; } /** The bare address of a From header: `Mia ` is `mia@example.com`. */ @@ -69,7 +85,3 @@ export function addressOf(header: string | null): string | null { const match = /<([^>]+)>/.exec(header ?? '') ?? /([^\s<>"]+@[^\s<>"]+)/.exec(header ?? ''); return match ? match[1].trim().toLowerCase() : null; } - -function escapeRegExp(text: string): string { - return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); -} diff --git a/server/src/nest/mail-ingest/mail-ingest-process.service.ts b/server/src/nest/mail-ingest/mail-ingest-process.service.ts index 05a2c47ed..644998981 100644 --- a/server/src/nest/mail-ingest/mail-ingest-process.service.ts +++ b/server/src/nest/mail-ingest/mail-ingest-process.service.ts @@ -129,7 +129,7 @@ export class MailIngestProcessService { await this.keepForInbox(message, null, 'mail_unreadable', now); return; } - const outcome = this.outcomeBase(mail, person, now); + const outcome = this.outcomeBase(mail, person, message.transport, now); const address = message.address_id === null ? null : await this.addresses.findById(message.address_id); if (!address || !(await this.addresses.isLive(address))) { await this.messages.finish(message.id, { ...outcome, status: 'discarded', reason: 'address_invalid' }); @@ -410,10 +410,15 @@ export class MailIngestProcessService { }); } - /** What the mail says about itself, before any booking in it is read. */ - private outcomeBase(mail: WalkedMail, person: MailIngestPersonRow, now: Date): MailIngestMessageOutcome { + /** What the mail says about itself, before any booking in it is read; `transport` is the one it came by. */ + private outcomeBase( + mail: WalkedMail, + person: MailIngestPersonRow, + transport: string, + now: Date, + ): MailIngestMessageOutcome { const hints: MailIngestHint[] = []; - if (!senderVerified(mail, person.email)) hints.push('sender_unverified'); + if (!senderVerified(mail, person.email, transport)) hints.push('sender_unverified'); if (mail.skippedAttachments > 0) hints.push('attachments_skipped'); return { status: 'processed', diff --git a/server/tests/unit/nest/mail-ingest/mail-ingest-helpers.test.ts b/server/tests/unit/nest/mail-ingest/mail-ingest-helpers.test.ts index 8994336bd..e7ddf4396 100644 --- a/server/tests/unit/nest/mail-ingest/mail-ingest-helpers.test.ts +++ b/server/tests/unit/nest/mail-ingest/mail-ingest-helpers.test.ts @@ -62,17 +62,35 @@ describe('classifyMail', () => { }); describe('senderVerified', () => { - const signed = { 'Authentication-Results': 'mx.example.net; dkim=pass header.d=example.com; spf=pass' }; + const verdict = (results: string) => ({ 'Authentication-Results': `mx.example.net; ${results}` }); + const signed = verdict('dkim=pass header.d=example.com; spf=pass'); - it('holds for the person’s own address, signed by its domain', () => { - expect(senderVerified(mail(signed, BOOKING_BODY), 'Mia@Example.com')).toBe(true); + it('holds for the person’s own address, signed by its domain, as the IMAP box’s server judged', () => { + expect(senderVerified(mail(signed, BOOKING_BODY), 'Mia@Example.com', 'imap')).toBe(true); + const dmarc = verdict('dkim=fail; dmarc=pass (p=reject) header.from=example.com'); + expect(senderVerified(mail(dmarc, BOOKING_BODY), 'mia@example.com', 'imap')).toBe(true); }); it('does not hold for another sender or without a signature', () => { - expect(senderVerified(mail(signed, BOOKING_BODY), 'other@example.com')).toBe(false); - expect(senderVerified(mail({}, BOOKING_BODY), 'mia@example.com')).toBe(false); - const forged = { 'Authentication-Results': 'mx.example.net; dkim=pass header.d=evil.test' }; - expect(senderVerified(mail(forged, BOOKING_BODY), 'mia@example.com')).toBe(false); + expect(senderVerified(mail(signed, BOOKING_BODY), 'other@example.com', 'imap')).toBe(false); + expect(senderVerified(mail({}, BOOKING_BODY), 'mia@example.com', 'imap')).toBe(false); + const forged = verdict('dkim=pass header.d=evil.test'); + expect(senderVerified(mail(forged, BOOKING_BODY), 'mia@example.com', 'imap')).toBe(false); + }); + + it('does not hold for a pass that names another domain than the From one', () => { + const lookalike = verdict('dkim=pass header.d=example.com.evil.test'); + expect(senderVerified(mail(lookalike, BOOKING_BODY), 'mia@example.com', 'imap')).toBe(false); + const unaligned = verdict('dmarc=pass header.from=evil.test'); + expect(senderVerified(mail(unaligned, BOOKING_BODY), 'mia@example.com', 'imap')).toBe(false); + const bare = verdict('dmarc=pass'); + expect(senderVerified(mail(bare, BOOKING_BODY), 'mia@example.com', 'imap')).toBe(false); + }); + + it('never holds where the sender could have written the verdict itself', () => { + for (const transport of ['cloud', 'webhook', 'upload']) { + expect(senderVerified(mail(signed, BOOKING_BODY), 'mia@example.com', transport), transport).toBe(false); + } }); it('reads a bare address out of a From header', () => {