diff --git a/docs/agent-protocol.md b/docs/agent-protocol.md index 2bcc6fb..ded33dd 100644 --- a/docs/agent-protocol.md +++ b/docs/agent-protocol.md @@ -58,12 +58,12 @@ From the extension: | `tab id required` | `args[0]` is missing or not a non-negative integer | | `rate limit reached` | More than 10 requests in a rolling 10 min (`status` is not counted) | | `tab not found` | No tab with that id | -| `site is not allowlisted` | The tab's host is not in the allowlist (exact host or subdomain, https only) | +| `site is not allowlisted` | The tab's host is not in the allowlist (exact host or subdomain, https only), or the tab moved to such a site while waiting | | `no fresh code` | `fill_code` without wait: no unused code from the last 10 min | | `timed out waiting for a code` | `fill_code` with wait: none arrived in time | | `no code field filled on this tab` | A code exists but no allowlisted frame had a field that took it | | `no fresh sign-in link` | `open_link`: no unused link from the last 10 min | -| `link site is not allowlisted` | The link's own host is not in the allowlist | +| `link site is not allowlisted` | The link's own host is not in the allowlist (with all sites on: not on the sender's own site) | | any other text | An exception inside the extension (its message) | From the host: @@ -80,3 +80,7 @@ From the client side (no reply at all): | Socket file missing (ENOENT) | Host not running: Agent sign-in is off, the permission is not granted, `native/install.sh` was not run, or the browser is closed | | Connection refused (ECONNREFUSED) | Stale socket from a host that crashed; the browser will start a new host when it reconnects | | Empty reply / closed early | Host exited mid-request | + +## 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. diff --git a/entrypoints/background.ts b/entrypoints/background.ts index 0d7fcbd..95dc8db 100644 --- a/entrypoints/background.ts +++ b/entrypoints/background.ts @@ -475,6 +475,7 @@ async function pollOne(state: PersistedState, account: Account): Promise {state.settings.agentAllowlist.includes(ALL_SITES) && (

- All https sites. The code still goes only to the tab’s own site, and the rate limit stays. + All https sites. A site that is not in the list only gets codes and links mailed from that same site, and the rate limit stays.

)}

Recent requests

