entry.ts
1import { setupMessageHandler, setRestoreReady } from "./messages";2import { setupAutoLock } from "./alarms";3import { setupSyncAlarm } from "./sync-alarm";4import { setupPasskeyProxy } from "./passkey";5import { setupCommands } from "./commands";6import { setupClipboardClearAlarm } from "./clipboard";7import { restoreFromSession, isUnlocked, lock } from "./vault";8import { ensureSettingsLoaded } from "./vault";9import { isRecoveryConfirmPending } from "./recovery-pending";10import { denyAllPendingPasskeyRequests } from "./passkey";11import { hydratePersistedSyncState, setSyncRunner, triggerSyncIfReachable } from "./sync-trigger";12import { productionSyncRunner } from "./sync-runner";13import { repairPairedRevocationOverlaps } from "./paired-devices";1415// Keep alive for MV3 service worker16chrome.runtime.onInstalled.addListener(() => {});1718// The chrome.idle.onStateChanged listener lives in ./alarms (the architectural19// home for auto-lock timing). Keeping it in only one place avoids drift.2021// Lock-on-close via port disconnect. The popup opens a nonce'd port22// ("popup-<uuid>"); when that port disconnects (popup closed), lock if23// lockOnClose is enabled. Previously also declared in the never-bundled24// index.ts (deleted in LV-5) — this is now the single registration.25//26// LV-5: the lock port name carries a per-popup-open random suffix. Another27// extension cannot open ports INTO this extension (onConnectExternal is not28// registered), so the nonce is defense-in-depth — a port named exactly29// "popup" from anything that isn't our popup no longer reliably triggers30// deny-all + lock once the legacy window closes.31const POPUP_PORT_PREFIX = "popup-";3233chrome.runtime.onConnect.addListener((port) => {34 // Accept legacy bare "popup" for one release so an in-flight popup from the35 // previous version straddling the update doesn't lose lock-on-close.36 // TODO(2026-Q3): drop the bare "popup" acceptance once v3.8.0 has rolled out.37 if (port.name !== "popup" && !port.name.startsWith(POPUP_PORT_PREFIX)) return;38 port.onDisconnect.addListener(async () => {39 await denyAllPendingPasskeyRequests();40 const s = await ensureSettingsLoaded();41 if (!isUnlocked() || s.lockOnClose === false) return;42 if (await isRecoveryConfirmPending()) return;43 lock();44 });45});4647export function start(): void {48 // MV3: the chrome.runtime.onMessage listener MUST be registered during the49 // worker's initial SYNCHRONOUS evaluation. If registration is deferred behind50 // an await, a popup/content message that wakes a cold worker is dispatched51 // before the listener exists → "receiving end does not exist", and the popup52 // misreads the failed HAS_VAULT as "no vault" and shows onboarding.53 //54 // So: kick off session-restore, hand its promise to the message layer as a55 // gate, and register the handler synchronously. Unlock-dependent handlers56 // await the gate (see handleMessage), which preserves the SEV-3-B fix — no57 // spurious lock screen — without delaying listener registration.58 const restored = restoreFromSession();59 setRestoreReady(restored);60 setupMessageHandler();61 void setupAutoLock();62 setupCommands();63 // HV-2: clipboard auto-clear backstop must survive worker eviction; the64 // listener has to be registered during initial synchronous evaluation so65 // a cold worker woken BY the alarm still handles it.66 setupClipboardClearAlarm();67 void setupPasskeyProxy();68 // Wire the sync production runner into the dispatcher. Without this69 // `triggerSyncIfReachable` is a no-op (the runner slot starts null) — the70 // pairing/transport/coordinator stack ships dormant until this assignment.71 setSyncRunner(productionSyncRunner);72 // 10-minute background sync. Must be registered during initial synchronous73 // evaluation for the same reason as the clipboard alarm: a cold worker74 // woken BY the sync alarm needs the listener already attached. Setup is75 // idempotent — calling it after a SW revival is safe.76 setupSyncAlarm();77 // First dial without waiting for the alarm tick. The runner is wired and78 // the vault is unlocked enough that listPairedDevices works — the in-flight79 // guard inside triggerSyncIfReachable handles the case where another path80 // (e.g. a fresh pairing completing on the same wake) is already running.81 void restored82 .then(() => repairPairedRevocationOverlaps())83 .then(() => hydratePersistedSyncState())84 .then(() => triggerSyncIfReachable({ reason: "startup" }))85 .catch(() => undefined);86}8788start();