Linux + iPhone Continuity / iMessage / SMS
feat(otp): map known sender domains to their login domain
c10l24ae315cee22
diff --git a/extension/src/mail/extractor.js b/extension/src/mail/extractor.jsindex b61bf5a..46653e8 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 } from '../shared/native.js';+import { sendToNativeHost, hostMatchesDomain, registrableDomain } from '../shared/native.js';const OTP_CONTEXT_KEYWORDS = ['code', 'otp', 'one-time', 'verification', 'confirm', 'passcode',@@ -130,16 +130,32 @@ function returnPathDomain(headers) {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.+const SENDER_DOMAIN_ALIASES = new Map([+ // Facebook sends security/verification mail from facebookmail.com.+ ['facebookmail.com', 'facebook.com'],+]);++export function canonicalizeSenderDomain(domain) {+ const rd = registrableDomain(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.export function resolveSenderDomain(message, headers) {const from = extractSenderDomain(message?.author);const dkim = dkimDomains(headers);+ let domain;if (dkim.length > 0) {- return dkim.find((d) => hostMatchesDomain(d, from)) || dkim[dkim.length - 1];+ domain = dkim.find((d) => hostMatchesDomain(d, from)) || dkim[dkim.length - 1];+ } else {+ domain = returnPathDomain(headers) || from;}- return returnPathDomain(headers) || from;+ return canonicalizeSenderDomain(domain);}// 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 7d2e09f..cc3f221 100644--- a/extension/test/extractor.test.js+++ b/extension/test/extractor.test.js@@ -1,4 +1,4 @@-import { describe, it, expect, vi } from 'vitest';+import { describe, it, expect, vi, beforeEach } from 'vitest';global.messenger = {messages: {@@ -32,17 +32,19 @@ global.messenger = {}};+const nativePostMessage = vi.fn();+global.browser = {runtime: {connectNative: vi.fn().mockReturnValue({- postMessage: vi.fn(),+ postMessage: nativePostMessage,onMessage: { addListener: vi.fn() },onDisconnect: { addListener: vi.fn() }})}};-import { scoreCandidate, isFalsePositive, OTP_PATTERNS, processMessage, extractSenderDomain, resolveSenderDomain } from '../src/mail/extractor.js';+import { scoreCandidate, isFalsePositive, OTP_PATTERNS, processMessage, extractSenderDomain, resolveSenderDomain, canonicalizeSenderDomain } from '../src/mail/extractor.js';function extractOtpCandidates(text) {const candidates = [];@@ -167,7 +169,7 @@ describe('extractSenderDomain', () => {describe('resolveSenderDomain', () => {it('prefers the DKIM signing domain over the From header', () => {expect(resolveSenderDomain(- { author: 'Amazon <[email protected]>' },+ { author: 'Amazon <[email protected]>' },{ 'dkim-signature': ['v=1; a=rsa-sha256; d=amazon.com; s=mail'] })).toBe('amazon.com');});@@ -182,8 +184,8 @@ describe('resolveSenderDomain', () => {it('falls back to Return-Path when there is no DKIM header', () => {expect(resolveSenderDomain({ author: 'Amazon <[email protected]>' },- { 'return-path': ['[email protected]'] }- )).toBe('bounce.amazon.com');+ { 'return-path': ['[email protected]'] }+ )).toBe('amazon.co.uk');});it('falls back to the From header when headers are missing', () => {@@ -194,11 +196,41 @@ describe('resolveSenderDomain', () => {expect(resolveSenderDomain({ author: 'Facebook <[email protected]>' },{ 'dkim-signature': ['v=1; d=sendgrid.net; s=1', 'v=1; d=facebookmail.com; s=2'] }- )).toBe('facebookmail.com');+ )).toBe('facebook.com');+ });++ it('maps a known sender domain to its login domain', () => {+ expect(resolveSenderDomain(+ { author: 'Facebook <[email protected]>' },+ {}+ )).toBe('facebook.com');+ });+});++describe('canonicalizeSenderDomain', () => {+ it('maps facebookmail.com to facebook.com', () => {+ expect(canonicalizeSenderDomain('facebookmail.com')).toBe('facebook.com');+ });++ it('maps a subdomain of a known sender domain', () => {+ expect(canonicalizeSenderDomain('mail.facebookmail.com')).toBe('facebook.com');+ });++ it('returns the registrable domain for unrelated domains', () => {+ expect(canonicalizeSenderDomain('amazon.com')).toBe('amazon.com');+ expect(canonicalizeSenderDomain('www.amazon.co.uk')).toBe('amazon.co.uk');+ });++ it('returns empty for empty input', () => {+ expect(canonicalizeSenderDomain('')).toBe('');});});describe('processMessage sends the sender domain', () => {+ beforeEach(() => {+ nativePostMessage.mockClear();+ });+it('includes sender_domain in the new_otp message', async () => {global.messenger.messages.listInlineTextParts.mockResolvedValueOnce([{ contentType: 'text/plain', content: 'Your verification code is 123456' }@@ -210,8 +242,7 @@ describe('processMessage sends the sender domain', () => {author: 'Amazon <[email protected]>'});- const postMessage = global.browser.runtime.connectNative.mock.results[0].value.postMessage;- expect(postMessage).toHaveBeenCalledWith(+ expect(nativePostMessage).toHaveBeenCalledWith(expect.objectContaining({command: 'new_otp',otp: '123456',@@ -225,22 +256,21 @@ describe('processMessage sends the sender domain', () => {{ contentType: 'text/plain', content: 'Your verification code is 123456' }]);global.messenger.messages.getFull.mockResolvedValueOnce({- headers: { 'dkim-signature': ['v=1; d=accounts.google.com; s=mail'] },+ headers: { 'dkim-signature': ['v=1; d=amazon.com; s=mail'] },parts: []});await processMessage({id: 43,subject: 'Your verification code',- author: 'Google <[email protected]>'+ author: 'Amazon <[email protected]>'});- const postMessage = global.browser.runtime.connectNative.mock.results[0].value.postMessage;- expect(postMessage).toHaveBeenCalledWith(+ expect(nativePostMessage).toHaveBeenCalledWith(expect.objectContaining({command: 'new_otp',otp: '123456',- sender_domain: 'accounts.google.com'+ sender_domain: 'amazon.com'}));});