Linux + iPhone Continuity / iMessage / SMS
fix(otp): only pin mail OTPs to a server-validated DKIM domain
c10l412a6bf2e90a
diff --git a/extension/src/mail/extractor.js b/extension/src/mail/extractor.jsindex 46653e8..e74864e 100644--- a/extension/src/mail/extractor.js+++ b/extension/src/mail/extractor.js@@ -3,7 +3,7 @@// single entry point for the mail extensionimport '../background/background.js';-import { sendToNativeHost, hostMatchesDomain, registrableDomain } from '../shared/native.js';+import { sendToNativeHost, registrableDomain } from '../shared/native.js';const OTP_CONTEXT_KEYWORDS = ['code', 'otp', 'one-time', 'verification', 'confirm', 'passcode',@@ -105,31 +105,40 @@ export function extractSenderDomain(author) {}function normalizeDomainValue(d) {- return String(d || '').toLowerCase().replace(/\.+$/, '');+ return String(d || '').toLowerCase().replace(/\.+$/, '').replace(/"/g, '');}-// DKIM-Signature headers are `;`-separated tag=value pairs; `d=` is the signing-// domain. An email may carry several signatures (one per hop).-function dkimDomains(headers) {- const raw = headers?.['dkim-signature'] || [];+// Only trust domains the receiving server actually validated. Raw From/Return-Path+// and even a raw DKIM-Signature header are attacker-controlled; the server's own+// Authentication-Results header records what really passed. We only look at the+// first (topmost) header — the one added by the user's receiving server.+function validatedDkimDomains(headers) {+ const raw = headers?.['authentication-results'] || [];+ if (!raw.length) return [];const domains = [];- for (const value of raw) {- for (const part of String(value).split(';')) {- const m = part.trim().match(/^d\s*=\s*(?:"([^"]+)"|([^;\s]+))/i);- if (m) {- const d = normalizeDomainValue(m[1] || m[2] || '');- if (d) domains.push(d);+ for (const part of String(raw[0]).split(';')) {+ if (!/dkim\s*=\s*pass/i.test(part)) continue;++ let m = part.match(/header\.d\s*=\s*"?([^";\s]+)"?/i);+ if (m) {+ const d = normalizeDomainValue(m[1]);+ if (d) {+ domains.push(d);+ continue;}}++ m = part.match(/header\.i\s*=\s*"?([^";\s]+)"?/i);+ if (m) {+ const addr = m[1];+ const at = addr.indexOf('@');+ const d = normalizeDomainValue(at >= 0 ? addr.slice(at + 1) : addr);+ if (d) domains.push(d);+ }}return domains;}-function returnPathDomain(headers) {- const raw = headers?.['return-path'] || [];- return extractSenderDomain(raw[0]);-}-// Some services send transactional mail (including OTPs) from a registrable// domain that differs from the site the user logs in on. Map those sender// domains to the login domain so the correlation still matches.@@ -143,19 +152,19 @@ export function canonicalizeSenderDomain(domain) {return SENDER_DOMAIN_ALIASES.get(rd) || rd;}-// Prefer the DKIM signing domain, then the Return-Path, then the From header.-// DKIM and Return-Path are validated by the receiving server, so they are-// harder to spoof than a free-form From display name.+// Derive the sender domain solely from the server-validated DKIM result. When+// the receiving server did not record a passing DKIM signature, return "" so the+// OTP is left unpinned instead of trusting a spoofable From/Return-Path header.export function resolveSenderDomain(message, headers) {const from = extractSenderDomain(message?.author);- const dkim = dkimDomains(headers);- let domain;- if (dkim.length > 0) {- domain = dkim.find((d) => hostMatchesDomain(d, from)) || dkim[dkim.length - 1];- } else {- domain = returnPathDomain(headers) || from;+ const validated = validatedDkimDomains(headers);+ if (validated.length > 0) {+ // Prefer the validated domain aligned with the From header, otherwise the+ // first one the receiving server reported.+ const domain = validated.find((d) => registrableDomain(d) === registrableDomain(from)) || validated[0];+ return canonicalizeSenderDomain(domain);}- return canonicalizeSenderDomain(domain);+ return '';}// Fetch the raw headers and derive the best sender domain. getFull() is thediff --git a/extension/test/extractor.test.js b/extension/test/extractor.test.jsindex cc3f221..2c1216a 100644--- a/extension/test/extractor.test.js+++ b/extension/test/extractor.test.js@@ -167,42 +167,42 @@ describe('extractSenderDomain', () => {});describe('resolveSenderDomain', () => {- it('prefers the DKIM signing domain over the From header', () => {+ it('uses the DKIM domain the receiving server validated', () => {expect(resolveSenderDomain({ author: 'Amazon <[email protected]>' },- { 'dkim-signature': ['v=1; a=rsa-sha256; d=amazon.com; s=mail'] }+ { 'authentication-results': ['mx.example.com; dkim=pass header.d=amazon.com [email protected]'] })).toBe('amazon.com');});- it('parses a quoted DKIM d= tag', () => {+ it('falls back to header.i when header.d is absent', () => {expect(resolveSenderDomain(- { author: 'Amazon <[email protected]>' },- { 'dkim-signature': ['v=1; d="amazon.com"; s=mail'] }+ { author: 'Amazon <[email protected]>' },+ { 'authentication-results': ['example.com; dkim=pass [email protected] header.s=mail'] })).toBe('amazon.com');});- it('falls back to Return-Path when there is no DKIM header', () => {+ it('ignores a failed DKIM result and leaves the OTP unpinned', () => {expect(resolveSenderDomain({ author: 'Amazon <[email protected]>' },- { 'return-path': ['[email protected]'] }- )).toBe('amazon.co.uk');+ { 'authentication-results': ['example.com; dkim=fail header.d=amazon.com'] }+ )).toBe('');});- it('falls back to the From header when headers are missing', () => {- expect(resolveSenderDomain({ author: 'Amazon <[email protected]>' }, {})).toBe('amazon.com');+ it('leaves the OTP unpinned when Authentication-Results is missing', () => {+ expect(resolveSenderDomain({ author: 'Amazon <[email protected]>' }, {})).toBe('');});- it('prefers the DKIM domain aligned with From among multiple signatures', () => {+ it('prefers the validated domain aligned with From among multiple passes', () => {expect(resolveSenderDomain({ author: 'Facebook <[email protected]>' },- { 'dkim-signature': ['v=1; d=sendgrid.net; s=1', 'v=1; d=facebookmail.com; s=2'] }+ { 'authentication-results': ['example.com; dkim=pass header.d=sendgrid.net; dkim=pass header.d=facebookmail.com'] })).toBe('facebook.com');});- it('maps a known sender domain to its login domain', () => {+ it('maps a validated sender domain through the alias table', () => {expect(resolveSenderDomain({ author: 'Facebook <[email protected]>' },- {}+ { 'authentication-results': ['example.com; dkim=pass header.d=facebookmail.com'] })).toBe('facebook.com');});});@@ -231,7 +231,7 @@ describe('processMessage sends the sender domain', () => {nativePostMessage.mockClear();});- it('includes sender_domain in the new_otp message', async () => {+ it('leaves sender_domain empty when the server did not validate DKIM', async () => {global.messenger.messages.listInlineTextParts.mockResolvedValueOnce([{ contentType: 'text/plain', content: 'Your verification code is 123456' }]);@@ -246,17 +246,17 @@ describe('processMessage sends the sender domain', () => {expect.objectContaining({command: 'new_otp',otp: '123456',- sender_domain: 'amazon.com'+ sender_domain: ''}));});- it('uses the DKIM signing domain when available', async () => {+ it('uses the validated DKIM domain when available', async () => {global.messenger.messages.listInlineTextParts.mockResolvedValueOnce([{ contentType: 'text/plain', content: 'Your verification code is 123456' }]);global.messenger.messages.getFull.mockResolvedValueOnce({- headers: { 'dkim-signature': ['v=1; d=amazon.com; s=mail'] },+ headers: { 'authentication-results': ['example.com; dkim=pass header.d=amazon.com'] },parts: []});