Skip to content

Linux + iPhone Continuity / iMessage / SMS

feat(otp): derive sender domain from DKIM/Return-Path and match by eTLD+1

c10lbbd2755f45a2
diff --git a/extension/src/mail/extractor.js b/extension/src/mail/extractor.js
index f690a49..b61bf5a 100644
--- a/extension/src/mail/extractor.js
+++ b/extension/src/mail/extractor.js
@@ -3,7 +3,7 @@
// single entry point for the mail extension
import '../background/background.js';
-import { sendToNativeHost } from '../shared/native.js';
+import { sendToNativeHost, hostMatchesDomain } from '../shared/native.js';
const OTP_CONTEXT_KEYWORDS = [
'code', 'otp', 'one-time', 'verification', 'confirm', 'passcode',
@@ -104,6 +104,56 @@ export function extractSenderDomain(author) {
return domain.toLowerCase().replace(/\.+$/, '');
}
+function normalizeDomainValue(d) {
+ return String(d || '').toLowerCase().replace(/\.+$/, '');
+}
+
+// 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'] || [];
+ 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);
+ }
+ }
+ }
+ return domains;
+}
+
+function returnPathDomain(headers) {
+ const raw = headers?.['return-path'] || [];
+ return extractSenderDomain(raw[0]);
+}
+
+// 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);
+ if (dkim.length > 0) {
+ return dkim.find((d) => hostMatchesDomain(d, from)) || dkim[dkim.length - 1];
+ }
+ return returnPathDomain(headers) || from;
+}
+
+// Fetch the raw headers and derive the best sender domain. getFull() is the
+// only API that exposes headers; it is only called once an OTP candidate has
+// been found, so ordinary (non-OTP) mail never pays for it.
+async function getSenderDomain(message) {
+ try {
+ const full = await messenger.messages.getFull(message.id);
+ return resolveSenderDomain(message, full.headers);
+ } catch (e) {
+ return extractSenderDomain(message.author);
+ }
+}
+
// ---------------------------------------------------------------------------
// FALLBACK text extraction: manually walk getFull() parts.
// Recovery path for messages whose MIME tree makes listInlineTextParts() throw.
@@ -275,11 +325,12 @@ export async function processMessage(message) {
if (best.score > 0) {
console.log("Found OTP candidate in email with score:", best.score);
+ const senderDomain = await getSenderDomain(message);
sendToNativeHost({
command: "new_otp",
otp: best.num.replace(/[-\s]/g, ''),
source: message.subject,
- sender_domain: extractSenderDomain(message.author)
+ sender_domain: senderDomain
});
}
}
diff --git a/extension/src/shared/native.js b/extension/src/shared/native.js
index ff8ef7b..edaa7a5 100644
--- a/extension/src/shared/native.js
+++ b/extension/src/shared/native.js
@@ -81,16 +81,77 @@ function normalizeDomain(d) {
return String(d || '').toLowerCase().replace(/\.+$/, '');
}
-// A page host matches a pinned sender domain when it is equal or a subdomain:
-// "amazon.com" matches "amazon.com" and "www.amazon.com", but not
-// "amazon.com.evil.com" or "evilamazon.com". An empty domain means the OTP is
+// Common two-label public suffixes. Without these, "amazon.co.uk" would be
+// reduced to "co.uk" and match every *.co.uk site. This is a curated subset of
+// the Public Suffix List covering the common country-code second-level domains.
+const MULTI_PART_SUFFIXES = new Set([
+ 'co.uk', 'org.uk', 'gov.uk', 'ac.uk', 'net.uk', 'me.uk', 'ltd.uk', 'plc.uk', 'sch.uk',
+ 'com.au', 'net.au', 'org.au', 'edu.au', 'gov.au', 'asn.au', 'id.au',
+ 'co.jp', 'or.jp', 'ne.jp', 'ac.jp', 'go.jp',
+ 'co.nz', 'net.nz', 'org.nz', 'govt.nz', 'ac.nz',
+ 'com.br', 'net.br', 'org.br', 'gov.br', 'edu.br',
+ 'co.in', 'net.in', 'org.in', 'gen.in', 'firm.in', 'ind.in',
+ 'co.kr', 'or.kr', 'ne.kr', 'go.kr', 'ac.kr',
+ 'com.mx', 'net.mx', 'org.mx', 'edu.mx', 'gob.mx',
+ 'com.ar', 'net.ar', 'org.ar', 'gob.ar', 'edu.ar',
+ 'co.za', 'org.za', 'net.za', 'gov.za', 'ac.za',
+ 'com.sg', 'net.sg', 'org.sg', 'gov.sg', 'edu.sg',
+ 'com.hk', 'net.hk', 'org.hk', 'edu.hk', 'gov.hk', 'idv.hk',
+ 'com.tw', 'net.tw', 'org.tw', 'edu.tw', 'gov.tw',
+ 'com.cn', 'net.cn', 'org.cn', 'gov.cn', 'edu.cn',
+ 'com.tr', 'net.tr', 'org.tr', 'gov.tr', 'edu.tr',
+ 'com.ua', 'net.ua', 'org.ua', 'gov.ua', 'edu.ua',
+ 'com.ru', 'net.ru', 'org.ru',
+ 'co.id', 'net.id', 'or.id', 'go.id', 'ac.id',
+ 'co.th', 'in.th', 'go.th', 'ac.th',
+ 'com.vn', 'net.vn', 'org.vn', 'gov.vn', 'edu.vn',
+ 'co.il', 'org.il', 'net.il', 'gov.il', 'ac.il',
+ 'com.ph', 'net.ph', 'org.ph', 'gov.ph', 'edu.ph',
+ 'com.my', 'net.my', 'org.my', 'gov.my', 'edu.my',
+ 'com.pk', 'net.pk', 'org.pk', 'gov.pk', 'edu.pk',
+ 'com.eg', 'net.eg', 'org.eg', 'gov.eg', 'edu.eg',
+ 'com.sa', 'net.sa', 'org.sa', 'gov.sa', 'edu.sa',
+ 'com.ng', 'net.ng', 'org.ng', 'gov.ng', 'edu.ng',
+ 'co.ke', 'or.ke', 'ne.ke', 'go.ke', 'ac.ke',
+ 'com.co', 'net.co', 'org.co', 'gov.co', 'edu.co',
+ 'com.pe', 'net.pe', 'org.pe', 'gob.pe', 'edu.pe',
+ 'com.ec', 'net.ec', 'org.ec', 'gob.ec', 'edu.ec',
+ 'com.uy', 'net.uy', 'org.uy', 'gub.uy', 'edu.uy',
+ 'com.ve', 'net.ve', 'org.ve', 'gob.ve', 'edu.ve',
+ 'com.py', 'net.py', 'org.py', 'gov.py', 'edu.py',
+ 'com.bo', 'net.bo', 'org.bo', 'gob.bo', 'edu.bo',
+ 'com.do', 'net.do', 'org.do', 'gov.do', 'edu.do',
+ 'com.gt', 'net.gt', 'org.gt', 'gob.gt', 'edu.gt',
+ 'co.cr', 'or.cr', 'go.cr', 'ac.cr'
+]);
+
+// eTLD+1 approximation: strip the public suffix and return it plus one label.
+export function registrableDomain(host) {
+ const d = normalizeDomain(host);
+ if (!d) return '';
+ // IP literals are not DNS names; compare them verbatim.
+ if (/^\d{1,3}(\.\d{1,3}){3}$/.test(d) || d.includes(':')) return d;
+
+ const labels = d.split('.');
+ if (labels.length === 1) return d;
+
+ const lastTwo = labels.slice(-2).join('.');
+ if (MULTI_PART_SUFFIXES.has(lastTwo)) {
+ if (labels.length === 2) return d;
+ return labels.slice(-3).join('.');
+ }
+ return lastTwo;
+}
+
+// A page host matches a pinned sender domain when they share the same
+// registrable domain: "accounts.google.com" matches "google.com", and
+// "www.amazon.co.uk" matches "amazon.co.uk", while "amazon.com.evil.com" and
+// "evilamazon.com" never match "amazon.com". An empty domain means the OTP is
// not pinned and any host may take it.
export function hostMatchesDomain(host, domain) {
const d = normalizeDomain(domain);
if (!d) return true;
- const h = normalizeDomain(host);
- if (h === d) return true;
- return h.endsWith('.' + d);
+ return registrableDomain(host) === registrableDomain(d);
}
// Deliver an OTP only to tabs that asked for one, most-relevant first, and stop
diff --git a/extension/test/extractor.test.js b/extension/test/extractor.test.js
index 55c9c86..7d2e09f 100644
--- a/extension/test/extractor.test.js
+++ b/extension/test/extractor.test.js
@@ -42,7 +42,7 @@ global.browser = {
}
};
-import { scoreCandidate, isFalsePositive, OTP_PATTERNS, processMessage, extractSenderDomain } from '../src/mail/extractor.js';
+import { scoreCandidate, isFalsePositive, OTP_PATTERNS, processMessage, extractSenderDomain, resolveSenderDomain } from '../src/mail/extractor.js';
function extractOtpCandidates(text) {
const candidates = [];
@@ -164,6 +164,40 @@ describe('extractSenderDomain', () => {
});
});
+describe('resolveSenderDomain', () => {
+ it('prefers the DKIM signing domain over the From header', () => {
+ expect(resolveSenderDomain(
+ { author: 'Amazon <[email protected]>' },
+ { 'dkim-signature': ['v=1; a=rsa-sha256; d=amazon.com; s=mail'] }
+ )).toBe('amazon.com');
+ });
+
+ it('parses a quoted DKIM d= tag', () => {
+ expect(resolveSenderDomain(
+ { author: 'Amazon <[email protected]>' },
+ { 'dkim-signature': ['v=1; d="amazon.com"; s=mail'] }
+ )).toBe('amazon.com');
+ });
+
+ 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');
+ });
+
+ it('falls back to the From header when headers are missing', () => {
+ expect(resolveSenderDomain({ author: 'Amazon <[email protected]>' }, {})).toBe('amazon.com');
+ });
+
+ it('prefers the DKIM domain aligned with From among multiple signatures', () => {
+ 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');
+ });
+});
+
describe('processMessage sends the sender domain', () => {
it('includes sender_domain in the new_otp message', async () => {
global.messenger.messages.listInlineTextParts.mockResolvedValueOnce([
@@ -185,6 +219,31 @@ describe('processMessage sends the sender domain', () => {
})
);
});
+
+ it('uses the DKIM signing 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=accounts.google.com; s=mail'] },
+ parts: []
+ });
+
+ await processMessage({
+ id: 43,
+ subject: 'Your verification code',
+ author: 'Google <[email protected]>'
+ });
+
+ const postMessage = global.browser.runtime.connectNative.mock.results[0].value.postMessage;
+ expect(postMessage).toHaveBeenCalledWith(
+ expect.objectContaining({
+ command: 'new_otp',
+ otp: '123456',
+ sender_domain: 'accounts.google.com'
+ })
+ );
+ });
});
describe('Real email fixtures', () => {
diff --git a/extension/test/native.test.js b/extension/test/native.test.js
index 5d7e44d..4edea2e 100644
--- a/extension/test/native.test.js
+++ b/extension/test/native.test.js
@@ -31,7 +31,7 @@ beforeEach(() => {
installChrome([{ id: 1 }, { id: 2 }, { id: 3 }]);
});
-const { connectToNativeHost, registerOtpRequest, _resetOtpRequests, hostMatchesDomain } =
+const { connectToNativeHost, registerOtpRequest, _resetOtpRequests, hostMatchesDomain, registrableDomain } =
await import('../src/shared/native.js');
describe('native host receive/routing', () => {
@@ -155,4 +155,33 @@ describe('hostMatchesDomain', () => {
it('normalizes case and trailing dots', () => {
expect(hostMatchesDomain('www.amazon.com', 'Amazon.COM.')).toBe(true);
});
+
+ it('matches an apex page against a subdomain sender', () => {
+ expect(hostMatchesDomain('google.com', 'accounts.google.com')).toBe(true);
+ });
+
+ it('matches ccTLD subdomains', () => {
+ expect(hostMatchesDomain('www.amazon.co.uk', 'amazon.co.uk')).toBe(true);
+ });
+
+ it('rejects a different registrable domain under the same ccTLD', () => {
+ expect(hostMatchesDomain('evil.co.uk', 'amazon.co.uk')).toBe(false);
+ });
+
+ it('rejects suffix spoofs on ccTLD domains', () => {
+ expect(hostMatchesDomain('amazon.co.uk.evil.com', 'amazon.co.uk')).toBe(false);
+ });
+});
+
+describe('registrableDomain', () => {
+ it('returns eTLD+1 for common domains', () => {
+ expect(registrableDomain('www.amazon.com')).toBe('amazon.com');
+ expect(registrableDomain('accounts.google.com')).toBe('google.com');
+ expect(registrableDomain('www.amazon.co.uk')).toBe('amazon.co.uk');
+ expect(registrableDomain('amazon.com.evil.com')).toBe('evil.com');
+ });
+
+ it('leaves IP literals alone', () => {
+ expect(registrableDomain('192.168.1.1')).toBe('192.168.1.1');
+ });
});
diff --git a/src/core/src/otp.cpp b/src/core/src/otp.cpp
index df8bdf2..c54a85b 100644
--- a/src/core/src/otp.cpp
+++ b/src/core/src/otp.cpp
@@ -5,6 +5,8 @@
#include <mutex>
#include <regex>
#include <string_view>
+#include <unordered_set>
+#include <vector>
namespace tether {
@@ -43,18 +45,111 @@ namespace tether {
return d;
}
- // A page host matches a pinned sender domain when it is equal or a
- // subdomain: "amazon.com" matches "amazon.com" and "www.amazon.com", but
- // not "amazon.com.evil.com" or "evilamazon.com". An empty domain means the
+ // Common two-label public suffixes. Without these, "amazon.co.uk" would
+ // be reduced to "co.uk" and match every *.co.uk site. Keep this in sync
+ // with MULTI_PART_SUFFIXES in extension/src/shared/native.js.
+ const std::unordered_set<std::string> kMultiPartSuffixes = {
+ "co.uk", "org.uk", "gov.uk", "ac.uk", "net.uk", "me.uk", "ltd.uk", "plc.uk", "sch.uk",
+ "com.au", "net.au", "org.au", "edu.au", "gov.au", "asn.au", "id.au",
+ "co.jp", "or.jp", "ne.jp", "ac.jp", "go.jp",
+ "co.nz", "net.nz", "org.nz", "govt.nz", "ac.nz",
+ "com.br", "net.br", "org.br", "gov.br", "edu.br",
+ "co.in", "net.in", "org.in", "gen.in", "firm.in", "ind.in",
+ "co.kr", "or.kr", "ne.kr", "go.kr", "ac.kr",
+ "com.mx", "net.mx", "org.mx", "edu.mx", "gob.mx",
+ "com.ar", "net.ar", "org.ar", "gob.ar", "edu.ar",
+ "co.za", "org.za", "net.za", "gov.za", "ac.za",
+ "com.sg", "net.sg", "org.sg", "gov.sg", "edu.sg",
+ "com.hk", "net.hk", "org.hk", "edu.hk", "gov.hk", "idv.hk",
+ "com.tw", "net.tw", "org.tw", "edu.tw", "gov.tw",
+ "com.cn", "net.cn", "org.cn", "gov.cn", "edu.cn",
+ "com.tr", "net.tr", "org.tr", "gov.tr", "edu.tr",
+ "com.ua", "net.ua", "org.ua", "gov.ua", "edu.ua",
+ "com.ru", "net.ru", "org.ru",
+ "co.id", "net.id", "or.id", "go.id", "ac.id",
+ "co.th", "in.th", "go.th", "ac.th",
+ "com.vn", "net.vn", "org.vn", "gov.vn", "edu.vn",
+ "co.il", "org.il", "net.il", "gov.il", "ac.il",
+ "com.ph", "net.ph", "org.ph", "gov.ph", "edu.ph",
+ "com.my", "net.my", "org.my", "gov.my", "edu.my",
+ "com.pk", "net.pk", "org.pk", "gov.pk", "edu.pk",
+ "com.eg", "net.eg", "org.eg", "gov.eg", "edu.eg",
+ "com.sa", "net.sa", "org.sa", "gov.sa", "edu.sa",
+ "com.ng", "net.ng", "org.ng", "gov.ng", "edu.ng",
+ "co.ke", "or.ke", "ne.ke", "go.ke", "ac.ke",
+ "com.co", "net.co", "org.co", "gov.co", "edu.co",
+ "com.pe", "net.pe", "org.pe", "gob.pe", "edu.pe",
+ "com.ec", "net.ec", "org.ec", "gob.ec", "edu.ec",
+ "com.uy", "net.uy", "org.uy", "gub.uy", "edu.uy",
+ "com.ve", "net.ve", "org.ve", "gob.ve", "edu.ve",
+ "com.py", "net.py", "org.py", "gov.py", "edu.py",
+ "com.bo", "net.bo", "org.bo", "gob.bo", "edu.bo",
+ "com.do", "net.do", "org.do", "gov.do", "edu.do",
+ "com.gt", "net.gt", "org.gt", "gob.gt", "edu.gt",
+ "co.cr", "or.cr", "go.cr", "ac.cr"
+ };
+
+ std::vector<std::string> split_labels(const std::string& s) {
+ std::vector<std::string> labels;
+ std::string cur;
+ for (char c : s) {
+ if (c == '.') {
+ labels.push_back(cur);
+ cur.clear();
+ } else {
+ cur += c;
+ }
+ }
+ labels.push_back(cur);
+ return labels;
+ }
+
+ bool is_ip_literal(const std::string& d) {
+ if (d.find(':') != std::string::npos)
+ return true;
+ int dots = 0;
+ for (unsigned char c : d) {
+ if (c == '.') {
+ ++dots;
+ continue;
+ }
+ if (!std::isdigit(c))
+ return false;
+ }
+ return dots == 3;
+ }
+
+ // eTLD+1 approximation: strip the public suffix and return it plus one label.
+ std::string registrable_domain(const std::string& host) {
+ const std::string d = normalize_domain(host);
+ if (d.empty())
+ return d;
+ if (is_ip_literal(d))
+ return d;
+
+ const std::vector<std::string> labels = split_labels(d);
+ if (labels.size() == 1)
+ return d;
+
+ const std::string last_two = labels[labels.size() - 2] + "." + labels.back();
+ if (kMultiPartSuffixes.count(last_two) != 0) {
+ if (labels.size() == 2)
+ return d;
+ return labels[labels.size() - 3] + "." + last_two;
+ }
+ return last_two;
+ }
+
+ // A page host matches a pinned sender domain when they share the same
+ // registrable domain: "accounts.google.com" matches "google.com", and
+ // "www.amazon.co.uk" matches "amazon.co.uk", while "amazon.com.evil.com"
+ // and "evilamazon.com" never match "amazon.com". An empty domain means the
// code is not pinned and any host may claim it.
bool host_matches_domain(const std::string& host, const std::string& domain) {
- const std::string h = normalize_domain(host);
const std::string d = normalize_domain(domain);
if (d.empty())
return true;
- if (h == d)
- return true;
- return h.size() > d.size() && h.ends_with("." + d);
+ return registrable_domain(host) == registrable_domain(d);
}
} // namespace
diff --git a/test/test_otp.cpp b/test/test_otp.cpp
index de02eaa..5518430 100644
--- a/test/test_otp.cpp
+++ b/test/test_otp.cpp
@@ -151,6 +151,23 @@ TEST_F(OtpTest, SenderDomainIsNormalized) {
EXPECT_FALSE(otp_take_for_host("amazon.com.evil.com", t0));
}
+TEST_F(OtpTest, PinnedCodeMatchesApexWhenSenderIsSubdomain) {
+ otp_store("123456", "accounts.google.com", t0);
+ EXPECT_TRUE(otp_take_for_host("google.com", t0));
+}
+
+TEST_F(OtpTest, PinnedCodeMatchesRegistrableCctld) {
+ otp_store("123456", "amazon.co.uk", t0);
+ EXPECT_TRUE(otp_take_for_host("www.amazon.co.uk", t0));
+ EXPECT_FALSE(otp_take_for_host("amazon.com", t0));
+ EXPECT_FALSE(otp_take_for_host("evil.co.uk", t0));
+}
+
+TEST_F(OtpTest, PinnedCodeRejectsCctldSpoof) {
+ otp_store("123456", "amazon.co.uk", t0);
+ EXPECT_FALSE(otp_take_for_host("amazon.co.uk.evil.com", t0));
+}
+
TEST_F(OtpTest, ConsumeClearsMatchingId) {
uint64_t id = otp_store("123456", "", t0);
otp_consume(id);