diff --git a/lib/agent.ts b/lib/agent.ts index 3a95f19..ec625b0 100644 --- a/lib/agent.ts +++ b/lib/agent.ts @@ -4,6 +4,7 @@ * code or the URL. Chrome access is injected through AgentDeps so this file * is unit-testable. */ +import { getDomain } from 'tldts'; import { CODE_MAX_AGE_MS, type CodeEntry } from './types'; export const DEFAULT_AGENT_ALLOWLIST = ['github.com', 'google.com', 'vercel.com', 'apple.com']; @@ -75,18 +76,49 @@ export function isAllowedUrl(url: string, allowlist: string[]): boolean { ); } -/** True when the URL is https and on the tab's own host or a subdomain of it. */ -export function sameSite(url: string, tabHost: string): boolean { - if (!tabHost) return false; - let parsed: URL; +/** + * Registrable domain (site) of a host, counting private suffixes such as + * vercel.app or github.io as public. Empty when unparsable or when the host is + * itself a public suffix. Fails closed. + */ +export function siteOf(host: string): string { try { - parsed = new URL(url); + const name = new URL('https://' + host.trim()).hostname.toLowerCase().replace(/\.$/, ''); + return getDomain(name, { allowPrivateDomains: true }) ?? ''; } catch { - return false; + return ''; + } +} + +/** Domain of the address in a From header: after the last '@' of the last <...>. */ +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 ''; + } +} + +/** Site of the entry's sender; empty when unknown or when the mail failed DMARC. */ +export function entrySite(e: CodeEntry): string { + if (e.dmarc === 'fail') return ''; + return siteOf(senderDomain(e.from)); +} + +/** Site of an https URL; empty for anything else. */ +export function urlSite(url: string): string { + try { + const parsed = new URL(url); + return parsed.protocol === 'https:' ? siteOf(parsed.hostname) : ''; + } catch { + return ''; } - if (parsed.protocol !== 'https:') return false; - const host = parsed.hostname.toLowerCase().replace(/\.$/, ''); - return host === tabHost || host.endsWith(`.${tabHost}`); } export class RateLimiter { @@ -136,11 +168,13 @@ export function newestUsable( used: ReadonlySet, now: number, want: 'code' | 'link', + accept?: (e: CodeEntry) => boolean, ): CodeEntry | undefined { return entries .filter((e) => { if (used.has(e.id)) return false; if (now - e.receivedAt > CODE_MAX_AGE_MS) return false; + if (accept && !accept(e)) return false; return want === 'code' ? (e.kind ?? 'code') === 'code' : Boolean(e.url); }) .sort((a, b) => b.receivedAt - a.receivedAt)[0]; @@ -217,21 +251,48 @@ export async function runAgentCommand(request: AgentRequest, deps: AgentDeps): P if (tabId === null) return finish({ ok: false, error: 'tab id required' }); if (!deps.limiter.take(deps.now())) return finish({ ok: false, error: 'rate limit reached' }); - const url = await deps.getTabUrl(tabId); - if (!url) return finish({ ok: false, error: 'tab not found' }); - host = hostOf(url); - if (!isAllowedUrl(url, config.allowlist)) return finish({ ok: false, error: 'site is not allowlisted' }); - // With '*' the code and links still go only to listed hosts or the tab's own site. const normalized = normalizeAllowlist(config.allowlist); const hasAll = normalized.includes(ALL_SITES); const listedOnly = normalized.filter((e) => e !== ALL_SITES); - const destinationOk = (u: string): boolean => isAllowedUrl(u, listedOnly) || (hasAll && sameSite(u, host)); + + type Ctx = + | { error: string } + | { + accept: (e: CodeEntry) => boolean; + frameOk: (e: CodeEntry) => (u: string) => boolean; + linkOk: (e: CodeEntry, u: string) => boolean; + }; + /** + * Reads the tab's current URL and decides the mode. Listed sites take any + * sender. With '*', other https sites only take mail from their own site. + */ + const context = async (): Promise => { + const url = await deps.getTabUrl(tabId); + if (!url) return { error: 'tab not found' }; + host = hostOf(url); + if (isAllowedUrl(url, listedOnly)) { + return { + accept: () => true, + frameOk: () => (u) => isAllowedUrl(u, listedOnly), + linkOk: (_e, u) => isAllowedUrl(u, listedOnly), + }; + } + const tabSite = hasAll ? urlSite(url) : ''; + if (!tabSite) return { error: 'site is not allowlisted' }; + return { + accept: (e) => entrySite(e) !== '' && entrySite(e) === tabSite, + frameOk: (e) => (u) => urlSite(u) === entrySite(e), + linkOk: (e, u) => urlSite(u) === entrySite(e), + }; + }; if (command === 'pending') { + const ctx = await context(); + if ('error' in ctx) return finish({ ok: false, error: ctx.error }); const [hasField, entries, used] = await Promise.all([deps.hasCodeField(tabId), deps.getEntries(), deps.getUsed()]); const now = deps.now(); - const code = newestUsable(entries, used, now, 'code'); - const link = newestUsable(entries, used, now, 'link'); + const code = newestUsable(entries, used, now, 'code', ctx.accept); + const link = newestUsable(entries, used, now, 'link', ctx.accept); const pick = (e?: CodeEntry) => (e ? { sender: senderName(e.from), ageSeconds: age(e, now) } : null); return finish({ ok: true, hasField, codeAvailable: pick(code), linkAvailable: pick(link) }); } @@ -239,9 +300,15 @@ export async function runAgentCommand(request: AgentRequest, deps: AgentDeps): P if (command === 'fill_code') { const deadline = deps.now() + waitArg(request.args) * 1000; for (;;) { - const entry = newestUsable(await deps.getEntries(), await deps.getUsed(), deps.now(), 'code'); + // Re-read the tab on every pass: it may have navigated while we waited. + let ctx = await context(); + if ('error' in ctx) return finish({ ok: false, error: ctx.error }); + const entry = newestUsable(await deps.getEntries(), await deps.getUsed(), deps.now(), 'code', ctx.accept); if (entry) { - const filled = await deps.fill(tabId, entry.code, destinationOk); + ctx = await context(); + if ('error' in ctx) return finish({ ok: false, error: ctx.error }); + if (!ctx.accept(entry)) return finish({ ok: false, error: 'site is not allowlisted' }); + const filled = await deps.fill(tabId, entry.code, ctx.frameOk(entry)); if (!filled) return finish({ ok: false, error: 'no code field filled on this tab' }); await deps.markUsed(entry.id); return finish({ ok: true, filled: true, sender: senderName(entry.from), ageSeconds: age(entry, deps.now()) }); @@ -260,9 +327,14 @@ export async function runAgentCommand(request: AgentRequest, deps: AgentDeps): P } // open_link - const entry = newestUsable(await deps.getEntries(), await deps.getUsed(), deps.now(), 'link'); + let ctx = await context(); + if ('error' in ctx) return finish({ ok: false, error: ctx.error }); + const entry = newestUsable(await deps.getEntries(), await deps.getUsed(), deps.now(), 'link', ctx.accept); if (!entry?.url) return finish({ ok: false, error: 'no fresh sign-in link' }); - if (!destinationOk(entry.url)) return finish({ ok: false, error: 'link site is not allowlisted' }); + ctx = await context(); + if ('error' in ctx) return finish({ ok: false, error: ctx.error }); + if (!ctx.accept(entry)) return finish({ ok: false, error: 'site is not allowlisted' }); + if (!ctx.linkOk(entry, entry.url)) return finish({ ok: false, error: 'link site is not allowlisted' }); await deps.navigate(tabId, entry.url); await deps.markUsed(entry.id); return finish({ ok: true, opened: true, sender: senderName(entry.from), ageSeconds: age(entry, deps.now()) }); diff --git a/lib/gmail.ts b/lib/gmail.ts index e533d19..c72d09a 100644 --- a/lib/gmail.ts +++ b/lib/gmail.ts @@ -31,6 +31,7 @@ export interface ParsedMessage { /** Raw HTML part, kept so verification buttons can be read from hrefs. */ html: string; receivedAt: number; + dmarc?: 'pass' | 'fail'; } interface GmailHeader { @@ -149,7 +150,10 @@ export function parseGmailMessage(raw: GmailMessage): ParsedMessage { const html = htmlPart ? decodeBody(htmlPart) : ''; const body = plain ? decodeBody(plain) : html ? htmlToText(html) : raw.snippet ?? ''; const receivedAt = raw.internalDate ? Number(raw.internalDate) : Date.parse(header(headers, 'Date')) || Date.now(); - return { id: raw.id, from, subject, body, html, receivedAt }; + // 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 } : {}) }; } function header(headers: GmailHeader[], name: string): string { diff --git a/lib/types.ts b/lib/types.ts index 02754ba..2cde7e1 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -28,6 +28,8 @@ export interface CodeEntry { kind?: EntryKind; /** Verification or sign-in URL. Present on link entries, and on a code when the same message has both. */ url?: string; + /** Gmail's own DMARC verdict for the message, when it reported one. */ + dmarc?: 'pass' | 'fail'; } export type PopupShows = 'all' | 'codes' | 'links'; diff --git a/package.json b/package.json index 544bf59..1004333 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "dependencies": { "extpay": "^3.1.2", "react": "^19.1.0", - "react-dom": "^19.1.0" + "react-dom": "^19.1.0", + "tldts": "^7.5.4" }, "devDependencies": { "@types/react": "^19.1.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f781608..8f84eab 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -17,6 +17,9 @@ importers: react-dom: specifier: ^19.1.0 version: 19.3.0(react@19.3.0) + tldts: + specifier: ^7.5.4 + version: 7.5.4 devDependencies: '@types/react': specifier: ^19.1.0 @@ -2072,6 +2075,13 @@ packages: resolution: {integrity: sha512-u8KszXvGfU68hVcZpRHKG28T0krMuv2G5nDhiHaMLen/gIuFEgIJhaJuO69qjnXg5paSrbPMFfx3brNuN8eVSg==} engines: {node: '>=14.0.0'} + tldts-core@7.5.4: + resolution: {integrity: sha512-9+4zWtKEDZ3V3FoZ1B97rv+5z5oqp7yeXGVze3CxgQ+CJro9do9DCPD00wxstYZDXDMXsurkBerFfiB6GYtPbA==} + + tldts@7.5.4: + resolution: {integrity: sha512-Rn2ABgjHZrA+jzqom5zhxA0hLohzYrIgfCPyhpTUxqsUrcsBYuab49LseR9waB9Jr8/MxdodvMHA91oRiGLO3Q==} + hasBin: true + tmp@0.2.5: resolution: {integrity: sha512-voyz6MApa1rQGUxT3E+BK7/ROe8itEx7vD8/HEvt4xwXucvQ5G5oeEiHkmHZJuBO21RpOf+YYm9MOivj709jow==} engines: {node: '>=14.14'} @@ -4186,6 +4196,12 @@ snapshots: tinyspy@4.0.6: {} + tldts-core@7.5.4: {} + + tldts@7.5.4: + dependencies: + tldts-core: 7.5.4 + tmp@0.2.5: {} to-regex-range@5.0.1: diff --git a/tests/agent.test.ts b/tests/agent.test.ts index 486d6a0..e3dd135 100644 --- a/tests/agent.test.ts +++ b/tests/agent.test.ts @@ -7,6 +7,9 @@ import { isAllowedUrl, normalizeAllowlist, runAgentCommand, + senderDomain, + siteOf, + urlSite, sanitizeReply, type AgentDeps, type AgentLogEntry, @@ -239,25 +242,123 @@ describe('runAgentCommand', () => { expect(await runAgentCommand({ command: 'pending' }, deps)).toMatchObject({ error: 'tab id required' }); }); - it('with * fills on any https tab but only into the tab site and listed frames', async () => { - const h = makeDeps({ tabUrl: 'https://example.org/login', allowlist: ['*'] }); + it('with * fills on an unlisted tab only from a sender of the same site', async () => { + const entries = [ + entry({ id: 'a:2', code: '222222', from: 'X ', receivedAt: NOW - 5_000 }), + entry({ id: 'a:3', code: '333333', from: 'Ex ', receivedAt: NOW - 20_000 }), + ]; + const h = makeDeps({ tabUrl: 'https://app.example.org/login', allowlist: ['*'], entries }); expect(await runAgentCommand({ command: 'fill_code', args: [5] }, h.deps)).toMatchObject({ ok: true, filled: true }); + expect(h.calls.fill).toEqual(['333333']); expect(h.calls.probe).toEqual([true, true, false]); - const listed = makeDeps({ tabUrl: 'https://example.org/login', allowlist: ['*', 'other.net'] }); - await runAgentCommand({ command: 'fill_code', args: [5] }, listed.deps); - expect(listed.calls.probe).toEqual([true, true, true]); }); - it('with * open_link opens a link on the tab host but not another host', async () => { + it('with * a phishing tab gets nothing from another site sender', async () => { + const h = makeDeps({ tabUrl: 'https://github-login.example-phish.com/x', allowlist: ['*'] }); + expect(await runAgentCommand({ command: 'fill_code', args: [5] }, h.deps)).toMatchObject({ ok: false }); + expect(h.calls.fill).toHaveLength(0); + const p = await runAgentCommand({ command: 'pending', args: [5] }, h.deps); + expect(p).toMatchObject({ ok: true, codeAvailable: null, linkAvailable: null }); + }); + + it('without * an unlisted tab is refused', async () => { + const h = makeDeps({ tabUrl: 'https://app.example.org/', allowlist: ['github.com'], entries: [entry({ from: 'a@example.org' })] }); + expect(await runAgentCommand({ command: 'fill_code', args: [5] }, h.deps)).toMatchObject({ error: 'site is not allowlisted' }); + }); + + it('private suffixes are public suffixes', async () => { + const e = [entry({ from: 'x@foo.vercel.app' })]; + const other = makeDeps({ tabUrl: 'https://bar.vercel.app/', allowlist: ['*'], entries: e }); + await runAgentCommand({ command: 'fill_code', args: [5] }, other.deps); + expect(other.calls.fill).toHaveLength(0); + const same = makeDeps({ tabUrl: 'https://foo.vercel.app/', allowlist: ['*'], entries: e }); + await runAgentCommand({ command: 'fill_code', args: [5] }, same.deps); + expect(same.calls.fill).toHaveLength(1); + const bare = makeDeps({ tabUrl: 'https://vercel.app/', allowlist: ['*'], entries: [entry({ from: 'x@vercel.app' })] }); + await runAgentCommand({ command: 'fill_code', args: [5] }, bare.deps); + expect(bare.calls.fill).toHaveLength(0); + }); + + it('a display name with an address does not fool the match', async () => { + const h = makeDeps({ + tabUrl: 'https://example.org/', + allowlist: ['*'], + entries: [entry({ from: '"noreply@example.org" ' })], + }); + await runAgentCommand({ command: 'fill_code', args: [5] }, h.deps); + expect(h.calls.fill).toHaveLength(0); + }); + + it('dmarc fail is excluded in sender mode but fine for listed sites', async () => { + const e = [entry({ from: 'a@example.org', dmarc: 'fail' })]; + const s = makeDeps({ tabUrl: 'https://example.org/', allowlist: ['*'], entries: e }); + await runAgentCommand({ command: 'fill_code', args: [5] }, s.deps); + expect(s.calls.fill).toHaveLength(0); + const l = makeDeps({ entries: [entry({ dmarc: 'fail' })], allowlist: ['*', 'github.com'] }); + await runAgentCommand({ command: 'fill_code', args: [5] }, l.deps); + expect(l.calls.fill).toHaveLength(1); + }); + + it('listed tab with * still takes any sender', async () => { + const h = makeDeps({ allowlist: ['*', 'github.com'], entries: [entry({ from: 'Z ' })] }); + await runAgentCommand({ command: 'fill_code', args: [5] }, h.deps); + expect(h.calls.fill).toHaveLength(1); + }); + + it('re-reads the tab URL while waiting and does not fill a navigated tab', async () => { + const h = makeDeps({ tabUrl: 'https://app.example.org/', allowlist: ['*'], entries: [] }); + let reads = 0; + h.deps.getTabUrl = async () => (reads++ === 0 ? 'https://app.example.org/' : 'https://evil.test/'); + h.deps.getEntries = async () => (h.calls.polls >= 1 ? [entry({ from: 'a@example.org', receivedAt: h.deps.now() })] : []); + const r = await runAgentCommand({ command: 'fill_code', args: [5, 60] }, h.deps); + expect(r).toMatchObject({ ok: false }); + expect(h.calls.fill).toHaveLength(0); + }); + + it('a vanished tab while waiting returns tab not found', async () => { + const h = makeDeps({ tabUrl: 'https://app.example.org/', allowlist: ['*'], entries: [] }); + let reads = 0; + h.deps.getTabUrl = async () => (reads++ === 0 ? 'https://app.example.org/' : null); + const r = await runAgentCommand({ command: 'fill_code', args: [5, 60] }, h.deps); + expect(r).toMatchObject({ ok: false, error: 'tab not found' }); + }); + + it('open_link in sender mode needs sender and link on the tab site', async () => { const opts = { tabUrl: 'https://example.org/login', allowlist: ['*'] }; - const good = makeDeps({ ...opts, entries: [entry({ kind: 'link', url: 'https://login.example.org/v?t=s' })] }); + const good = makeDeps({ ...opts, entries: [entry({ kind: 'link', from: 'a@example.org', url: 'https://login.example.org/v?t=s' })] }); expect(await runAgentCommand({ command: 'open_link', args: [5] }, good.deps)).toMatchObject({ ok: true, opened: true }); expect(good.calls.nav).toEqual(['https://login.example.org/v?t=s']); - const bad = makeDeps({ ...opts, entries: [entry({ kind: 'link', url: 'https://other.net/v' })] }); - expect(await runAgentCommand({ command: 'open_link', args: [5] }, bad.deps)).toMatchObject({ - ok: false, - error: 'link site is not allowlisted', - }); - expect(bad.calls.nav).toHaveLength(0); + const offsite = makeDeps({ ...opts, entries: [entry({ kind: 'link', from: 'a@example.org', url: 'https://other.net/v' })] }); + expect(await runAgentCommand({ command: 'open_link', args: [5] }, offsite.deps)).toMatchObject({ ok: false }); + expect(offsite.calls.nav).toHaveLength(0); + const foreign = makeDeps({ ...opts, entries: [entry({ kind: 'link', from: 'a@other.net', url: 'https://example.org/v' })] }); + expect(await runAgentCommand({ command: 'open_link', args: [5] }, foreign.deps)).toMatchObject({ ok: false }); + expect(foreign.calls.nav).toHaveLength(0); + }); +}); + +describe('siteOf and senderDomain', () => { + it('finds the registrable domain, incl. private suffixes', () => { + expect(siteOf('a.b.example.org')).toBe('example.org'); + expect(siteOf('Example.ORG.')).toBe('example.org'); + expect(siteOf('x.co.jp')).toBe('x.co.jp'); + expect(siteOf('foo.vercel.app')).toBe('foo.vercel.app'); + expect(siteOf('bücher.example')).toBe('xn--bcher-kva.example'); + }); + 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 <...>', () => { + 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@example.org ')).toBe('example.org'); + expect(senderDomain('no address')).toBe(''); + expect(senderDomain('a@')).toBe(''); + }); + it('urlSite requires https', () => { + expect(urlSite('http://example.org')).toBe(''); + expect(urlSite('https://a.example.org/x')).toBe('example.org'); + expect(urlSite('junk')).toBe(''); }); }); diff --git a/tests/gmail.test.ts b/tests/gmail.test.ts index 514e568..fb36567 100644 --- a/tests/gmail.test.ts +++ b/tests/gmail.test.ts @@ -22,6 +22,21 @@ describe('gmail parsing', () => { expect(raw.receivedAt).toBe(1700000000000); }); + it('reads the first Authentication-Results header for the DMARC verdict', () => { + const mk = (values: string[]) => + parseGmailMessage({ + id: 'd', + payload: { + headers: values.map((value) => ({ name: 'Authentication-Results', value })), + mimeType: 'text/plain', + body: { data: encode('x') }, + }, + }).dmarc; + expect(mk(['mx.google.com; dmarc=pass (p=NONE) header.from=a.com', 'x; dmarc=fail'])).toBe('pass'); + expect(mk(['mx.google.com; spf=pass; dmarc=fail (p=REJECT)'])).toBe('fail'); + expect(mk(['mx.google.com; spf=pass'])).toBeUndefined(); + }); + it('keeps the html part when a plain part is also present', () => { const raw = parseGmailMessage({ id: 'm2',