Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix browser extension not receiving OTP

Zack Bartel5bad703a2348
diff --git a/extension/src/content/autofill.js b/extension/src/content/autofill.js
index 430af19..4afaddf 100644
--- a/extension/src/content/autofill.js
+++ b/extension/src/content/autofill.js
@@ -154,7 +154,9 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (otpInterval) clearInterval(otpInterval);
} else {
const regularFields = findOtpInputs(document);
- if (regularFields.length > 0 && regularFields[0].value === "") {
+ // Fill when empty, or overwrite a stale/partial value — but skip if the code
+ // is already present to avoid redundant re-fills and event storms.
+ if (regularFields.length > 0 && regularFields[0].value !== otp) {
console.log("Autofilling OTP:", otp);
regularFields[0].value = otp;
diff --git a/extension/src/shared/native.js b/extension/src/shared/native.js
index e6d14bf..5e7b570 100644
--- a/extension/src/shared/native.js
+++ b/extension/src/shared/native.js
@@ -34,6 +34,11 @@ export function connectToNativeHost() {
}
export function sendToNativeHost(message) {
+ // Under MV3 the service worker (and its native host) can be torn down between
+ // sends; lazily re-establish the port so messages and the subscription survive.
+ if (!port) {
+ connectToNativeHost();
+ }
if (port) {
port.postMessage(message);
} else {
@@ -43,12 +48,16 @@ export function sendToNativeHost(message) {
function handleNativeMessage(message) {
// Dispatch native messages to other parts of the extension
- if (message.command === "otp_available") {
- // Notify content scripts or popup
+ if (message.command === "otp_available" && message.otp) {
+ // Deliver to every tab; the content script only fills when the page actually
+ // has OTP fields, so we don't depend on which tab happens to be focused.
if (typeof chrome !== 'undefined' && chrome.tabs) {
- chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
- if (tabs[0]) {
- chrome.tabs.sendMessage(tabs[0].id, { action: "fill_otp", otp: message.otp });
+ chrome.tabs.query({}, function(tabs) {
+ for (const tab of tabs) {
+ chrome.tabs.sendMessage(tab.id, { action: "fill_otp", otp: message.otp }, () => {
+ // Ignore tabs without a content script listening.
+ void chrome.runtime.lastError;
+ });
}
});
}
diff --git a/extension/test/native.test.js b/extension/test/native.test.js
new file mode 100644
index 0000000..22430b9
--- /dev/null
+++ b/extension/test/native.test.js
@@ -0,0 +1,60 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest';
+
+// Capture the onMessage listener registered by connectToNativeHost so we can
+// simulate the daemon pushing events to the native host.
+let messageListener = null;
+
+const mockPort = {
+ postMessage: vi.fn(),
+ onMessage: { addListener: vi.fn((cb) => { messageListener = cb; }) },
+ onDisconnect: { addListener: vi.fn() }
+};
+
+beforeEach(() => {
+ messageListener = null;
+ mockPort.postMessage.mockClear();
+
+ global.chrome = {
+ runtime: {
+ connectNative: vi.fn().mockReturnValue(mockPort),
+ lastError: null
+ },
+ tabs: {
+ // native.js uses the callback form of query()
+ query: vi.fn((_filter, cb) => cb([{ id: 1 }, { id: 2 }, { id: 3 }])),
+ sendMessage: vi.fn((_id, _msg, cb) => { if (cb) cb(); })
+ }
+ };
+ // Force native.js down the chrome.* branch.
+ global.browser = undefined;
+});
+
+import { connectToNativeHost } from '../src/shared/native.js';
+
+describe('native host receive/routing', () => {
+ it('delivers otp_available to every tab, not just the active one', () => {
+ connectToNativeHost();
+ expect(messageListener).toBeTypeOf('function');
+
+ messageListener({ command: 'otp_available', otp: '123456' });
+
+ expect(chrome.tabs.query).toHaveBeenCalledWith({}, expect.any(Function));
+ expect(chrome.tabs.sendMessage).toHaveBeenCalledTimes(3);
+ for (const id of [1, 2, 3]) {
+ expect(chrome.tabs.sendMessage).toHaveBeenCalledWith(
+ id,
+ { action: 'fill_otp', otp: '123456' },
+ expect.any(Function)
+ );
+ }
+ });
+
+ it('ignores an otp_available event with an empty code', () => {
+ connectToNativeHost();
+ expect(messageListener).toBeTypeOf('function');
+
+ messageListener({ command: 'otp_available', otp: '' });
+
+ expect(chrome.tabs.sendMessage).not.toHaveBeenCalled();
+ });
+});
diff --git a/src/core/src/net.cpp b/src/core/src/net.cpp
index 10953b7..a56cafa 100644
--- a/src/core/src/net.cpp
+++ b/src/core/src/net.cpp
@@ -418,6 +418,12 @@ namespace tether {
}
} else if (j.contains("command") && j["command"] == "new_otp" && j.contains("otp")) {
g_current_otp = j["otp"];
+ // Push the code to local subscribers (browser extension, GTK, etc.)
+ // so the browser fills it without depending on its polling timing.
+ nlohmann::json event;
+ event["command"] = "otp_available";
+ event["otp"] = g_current_otp;
+ broadcast_local_event(event.dump(), client_fd);
std::string payload = "{\"status\":\"ok\"}\n";
if (write(client_fd, payload.c_str(), payload.size()) < 0) {
debug::log(ERR, "net write error\n");
@@ -839,8 +845,12 @@ namespace tether {
// OTP sent from a mobile client (iPhone Share Extension) over mTLS.
// Store it in the global vault so the browser extension can retrieve it.
g_current_otp = j["otp"].get<std::string>();
- // Notify local subscribers (GTK, browser ext, etc.)
- broadcast_local_event(j.dump());
+ // Notify local subscribers (GTK, browser ext, etc.) using the
+ // otp_available shape the browser extension listens for.
+ nlohmann::json event;
+ event["command"] = "otp_available";
+ event["otp"] = g_current_otp;
+ broadcast_local_event(event.dump());
std::string payload = "{\"status\":\"ok\"}\n";
robust_ssl_write(ssl, payload.c_str(), payload.size());
continue;