messages.ts

87.4 KB · raw

1/**2 * messages.ts — the only background message handler.3 *4 * All `chrome.runtime.onMessage` handling for the service worker lives here.5 * Do not handle messages inline in entry.ts, alarms.ts, or any other6 * background file. Every background-bound MessageType in shared/types.ts7 * must have a case below. Content-script-bound message types (PING,8 * SUMMON_AUTOFILL, VAULT_LOCKED) are documented in shared/types.ts and9 * intentionally have no case here — they are sent *to* content scripts.10 */1112import { copyToClipboard, getEffectiveClearSeconds } from "./clipboard";13import { getPendingSyncAlerts, clearPendingSyncAlerts } from "./sync-alerts";14import { extractDomain } from "../shared/domain";15import {16  getPendingVouchedDevice,17  listPendingVouchedDevices,18  removePendingVouchedDevice,19} from "./pending-vouched-devices";20import {21  clearRecoveryConfirmPending,22  isRecoveryConfirmPending,23  setRecoveryConfirmPending,24} from "./recovery-pending";25import { setupAutoLock, touchAutoLock } from "./alarms";26import { runSelfTest } from "./self-test";27import * as vault from "./vault";28import {29  handleEstimateCodexPages,30  handleGetCodexVaultData,31  handleRestoreFromCodex,32} from "./codex-handlers";33import type { CodexRestorePayload } from "../shared/codex-types";34import {35  activateLicense,36  deactivateLicense,37  getLicenseInfo,38  getLicenseKey,39  isProUser,40  markCelebrationPlayed,41  shouldPlayCelebration,42} from "./license";43import {44  completeFirefoxPasskeyCreate,45  getPasskeyProviderState,46  getPendingPasskeyRequest,47  handleFirefoxPasskeyCreate,48  handleFirefoxPasskeyGet,49  handlePasskeyHasCredentials,50  initiateFirefoxPasskeyCreate,51  isPasskeyOfferDeclined,52  onVaultUnlocked,53  recordPasskeyOfferDecline,54  resolvePendingPasskeyRequest,55  setPasskeyProviderEnabled,56} from "./passkey";57import { buildAdvancedHealthReport } from "./health";58import { storageClear } from "../shared/storage";59import { findInvalidWords } from "../shared/recovery-words";60import type {61  Message,62  MessageResponse,63  MessageType,64  ProFeature,65  VaultEntry,66} from "../shared/types";67import { isPasskeyOnly } from "../shared/types";68import { generateTOTP } from "../crypto/totp";69import { isValidRpId } from "../shared/passkey-utils";70import { isValidMLFeatureBatch, predictBatch as mlPredictBatch } from "./ml";71import {72  buildLocalIdentity,73  getOrCreateDeviceId,74  setDeviceName as setDeviceNameV3,75  type LocalDeviceIdentity,76} from "./device-identity";77import {78  isDeviceRevoked,79  listPairedDevices as listPairedDevicesV3,80  renamePairedDevice as renamePairedDeviceV3,81  revokeDeviceById as revokeDeviceByIdV3,82  TrustDataCorruptedError,83  upsertPairedDevice as upsertPairedDeviceV3,84} from "./paired-devices";85import { dropPairSecret } from "./pair-secrets";86// Sync and pairing are imported statically, never with `await import()`.87// `import()` throws `TypeError: import() is disallowed on88// ServiceWorkerGlobalScope` at runtime in MV3, and Vite's preload helper89// reports the failure through `window.dispatchEvent`, which does not exist in90// a worker either — so the real reason was replaced by "window is not defined"91// and the pairing UI just stopped. scripts/check-sw-static-imports.mjs fails92// the build if a runtime import() reappears anywhere in the worker's graph.93import {94  cancelRelayAdoptionSource,95  isRelayAdoptionSourceInFlight,96  resolveReceiverSasConfirmation,97  resolveSourceSasConfirmation,98  startRelayAdoptionReceiver,99  startRelayAdoptionSource,100} from "./relay-adoption-bg";101import {102  listPendingAdoptionHolders,103  removePendingAdoptionHolder,104} from "./pending-adoption-holder";105import {106  getSyncSettings,107  setRelayUrl,108  setSyncMode,109  type SyncMode,110} from "./sync-settings";111import {112  getSyncState,113  triggerSyncIfReachable,114} from "./sync-trigger";115import {116  browserGenerationIo,117  confirmSecureReset,118  enterResetRequired,119  resolveGeneration,120} from "./sync-generation";121import { isMessageSenderAllowed } from "./message-policy";122123const SUMMON_CAPABILITY_TTL_MS = 30_000;124type SummonCapability = { tabId: number; frameId: number; origin: string; operation: "inventory" | "fill"; expiresAt: number };125const summonCapabilities = new Map<string, SummonCapability>();126127export function createSummonCapability(tabId: number, origin: string, frameId = 0): string {128  const token = crypto.randomUUID();129  summonCapabilities.set(token, { tabId, frameId, origin, operation: "inventory", expiresAt: Date.now() + SUMMON_CAPABILITY_TTL_MS });130  return token;131}132133function consumeSummonCapability(token: unknown, sender: Partial<chrome.runtime.MessageSender>, operation: "inventory" | "fill"): boolean {134  if (typeof token !== "string") return false;135  const cap = summonCapabilities.get(token);136  if (!cap) return false;137  if (cap.expiresAt <= Date.now()) {138    summonCapabilities.delete(token);139    return false;140  }141  if (sender.tab?.id !== cap.tabId || (sender.frameId ?? 0) !== cap.frameId || attestedSenderOrigin(sender) !== cap.origin) return false;142  if (cap.operation !== operation) return false;143  if (operation === "inventory") cap.operation = "fill";144  else summonCapabilities.delete(token);145  return true;146}147148// Compile-time E2E flag, folded to a literal by Vite's `define` (false in149// every production build — verified in the bundled output). Mirrors the150// create-path bypass in passkey.ts so the Playwright passkey smoke can enable151// the provider without a real Pro licence; the gate is fully intact for users.152declare const __PEACH_E2E_PASSKEYS__: boolean;153const PASSKEY_E2E_PRO_BYPASS = __PEACH_E2E_PASSKEYS__ === true;154155function unhexBytes(hex: string): Uint8Array {156  if (hex.length % 2 !== 0) throw new Error("invalid hex");157  const out = new Uint8Array(hex.length / 2);158  for (let i = 0; i < out.length; i++) {159    out[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);160  }161  return out;162}163164/**165 * The single Pro gate. Every gated handler calls this first; the broadcast166 * lets the popup react (navigate to the License section, highlight the167 * relevant feature). Background never touches the popup view directly —168 * the popup owns navigation. See Projects.md "Pro gate rule".169 */170async function requirePro(171  feature: ProFeature,172  trigger: "user" | "background" = "user"173): Promise<{ allowed: boolean }> {174  if (PASSKEY_E2E_PRO_BYPASS && feature === "passkeys") return { allowed: true };175  if (await isProUser()) return { allowed: true };176177  if (trigger === "user") {178    // User deliberately triggered this feature — redirect the popup immediately.179    chrome.runtime180      .sendMessage({ type: "PRO_GATE_TRIGGERED", payload: { feature } })181      .catch(() => {182        // Popup may be closed — the gate signal is best-effort.183      });184  } else {185    // Background trigger (e.g. passive sync initiation) — no popup is open.186    // Queue a notice so the next popup open shows a soft banner instead of187    // silently failing.188    if (chrome.storage.session) {189      chrome.storage.session190        .set({ pendingProNotice: { feature, triggeredAt: Date.now() } })191        .catch(() => {});192    }193  }194195  return { allowed: false };196}197198// LV-3: minimum interval between non-test canary pings per webhook+entry.199const CANARY_MIN_INTERVAL_MS = 60 * 60 * 1000; // 1 hour200201const PRIVATE_IPV4_RANGES = [202  { prefix: "10." },203  { prefix: "127." },204  { prefix: "169.254." },205  { prefix: "192.168." },206];207208export function setupMessageHandler(): void {209  chrome.runtime.onMessage.addListener(210    (msg: unknown, sender, sendResponse) => {211      if (!isMessage(msg)) {212        sendResponse({ ok: false, error: "invalid_message" });213        return true;214      }215      handleMessage(msg, sender)216        .then(sendResponse)217        .catch((err) =>218          sendResponse({219            ok: false,220            error: err instanceof Error ? err.message : String(err),221          })222        );223      return true;224    }225  );226}227228function isMessage(m: unknown): m is Message {229  return typeof m === "object" && m !== null && "type" in m;230}231232/**233 * The browser-attested origin of the page that originated this message. A web234 * page cannot call chrome.runtime.sendMessage directly (no externally_connectable),235 * so every page-driven message arrives via the content-script relay and the236 * browser stamps `sender.origin`/`sender.url` with the real frame origin — a237 * value the page cannot forge. This is the ONLY trustworthy origin for passkey238 * rpId<->origin enforcement; payload-supplied origins are attacker-controlled.239 */240function attestedSenderOrigin(sender: Partial<chrome.runtime.MessageSender>): string | undefined {241  if (sender.origin) return sender.origin;242  if (sender.url) {243    try { return new URL(sender.url).origin; } catch { return undefined; }244  }245  return undefined;246}247248function isExtensionPageSender(sender: Partial<chrome.runtime.MessageSender>): boolean {249  return sender.url?.startsWith("chrome-extension://") === true ||250    sender.url?.startsWith("moz-extension://") === true;251}252253/** True when the message came from a page frame rather than extension UI. */254function isPageSender(sender: Partial<chrome.runtime.MessageSender>): boolean {255  return !isExtensionPageSender(sender) &&256    (sender.tab !== undefined || sender.url !== undefined);257}258259function entryDomains(url: unknown, additionalUrls: unknown): Set<string> {260  const domains = new Set<string>();261  const primary = typeof url === "string" ? extractDomain(url) : null;262  if (primary) domains.add(primary);263  if (Array.isArray(additionalUrls)) {264    for (const extra of additionalUrls) {265      if (typeof extra !== "string") continue;266      const domain = extractDomain(extra);267      if (domain) domains.add(domain);268    }269  }270  return domains;271}272273/**274 * PB-26-106: the autofill read path re-validates the frame origin against the275 * entry's domains on every request, so an unbound write path would let a276 * content script repoint an entry at its own origin and then read the277 * credential through a read path that is behaving correctly.278 *279 * Creating an entry: a page may only bind it to itself. Mutating one: a page280 * may never GROW the domain set — only shrink it, and never below its own281 * attested origin.282 *283 * Self-binding on mutation was considered and is not enough: the content284 * script on a foreign origin IS the attacker in the reported chain, so letting285 * it attach "its own" origin lets it mint the very capability the read path286 * exists to withhold. The legitimate flow that needs a new domain — the inline287 * "remember this site" toast — therefore asks the popup instead288 * (REQUEST_REMEMBER_SITE), which is an extension page and exempt here.289 *290 * Extension pages are exempt: the popup edits entries for sites it is not on.291 */292function senderMayBindEntryDomains(293  sender: Partial<chrome.runtime.MessageSender>,294  domains: Set<string>,295): boolean {296  if (isExtensionPageSender(sender)) return true;297  const domain = attestedSenderDomain(sender);298  if (domain === null) return false;299  for (const candidate of domains) {300    if (candidate !== domain) return false;301  }302  return true;303}304305function senderMayMutateEntryDomains(306  sender: Partial<chrome.runtime.MessageSender>,307  before: Set<string>,308  after: Set<string>,309): boolean {310  if (isExtensionPageSender(sender)) return true;311  const domain = attestedSenderDomain(sender);312  if (domain === null || !after.has(domain)) return false;313  for (const kept of after) {314    if (!before.has(kept)) return false;315  }316  return true;317}318319/**320 * Null for extension pages (which the callers exempt), for senders with no321 * attested origin at all, and for unparseable frame urls.322 */323function attestedSenderDomain(sender: Partial<chrome.runtime.MessageSender>): string | null {324  if (sender.tab === undefined && sender.url === undefined) return null;325  const origin = attestedSenderOrigin(sender);326  if (!origin) return null;327  try { return extractDomain(new URL(origin).hostname); } catch { return null; }328}329330function patchChangesEntryOrigin(patch: object): boolean {331  return Object.hasOwn(patch, "url") || Object.hasOwn(patch, "additionalUrls");332}333334/** The entry's domain-bearing fields as the patch would leave them. */335function patchedUrlShape(336  patch: object,337  existing: Pick<VaultEntry, "url" | "additionalUrls">,338): { url: unknown; additionalUrls: unknown } {339  const candidate = patch as { url?: unknown; additionalUrls?: unknown };340  return {341    url: Object.hasOwn(patch, "url") ? candidate.url : existing.url,342    additionalUrls: Object.hasOwn(patch, "additionalUrls")343      ? candidate.additionalUrls344      : existing.additionalUrls,345  };346}347// Resolves once restoreFromSession() has run, so handlers see the correct348// post-restore unlock state. entry.ts registers this BEFORE the message349// handler so the onMessage listener is added synchronously at SW startup350// (MV3 requirement) while unlock-dependent handlers still wait for restore.351let restoreReady: Promise<void> = Promise.resolve();352const MAX_ML_BATCH_FIELDS = 128;353const MAX_ML_FIELDS_PER_SECOND = 256;354const mlWorkBySender = new Map<string, { startedAt: number; fields: number }>();355356function claimMLWork(sender: Partial<chrome.runtime.MessageSender>, fields: number): boolean {357  if (fields === 0) return true;358  const now = Date.now();359  for (const [key, value] of mlWorkBySender) {360    if (now - value.startedAt >= 1_000) mlWorkBySender.delete(key);361  }362  const key = `${sender.tab?.id ?? "ui"}:${sender.frameId ?? 0}:${attestedSenderOrigin(sender) ?? "extension"}`;363  const current = mlWorkBySender.get(key);364  if (!current) {365    mlWorkBySender.set(key, { startedAt: now, fields });366    return fields <= MAX_ML_FIELDS_PER_SECOND;367  }368  if (current.fields + fields > MAX_ML_FIELDS_PER_SECOND) return false;369  current.fields += fields;370  return true;371}372373/** Called by entry.ts at startup with the in-flight restoreFromSession promise. */374export function setRestoreReady(p: Promise<unknown>): void {375  restoreReady = p.then(() => undefined, () => undefined);376}377378export async function handleMessage(379  msg: Message,380  sender: Partial<chrome.runtime.MessageSender> = {}381): Promise<MessageResponse> {382  try {383    // Wait for session-restore to finish before reading any unlock/vault state.384    // On a cold SW start the popup's first message arrives while restore is385    // still in flight; without this gate IS_UNLOCKED would report locked (a386    // spurious lock screen) and the storage reads could race. The onMessage387    // listener itself is registered synchronously (entry.ts) so this gate, not388    // listener registration, is what absorbs the cold-start window.389    await restoreReady;390    // Validate sender — only allow messages from our extension pages/scripts.391    // `sender.id` is undefined for messages originating from extension UI392    // (popup/options), which is legitimate; reject only when it is defined393    // and does not match our extension id.394    if (395      sender.id !== undefined &&396      sender.id !== chrome.runtime.id397    ) {398      return { ok: false, error: "invalid_sender" };399    }400401    // PB-26-016: sender authorization is an exhaustive policy, not a402    // hand-maintained privileged denylist. A missing/unknown policy entry or a403    // background-only message fails closed before any handler runs.404    // Extension UI messages may legitimately omit sender.url. Resolve the405    // browser-attested extension origin only when a URL is present; this also406    // keeps test/runtime shims without getURL fail-closed for URL-bearing407    // senders instead of throwing before policy evaluation.408    const extensionOrigin = sender.url && typeof chrome.runtime.getURL === "function"409      ? chrome.runtime.getURL("")410      : "";411    if (!isMessageSenderAllowed(msg.type, sender, chrome.runtime.id, extensionOrigin)) {412      return { ok: false, error: "invalid_sender" };413    }414415    if (shouldRefreshAutoLock(msg.type, sender) && vault.isUnlocked()) {416      touchAutoLock();417    }418419    switch (msg.type) {420      case "CREATE_VAULT": {421        const { password } = msg.payload as { password: string };422        // Refuse to create a vault under an empty master password — the derived423        // key would be publicly known and the at-rest AES-GCM protection void424        // (audit HIGH; the CSV-import path previously passed ""). Length/strength425        // policy lives in the onboarding UI; this is the non-negotiable floor.426        if (typeof password !== "string" || password.length === 0) {427          return { ok: false, error: "invalid_payload" };428        }429        await vault.createVault(password);430        await setupAutoLock();431        broadcastToContentScripts({ type: "VAULT_UNLOCKED" });432        onVaultUnlocked();433        return { ok: true };434      }435      case "CREATE_VAULT_FROM_ROOT_KEY": {436        const { password, rootKeyHex } = (msg.payload ?? {}) as {437          password?: unknown;438          rootKeyHex?: unknown;439        };440        if (typeof password !== "string" || password.length === 0 || typeof rootKeyHex !== "string") {441          return { ok: false, error: "invalid_payload" };442        }443444        try {445          const { vaultId } = await vault.createVaultFromRootKey(password, rootKeyHex);446          await setupAutoLock();447          broadcastToContentScripts({ type: "VAULT_UNLOCKED" });448          onVaultUnlocked();449          return { ok: true, vaultId };450        } catch (err) {451          return { ok: false, error: err instanceof Error ? err.message : String(err) };452        }453      }454      case "UNLOCK": {455        const { password } = msg.payload as { password: string };456        const ok = await vault.unlock(password);457        if (ok) {458          // KAT: verify the WebCrypto stack is healthy before the user relies on it.459          // Runs after unlock so latency is hidden behind the vault-load time.460          const kat = await runSelfTest();461          if (!kat.ok) {462            console.error("[Peach] Crypto self-test FAILED:", kat.error);463            await vault.lock();464            return { ok: false, error: "self_test_failed" };465          }466          await vault.updateSettings({ pinPreferred: false });467          await setupAutoLock();468          broadcastToContentScripts({ type: "VAULT_UNLOCKED" });469          onVaultUnlocked();470          return { ok: true };471        }472        // Distinguish corruption from wrong password so the popup can offer473        // a history-restore path instead of falsely accusing the user of474        // mistyping (M7/COR-007).475        const failure = vault.consumeLastUnlockFailureReason();476        if (failure) {477          return {478            ok: false,479            error: "vault_corrupted",480            canRestoreFromHistory: true,481          } as MessageResponse;482        }483        return { ok: false };484      }485      case "UNLOCK_WITH_PIN": {486        const { pin } = (msg.payload ?? {}) as { pin?: unknown };487        if (typeof pin !== "string") return { ok: false, error: "invalid_payload" };488        const ok = await vault.unlockWithPin(pin);489        if (!ok) {490          const status = await vault.getPinUnlockStatus();491          if (status.configured && !status.available) {492            return {493              ok: false,494              error: status.attemptsRemaining === 0495                ? "pin_attempts_exceeded"496                : "pin_session_expired",497            };498          }499          return { ok: false };500        }501        const kat = await runSelfTest();502        if (!kat.ok) {503          await vault.lock();504          return { ok: false, error: "self_test_failed" };505        }506        await vault.updateSettings({ pinPreferred: true });507        await setupAutoLock();508        broadcastToContentScripts({ type: "VAULT_UNLOCKED" });509        onVaultUnlocked();510        return { ok: true };511      }512      case "GET_PIN_STATUS":513        return { ok: true, data: await vault.getPinUnlockStatus() };514      case "VALIDATE_RECOVERY_WORDS": {515        const { words } = msg.payload as { words: string[] };516        const invalid = findInvalidWords(words);517        return { ok: true, data: { valid: invalid.length === 0, invalid } };518      }519      case "GET_RECOVERY_WORDS": {520        // Must be unlocked and have recovery set up (vault must be v2.12+).521        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };522        // LV-4: the recovery words re-derive the ROOT KEY — the highest-523        // value secret in the vault. A session unlock (which may be hours524        // old on a shared machine) is not enough; require a fresh password525        // proof, matching the 1Password/Bitwarden pattern for revealing526        // escrow material. EXEMPTION: the create-vault / rotation confirmation527        // window (peach_recovery_pending) shows the words immediately after528        // the user authenticated, and that confirmation UI has no password to529        // resend — skip the re-auth there only.530        if (!(await isRecoveryConfirmPending())) {531          const { password } = (msg.payload ?? {}) as { password?: string };532          if (typeof password !== "string" || !password) {533            return { ok: false, error: "password_required" };534          }535          if (!(await vault.verifyMainPassword(password))) {536            return { ok: false, error: "wrong_password" };537          }538        }539        try {540          const words = await vault.getRecoveryWords();541          return { ok: true, data: words };542        } catch {543          return { ok: false, error: "recovery_not_setup" };544        }545      }546      case "GET_RECOVERY_CONFIRM_PENDING":547        return { ok: true, data: await isRecoveryConfirmPending() };548      case "SET_RECOVERY_CONFIRM_PENDING":549        await setRecoveryConfirmPending();550        return { ok: true };551      case "CLEAR_RECOVERY_CONFIRM_PENDING":552        await clearRecoveryConfirmPending();553        return { ok: true };554      case "RECOVER_WITH_WORDS": {555        // User has lost their password but has their 12 recovery words.556        // We: (1) validate the words, (2) re-encrypt vault with new password.557        const { words, newPassword } = msg.payload as {558          words: string[];559          newPassword: string;560        };561        const stored = await vault.loadSlotA();562        if (!stored) return { ok: false, error: "no_vault" };563        if (!stored.recovery_salt || !stored.recovery_wrapped_root_key) {564          return { ok: false, error: "recovery_not_setup" };565        }566        // Unwrap the root key from the recovery escrow.567        try {568          await vault.unwrapRootKeyForRecovery(569            stored.recovery_wrapped_root_key,570            stored.recovery_nonce ?? "",571            stored.recovery_tag ?? "",572            stored.recovery_salt,573            words574          );575        } catch {576          return { ok: false, error: "invalid_recovery_words" };577        }578        // Re-key: derive fresh salt + key from new password.579        const newSalt = vault.generateSalt();580        const newKey = await vault.deriveKey(newPassword, newSalt);581        // Re-wrap root key under the new password's derived key so the same582        // recovery words continue to work after the password change.583        await vault.rewrapRootKeyForPassword(newKey);584        // Re-encrypt vault entries with the new key.585        await vault.changePasswordWithNewKey(newSalt, newKey);586        await setupAutoLock();587        broadcastToContentScripts({ type: "VAULT_UNLOCKED" });588        return { ok: true };589      }590      case "LOCK":591        await vault.lock();592        return { ok: true };593      case "IS_UNLOCKED":594        return { ok: true, data: vault.isUnlocked() };595      case "GET_VAULT_STATUS":596        return { ok: true, unlocked: vault.isUnlocked() };597      case "HAS_VAULT":598        return { ok: true, data: await vault.hasVault() };599      case "GET_ENTRIES":600        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };601        return { ok: true, data: vault.getEntries() };602      case "EXPORT_ENTRIES": {603        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };604        const { password } = (msg.payload ?? {}) as { password?: unknown };605        if (typeof password !== "string" || !password) {606          return { ok: false, error: "password_required" };607        }608        if (!(await vault.verifyMainPassword(password))) {609          return { ok: false, error: "wrong_password" };610        }611        return { ok: true, data: vault.getEntries() };612      }613      case "ADD_ENTRY": {614        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };615        const payload = msg.payload;616        if (!payload || typeof payload !== "object" || Array.isArray(payload)) {617          return { ok: false, error: "invalid_payload" };618        }619        // PB-26-106: a page may only create an entry bound to itself. Entries620        // carrying no url have no domain surface to bind, so they are exempt.621        if (isPageSender(sender)) {622          const draft = payload as { url?: unknown; additionalUrls?: unknown };623          const domains = entryDomains(draft.url, draft.additionalUrls);624          if (domains.size > 0 && !senderMayBindEntryDomains(sender, domains)) {625            return { ok: false, error: "origin_binding_required" };626          }627        }628        const entry = await vault.addEntry(payload as Parameters<typeof vault.addEntry>[0]);629        return { ok: true, data: entry };630      }631      case "BATCH_ADD_ENTRIES": {632        const payload = msg.payload as { entries?: unknown } | null | undefined;633        if (!payload || typeof payload !== "object" || Array.isArray(payload)) {634          return { ok: false, error: "invalid_payload" };635        }636        if (!Array.isArray(payload.entries)) {637          return { ok: false, error: "invalid_payload" };638        }639        const added = await vault.batchAddEntries(payload.entries as Parameters<typeof vault.batchAddEntries>[0]);640        return { ok: true, data: added };641      }642      case "RESTORE_ENTRIES": {643        const payload = msg.payload as644          | { entries?: unknown; mode?: unknown; settings?: unknown }645          | null646          | undefined;647        if (!payload || typeof payload !== "object" || Array.isArray(payload)) {648          return { ok: false, error: "invalid_payload" };649        }650        if (!Array.isArray(payload.entries)) {651          return { ok: false, error: "invalid_payload" };652        }653        if (payload.mode !== "replace" && payload.mode !== "merge") {654          return { ok: false, error: "invalid_payload" };655        }656        const summary = await vault.restoreEntries(657          payload.entries as Parameters<typeof vault.restoreEntries>[0],658          payload.mode659        );660        if (payload.settings && typeof payload.settings === "object" && !Array.isArray(payload.settings)) {661          await vault.updateSettings(payload.settings as vault.VaultSettings);662        }663        return { ok: true, data: summary };664      }665      case "CLEAR_VAULT_ENTRIES": {666        await vault.clearVaultEntries("REPLACE_ALL");667        return { ok: true };668      }669      case "GET_VAULT_HISTORY": {670        // The history store is real-mode state by construction, so a decoy671        // session must not see it — this is where the duress session would672        // otherwise leak the real vault's activity and size (PB-26-915).673        if (vault.isDecoyMode()) return { ok: true, data: [] };674        const snapshots = await vault.getVaultHistory();675        return { ok: true, data: snapshots };676      }677      case "RESTORE_VAULT_SNAPSHOT": {678        // Same guard on the write side: a coerced session must not be able to679        // roll the real vault backwards onto an earlier snapshot.680        if (vault.isDecoyMode()) return { ok: false, error: "vault_locked" };681        const { snapshotId } = msg.payload as { snapshotId: string };682        const result = await vault.restoreVaultSnapshot(snapshotId);683        if (!result.ok) return { ok: false, error: result.error };684        broadcastToContentScripts({ type: "VAULT_LOCKED" });685        return { ok: true };686      }687      case "COUNT_DUPLICATES": {688        return { ok: true, data: vault.countDuplicates() };689      }690      case "DEDUPLICATE_ENTRIES": {691        const removed = await vault.deduplicateEntries();692        return { ok: true, data: removed };693      }694      case "UPDATE_ENTRY": {695        const payload = msg.payload as { id?: unknown; patch?: unknown } | null | undefined;696        if (!payload || typeof payload !== "object" || Array.isArray(payload)) {697          return { ok: false, error: "invalid_payload" };698        }699        const { id, patch } = payload;700        if (typeof id !== "string" || !id || !patch || typeof patch !== "object" || Array.isArray(patch)) {701          return { ok: false, error: "invalid_payload" };702        }703        const existingEntry = vault.getEntryById(id);704        if (!existingEntry) return { ok: false, error: "entry_not_found" };705        if (patchChangesEntryOrigin(patch) || isPageSender(sender)) {706          const before = entryDomains(existingEntry.url, existingEntry.additionalUrls);707          const after = patchedUrlShape(patch, existingEntry);708          if (!senderMayMutateEntryDomains(709            sender,710            before,711            entryDomains(after.url, after.additionalUrls),712          )) {713            return { ok: false, error: "origin_binding_required" };714          }715        }716        // Single enforcement point for the Canary Entries Pro gate. Only717        // gates the transition into a canary entry — clearing/disabling a718        // canary (`canary: undefined`) is always allowed, so existing719        // canary entries remain manageable.720        const incomingCanary = (patch as { canary?: unknown }).canary;721        if (incomingCanary && typeof incomingCanary === "object") {722          const existing = vault.getEntryById(id);723          if (!existing?.canary) {724            const gate = await requirePro("canary_entry");725            if (!gate.allowed) return { ok: false, error: "pro_required" };726          }727        }728        await vault.updateEntry(id, patch as Parameters<typeof vault.updateEntry>[1]);729        return { ok: true };730      }731      case "ADD_ADDITIONAL_URL": {732        const { id, url } = msg.payload as { id: string; url: string };733        const trimmed = typeof url === "string" ? url.trim() : "";734        if (!trimmed) return { ok: false, error: "url_required" };735        const existing = vault.getEntryById(id);736        if (!existing) return { ok: false, error: "entry_not_found" };737        const merged = [...(existing.additionalUrls ?? []), trimmed];738        if (!senderMayMutateEntryDomains(739          sender,740          entryDomains(existing.url, existing.additionalUrls),741          entryDomains(existing.url, merged),742        )) {743          return { ok: false, error: "origin_binding_required" };744        }745        if (existing.type !== "login") return { ok: false, error: "login_entries_only" };746        await vault.updateEntry(id, { additionalUrls: merged });747        return { ok: true };748      }749      case "REQUEST_REMEMBER_SITE": {750        // PB-26-106: growing an entry's domain set needs a decision the page751        // cannot make for itself, so the page asks and the popup confirms. A752        // page may only ask about its own attested origin — otherwise this753        // becomes the same unbound write under a friendlier name.754        const { id, url } = (msg.payload ?? {}) as { id?: unknown; url?: unknown };755        if (typeof id !== "string" || !id) return { ok: false, error: "invalid_payload" };756        const trimmed = typeof url === "string" ? url.trim() : "";757        if (!trimmed) return { ok: false, error: "url_required" };758        if (!senderMayBindEntryDomains(sender, entryDomains(trimmed, undefined))) {759          return { ok: false, error: "origin_binding_required" };760        }761        if (!vault.getEntryById(id)) return { ok: false, error: "entry_not_found" };762        if (chrome.storage.session) {763          await chrome.storage.session.set({ pendingRememberSite: { entryId: id, url: trimmed } });764        }765        try { await chrome.action.openPopup(); } catch { /* user opens via toolbar */ }766        return { ok: true };767      }768      case "DELETE_ENTRY": {769        const { id } = msg.payload as { id: string };770        await vault.deleteEntry(id);771        return { ok: true };772      }773      case "GET_DELETED_ENTRIES":774        return { ok: true, data: vault.getDeletedEntries() };775      case "RESTORE_ENTRY": {776        const { id } = msg.payload as { id: string };777        await vault.restoreEntry(id);778        return { ok: true };779      }780      case "PURGE_ENTRY": {781        const { id } = msg.payload as { id: string };782        await vault.purgeEntry(id);783        return { ok: true };784      }785      case "PURGE_EXPIRED_TRASH": {786        const { retentionMs } = msg.payload as { retentionMs: number };787        const purged = await vault.purgeExpiredTrash(retentionMs);788        return { ok: true, data: purged };789      }790      case "SAVE_NOTE": {791        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };792        const { id, title, body } = msg.payload as { id?: string; title: string; body: string };793        const now = Date.now();794        if (id) {795          const existing = vault.getEntryById(id);796          if (!existing) return { ok: false, error: "not_found" };797          await vault.updateEntry(id, { title: title || "Untitled", notes: body, modifiedAt: now });798          return { ok: true, data: vault.getEntryById(id) };799        } else {800          // PCH-01-026 #10: addEntry stamps its own createdAt/modifiedAt801          // (EntryDraft omits both), so passing them was the only reason the802          // `as any` cast existed. Dropping them makes the call type-check.803          const entry = await vault.addEntry({804            type: "note",805            title: title || "Untitled",806            notes: body,807          });808          return { ok: true, data: entry };809        }810      }811      case "DELETE_NOTE": {812        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };813        const { id } = msg.payload as { id: string };814        await vault.deleteEntry(id);815        return { ok: true };816      }817      case "GET_NOTES": {818        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };819        const notes = vault.getEntries().filter(820          (e) => e.type === "note" && !e.deletedAt821        );822        return { ok: true, data: notes } as MessageResponse;823      }824      case "SEARCH_VAULT": {825        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };826        // AUD-002: shape-validate the payload before calling string methods.827        // A raw cast lets a malformed message crash the handler with TypeError.828        const { query } = (msg.payload ?? {}) as { query?: unknown };829        if (typeof query !== "string") {830          return { ok: false, error: "invalid_payload" };831        }832        const q = query.toLowerCase();833        const searchResults = vault.getEntries().filter(834          (e) =>835            !e.deletedAt &&836            (e.title.toLowerCase().includes(q) ||837              e.notes?.toLowerCase().includes(q) ||838              e.username?.toLowerCase().includes(q))839        );840        return { ok: true, data: searchResults } as MessageResponse;841      }842      case "AUTOFILL_QUERY": {843        if (!vault.isUnlocked()) {844          return { ok: false, error: "vault_locked" };845        }846        const { domain, kind, capability } = msg.payload as {847          domain: string;848          kind?: "login" | "card" | "totp";849          capability?: string;850        };851        const includeAll = consumeSummonCapability(capability, sender, "inventory");852        if (capability !== undefined && !includeAll) {853          return { ok: false, error: "entry_not_found" };854        }855        // kind="card": return saved cards as compact summaries for the856        // inline card silver platter. Cards are not domain-scoped — every857        // saved card is a candidate on every checkout page — so domain is858        // ignored when kind === "card".859        if (kind === "card") {860          if (!includeAll) return { ok: true, data: [] };861          const cards = vault.getEntries().filter((e) => e.type === "card");862          return {863            ok: true,864            data: cards.map((e) => ({865              id: e.id,866              title: e.cardNickname || e.title,867              cardholderName: e.cardholderName,868              last4: e.last4,869              expMonth: e.expMonth,870              expYear: e.expYear,871              network: e.network,872            })),873          };874        }875        const scopedDomain = isPageSender(sender)876          ? attestedSenderDomain(sender)877          : typeof domain === "string"878            ? extractDomain(domain)879            : null;880        if (!includeAll && !scopedDomain) {881          return isPageSender(sender)882            ? { ok: false, error: "origin_unverified" }883            : { ok: true, data: [] };884        }885        if (kind === "totp") {886          const matches = (887            includeAll888              ? vault.getEntries().filter((entry) => entry.type !== "note" && entry.type !== "card")889              : vault.queryDomain(scopedDomain!)890          ).filter((entry) => !isPasskeyOnly(entry) && Boolean(entry.totp));891          return {892            ok: true,893            data: matches.map((e) => ({894              id: e.id,895              title: e.title,896              username: e.username,897              email: e.email,898              url: e.url,899              hasTotp: true,900            })),901          };902        }903        // The inline selector fills username+password into a form. Passkey-904        // only entries have no password to fill; sign-in for those goes905        // through the WebAuthn path instead. Filter them out everywhere so906        // the user doesn't see four "GitHub" rows that mostly do nothing.907        // Notes and cards never belonged here either.908        const matches = (909          includeAll910            ? vault911                .getEntries()912                .filter((entry) => entry.type !== "note" && entry.type !== "card")913            : vault.queryDomain(scopedDomain!)914        ).filter((entry) => !isPasskeyOnly(entry));915        return {916          ok: true,917          data: matches.map((e) =>918            includeAll919              ? {920                  id: e.id,921                  title: e.title,922                  // username/email/url shipped so the inline popup's search923                  // affordance can match on them, not just titles. No secrets924                  // cross the boundary — same shape as the domain-matched925                  // response below minus hasTotp (which the search list926                  // doesn't surface).927                  username: e.username,928                  email: e.email,929                  url: e.url,930                  hasPasskey: e.passkey !== undefined,931                }932              : {933                  id: e.id,934                  title: e.title,935                  username: e.username,936                  email: e.email,937                  url: e.url,938                  hasTotp: Boolean(e.totp),939                  hasPasskey: e.passkey !== undefined,940                }941          ),942        };943      }944      case "GET_ENTRY_FOR_FILL": {945        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };946        const { id, capability } = msg.payload as {947          id: string;948          capability?: string;949        };950        const capabilityAuthorized = capability !== undefined && consumeSummonCapability(capability, sender, "fill");951        const senderDomain = isPageSender(sender) ? attestedSenderDomain(sender) : null;952        if (isPageSender(sender) && !senderDomain) {953          return { ok: false, error: "origin_unverified" };954        }955        const entry = vault.getEntryById(id);956        if (!entry) return { ok: false, error: "entry_not_found" };957958        // MV-1: re-validate the entry against the requesting frame's origin.959        // sender.url / sender.tab.url are browser-attested — a page cannot960        // forge them. PB-26-005: the FRAME url (sender.url) is authoritative961        // for an all_frames content script; the top-frame tab.url would962        // misattribute a cross-origin iframe's request to the top page. When963        // the request does not come from a tab at all (popup surfaces use964        // GET_ENTRY_FOR_EDIT, not this), refuse.965        if (senderDomain && !capabilityAuthorized) {966          if (!entryDomains(entry.url, entry.additionalUrls).has(senderDomain)) {967            // Domain mismatch — refuse silently (no oracle about which ids exist).968            return { ok: false, error: "entry_not_found" };969          }970        }971        // Passkey-only entries (passkey credential, no password) cannot be972        // filled into a form — sign-in goes through the WebAuthn path. A973        // login that ALSO has a passkey is still fillable for its password.974        if (isPasskeyOnly(entry)) return { ok: false, error: "passkey_not_fillable" };975        // Strip the passkey credential AND the raw base32 TOTP seed from the976        // response. Content scripts never need either: the private key is the977        // authenticator's, and handing over the TOTP seed would let a978        // compromised content script mint valid codes indefinitely (SEV-3-C).979        // Generate the *current* 6-digit code here in the background and send980        // only that, plus the seconds left so the UI can render a countdown.981        // Also drop passwordHistory: filling needs only the *current* password.982        // Handing every past password to a content script during passive983        // autofill on an arbitrary page would widen the blast radius of a984        // compromised page far beyond the one credential being filled.985        const {986          passkey: _strippedPasskey,987          totp: seed,988          totpAlgorithm,989          totpDigits,990          totpPeriod,991          passwordHistory: _ph,992          ...safeEntry993        } = entry;994        let totpCode: string | undefined;995        let totpRemaining: number | undefined;996        if (seed) {997          try {998            // PCH-01-019: honour stored algorithm/digits/period (defaults apply999            // when these are undefined, i.e. RFC-default bare-seed entries).1000            const generated = generateTOTP(seed, totpAlgorithm, totpDigits, totpPeriod);1001            totpCode = generated.code;1002            totpRemaining = generated.remaining;1003          } catch {1004            totpCode = undefined;1005          }1006        }1007        if (isPageSender(sender) && vault.isUnlocked()) touchAutoLock();1008        return { ok: true, data: { ...safeEntry, totpCode, totpRemaining } };1009      }1010      // Trusted full-entry fetch for extension-owned edit surfaces. Content1011      // scripts route editing to the popup and cannot call this message.1012      case "GET_ENTRY_FOR_EDIT": {1013        if (isPageSender(sender)) return { ok: false, error: "invalid_sender" };1014        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };1015        const { id } = msg.payload as { id: string };1016        const entry = vault.getEntryById(id);1017        if (!entry) return { ok: false, error: "entry_not_found" };10181019        const { passkey: _passkey, ...safeEntry } = entry;1020        return { ok: true, data: safeEntry };1021      }1022      case "COPY": {1023        // Page relays must carry the browser-attested frame origin, as with1024        // SUMMON/fill. The policy admits content scripts; this binds the1025        // admitted page sender before arming clipboard auto-clear.1026        if (isPageSender(sender) && !attestedSenderOrigin(sender)) {1027          return { ok: false, error: "invalid_sender" };1028        }1029        // AUD-002: shape-validate the payload before passing to the clipboard1030        // helper. A raw cast lets a malformed message reach the clipboard API1031        // with a non-string and throw.1032        const { text, label, alreadyWritten } = (msg.payload ?? {}) as {1033          text?: unknown;1034          label?: unknown;1035          alreadyWritten?: unknown;1036        };1037        if (typeof text !== "string" || typeof label !== "string") {1038          return { ok: false, error: "invalid_payload" };1039        }1040        await copyToClipboard(text, label, Boolean(alreadyWritten));1041        return { ok: true, clearSeconds: getEffectiveClearSeconds() };1042      }1043      case "CHECK_ALL_BREACHES": {1044        const allEntries = vault.getEntries();1045        const results: Record<string, number> = {};1046        for (const entry of allEntries) {1047          if (entry.type === "note") continue;1048          if (!entry.password) continue;1049          results[entry.id] = await vault.checkBreach(entry.password);1050          // Small delay — be a good citizen to the HIBP API1051          await new Promise((r) => setTimeout(r, 200));1052        }1053        // Persist any non-zero counts back to entries1054        for (const [id, count] of Object.entries(results)) {1055          const entry = vault.getEntryById(id);1056          if (entry && (entry.breach ?? 0) !== count) {1057            await vault.updateEntry(id, { breach: count > 0 ? count : undefined });1058          }1059        }1060        return { ok: true, data: results };1061      }1062      case "GET_SETTINGS": {1063        const settings = await vault.ensureSettingsLoaded();1064        if (!isPageSender(sender)) return { ok: true, data: settings };10651066        const safeSettings: Pick<vault.VaultSettings, "accentColor" | "theme"> & {1067          manualSummonDomains?: NonNullable<vault.VaultSettings["manualSummonDomains"]>;1068        } = {1069          accentColor: settings.accentColor,1070          theme: settings.theme,1071        };1072        const domain = attestedSenderDomain(sender);1073        const ownSummon = domain ? settings.manualSummonDomains?.[domain] : undefined;1074        if (domain && ownSummon) safeSettings.manualSummonDomains = { [domain]: ownSummon };1075        return { ok: true, data: safeSettings };1076      }1077      case "UPDATE_SETTINGS": {1078        const patch = msg.payload;1079        if (!patch || typeof patch !== "object" || Array.isArray(patch)) {1080          return { ok: false, error: "invalid_payload" };1081        }1082        await vault.updateSettings(patch as vault.VaultSettings);1083        if (typeof (patch as Record<string, unknown>).autoLockMinutes === "number") {1084          await setupAutoLock();1085          if (vault.isUnlocked()) touchAutoLock();1086        }1087        return { ok: true };1088      }1089      case "SETUP_PIN": {1090        const { currentPassword, pin } = (msg.payload ?? {}) as {1091          currentPassword?: unknown;1092          pin?: unknown;1093        };1094        if (typeof currentPassword !== "string" || typeof pin !== "string") {1095          return { ok: false, error: "invalid_payload" };1096        }1097        return (await vault.setupPinUnlock(currentPassword, pin))1098          ? { ok: true }1099          : { ok: false, error: "incorrect_current_password" };1100      }1101      case "DISABLE_PIN": {1102        const { currentPassword } = (msg.payload ?? {}) as { currentPassword?: unknown };1103        if (typeof currentPassword !== "string") return { ok: false, error: "invalid_payload" };1104        return (await vault.disablePinUnlock(currentPassword))1105          ? { ok: true }1106          : { ok: false, error: "incorrect_current_password" };1107      }1108      case "GET_PENDING_PASSKEY":1109        return { ok: true, data: await getPendingPasskeyRequest() };1110      case "APPROVE_PASSKEY": {1111        const { requestId } = msg.payload as { requestId: number };1112        await resolvePendingPasskeyRequest(requestId, true);1113        return { ok: true };1114      }1115      case "DENY_PASSKEY": {1116        const { requestId } = msg.payload as { requestId: number };1117        await resolvePendingPasskeyRequest(requestId, false);1118        return { ok: true };1119      }1120      case "GET_PASSKEY_PROVIDER_STATE":1121        return { ok: true, data: await getPasskeyProviderState() };1122      case "SET_PASSKEY_PROVIDER_ENABLED": {1123        const { enabled } = msg.payload as { enabled: boolean };1124        // Disabling is always free; gating fires only when a free user is1125        // trying to turn passkey interception on.1126        if (enabled) {1127          const gate = await requirePro("passkeys");1128          if (!gate.allowed) return { ok: false, error: "pro_required" };1129        }1130        return { ok: true, data: await setPasskeyProviderEnabled(enabled) };1131      }1132      // AUD-003: this handler POSTs `entryTitle` to a user-configured HTTPS1133      // endpoint. Intentional product behavior — users configure their own1134      // dead-man's-switch. isAllowedCanaryWebhookUrl blocks localhost, private1135      // IPs, and non-HTTPS schemes, but cannot defend against every DNS-1136      // rebinding case. The UI under CanarySection (Entry.tsx) discloses to1137      // users that the entry title leaves the device when the canary fires.1138      case "FIRE_CANARY_PING": {1139        // Canary alert fires unconditionally for any entry with canary: true.1140        // The Pro gate only governs creation/flagging of canary entries; once1141        // an entry is flagged, the alert path is a security property and must1142        // never be license-gated.1143        const { webhookUrl, entryTitle, trigger } = msg.payload as {1144          webhookUrl: string;1145          entryTitle: string;1146          trigger: "password_copied" | "username_copied" | "autofilled" | "submitted" | "test";1147        };1148        if (trigger === "test" && !isExtensionPageSender(sender)) {1149          return { ok: false, error: "invalid_sender" };1150        }1151        if (!isAllowedCanaryWebhookUrl(webhookUrl)) {1152          return { ok: false, error: "invalid_webhook_url" };1153        }1154        // LV-3: debounce non-test pings per (webhook, entry) so a page that1155        // strobes focus/fill events can't turn the canary into a request1156        // firehose at the user's endpoint. Test pings are exempt — they are1157        // explicit user clicks in the settings UI and are awaited for1158        // feedback. The FIRST ping in a window always goes out, so the1159        // dead-man's-switch property (canary fires on access) is preserved;1160        // only repeats within the window are coalesced.1161        if (trigger !== "test") {1162          const keyDigest = new Uint8Array(1163            await crypto.subtle.digest(1164              "SHA-256",1165              new TextEncoder().encode(`${webhookUrl}:${entryTitle}`),1166            ),1167          );1168          const keySuffix = Array.from(keyDigest)1169            .map((byte) => byte.toString(16).padStart(2, "0"))1170            .join("");1171          const rlKey = `canary-last-fetch:${keySuffix}`;1172          const legacyRlKey = `canary-last-fetch:${webhookUrl}:${entryTitle}`;1173          const stored = await chrome.storage.local.get(rlKey);1174          await chrome.storage.local.remove(legacyRlKey);1175          const last = (stored as Record<string, unknown>)[rlKey];1176          if (typeof last === "number" && Date.now() - last < CANARY_MIN_INTERVAL_MS) {1177            return { ok: true, data: { rateLimited: true } };1178          }1179          await chrome.storage.local.set({ [rlKey]: Date.now() });1180        }1181        const body = JSON.stringify({1182          event: trigger === "test" ? "peach_canary_test" : "peach_canary_accessed",1183          entry: entryTitle,1184          trigger,1185          timestamp: new Date().toISOString(),1186          source: "peach-extension",1187        });1188        if (trigger === "test") {1189          // Permitted outbound fetch: user-configured canary alert endpoint1190          // (validated by isAllowedCanaryWebhookUrl above). This is the1191          // only non-HIBP outbound call in the extension — see1192          // Projects.md §3.7. Test pings are awaited so the UI can show1193          // success/failure.1194          try {1195            const res = await fetch(webhookUrl, {1196              method: "POST",1197              headers: { "Content-Type": "application/json" },1198              body,1199            });1200            return { ok: res.ok, error: res.ok ? undefined : `HTTP ${res.status}` };1201          } catch (err) {1202            return { ok: false, error: err instanceof Error ? err.message : "network_error" };1203          }1204        }1205        // Non-test triggers are fire-and-forget1206        fetch(webhookUrl, {1207          method: "POST",1208          headers: { "Content-Type": "application/json" },1209          body,1210        }).catch(() => {});1211        return { ok: true };1212      }1213      case "ACTIVATE_LICENSE": {1214        const { key } = msg.payload as { key: string };1215        const licenseInfo = await activateLicense(key);1216        return { ok: true, data: licenseInfo };1217      }1218      case "GET_LICENSE": {1219        const licenseInfo = await getLicenseInfo();1220        return { ok: true, data: licenseInfo };1221      }1222      case "GET_LICENSE_KEY": {1223        const key = await getLicenseKey();1224        return { ok: true, data: key };1225      }1226      case "DEACTIVATE_LICENSE": {1227        await deactivateLicense();1228        return { ok: true };1229      }1230      case "SHOULD_PLAY_CELEBRATION": {1231        const should = await shouldPlayCelebration();1232        return { ok: true, data: should };1233      }1234      case "MARK_CELEBRATION_PLAYED": {1235        await markCelebrationPlayed();1236        return { ok: true };1237      }1238      case "GET_CODEX_VAULT_DATA": {1239        // Pro-gated. requirePro broadcasts PRO_GATE_TRIGGERED on miss so the1240        // popup navigates to the License section without further coordination.1241        // Also privileged-page-gated (PB-26-001), and recoveryWords inside the1242        // snapshot additionally require a fresh master-password proof.1243        const gate = await requirePro("codex");1244        if (!gate.allowed) return { ok: false, error: "pro_required" };1245        const { password } = (msg.payload ?? {}) as { password?: string };1246        return await handleGetCodexVaultData(password);1247      }1248      case "ESTIMATE_CODEX_PAGES": {1249        const gate = await requirePro("codex");1250        if (!gate.allowed) return { ok: false, error: "pro_required" };1251        return await handleEstimateCodexPages();1252      }1253      case "RESTORE_FROM_CODEX": {1254        // !! IMPORTANT: intentionally NOT Pro-gated. A free device must still1255        // be able to restore a vault that was printed by a Pro user.1256        // DO NOT add a Pro gate here.1257        // A decoy session, though, holds no real-mode authority: this path1258        // writes SLOT_A/SLOT_B and snapshots history, so it carries the same1259        // guard its thirteen neighbours do (PB-26-915).1260        if (vault.isDecoyMode()) return { ok: false, error: "vault_locked" };1261        const payload = msg.payload as CodexRestorePayload;1262        return await handleRestoreFromCodex(payload);1263      }1264      case "OPEN_PRO_GATE": {1265        // Content script requests: open popup and navigate to License section.1266        const { feature } = (msg.payload ?? {}) as { feature?: string };1267        if (chrome.storage.session) {1268          await chrome.storage.session.set({ pendingProGate: { feature: feature ?? "passkeys" } });1269        }1270        try { await chrome.action.openPopup(); } catch { /* badge fallback */ }1271        return { ok: true };1272      }1273      case "OPEN_POPUP": {1274        // Content script asks to open the trusted extension popup — used by the1275        // inline locked bubble so the master password is entered in the popup,1276        // never in page-hosted DOM (audit HIGH). Best-effort: openPopup needs a1277        // user gesture + Chrome 127+/Firefox; on failure the user clicks the1278        // toolbar icon. The badge already signals Peach needs attention.1279        //1280        // Set a session flag so the popup knows it was opened for inline unlock1281        // and should close itself once the vault is unlocked (returning the user1282        // to the inline autofill selector on the page).1283        const { entryId } = (msg.payload ?? {}) as { entryId?: unknown };1284        if (entryId !== undefined && (typeof entryId !== "string" || entryId.length === 0 || entryId.length > 64)) {1285          return { ok: false, error: "invalid_payload" };1286        }1287        if (chrome.storage.session) {1288          if (typeof entryId === "string") {1289            await chrome.storage.session.set({ peach_open_entry: entryId });1290          } else {1291            await chrome.storage.session.set({ peach_inline_unlock: true });1292          }1293        }1294        try { await chrome.action.openPopup(); } catch { /* user opens via toolbar */ }1295        return { ok: true };1296      }1297      case "OPEN_DETACHED_POPUP": {1298        // A window created by the action popup and then orphaned with1299        // window.close() is cascaded shut by Chrome, so the file-picker1300        // flows (OS dialogs that dismiss the popup on focus loss) ask the SW1301        // to open the standalone window instead — the SW has no window to be1302        // associated with. The query string is allowlisted so this can never1303        // open an arbitrary URL.1304        const { route } = (msg.payload ?? {}) as { route?: string };1305        const search = route === "peachscript"1306          ? "?restore=peachscript"1307          : route === "csv"1308            ? "?restore=csv"1309            : route === "codex-pdf"1310              ? "?codex=pdf"1311              : null;1312        if (!search) return { ok: false, error: "bad_route" };1313        try {1314          const popupHtml =1315            chrome.runtime.getManifest().action?.default_popup ?? "src/popup/index.html";1316          await chrome.windows.create({1317            url: chrome.runtime.getURL(`${popupHtml}${search}`),1318            type: "popup",1319            width: 320,1320            height: 550,1321          });1322          return { ok: true, data: { detached: true } };1323        } catch {1324          return { ok: false };1325        }1326      }1327      case "GET_IDENTITIES": {1328        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };1329        return { ok: true, data: vault.getIdentities() };1330      }1331      case "TOUCH_AUTO_LOCK": {1332        // touchAutoLock() is already called by shouldRefreshAutoLock above;1333        // this case just gives the popup a typed message to keep the session alive.1334        return { ok: true };1335      }1336      case "GET_ADVANCED_HEALTH_REPORT": {1337        const gate = await requirePro("advanced_health");1338        if (!gate.allowed) return { ok: false, error: "pro_required" };1339        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };1340        const report = await buildAdvancedHealthReport(vault.getEntries());1341        return { ok: true, data: report };1342      }1343      case "IS_DURESS_ENABLED": {1344        const enabled = await vault.isDuressEnabled();1345        return { ok: true, data: enabled };1346      }1347      case "SETUP_DURESS": {1348        // Single enforcement point for the Duress Vault Pro gate. Free users1349        // who somehow reach this handler are rejected here; the popup also1350        // pre-checks isPro() to avoid showing the setup form to free users,1351        // but server-side is the actual security boundary.1352        const gate = await requirePro("duress_vault");1353        if (!gate.allowed) return { ok: false, error: "pro_required" };1354        // MV3 service workers restart after inactivity, losing the in-memory1355        // derived key. Detect this before calling setupDuress so the popup1356        // can show a meaningful prompt to re-lock and re-unlock.1357        if (!vault.isUnlocked()) return { ok: false, error: "session_expired" };1358        const { duressPassword } = msg.payload as { duressPassword: string };1359        await vault.setupDuress(duressPassword);1360        return { ok: true };1361      }1362      case "DISABLE_DURESS": {1363        await vault.disableDuress();1364        return { ok: true };1365      }1366      case "VERIFY_MAIN_PASSWORD": {1367        const { password } = msg.payload as { password: string };1368        const ok = await vault.verifyMainPassword(password);1369        return { ok: true, data: ok };1370      }1371      case "CHANGE_PASSWORD": {1372        const { currentPassword, newPassword } = msg.payload as {1373          currentPassword: string;1374          newPassword: string;1375        };1376        const changed = await vault.changePassword(currentPassword, newPassword);1377        return changed1378          ? { ok: true }1379          : { ok: false, error: "incorrect_current_password" };1380      }1381      case "GET_SITE_PASSWORD_POLICY": {1382        const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });1383        if (!tab?.id) return { ok: true, data: null };1384        try {1385          const response = await chrome.tabs.sendMessage(tab.id, {1386            type: "GET_SITE_PASSWORD_POLICY",1387          });1388          return (response as MessageResponse) ?? { ok: true, data: null };1389        } catch {1390          return { ok: true, data: null };1391        }1392      }1393      case "RECORD_MANUAL_SUMMON": {1394        const { domain } = (msg.payload ?? {}) as { domain?: unknown };1395        const origin = attestedSenderOrigin(sender);1396        let senderDomain: string | null = null;1397        if (origin) {1398          try {1399            const hostname = new URL(origin).hostname;1400            senderDomain = extractDomain(hostname) ?? hostname;1401          } catch {1402            senderDomain = null;1403          }1404        }1405        if (typeof domain !== "string" || domain.length > 253 || domain !== senderDomain) {1406          return { ok: false, error: "invalid_domain" };1407        }1408        const settings = vault.getSettings();1409        const existing = settings.manualSummonDomains?.[domain];1410        const now = Date.now();1411        await vault.updateSettings({1412          manualSummonDomains: {1413            ...settings.manualSummonDomains,1414            [domain]: {1415              firstSummonedAt: existing?.firstSummonedAt ?? now,1416              summonCount: (existing?.summonCount ?? 0) + 1,1417              lastSummonedAt: now,1418            },1419          },1420        });1421        return { ok: true };1422      }1423      case "DECLINE_CARD_SAVE": {1424        // PB-26-108: the declined-card digest is keyed by root-key material, so1425        // a decline cannot be recorded while locked. Offers are not shown while1426        // locked either, so this is unreachable in practice.1427        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };1428        const { last4, expMonth, expYear } = msg.payload as { last4: string; expMonth: string; expYear: string };1429        await vault.declineCardSaveOffer(last4, expMonth, expYear);1430        return { ok: true };1431      }1432      case "IS_CARD_DECLINED": {1433        const { last4, expMonth, expYear } = msg.payload as { last4: string; expMonth: string; expYear: string };1434        const declined = await vault.isCardDeclined(last4, expMonth, expYear);1435        return { ok: true, data: declined };1436      }1437      case "INITIATE_FIREFOX_PASSKEY_CREATE": {1438        const { rpId, rpName, userName, userDisplayName } = (msg.payload ?? {}) as {1439          rpId?: string;1440          rpName?: string;1441          userName?: string;1442          userDisplayName?: string;1443        };1444        if (!rpId || !userName) return { ok: false, error: "rpid_and_username_required" };1445        return await initiateFirefoxPasskeyCreate({1446          rpId,1447          rpName: rpName ?? rpId,1448          userName,1449          userDisplayName,1450        }, attestedSenderOrigin(sender));1451      }14521453      case "COMPLETE_FIREFOX_PASSKEY_CREATE": {1454        const payload = msg.payload as Parameters<typeof completeFirefoxPasskeyCreate>[0] | undefined;1455        if (!payload) return { ok: false, error: "missing_payload" };1456        return await completeFirefoxPasskeyCreate(payload);1457      }14581459      case "PASSKEY_OFFER_DECLINE": {1460        const { domain } = (msg.payload ?? {}) as { domain?: string };1461        const origin = attestedSenderOrigin(sender);1462        if (!domain || !origin || !isValidRpId(domain, origin)) return { ok: false, error: "invalid_domain" };1463        await recordPasskeyOfferDecline(domain);1464        return { ok: true };1465      }14661467      case "GET_PASSKEY_OFFER_DECLINED": {1468        const { domain } = (msg.payload ?? {}) as { domain?: string };1469        const origin = attestedSenderOrigin(sender);1470        if (!domain || !origin || !isValidRpId(domain, origin)) return { ok: false, error: "invalid_domain" };1471        return { ok: true, data: await isPasskeyOfferDeclined(domain) };1472      }14731474      case "PASSKEY_HAS_CREDENTIALS": {1475        const payload = msg.payload as {1476          rpId?: string;1477          allowCredentials?: Array<{ id: string; type?: string }>;1478        } | undefined;1479        if (!payload?.rpId) return { ok: false, error: "rpid_required" };1480        return await handlePasskeyHasCredentials({1481          rpId: payload.rpId,1482          allowCredentials: payload.allowCredentials ?? [],1483        }, attestedSenderOrigin(sender));1484      }14851486      case "PASSKEY_CREATE": {1487        const payload = msg.payload as Parameters<typeof handleFirefoxPasskeyCreate>[0] | undefined;1488        if (!payload?.rpId || !payload?.userName) {1489          return { ok: false, error: "rpid_and_username_required" };1490        }1491        return await handleFirefoxPasskeyCreate(payload, attestedSenderOrigin(sender));1492      }14931494      case "PASSKEY_GET": {1495        const payload = msg.payload as Parameters<typeof handleFirefoxPasskeyGet>[0] | undefined;1496        if (!payload?.rpId) return { ok: false, error: "rpid_required" };1497        return await handleFirefoxPasskeyGet(payload, attestedSenderOrigin(sender));1498      }14991500      case "PASSKEY_AUTH_RESPONSE": {1501        const { requestId, approved } = (msg.payload ?? {}) as {1502          requestId?: string | number;1503          approved?: boolean;1504        };1505        if (requestId === undefined || requestId === null || typeof approved !== 'boolean') {1506          return { ok: false, error: 'requestId and approved required' };1507        }1508        const pending = await getPendingPasskeyRequest();1509        const senderOrigin = attestedSenderOrigin(sender);1510        if (1511          (pending && pending.requestId !== Number(requestId)) ||1512          (pending?.tabId !== undefined && sender.tab?.id !== pending.tabId) ||1513          (pending?.origin !== undefined && senderOrigin !== pending.origin)1514        ) {1515          return { ok: false, error: "invalid_passkey_approval_sender" };1516        }1517        await resolvePendingPasskeyRequest(Number(requestId), approved);1518        return { ok: true };1519      }15201521      case "GET_PASSKEY_FOR_DOMAIN": {1522        const { domain } = (msg.payload ?? {}) as { domain?: string };1523        if (!domain) return { ok: false, error: "domain_required" };1524        // Scope to the caller's own origin: a page may only ask about passkeys1525        // for a domain it actually owns, so an arbitrary page cannot enumerate1526        // which sites the user holds passkeys for or read the username (audit MEDIUM).1527        const origin = attestedSenderOrigin(sender);1528        if (!origin || !isValidRpId(domain, origin)) return { ok: true, data: null };1529        if (!vault.isUnlocked()) return { ok: true, data: null };1530        const match = vault1531          .getEntries()1532          .find((entry) => entry.passkey?.rpId === domain);1533        if (!match || !match.passkey) return { ok: true, data: null };1534        // Slim shape — only the fields State 11 needs. Never expose private1535        // key material, even though this is locked behind isUnlocked.1536        // bannerShown is the per-domain "already surfaced" flag, kept in1537        // trusted background session storage so content scripts never touch1538        // chrome.storage.session directly (PB-26-203).1539        let bannerShown = false;1540        if (chrome.storage.session) {1541          const flag = await chrome.storage.session.get(`peach_state11_shown:${domain}`);1542          bannerShown = Boolean((flag as Record<string, unknown>)[`peach_state11_shown:${domain}`]);1543        }1544        return {1545          ok: true,1546          data: {1547            rpId: match.passkey.rpId,1548            userName: match.passkey.userName,1549            browser: match.passkey.browser,1550            bannerShown,1551          },1552        };1553      }15541555      case "MARK_PASSKEY_BANNER_SHOWN": {1556        const { domain } = (msg.payload ?? {}) as { domain?: string };1557        if (typeof domain !== "string" || !domain) return { ok: false, error: "domain_required" };1558        // Same origin binding as the read path: a page may only mark its own1559        // domain, and the flag reveals nothing either way.1560        const origin = attestedSenderOrigin(sender);1561        if (!origin || !isValidRpId(domain, origin)) return { ok: true };1562        if (chrome.storage.session) {1563          await chrome.storage.session.set({ [`peach_state11_shown:${domain}`]: Date.now() });1564        }1565        return { ok: true };1566      }15671568      case "DESTROY_VAULT": {1569        // Irreversible by design. Lock zeroes the derived key + in-memory entries;1570        // storage.local.clear() wipes every Peach key (slots, settings, license,1571        // onboarded flag, history, sync state, declined cards, recall, etc.);1572        // session.clear() wipes pending passkey requests + pro-notice queues.1573        // Caller (Panic.tsx) handles UI navigation back to onboarding.1574        //1575        // The Pro license is the one thing that must survive: the user paid for1576        // it, and destroying a vault (panic wipe, fresh start) is not a refund.1577        // Capture it before the clear and restore it after. peach_pro_celebrated1578        // rides along so the Pro celebration doesn't replay on the next unlock.1579        const preserved = await chrome.storage.local.get([1580          "peach_license",1581          "peach_pro_celebrated",1582        ]);1583        await vault.lock();1584        await storageClear();1585        await vault.clearVaultHistory();1586        const toRestore: Record<string, unknown> = {};1587        if (preserved.peach_license !== undefined) toRestore.peach_license = preserved.peach_license;1588        if (preserved.peach_pro_celebrated !== undefined) toRestore.peach_pro_celebrated = preserved.peach_pro_celebrated;1589        if (Object.keys(toRestore).length > 0) {1590          await chrome.storage.local.set(toRestore);1591        }1592        if (chrome.storage.session) {1593          await chrome.storage.session.clear();1594        }1595        await clearRecoveryConfirmPending();1596        return { ok: true };1597      }15981599      case "GET_IDENTITY_SUGGESTIONS": {1600        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };1601        const entries = vault.getEntries();1602        const suggestions = buildIdentitySuggestions(entries);1603        return { ok: true, data: { suggestions } };1604      }1605      case "ML_INIT":1606        return { ok: true };16071608      case "ML_PREDICT_BATCH": {1609        const { featuresArray } = (msg.payload ?? {}) as { featuresArray?: unknown };1610        if (!isValidMLFeatureBatch(featuresArray, MAX_ML_BATCH_FIELDS)) {1611          return { ok: false, error: "invalid_payload" };1612        }1613        if (!claimMLWork(sender, featuresArray.length)) {1614          return { ok: false, error: "rate_limited" };1615        }1616        return { ok: true, data: mlPredictBatch(featuresArray) };1617      }16181619      // ── Vault sync (v3) ───────────────────────────────────────────────1620      // Returns the public-facing local identity needed by the popup to1621      // drive Phase-1 audio pairing. The private long-term key never1622      // leaves the background.1623      case "GET_LOCAL_IDENTITY": {1624        // Not Pro-gated: a free device may attempt to pair so it can RECEIVE a1625        // Pro license from an already-Pro peer (at least one device must be Pro).1626        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };1627        // The identity is sealed under its own key rather than derived from the1628        // root, so pairing survives a master-password change (SEV-2-B).1629        const identity: LocalDeviceIdentity & { license?: string } = await buildLocalIdentity();1630        // If this device is Pro, advertise the signed license so the peer can1631        // activate it after SAS confirmation — pairing carries Pro across.1632        if (await isProUser()) {1633          const license = await getLicenseKey();1634          if (license) identity.license = license;1635        }1636        return { ok: true, data: identity };1637      }16381639      case "LIST_PAIRED_DEVICES": {1640        if (vault.isDecoyMode()) return { ok: true, data: { devices: [] } };1641        try {1642          const devices = await listPairedDevicesV3();1643          return { ok: true, data: { devices } };1644        } catch (err) {1645          if (err instanceof TrustDataCorruptedError) {1646            return { ok: false, error: "trust_data_corrupted" };1647          }1648          throw err;1649        }1650      }16511652      case "RENAME_PAIRED_DEVICE": {1653        if (vault.isDecoyMode()) return { ok: false, error: "vault_locked" };1654        const { deviceId, name } = (msg.payload ?? {}) as { deviceId?: string; name?: string };1655        if (typeof deviceId !== "string" || !deviceId || typeof name !== "string") {1656          return { ok: false, error: "invalid_payload" };1657        }1658        await renamePairedDeviceV3(deviceId, name);1659        return { ok: true };1660      }16611662      case "UNPAIR_DEVICE": {1663        if (vault.isDecoyMode()) return { ok: false, error: "vault_locked" };1664        const { deviceId } = (msg.payload ?? {}) as { deviceId?: string };1665        if (typeof deviceId !== "string" || !deviceId) {1666          return { ok: false, error: "invalid_payload" };1667        }1668        // A damaged legacy identity must not prevent the user from evicting a1669        // peer locally. Sync is already blocked until the secure reset repairs1670        // this installation, so the local issuer marker is sufficient for the1671        // tombstone that will be cleared with the archived generation.1672        let revokedByDeviceId: string;1673        try {1674          revokedByDeviceId = await getOrCreateDeviceId();1675        } catch (error) {1676          if (!(error instanceof Error) || error.message !== "identity_migration_required") throw error;1677          revokedByDeviceId = "local";1678        }1679        await revokeDeviceByIdV3(deviceId, revokedByDeviceId);1680        // PB-26-213: forget the secret now so no further dial can use it.1681        // Removals that happen while locked leave orphans for the next1682        // unlock's reconcile to prune.1683        const unpairRootHex = vault.getRootKeyHex();1684        if (unpairRootHex) {1685          const unpairRoot = unhexBytes(unpairRootHex);1686          try {1687            await dropPairSecret(deviceId, unpairRoot);1688          } finally {1689            unpairRoot.fill(0);1690          }1691        } else {1692          await dropPairSecret(deviceId, null);1693        }1694        // Sync against the remaining hosts — the unpaired device no longer1695        // matters but the next alarm tick is up to ten minutes away.1696        void triggerSyncIfReachable({ reason: "device_list_changed" }).catch(() => undefined);1697        return { ok: true };1698      }16991700      case "RENAME_LOCAL_DEVICE": {1701        if (vault.isDecoyMode()) return { ok: false, error: "vault_locked" };1702        const { name } = (msg.payload ?? {}) as { name?: string };1703        if (typeof name !== "string") return { ok: false, error: "invalid_payload" };1704        await setDeviceNameV3(name);1705        return { ok: true };1706      }17071708      // ── Relay pairing ─────────────────────────────────────────────────────1709      // Not Pro-gated (a free device may pair to receive Pro/sync). The whole NK1710      // session runs in the background; the SAS and the terminal result reach1711      // the popup as ADOPTION_SOURCE_SAS / ADOPTION_SOURCE_RESULT pushes.1712      case "START_ADOPTION_SOURCE": {1713        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };1714        if (isRelayAdoptionSourceInFlight()) {1715          return { ok: false, error: "adoption_source_in_progress" };1716        }1717        try {1718          return { ok: true, data: await startRelayAdoptionSource() };1719        } catch (error) {1720          return {1721            ok: false,1722            error: error instanceof Error ? error.message : "adoption_source_failed",1723          };1724        }1725      }17261727      // Onboarding receiver — empty extension shows a QR, phone scans and1728      // sends the vault. The popup supplies the local-only password here so1729      // the background can wrap the adopted root key at rest without ever1730      // asking the user to retype it.1731      case "START_ADOPTION_RECEIVER": {1732        if (isRelayAdoptionSourceInFlight()) {1733          return { ok: false, error: "adoption_receiver_in_progress" };1734        }1735        const password = (msg.payload as { password?: unknown } | undefined)?.password;1736        if (typeof password !== "string" || password.length === 0) {1737          return { ok: false, error: "invalid_payload" };1738        }1739        try {1740          return { ok: true, data: await startRelayAdoptionReceiver(password) };1741        } catch (error) {1742          return {1743            ok: false,1744            error: error instanceof Error ? error.message : "adoption_receiver_failed",1745          };1746        }1747      }17481749      // Source-side mirror of RECEIVER_SAS_DECISION: the popup's confirm/cancel1750      // on the source's SAS screen. The id round-trips the same way so a stale1751      // late response can't approve the wrong session.1752      case "RECEIVER_SAS_DECISION": {1753        const payload = msg.payload as { id?: unknown; sas?: unknown; ok?: unknown } | undefined;1754        if (typeof payload?.id !== "string" || typeof payload.sas !== "string" || typeof payload.ok !== "boolean") {1755          return { ok: false, error: "invalid_payload" };1756        }1757        return { ok: resolveReceiverSasConfirmation(payload.id, payload.sas, payload.ok) };1758      }17591760      case "SOURCE_SAS_DECISION": {1761        const payload = msg.payload as { id?: unknown; ok?: unknown } | undefined;1762        if (typeof payload?.id !== "string" || typeof payload?.ok !== "boolean") {1763          return { ok: false, error: "invalid_payload" };1764        }1765        return { ok: resolveSourceSasConfirmation(payload.id, payload.ok) };1766      }1767      case "LIST_PENDING_ADOPTION_HOLDERS": {1768        return { ok: true, data: await listPendingAdoptionHolders() };1769      }17701771      case "REMOVE_PENDING_ADOPTION_HOLDER": {1772        await removePendingAdoptionHolder();1773        return { ok: true };1774      }177517761777      case "CANCEL_ADOPTION_SOURCE": {1778        cancelRelayAdoptionSource();1779        return { ok: true };1780      }17811782      case "TRIGGER_SYNC": {1783        if (vault.isDecoyMode()) return { ok: true };1784        const payload = (msg.payload ?? {}) as {1785          reason?: "vault_change" | "foreground" | "manual";1786          force?: boolean;1787        };1788        const reason = payload.reason ?? "foreground";17891790        void triggerSyncIfReachable({ reason, force: payload.force === true });1791        return { ok: true };1792      }17931794      case "GET_SYNC_STATE": {1795        if (vault.isDecoyMode()) return { ok: true, data: { status: "idle" } };1796        return { ok: true, data: getSyncState() };1797      }17981799      // §11. The generation state carries no secret — it is a version and a1800      // state name — so the panel can read it while the vault is locked.1801      case "GET_SYNC_GENERATION": {1802        return { ok: true, data: await resolveGeneration(browserGenerationIo) };1803      }18041805      case "SECURE_RESET": {1806        if (vault.isDecoyMode()) return { ok: false, error: "vault_locked" };1807        const payload = (msg.payload ?? {}) as { password?: unknown };1808        if (typeof payload.password !== "string" || payload.password.length === 0) {1809          return { ok: false, error: "Enter your master password" };1810        }1811        const wasLocked = !vault.isUnlocked();1812        const passwordValid = wasLocked1813          ? await vault.unlock(payload.password)1814          : await vault.verifyMainPassword(payload.password);1815        if (!passwordValid) {1816          return { ok: false, error: "Incorrect master password" };1817        }1818        if (wasLocked) {1819          await setupAutoLock();1820          broadcastToContentScripts({ type: "VAULT_UNLOCKED" });1821          onVaultUnlocked();1822        }1823        // Identity rotation needs the live vault root key. A password check is1824        // deliberately verification-only, so establish the real session first1825        // when Settings is opened after the vault session expired.1826        // Enter the blocking state before the reset steps: a crash after this1827        // point leaves the device refusing sync rather than mid-reset over live1828        // trust. Both are idempotent, so a retry is safe.1829        await enterResetRequired(browserGenerationIo);1830        const result = await confirmSecureReset(browserGenerationIo, {1831          nowMs: Date.now(),1832        });1833        return result === "V2_ANCHOR"1834          ? { ok: true, data: await resolveGeneration(browserGenerationIo) }1835          : {1836              ok: false,1837              error: "The reset did not complete — nothing was lost, try again",1838            };1839      }18401841      case "GET_SYNC_SETTINGS": {1842        if (vault.isDecoyMode()) return { ok: true, data: { mode: "off", relayUrl: "" } };1843        return { ok: true, data: await getSyncSettings() };1844      }18451846      case "SET_SYNC_SETTINGS": {1847        if (vault.isDecoyMode()) return { ok: false, error: "vault_locked" };1848        const payload = (msg.payload ?? {}) as {1849          mode?: unknown;1850          relayUrl?: unknown;1851        };1852        try {1853          // A custom relay URL implies private mode; Peach's own relay stays1854          // public. See setRelayUrl.1855          if (payload.relayUrl !== undefined) {1856            if (typeof payload.relayUrl !== "string") {1857              return { ok: false, error: "invalid_relay_url" };1858            }1859            return { ok: true, data: await setRelayUrl(payload.relayUrl) };1860          }1861          if (payload.mode !== undefined) {1862            if (payload.mode !== "public" && payload.mode !== "private" && payload.mode !== "off") {1863              return { ok: false, error: "invalid_sync_mode" };1864            }1865            return { ok: true, data: await setSyncMode(payload.mode as SyncMode) };1866          }1867          return { ok: true, data: await getSyncSettings() };1868        } catch (error) {1869          return {1870            ok: false,1871            error: error instanceof Error ? error.message : "invalid_relay_url",1872          };1873        }1874      }187518761877      case "GET_SYNC_ALERTS": {1878        if (vault.isDecoyMode()) return { ok: true, data: [] };1879        const { consume } = (msg.payload ?? {}) as { consume?: boolean };1880        const alerts = await getPendingSyncAlerts();1881        // The popup reads on open and acknowledges with consume:true after1882        // rendering, so a popup crash between read and render doesn't lose1883        // the alerts.1884        if (consume) await clearPendingSyncAlerts();1885        return { ok: true, data: alerts };1886      }18871888      case "GET_PENDING_VOUCHED_DEVICES": {1889        if (vault.isDecoyMode()) return { ok: true, data: [] };1890        return { ok: true, data: await listPendingVouchedDevices() };1891      }1892      case "CONFIRM_VOUCHED_DEVICE": {1893        if (vault.isDecoyMode()) return { ok: false, error: "vault_locked" };1894        const { deviceId } = msg.payload as { deviceId: string };1895        const pending = await getPendingVouchedDevice(deviceId);1896        if (!pending) return { ok: false, error: "not_pending" };1897        // Last gate before a vouched device becomes a sync peer. A queue entry1898        // can predate the revocation that names it, so revocation is re-checked1899        // here rather than trusted to have swept the queue.1900        if (await isDeviceRevoked(pending.deviceId, pending.longTermPublicKeyHex)) {1901          await removePendingVouchedDevice(deviceId);1902          return { ok: false, error: "device_revoked" };1903        }1904        await upsertPairedDeviceV3({1905          deviceId: pending.deviceId,1906          deviceName: pending.deviceName,1907          deviceType:1908            pending.deviceType as import("../shared/sync-types").PairedDevice["deviceType"],1909          longTermPublicKeyHex: pending.longTermPublicKeyHex,1910          signingPublicKeyHex: pending.signingPublicKeyHex,1911          wsPort: pending.wsPort,1912          pairedAt: pending.pairedAt,1913          trustSource: "vouched",1914        });1915        await removePendingVouchedDevice(deviceId);1916        // Newly confirmed vouched device is now a sync target — kick a round1917        // so the mesh introduction pays off without a 10-minute wait.1918        void triggerSyncIfReachable({ reason: "device_list_changed" }).catch(() => undefined);1919        return { ok: true };1920      }1921      case "REJECT_VOUCHED_DEVICE": {1922        if (vault.isDecoyMode()) return { ok: true };1923        const { deviceId } = msg.payload as { deviceId: string };1924        await removePendingVouchedDevice(deviceId);1925        return { ok: true };1926      }19271928      case "ROTATE_SYNC_IDENTITY": {1929        if (!vault.isUnlocked()) return { ok: false, error: "vault_locked" };1930        const result = await vault.rotateSyncIdentity();1931        return result.ok ? { ok: true } : { ok: false, error: result.error };1932      }19331934      default:1935        return { ok: false, error: "unknown_message_type" };1936    }1937  } catch (err) {1938    return {1939      ok: false,1940      error: err instanceof Error ? err.message : "operation_failed",1941    };1942  }1943}19441945// Returns username use-counts from the vault, sorted by frequency. Each row1946// also carries the entry's email so the popup can suggest the email/username1947// pair the user has actually used together.1948function buildIdentitySuggestions(1949  entries: VaultEntry[],1950): Array<{ email: string; username: string; count: number }> {1951  const usernameCounts = new Map<string, number>();1952  const usernameEmail = new Map<string, string>();1953  for (const entry of entries) {1954    const username = entry.username ?? "";1955    const email = (entry as VaultEntry & { email?: string }).email ?? "";1956    if (!username && !email) continue;1957    const key = username || email;1958    usernameCounts.set(key, (usernameCounts.get(key) ?? 0) + 1);1959    if (!usernameEmail.has(key)) usernameEmail.set(key, email);1960  }1961  return Array.from(usernameCounts.entries())1962    .sort((a, b) => b[1] - a[1])1963    .map(([username, count]) => ({1964      email: usernameEmail.get(username) ?? "",1965      username,1966      count,1967    }));1968}19691970/** Notify all content scripts that the vault just became available. */1971function broadcastToContentScripts(message: { type: string }): void {1972  if (!chrome.tabs?.query) return;1973  chrome.tabs.query({}, (tabs) => {1974    for (const tab of tabs) {1975      if (tab.id !== undefined) {1976        chrome.tabs.sendMessage(tab.id, message).catch(() => {1977          // Tab may not have a content script — silently ignore1978        });1979      }1980    }1981  });1982}19831984function shouldRefreshAutoLock(1985  type: MessageType,1986  sender: Partial<chrome.runtime.MessageSender>,1987): boolean {1988  if (!isExtensionPageSender(sender)) return false;1989  return (1990    type !== "HAS_VAULT" &&1991    type !== "IS_UNLOCKED" &&1992    type !== "GET_VAULT_STATUS" &&1993    type !== "LOCK" &&1994    type !== "UPDATE_SETTINGS" &&1995    type !== "GET_PENDING_PASSKEY" &&1996    type !== "GET_PASSKEY_PROVIDER_STATE" &&1997    type !== "DECLINE_CARD_SAVE" &&1998    type !== "IS_CARD_DECLINED" &&1999    type !== "AUTOFILL_QUERY" &&2000    type !== "ML_PREDICT_BATCH"2001  );2002}20032004function isAllowedCanaryWebhookUrl(value: string): boolean {2005  try {2006    const url = new URL(value);2007    if (url.protocol !== "https:") return false;20082009    const hostname = url.hostname.toLowerCase();2010    if (2011      hostname === "localhost" ||2012      hostname.endsWith(".localhost") ||2013      isPrivateIpv4(hostname) ||2014      isPrivateIpv6(hostname)2015    ) {2016      return false;2017    }20182019    return true;2020  } catch {2021    return false;2022  }2023}20242025function isPrivateIpv4(hostname: string): boolean {2026  if (PRIVATE_IPV4_RANGES.some((range) => hostname.startsWith(range.prefix))) {2027    return true;2028  }20292030  const octets = hostname.split(".").map((part) => Number(part));2031  if (octets.length !== 4 || octets.some((octet) => !Number.isInteger(octet) || octet < 0 || octet > 255)) {2032    return false;2033  }20342035  return octets[0] === 172 && octets[1] >= 16 && octets[1] <= 31;2036}20372038function isPrivateIpv6(hostname: string): boolean {2039  const normalized = hostname.replace(/^\[|\]$/g, "").toLowerCase();2040  // IPv4-mapped IPv6 literals embed a plain IPv4 address that the prefix2041  // list below can never match (WHATWG URL serializes [::ffff:192.168.0.1]2042  // as ::ffff:c0a8:1). Unwrap and test the embedded IPv4; unparseable mapped2043  // tails fail closed. (Codex Security 2026-08-04 LOW,2044  // ssrf.webhook-url-private-ip-filter.) DNS rebinding is out of scope for a2045  // one-shot host check.2046  const mapped = /^::ffff:(.+)$/.exec(normalized);2047  if (mapped) {2048    const tail = mapped[1] ?? "";2049    if (tail.includes(".")) return isPrivateIpv4(tail);2050    const parts = tail.split(":");2051    const [hi, lo] = parts;2052    if (2053      parts.length !== 2 ||2054      !hi ||2055      !lo ||2056      !/^[0-9a-f]{1,4}$/.test(hi) ||2057      !/^[0-9a-f]{1,4}$/.test(lo)2058    ) {2059      return true;2060    }2061    const octets = [2062      (Number.parseInt(hi, 16) >>> 8) & 0xff,2063      Number.parseInt(hi, 16) & 0xff,2064      (Number.parseInt(lo, 16) >>> 8) & 0xff,2065      Number.parseInt(lo, 16) & 0xff,2066    ];2067    return isPrivateIpv4(octets.join("."));2068  }2069  return (2070    normalized === "::1" ||2071    normalized.startsWith("fc") ||2072    normalized.startsWith("fd") ||2073    normalized.startsWith("fe80:")2074  );2075}