diff --git a/docs/agent-protocol.md b/docs/agent-protocol.md index 882b9c9..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 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, 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. */ +/** + * 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(); - const at = addr.lastIndexOf('@'); - if (at < 0) return ''; - const domain = addr.slice(at + 1).replace(/[>\s]+$/, ''); - if (!domain) return ''; - try { - return new URL('https://' + domain).hostname; - } catch { - return ''; + // Escapes are rare in real From headers and parse differently between tools. + if (from.includes('\\')) return ''; + 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) return ''; + const addr = (brackets.length ? brackets[0][1] : text).trim(); + 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 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 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 === 'fail') 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 c72d09a..268fa49 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,9 +153,46 @@ 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; - return { id: raw.id, from, subject, body, html, receivedAt, ...(dmarc ? { dmarc } : {}) }; + // 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, + subject, + body, + html, + receivedAt, + ...(dmarc ? { dmarc } : {}), + ...(dmarcFrom ? { dmarcFrom } : {}), + }; +} + +/** + * 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): { 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 {}; + const [authserv, ...methods] = text.split(';').map((s) => s.trim()); + 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 { 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.-]+)$/; + 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 5bf4e22..c40e249 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,9 @@ function entry(over: Partial = {}): CodeEntry { subject: 's', 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, }; } @@ -349,13 +353,53 @@ 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(''); + // 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(''); + } + }); + 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('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(''); diff --git a/tests/gmail.test.ts b/tests/gmail.test.ts index fb36567..ff31cb5 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', () => { @@ -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,48 @@ 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({}); + 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').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').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').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', () => { const raw = parseGmailMessage({ id: 'm2',