Files
CustosPublic/extension/background.js
T

92 lines
3.3 KiB
JavaScript

// Custos's service worker (docs/BROWSER.md). It owns the connection to the native host and answers the
// content scripts and the popup. The page origin it sends to Custos is always the browser's (`sender.url`,
// the tab's URL), never what a page says about itself; Custos decides which logins belong to it.
const HOST = 'cz.marek_novak.custos';
/** The native host, connected on first use; Edge and Chrome keep the worker alive while it is open. */
let port = null;
let seq = 0;
const waiting = new Map();
function connect() {
if (port) return port;
port = chrome.runtime.connectNative(HOST);
port.onMessage.addListener((msg) => {
const w = waiting.get(msg.seq);
if (!w) return;
waiting.delete(msg.seq);
w(msg);
});
port.onDisconnect.addListener(() => {
// Not registered, or the host exited: every open request fails the same way.
const why = chrome.runtime.lastError?.message ?? '';
const error = /not found|forbidden/i.test(why) ? 'not_connected' : 'not_running';
for (const w of waiting.values()) w({ ok: false, error });
waiting.clear();
port = null;
});
return port;
}
function ask(request) {
return new Promise((resolve) => {
const n = ++seq;
waiting.set(n, resolve);
try {
connect().postMessage({ ...request, seq: n });
} catch {
waiting.delete(n);
resolve({ ok: false, error: 'not_connected' });
}
// Custos may wait up to a minute for an unlock or an approval.
setTimeout(() => {
if (waiting.delete(n)) resolve({ ok: false, error: 'no_answer' });
}, 75_000);
});
}
/** `https://host[:port]` of a page or frame URL; null for anything Custos never fills. */
function originOf(url) {
try {
const u = new URL(url);
if (u.protocol !== 'https:' && u.protocol !== 'http:') return null;
return u.origin;
} catch {
return null;
}
}
chrome.runtime.onMessage.addListener((msg, sender, reply) => {
// Only this extension's own pages and content scripts reach here; pages cannot send to it.
if (sender.id !== chrome.runtime.id) return false;
(async () => {
switch (msg?.kind) {
case 'status':
return reply(await ask({ op: 'browser_status' }));
case 'match': {
// From a content script: the logins for the frame it runs in.
const origin = originOf(sender.url);
if (!origin) return reply({ ok: false, error: 'no_match' });
return reply({ ...(await ask({ op: 'browser_match', origin })), origin });
}
case 'fill': {
// The secrets go back to the frame that asked, and only if Custos agrees the login belongs there.
const origin = originOf(sender.url);
if (!origin || typeof msg.id !== 'string') return reply({ ok: false, error: 'no_match' });
return reply(await ask({ op: 'browser_fill', id: msg.id, origin }));
}
case 'match-tab': {
// From the popup: the logins for the active tab's page.
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
const origin = tab && originOf(tab.url);
if (!origin) return reply({ ok: false, error: 'no_match', tab: tab?.id ?? null });
return reply({ ...(await ask({ op: 'browser_match', origin })), origin, tab: tab.id });
}
default:
return reply({ ok: false, error: 'bad_request' });
}
})();
return true;
});