fix(mail-ingest): read transfer bookings instead of filing them as forwarding confirmations

A forwarding subject now counts only from Gmail or Microsoft or together with a code in the body, so 'Votre transfert est confirmé' is read as a booking. The subject and From header are also matched in linear time.
This commit is contained in:
Maurice
2026-11-23 14:59:06 +01:00
parent 327539df14
commit 3b65da7626
2 changed files with 44 additions and 6 deletions
+15 -6
View File
@@ -12,8 +12,8 @@ type MailClass =
| { kind: 'drop'; reason: 'newsletter' | 'auto_reply' | 'empty_mail' };
const FORWARDING_SENDERS = /forwarding-noreply@google\.com|@accountprotection\.microsoft\.com|@microsoft\.com/i;
const FORWARDING_SUBJECT =
/(forwarding|weiterleitung|transfert|reenv[ií]o|inoltro|doorsturen).*(confirm|bestätig|verif|conferma|bevestig)/i;
const FORWARDING_WORD = /forwarding|weiterleitung|transfert|reenv[ií]o|inoltro|doorsturen/i;
const CONFIRM_WORD = /confirm|bestätig|verif|conferma|bevestig/i;
const CODE_RE = /(?:code|código|codice|kód)\D{0,40}?(\d{6,10})\b/i;
const AUTO_REPLY_SUBJECT =
/^(automatic reply|auto(matische)? ?(reply|antwort)|out of (the )?office|abwesenheit|r[ée]ponse automatique|risposta automatica|respuesta autom[áa]tica)/i;
@@ -30,9 +30,12 @@ const EMPTY_BODY_CHARS = 20;
export function classifyMail(mail: WalkedMail): MailClass {
const { envelope } = mail;
const subject = mail.subject ?? '';
const fromProvider = FORWARDING_SENDERS.test(envelope.from ?? '') && CODE_RE.test(mail.body);
if (FORWARDING_SUBJECT.test(subject) || fromProvider) {
return { kind: 'forwarding_confirmation', code: CODE_RE.exec(mail.body)?.[1] ?? null };
const code = CODE_RE.exec(mail.body)?.[1] ?? null;
// A subject alone is no proof: "Votre transfert aéroport est confirmé" is a booking.
const fromProvider = FORWARDING_SENDERS.test(envelope.from ?? '');
const subjectSays = asksToConfirmForwarding(subject);
if (fromProvider ? subjectSays || code !== null : subjectSays && code !== null) {
return { kind: 'forwarding_confirmation', code };
}
const autoSubmitted = (envelope.autoSubmitted ?? '').trim().toLowerCase();
const precedence = (envelope.precedence ?? '').trim().toLowerCase();
@@ -49,6 +52,12 @@ export function classifyMail(mail: WalkedMail): MailClass {
return { kind: 'read' };
}
/** A forwarding word with a confirm word after it, found without rescanning the subject from every forwarding word. */
function asksToConfirmForwarding(subject: string): boolean {
const word = FORWARDING_WORD.exec(subject);
return word !== null && CONFIRM_WORD.test(subject.slice(word.index + word[0].length));
}
/**
* Whether the mail came from the person themselves, signed: the From address
* is their account's, and the receiving server's verdict holds a DKIM pass
@@ -82,6 +91,6 @@ function passesFor(result: string, domain: string): boolean {
/** The bare address of a From header: `Mia <mia@example.com>` is `mia@example.com`. */
export function addressOf(header: string | null): string | null {
const match = /<([^>]+)>/.exec(header ?? '') ?? /([^\s<>"]+@[^\s<>"]+)/.exec(header ?? '');
const match = /<([^<>]+)>/.exec(header ?? '') ?? /(?<![^\s<>"])([^\s<>"]+@[^\s<>"]+)/.exec(header ?? '');
return match ? match[1].trim().toLowerCase() : null;
}
@@ -34,6 +34,34 @@ describe('classifyMail', () => {
'Confirmation code: 612345678. To allow forwarding, click the link.',
);
expect(classifyMail(gmail)).toEqual({ kind: 'forwarding_confirmation', code: '612345678' });
const relayed = mail({ Subject: 'Mail forwarding verification' }, 'Your code: 612345678, valid for 24 hours.');
expect(classifyMail(relayed)).toEqual({ kind: 'forwarding_confirmation', code: '612345678' });
const linkOnly = mail(
{ From: 'forwarding-noreply@google.com', Subject: 'Gmail Forwarding Confirmation' },
'Click the link below to allow forwarding to this address.',
);
expect(classifyMail(linkOnly)).toEqual({ kind: 'forwarding_confirmation', code: null });
});
it('reads a French transfer booking whose subject only sounds like a forwarding confirmation', () => {
const body = 'Votre transfert Orly vers Paris le 2030-03-02 à 10:00, réservation TR-4821.';
for (const subject of [
'Votre transfert aéroport est confirmé',
'Transfert Orly Paris : confirmation de réservation',
]) {
expect(classifyMail(mail({ Subject: subject }, body)), subject).toEqual({ kind: 'read' });
}
});
it('reads a long subject and From header in linear time', () => {
const started = Date.now();
const long = mail(
{ From: `${'<'.repeat(60_000)} ${'a'.repeat(60_000)}`, Subject: 'forwarding '.repeat(20_000) },
BOOKING_BODY,
);
expect(classifyMail(long)).toEqual({ kind: 'read' });
expect(addressOf(long.envelope.from)).toBeNull();
expect(Date.now() - started).toBeLessThan(1000);
});
it('drops an automatic reply, but not a confirmation a booking system generated', () => {
@@ -96,6 +124,7 @@ describe('senderVerified', () => {
it('reads a bare address out of a From header', () => {
expect(addressOf('Mia <MIA@example.com>')).toBe('mia@example.com');
expect(addressOf('mia@example.com')).toBe('mia@example.com');
expect(addressOf('"Mia, Travel" mia@example.com')).toBe('mia@example.com');
expect(addressOf(null)).toBeNull();
});
});