Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/agent-protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions entrypoints/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ async function pollOne(state: PersistedState, account: Account): Promise<CodeEnt
kind: code ? 'code' : 'link',
...(link ? { url: link.url } : {}),
...(message.dmarc ? { dmarc: message.dmarc } : {}),
...(message.dmarcFrom ? { dmarcFrom: message.dmarcFrom } : {}),
});
}
return found;
Expand Down
40 changes: 26 additions & 14 deletions lib/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,37 @@ 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();
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. */
Expand Down
45 changes: 42 additions & 3 deletions lib/gmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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=<plain domain>.
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 {
Expand Down
2 changes: 2 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
48 changes: 46 additions & 2 deletions tests/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
normalizeAllowlist,
runAgentCommand,
senderDomain,
entrySite,
siteOf,
urlSite,
sanitizeReply,
Expand Down Expand Up @@ -89,6 +90,9 @@ function entry(over: Partial<CodeEntry> = {}): 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,
};
}
Expand Down Expand Up @@ -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 <noreply@github.com>')).toBe('github.com');
expect(senderDomain('"noreply@github.com" <x@evil.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" <a@victim.test>',
'(\\) <a@x.test>) <b@victim.test>',
]) {
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" <a@evil.test> (note <b@good.com>)')).toBe('evil.test');
expect(senderDomain('<a@evil.test> (<b@good.com>)')).toBe('evil.test');
for (const odd of [
'A <a@evil.test>, B <b@good.com>',
'a@evil.test, b@good.com',
'<a@evil.test> <b@good.com>',
'X <a@evil.test',
'"unclosed <a@good.com>',
]) {
expect(senderDomain(odd)).toBe('');
}
expect(senderDomain('"GitHub, Inc." <noreply@github.com>')).toBe('github.com');
expect(senderDomain('GitHub (bot) <noreply@github.com>')).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('');
Expand Down
46 changes: 44 additions & 2 deletions tests/gmail.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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') },
},
Expand All @@ -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',
Expand Down
Loading