From 59bd31395f43b01502d0e94e2014465b814bbac1 Mon Sep 17 00:00:00 2001 From: Mehdi Date: Mon, 16 Nov 2026 02:50:33 -0600 Subject: [PATCH 1/5] Agent sign-in: with all sites on, require a DMARC pass and a plain sender domain --- docs/agent-protocol.md | 2 +- lib/agent.ts | 13 +++++++++---- tests/agent.test.ts | 10 ++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/docs/agent-protocol.md b/docs/agent-protocol.md index 882b9c9..89f12d3 100644 --- a/docs/agent-protocol.md +++ b/docs/agent-protocol.md @@ -83,7 +83,7 @@ From the client side (no reply at all): ## All sites -With `*` in the allowlist, listed sites work as before. A site that is not in the list only gets codes and links mailed from that same site (its registrable domain), and mail that failed Gmail's DMARC check is ignored for it. `pending` follows the same rule, and the tab is checked again just before each fill or open. +With `*` in the allowlist, listed sites work as before. A site that is not in the list only gets codes and links mailed from that same site (its registrable domain), and only mail that passed Gmail's DMARC check counts for it (a failed or missing result is ignored). `pending` follows the same rule, and the tab is checked again just before each fill or open. ## Config file diff --git a/lib/agent.ts b/lib/agent.ts index 5802967..4fc1b25 100644 --- a/lib/agent.ts +++ b/lib/agent.ts @@ -97,17 +97,22 @@ export function senderDomain(from: string): string { const at = addr.lastIndexOf('@'); if (at < 0) return ''; const domain = addr.slice(at + 1).replace(/[>\s]+$/, ''); - if (!domain) return ''; + // A plain domain only: no path, port, query, fragment or other URL parts. + if (!domain || /[\/\\?#:@%\[\]\s]/.test(domain)) return ''; try { - return new URL('https://' + domain).hostname; + const host = new URL('https://' + domain).hostname; + return /^[a-z0-9.-]+$/.test(host) ? host : ''; } catch { return ''; } } -/** Site of the entry's sender; empty when unknown or when the mail failed DMARC. */ +/** + * Site of the entry's sender; empty unless the mail passed DMARC (Gmail's own + * result) and the sender domain is plain. Fails closed. + */ export function entrySite(e: CodeEntry): string { - if (e.dmarc === 'fail') return ''; + if (e.dmarc !== 'pass') return ''; return siteOf(senderDomain(e.from)); } diff --git a/tests/agent.test.ts b/tests/agent.test.ts index 5bf4e22..513d7aa 100644 --- a/tests/agent.test.ts +++ b/tests/agent.test.ts @@ -9,6 +9,7 @@ import { normalizeAllowlist, runAgentCommand, senderDomain, + entrySite, siteOf, urlSite, sanitizeReply, @@ -89,6 +90,7 @@ function entry(over: Partial = {}): CodeEntry { subject: 's', receivedAt: NOW - 30_000, kind: 'code', + dmarc: 'pass', ...over, }; } @@ -356,6 +358,14 @@ describe('siteOf and senderDomain', () => { expect(senderDomain(' a@example.org ')).toBe('example.org'); expect(senderDomain('no address')).toBe(''); expect(senderDomain('a@')).toBe(''); + for (const odd of ['x@good.com/y', 'x@good.com#x', 'x@good.com:80', 'x@good.com?x', 'x@good.com\\y', 'x@go%6fd.com']) { + expect(senderDomain(odd)).toBe(''); + } + }); + it('entrySite needs a DMARC pass', () => { + expect(entrySite(entry({ from: 'a@example.org' }))).toBe('example.org'); + expect(entrySite(entry({ from: 'a@example.org', dmarc: undefined }))).toBe(''); + expect(entrySite(entry({ from: 'a@example.org', dmarc: 'fail' }))).toBe(''); }); it('urlSite requires https', () => { expect(urlSite('http://example.org')).toBe(''); From 2414a5f0de21fa2f9cbb6bcaab7c92fcd70a4bd4 Mon Sep 17 00:00:00 2001 From: Mehdi Date: Mon, 16 Nov 2026 02:52:03 -0600 Subject: [PATCH 2/5] Read the DMARC result only from a method segment of Gmail's own Authentication-Results header --- lib/gmail.ts | 23 +++++++++++++++++++++-- tests/gmail.test.ts | 17 ++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/lib/gmail.ts b/lib/gmail.ts index c72d09a..9d2ee27 100644 --- a/lib/gmail.ts +++ b/lib/gmail.ts @@ -151,11 +151,30 @@ export function parseGmailMessage(raw: GmailMessage): ParsedMessage { const body = plain ? decodeBody(plain) : html ? htmlToText(html) : raw.snippet ?? ''; const receivedAt = raw.internalDate ? Number(raw.internalDate) : Date.parse(header(headers, 'Date')) || Date.now(); // The first Authentication-Results header is the one Gmail itself added. - const auth = header(headers, 'Authentication-Results').toLowerCase(); - const dmarc = /\bdmarc=fail\b/.test(auth) ? 'fail' : /\bdmarc=pass\b/.test(auth) ? 'pass' : undefined; + const dmarc = dmarcResult(header(headers, 'Authentication-Results')); return { id: raw.id, from, subject, body, html, receivedAt, ...(dmarc ? { dmarc } : {}) }; } +/** + * DMARC verdict from Gmail's own Authentication-Results header (RFC 8601). + * Only a result at the start of a ';'-separated method segment counts, after + * quoted strings and comments (which can echo sender-chosen text) are removed, + * and only when the header was added by mx.google.com. Anything else: undefined. + */ +export function dmarcResult(value: string): 'pass' | 'fail' | undefined { + let text = value.toLowerCase().replace(/"(?:[^"\\]|\\.)*"/g, '""'); + for (let prev = ''; prev !== text; ) { + prev = text; + text = text.replace(/\([^()]*\)/g, ' '); + } + if (/["()]/.test(text)) return undefined; + const [authserv, ...methods] = text.split(';').map((s) => s.trim()); + if (authserv.split(/\s+/)[0] !== 'mx.google.com') return undefined; + const results = methods.map((m) => /^dmarc\s*=\s*([a-z]+)\b/.exec(m)?.[1]).filter(Boolean); + if (results.includes('fail')) return 'fail'; + return results.length > 0 && results.every((r) => r === 'pass') ? 'pass' : undefined; +} + function header(headers: GmailHeader[], name: string): string { return headers.find((item) => item.name.toLowerCase() === name.toLowerCase())?.value ?? ''; } diff --git a/tests/gmail.test.ts b/tests/gmail.test.ts index fb36567..78c2a85 100644 --- a/tests/gmail.test.ts +++ b/tests/gmail.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it, vi } from 'vitest'; -import { decodeBody, GmailApiError, gmailMessageUrl, htmlToText, parseGmailMessage, pollGmail } from '~/lib/gmail'; +import { decodeBody, dmarcResult, GmailApiError, gmailMessageUrl, htmlToText, parseGmailMessage, pollGmail } from '~/lib/gmail'; describe('gmail parsing', () => { it('decodes a base64url plain-text part', () => { @@ -37,6 +37,21 @@ describe('gmail parsing', () => { expect(mk(['mx.google.com; spf=pass'])).toBeUndefined(); }); + it('only trusts a dmarc result that starts a method segment of Gmail header', () => { + expect(dmarcResult('mx.google.com; dkim=pass header.i=@github.com; spf=pass; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=github.com')).toBe('pass'); + expect(dmarcResult('mx.google.com;\r\n dmarc=pass (p=NONE) header.from=a.com')).toBe('pass'); + // Sender-chosen text echoed in a comment or a property value does not count. + expect(dmarcResult('mx.google.com; spf=pass (google.com: domain of dmarc=pass@evil.test designates 1.2.3.4) smtp.mailfrom=dmarc=pass@evil.test')).toBeUndefined(); + expect(dmarcResult('mx.google.com; spf=pass smtp.mailfrom="x;dmarc=pass"@evil.test')).toBeUndefined(); + expect(dmarcResult('mx.google.com; spf=pass (a (nested; dmarc=pass) b) smtp.mailfrom=x@evil.test')).toBeUndefined(); + expect(dmarcResult('mx.google.com; spf=pass (unclosed; dmarc=pass')).toBeUndefined(); + // Another server's header, or no header. + expect(dmarcResult('evil.test; dmarc=pass')).toBeUndefined(); + expect(dmarcResult('')).toBeUndefined(); + expect(dmarcResult('mx.google.com; dmarc=fail (p=REJECT) header.from=a.com')).toBe('fail'); + expect(dmarcResult('mx.google.com; dmarc=none header.from=a.com')).toBeUndefined(); + }); + it('keeps the html part when a plain part is also present', () => { const raw = parseGmailMessage({ id: 'm2', From 8f9a9c082187d2827c9cf809927d3ac1d656736f Mon Sep 17 00:00:00 2001 From: Mehdi Date: Mon, 16 Nov 2026 02:54:16 -0600 Subject: [PATCH 3/5] Read the From header as one address after removing comments and quoted strings; stricter DMARC segment --- lib/agent.ts | 16 +++++++++++++--- lib/gmail.ts | 12 +++++++----- tests/agent.test.ts | 14 ++++++++++++++ tests/gmail.test.ts | 2 ++ 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/lib/agent.ts b/lib/agent.ts index 4fc1b25..dba1830 100644 --- a/lib/agent.ts +++ b/lib/agent.ts @@ -90,10 +90,20 @@ export function siteOf(host: string): string { } } -/** Domain of the address in a From header: after the last '@' of the last <...>. */ +/** + * Domain of the single address in a From header. Quoted strings and comments + * are removed first; more than one address, or anything unbalanced, gives ''. + */ export function senderDomain(from: string): string { - const brackets = [...from.matchAll(/<([^<>]*)>/g)]; - const addr = (brackets.length ? brackets[brackets.length - 1][1] : from).trim(); + let text = from.replace(/"(?:[^"\\]|\\.)*"/g, ' '); + for (let prev = ''; prev !== text; ) { + prev = text; + text = text.replace(/\([^()]*\)/g, ' '); + } + if (/["(),;]/.test(text)) return ''; + const brackets = [...text.matchAll(/<([^<>]*)>/g)]; + if (brackets.length > 1 || (brackets.length === 0 && /[<>]/.test(text))) return ''; + const addr = (brackets.length ? brackets[0][1] : text).trim(); const at = addr.lastIndexOf('@'); if (at < 0) return ''; const domain = addr.slice(at + 1).replace(/[>\s]+$/, ''); diff --git a/lib/gmail.ts b/lib/gmail.ts index 9d2ee27..afe6bb2 100644 --- a/lib/gmail.ts +++ b/lib/gmail.ts @@ -162,17 +162,19 @@ export function parseGmailMessage(raw: GmailMessage): ParsedMessage { * and only when the header was added by mx.google.com. Anything else: undefined. */ export function dmarcResult(value: string): 'pass' | 'fail' | undefined { - let text = value.toLowerCase().replace(/"(?:[^"\\]|\\.)*"/g, '""'); + let text = value.toLowerCase().replace(/"(?:[^"\\]|\\.)*"/g, ' '); for (let prev = ''; prev !== text; ) { prev = text; text = text.replace(/\([^()]*\)/g, ' '); } if (/["()]/.test(text)) return undefined; const [authserv, ...methods] = text.split(';').map((s) => s.trim()); - if (authserv.split(/\s+/)[0] !== 'mx.google.com') return undefined; - const results = methods.map((m) => /^dmarc\s*=\s*([a-z]+)\b/.exec(m)?.[1]).filter(Boolean); - if (results.includes('fail')) return 'fail'; - return results.length > 0 && results.every((r) => r === 'pass') ? 'pass' : undefined; + if (authserv !== 'mx.google.com') return undefined; + const dmarcs = methods.filter((m) => /^dmarc\s*=/.test(m)); + if (dmarcs.some((m) => /^dmarc\s*=\s*fail\b/.test(m))) return 'fail'; + // A pass segment holds nothing but the result and header.from=. + const strict = /^dmarc\s*=\s*pass(\s+header\.from\s*=\s*[a-z0-9.-]+)?$/; + return dmarcs.length > 0 && dmarcs.every((m) => strict.test(m)) ? 'pass' : undefined; } function header(headers: GmailHeader[], name: string): string { diff --git a/tests/agent.test.ts b/tests/agent.test.ts index 513d7aa..b550d13 100644 --- a/tests/agent.test.ts +++ b/tests/agent.test.ts @@ -358,6 +358,20 @@ describe('siteOf and senderDomain', () => { expect(senderDomain(' a@example.org ')).toBe('example.org'); expect(senderDomain('no address')).toBe(''); expect(senderDomain('a@')).toBe(''); + // An address inside a comment is not the sender. + expect(senderDomain('"X" (note )')).toBe('evil.test'); + expect(senderDomain(' ()')).toBe('evil.test'); + for (const odd of [ + 'A , B ', + 'a@evil.test, b@good.com', + ' ', + 'X ', + ]) { + expect(senderDomain(odd)).toBe(''); + } + expect(senderDomain('"GitHub, Inc." ')).toBe('github.com'); + expect(senderDomain('GitHub (bot) ')).toBe('github.com'); for (const odd of ['x@good.com/y', 'x@good.com#x', 'x@good.com:80', 'x@good.com?x', 'x@good.com\\y', 'x@go%6fd.com']) { expect(senderDomain(odd)).toBe(''); } diff --git a/tests/gmail.test.ts b/tests/gmail.test.ts index 78c2a85..d2a80ab 100644 --- a/tests/gmail.test.ts +++ b/tests/gmail.test.ts @@ -50,6 +50,8 @@ describe('gmail parsing', () => { expect(dmarcResult('')).toBeUndefined(); expect(dmarcResult('mx.google.com; dmarc=fail (p=REJECT) header.from=a.com')).toBe('fail'); expect(dmarcResult('mx.google.com; dmarc=none header.from=a.com')).toBeUndefined(); + expect(dmarcResult('mx.google.com; spf=pass smtp.mailfrom=a;dmarc=pass@e.test')).toBeUndefined(); + expect(dmarcResult('mx.google.com 1; dmarc=pass header.from=a.com')).toBeUndefined(); }); it('keeps the html part when a plain part is also present', () => { From 0494a718b16c8d994d2afdf174e0713a309aa0ad Mon Sep 17 00:00:00 2001 From: Mehdi Date: Mon, 16 Nov 2026 02:57:37 -0600 Subject: [PATCH 4/5] Match the From address against the domain Gmail's DMARC check evaluated; ASCII-only plain sender domain --- docs/agent-protocol.md | 2 +- entrypoints/background.ts | 1 + lib/agent.ts | 31 +++++++++++++---------------- lib/gmail.ts | 42 +++++++++++++++++++++++++++------------ lib/types.ts | 2 ++ tests/agent.test.ts | 24 ++++++++++++++++++++-- tests/gmail.test.ts | 32 ++++++++++++++++++----------- 7 files changed, 89 insertions(+), 45 deletions(-) diff --git a/docs/agent-protocol.md b/docs/agent-protocol.md index 89f12d3..79ad421 100644 --- a/docs/agent-protocol.md +++ b/docs/agent-protocol.md @@ -83,7 +83,7 @@ From the client side (no reply at all): ## All sites -With `*` in the allowlist, listed sites work as before. A site that is not in the list only gets codes and links mailed from that same site (its registrable domain), and only mail that passed Gmail's DMARC check counts for it (a failed or missing result is ignored). `pending` follows the same rule, and the tab is checked again just before each fill or open. +With `*` in the allowlist, listed sites work as before. A site that is not in the list only gets codes and links mailed from that same site (its registrable domain), and only mail that passed Gmail's DMARC check counts for it, with the From address and the domain Gmail checked on the same site (a failed or missing result, or a From header that is not one plain address, is ignored). `pending` follows the same rule, and the tab is checked again just before each fill or open. ## Config file diff --git a/entrypoints/background.ts b/entrypoints/background.ts index 27c1793..b7b25e9 100644 --- a/entrypoints/background.ts +++ b/entrypoints/background.ts @@ -557,6 +557,7 @@ async function pollOne(state: PersistedState, account: Account): Promise]*)>/g)]; - if (brackets.length > 1 || (brackets.length === 0 && /[<>]/.test(text))) return ''; + if (brackets.length > 1) return ''; const addr = (brackets.length ? brackets[0][1] : text).trim(); - const at = addr.lastIndexOf('@'); - if (at < 0) return ''; - const domain = addr.slice(at + 1).replace(/[>\s]+$/, ''); - // A plain domain only: no path, port, query, fragment or other URL parts. - if (!domain || /[\/\\?#:@%\[\]\s]/.test(domain)) return ''; - try { - const host = new URL('https://' + domain).hostname; - return /^[a-z0-9.-]+$/.test(host) ? host : ''; - } catch { - return ''; - } + if (!brackets.length && /[<>]/.test(addr)) return ''; + // One local part, one '@', and a plain ASCII domain (punycode for IDN). + const m = /^[^\s@<>]+@([A-Za-z0-9.-]+)$/.exec(addr); + return m ? m[1].toLowerCase() : ''; } /** * Site of the entry's sender; empty unless the mail passed DMARC (Gmail's own - * result) and the sender domain is plain. Fails closed. + * result) and the domain Gmail evaluated is on the same site as the From + * address read here. Fails closed. */ export function entrySite(e: CodeEntry): string { - if (e.dmarc !== 'pass') return ''; - return siteOf(senderDomain(e.from)); + if (e.dmarc !== 'pass' || !e.dmarcFrom) return ''; + const site = siteOf(senderDomain(e.from)); + return site && siteOf(e.dmarcFrom) === site ? site : ''; } /** Site of an https URL; empty for anything else. */ diff --git a/lib/gmail.ts b/lib/gmail.ts index afe6bb2..6d2a29e 100644 --- a/lib/gmail.ts +++ b/lib/gmail.ts @@ -32,6 +32,8 @@ export interface ParsedMessage { html: string; receivedAt: number; dmarc?: 'pass' | 'fail'; + /** On a DMARC pass: the From domain Gmail evaluated (header.from). */ + dmarcFrom?: string; } interface GmailHeader { @@ -151,30 +153,44 @@ export function parseGmailMessage(raw: GmailMessage): ParsedMessage { const body = plain ? decodeBody(plain) : html ? htmlToText(html) : raw.snippet ?? ''; const receivedAt = raw.internalDate ? Number(raw.internalDate) : Date.parse(header(headers, 'Date')) || Date.now(); // The first Authentication-Results header is the one Gmail itself added. - const dmarc = dmarcResult(header(headers, 'Authentication-Results')); - return { id: raw.id, from, subject, body, html, receivedAt, ...(dmarc ? { dmarc } : {}) }; + const { dmarc, dmarcFrom } = dmarcResult(header(headers, 'Authentication-Results')); + return { + id: raw.id, + from, + subject, + body, + html, + receivedAt, + ...(dmarc ? { dmarc } : {}), + ...(dmarcFrom ? { dmarcFrom } : {}), + }; } /** - * DMARC verdict from Gmail's own Authentication-Results header (RFC 8601). - * Only a result at the start of a ';'-separated method segment counts, after - * quoted strings and comments (which can echo sender-chosen text) are removed, - * and only when the header was added by mx.google.com. Anything else: undefined. + * DMARC verdict from Gmail's own Authentication-Results header (RFC 8601), + * plus, on a pass, the header.from domain Gmail evaluated. Only a result at the + * start of a ';'-separated method segment counts, after quoted strings and + * comments (which can echo sender-chosen text) are removed, and only when the + * header was added by mx.google.com. Anything unusual gives no verdict. */ -export function dmarcResult(value: string): 'pass' | 'fail' | undefined { - let text = value.toLowerCase().replace(/"(?:[^"\\]|\\.)*"/g, ' '); +export function dmarcResult(value: string): { dmarc?: 'pass' | 'fail'; dmarcFrom?: string } { + let text = value.toLowerCase(); + if (text.includes('\\')) return {}; + text = text.replace(/"[^"]*"/g, ' '); for (let prev = ''; prev !== text; ) { prev = text; text = text.replace(/\([^()]*\)/g, ' '); } - if (/["()]/.test(text)) return undefined; + if (/["()]/.test(text)) return {}; const [authserv, ...methods] = text.split(';').map((s) => s.trim()); - if (authserv !== 'mx.google.com') return undefined; + if (authserv !== 'mx.google.com') return {}; const dmarcs = methods.filter((m) => /^dmarc\s*=/.test(m)); - if (dmarcs.some((m) => /^dmarc\s*=\s*fail\b/.test(m))) return 'fail'; + if (dmarcs.some((m) => /^dmarc\s*=\s*fail\b/.test(m))) return { dmarc: 'fail' }; // A pass segment holds nothing but the result and header.from=. - const strict = /^dmarc\s*=\s*pass(\s+header\.from\s*=\s*[a-z0-9.-]+)?$/; - return dmarcs.length > 0 && dmarcs.every((m) => strict.test(m)) ? 'pass' : undefined; + const strict = /^dmarc\s*=\s*pass\s+header\.from\s*=\s*([a-z0-9.-]+)$/; + const domains = dmarcs.map((m) => strict.exec(m)?.[1]); + if (domains.length !== 1 || !domains[0]) return {}; + return { dmarc: 'pass', dmarcFrom: domains[0] }; } function header(headers: GmailHeader[], name: string): string { diff --git a/lib/types.ts b/lib/types.ts index 5ed1e98..081c50a 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -30,6 +30,8 @@ export interface CodeEntry { url?: string; /** Gmail's own DMARC verdict for the message, when it reported one. */ dmarc?: 'pass' | 'fail'; + /** On a DMARC pass: the From domain Gmail evaluated (header.from). */ + dmarcFrom?: string; } export type PopupShows = 'all' | 'codes' | 'links'; diff --git a/tests/agent.test.ts b/tests/agent.test.ts index b550d13..c40e249 100644 --- a/tests/agent.test.ts +++ b/tests/agent.test.ts @@ -91,6 +91,8 @@ function entry(over: Partial = {}): CodeEntry { receivedAt: NOW - 30_000, kind: 'code', dmarc: 'pass', + // Gmail's header.from: by default the From domain, as in real mail. + dmarcFrom: (over.from ?? 'noreply@github.com').split('@').pop()!.replace(/[>\s]/g, '').toLowerCase(), ...over, }; } @@ -351,10 +353,23 @@ describe('siteOf and senderDomain', () => { it('fails closed', () => { for (const v of ['', 'vercel.app', 'com', 'a b', '%%%', 'localhost', '127.0.0.1']) expect(siteOf(v)).toBe(''); }); - it('reads the domain after the last @ inside the last <...>', () => { + it('reads the domain of the one address in the From header', () => { expect(senderDomain('GitHub ')).toBe('github.com'); expect(senderDomain('"noreply@github.com" ')).toBe('evil.com'); - expect(senderDomain('a@b@Example.org')).toBe('example.org'); + expect(senderDomain('a@b@Example.org')).toBe(''); + expect(senderDomain('A@Example.ORG')).toBe('example.org'); + for (const odd of [ + '(c).x@attacker.test""v@victim.test', + 'a@vic­tim.test', + 'a@victim。test', + 'a@victim.test', + '<@route:a@victim.test>', + 'grp: a@victim.test;', + '"a\\"b" ', + '(\\) ) ', + ]) { + expect(senderDomain(odd)).toBe(''); + } expect(senderDomain(' a@example.org ')).toBe('example.org'); expect(senderDomain('no address')).toBe(''); expect(senderDomain('a@')).toBe(''); @@ -381,6 +396,11 @@ describe('siteOf and senderDomain', () => { expect(entrySite(entry({ from: 'a@example.org', dmarc: undefined }))).toBe(''); expect(entrySite(entry({ from: 'a@example.org', dmarc: 'fail' }))).toBe(''); }); + it('entrySite needs the domain Gmail evaluated to be on the same site', () => { + expect(entrySite(entry({ from: 'a@mail.example.org', dmarcFrom: 'example.org' }))).toBe('example.org'); + expect(entrySite(entry({ from: 'a@example.org', dmarcFrom: 'attacker.test' }))).toBe(''); + expect(entrySite(entry({ from: 'a@example.org', dmarcFrom: undefined }))).toBe(''); + }); it('urlSite requires https', () => { expect(urlSite('http://example.org')).toBe(''); expect(urlSite('https://a.example.org/x')).toBe('example.org'); diff --git a/tests/gmail.test.ts b/tests/gmail.test.ts index d2a80ab..67792aa 100644 --- a/tests/gmail.test.ts +++ b/tests/gmail.test.ts @@ -37,21 +37,29 @@ describe('gmail parsing', () => { expect(mk(['mx.google.com; spf=pass'])).toBeUndefined(); }); + it('keeps the header.from domain Gmail evaluated, and needs exactly one', () => { + expect(dmarcResult('mx.google.com; dmarc=pass (p=REJECT) header.from=GitHub.com')).toEqual({ dmarc: 'pass', dmarcFrom: 'github.com' }); + expect(dmarcResult('mx.google.com; dmarc=pass (p=NONE)')).toEqual({}); + expect(dmarcResult('mx.google.com; dmarc=pass header.from=a.com; dmarc=pass header.from=b.com')).toEqual({}); + expect(dmarcResult('mx.google.com; dmarc=pass header.from=xn--bcher-kva.example')).toEqual({ dmarc: 'pass', dmarcFrom: 'xn--bcher-kva.example' }); + expect(dmarcResult('mx.google.com; spf=pass smtp.mailfrom="a\\"b"; dmarc=pass header.from=a.com')).toEqual({}); + }); + it('only trusts a dmarc result that starts a method segment of Gmail header', () => { - expect(dmarcResult('mx.google.com; dkim=pass header.i=@github.com; spf=pass; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=github.com')).toBe('pass'); - expect(dmarcResult('mx.google.com;\r\n dmarc=pass (p=NONE) header.from=a.com')).toBe('pass'); + expect(dmarcResult('mx.google.com; dkim=pass header.i=@github.com; spf=pass; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=github.com').dmarc).toBe('pass'); + expect(dmarcResult('mx.google.com;\r\n dmarc=pass (p=NONE) header.from=a.com').dmarc).toBe('pass'); // Sender-chosen text echoed in a comment or a property value does not count. - expect(dmarcResult('mx.google.com; spf=pass (google.com: domain of dmarc=pass@evil.test designates 1.2.3.4) smtp.mailfrom=dmarc=pass@evil.test')).toBeUndefined(); - expect(dmarcResult('mx.google.com; spf=pass smtp.mailfrom="x;dmarc=pass"@evil.test')).toBeUndefined(); - expect(dmarcResult('mx.google.com; spf=pass (a (nested; dmarc=pass) b) smtp.mailfrom=x@evil.test')).toBeUndefined(); - expect(dmarcResult('mx.google.com; spf=pass (unclosed; dmarc=pass')).toBeUndefined(); + expect(dmarcResult('mx.google.com; spf=pass (google.com: domain of dmarc=pass@evil.test designates 1.2.3.4) smtp.mailfrom=dmarc=pass@evil.test').dmarc).toBeUndefined(); + expect(dmarcResult('mx.google.com; spf=pass smtp.mailfrom="x;dmarc=pass"@evil.test').dmarc).toBeUndefined(); + expect(dmarcResult('mx.google.com; spf=pass (a (nested; dmarc=pass) b) smtp.mailfrom=x@evil.test').dmarc).toBeUndefined(); + expect(dmarcResult('mx.google.com; spf=pass (unclosed; dmarc=pass').dmarc).toBeUndefined(); // Another server's header, or no header. - expect(dmarcResult('evil.test; dmarc=pass')).toBeUndefined(); - expect(dmarcResult('')).toBeUndefined(); - expect(dmarcResult('mx.google.com; dmarc=fail (p=REJECT) header.from=a.com')).toBe('fail'); - expect(dmarcResult('mx.google.com; dmarc=none header.from=a.com')).toBeUndefined(); - expect(dmarcResult('mx.google.com; spf=pass smtp.mailfrom=a;dmarc=pass@e.test')).toBeUndefined(); - expect(dmarcResult('mx.google.com 1; dmarc=pass header.from=a.com')).toBeUndefined(); + expect(dmarcResult('evil.test; dmarc=pass').dmarc).toBeUndefined(); + expect(dmarcResult('').dmarc).toBeUndefined(); + expect(dmarcResult('mx.google.com; dmarc=fail (p=REJECT) header.from=a.com').dmarc).toBe('fail'); + expect(dmarcResult('mx.google.com; dmarc=none header.from=a.com').dmarc).toBeUndefined(); + expect(dmarcResult('mx.google.com; spf=pass smtp.mailfrom=a;dmarc=pass@e.test').dmarc).toBeUndefined(); + expect(dmarcResult('mx.google.com 1; dmarc=pass header.from=a.com').dmarc).toBeUndefined(); }); it('keeps the html part when a plain part is also present', () => { From 2129f52592a12a730195fdf002c7b1ff834c9355 Mon Sep 17 00:00:00 2001 From: Mehdi Date: Mon, 16 Nov 2026 02:59:17 -0600 Subject: [PATCH 5/5] No DMARC verdict for a message with two From headers --- lib/gmail.ts | 4 +++- tests/gmail.test.ts | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/gmail.ts b/lib/gmail.ts index 6d2a29e..268fa49 100644 --- a/lib/gmail.ts +++ b/lib/gmail.ts @@ -153,7 +153,9 @@ export function parseGmailMessage(raw: GmailMessage): ParsedMessage { const body = plain ? decodeBody(plain) : html ? htmlToText(html) : raw.snippet ?? ''; const receivedAt = raw.internalDate ? Number(raw.internalDate) : Date.parse(header(headers, 'Date')) || Date.now(); // The first Authentication-Results header is the one Gmail itself added. - const { dmarc, dmarcFrom } = dmarcResult(header(headers, 'Authentication-Results')); + // With more than one From header it is unclear which one DMARC evaluated: no verdict. + const fromCount = headers.filter((h) => h.name.toLowerCase() === 'from').length; + const { dmarc, dmarcFrom } = fromCount === 1 ? dmarcResult(header(headers, 'Authentication-Results')) : {}; return { id: raw.id, from, diff --git a/tests/gmail.test.ts b/tests/gmail.test.ts index 67792aa..ff31cb5 100644 --- a/tests/gmail.test.ts +++ b/tests/gmail.test.ts @@ -27,7 +27,7 @@ describe('gmail parsing', () => { parseGmailMessage({ id: 'd', payload: { - headers: values.map((value) => ({ name: 'Authentication-Results', value })), + headers: [{ name: 'From', value: 'a@a.com' }, ...values.map((value) => ({ name: 'Authentication-Results', value }))], mimeType: 'text/plain', body: { data: encode('x') }, }, @@ -37,6 +37,23 @@ describe('gmail parsing', () => { expect(mk(['mx.google.com; spf=pass'])).toBeUndefined(); }); + it('gives no DMARC verdict when the message has two From headers', () => { + const msg = parseGmailMessage({ + id: 'd2', + payload: { + headers: [ + { name: 'From', value: 'a@a.com' }, + { name: 'From', value: 'b@b.com' }, + { name: 'Authentication-Results', value: 'mx.google.com; dmarc=pass header.from=a.com' }, + ], + mimeType: 'text/plain', + body: { data: encode('x') }, + }, + }); + expect(msg.dmarc).toBeUndefined(); + expect(msg.dmarcFrom).toBeUndefined(); + }); + it('keeps the header.from domain Gmail evaluated, and needs exactly one', () => { expect(dmarcResult('mx.google.com; dmarc=pass (p=REJECT) header.from=GitHub.com')).toEqual({ dmarc: 'pass', dmarcFrom: 'github.com' }); expect(dmarcResult('mx.google.com; dmarc=pass (p=NONE)')).toEqual({});