vault.ts

105.6 KB · raw

1/**2 * vault.ts — sole owner of vault state and chrome.storage vault keys.3 *4 * Every read/write of `peach_vault_a`, `peach_vault_b`, and `peach_settings`5 * goes through this module. No other file accesses those keys directly.6 * External callers reach vault operations via `messages.ts` — never by7 * importing from here cross-boundary (popup/content must not). Invariants8 * for the two-slot schema, unlock timing, and key/disk atomicity are in9 * Projects.md §3.2–§3.6.10 */1112import { deriveKey, deriveKeyRaw, generateSalt } from "../crypto/argon2";13import { extractDomain } from "../shared/domain";14import { clampSyncTimestamp } from "../shared/sync-limits";15import { recordSyncAlerts, type SyncAlert } from "./sync-alerts";16import {17  rootKeyToWords,18  wordsToRootKeyMaterial,19  deriveRecoveryKey,20} from "../shared/recovery-words";21import type {22  AccentColorName,23  Canary,24  LegacyVaultEntry,25  ManualSummonRecord,26  PasskeyData,27  SavedIdentity,28  StoredVault,29  VaultEntry,30  VaultHistory,31  VaultSnapshot,32} from "../shared/types";33import { decrypt, encrypt, zeroKey } from "./aes";34import { clearAutoLock, touchAutoLock } from "./alarms";35import { setRecoveryConfirmPending } from "./recovery-pending";3637type EntryDraft = Omit<38  VaultEntry,39  "id" | "createdAt" | "modifiedAt" | "deletedAt"40>;4142// Slot-neutral storage keys (v1.4.24+). Both slots always exist; slot B43// either holds the decoy vault (when duress is configured) or random noise44// that looks identical at rest. No external indicator is stored — duress45// configuration is recorded only inside the encrypted payload of slot A.46const SLOT_A = "peach_vault_a";47const SLOT_B = "peach_vault_b";4849// Legacy keys, migrated away on first access.50const LEGACY_MAIN = "peach_vault";51const LEGACY_DECOY = "peach_decoy_vault";52const LEGACY_DURESS_FLAG = "peach_duress";5354// Plaintext is padded to the next multiple of this size before encryption.55// This coarsens ciphertext-length leakage into 1 KiB buckets.56const PAD_BLOCK = 1024;5758// How many past passwords to retain per entry. Generous enough to be useful as59// a recovery aid while bounding the secret material we hold (and the ciphertext60// growth) for an entry whose password is rotated frequently.61const PASSWORD_HISTORY_MAX = 50;6263// When duress is configured, BOTH slots are padded to this fixed ceiling so64// their ciphertext lengths are identical and cannot reveal that a hidden decoy65// vault exists (audit MEDIUM — plausible-deniability leak). 256 KiB comfortably66// holds a very large vault (thousands of entries incl. passkeys). The decoy67// slot can't be re-encrypted on every real-vault save (the real session has no68// decoy key), so a fixed ceiling — rather than size-matching on each write — is69// what keeps the two slots equal. A real vault whose plaintext exceeds this70// would grow past the ceiling and re-expose the bit; that is an extreme,71// documented edge well beyond normal use.72const DURESS_SLOT_SIZE = 262_144;7374let derivedKey: Uint8Array | null = null;75// 32-byte random root key, separate from the derivedKey used for vault encryption.76// The root key is wrapped (encrypted) under the recovery key derived from the77// 12-word phrase. When the user changes their password, only the wrapping changes;78// the root key and recovery words remain stable.79let rootKey: Uint8Array | null = null;80let entries: VaultEntry[] = [];81let domainIndex: Map<string, VaultEntry[]> = new Map();82let vaultMode: "real" | "decoy" = "real";83// Cached post-unlock view of whether the real vault records that duress is84// configured. Only meaningful after a real-mode unlock; for decoy-mode unlock85// this stays false because we never see slot A's plaintext.86let duressConfigured = false;8788let migrationRan = false;8990// Set to a corruption reason during the last unlock() attempt when both slots91// decrypted but failed schema validation. Consumed and cleared by92// consumeLastUnlockFailureReason() so messages.ts can surface vault_corrupted93// distinctly from wrong-password (M7/COR-007).94//95// NOTE: module-level state — if vault.ts is ever moved to a worker context,96// this must be replaced with a structured return value from unlock() or a97// postMessage channel. The signal is invisible across module-instance98// boundaries.99let lastUnlockFailureReason: string | null = null;100101export function consumeLastUnlockFailureReason(): string | null {102  const r = lastUnlockFailureReason;103  lastUnlockFailureReason = null;104  return r;105}106107// ---------------------------------------------------------------------------108// Settings (persisted inside the encrypted vault store as a sidecar key)109// ---------------------------------------------------------------------------110111export type TextSize = "small" | "medium" | "large";112113export interface VaultSettings {114  accentColor?: AccentColorName;115  theme?: "dark" | "light";116  autoLockMinutes?: number;117  clipboardClearSeconds?: number;118  lockOnClose?: boolean;119  breachMonitoring?: boolean;120  passkeyProviderEnabled?: boolean;121  trashRetentionDays?: number;122  manualSummonDomains?: Record<string, ManualSummonRecord>;123  declinedCardFingerprints?: string[];124  /**125   * Text size scale for body copy and entry titles. Stops at "large" by126   * design — XL clips dense screens (Vault, Health) inside the fixed127   * 330×480 popup surface.128   */129  textSize?: TextSize;130  /**131   * When true, all entrance/exit transitions and ambient animations are132   * collapsed to instant. Honoured alongside the user's OS-level133   * `prefers-reduced-motion`.134   */135  reduceMotion?: boolean;136  /**137   * Android-only: when true, the master key is escrowed in AndroidKeyStore138   * and the lock screen offers biometric unlock. Toggled from Settings →139   * Security → Biometrics. Has no effect on desktop builds.140   */141  biometricsEnabled?: boolean;142  /**143   * User overrides for keyboard shortcuts. Keys are ShortcutAction names144   * (see src/shared/shortcuts.ts). Missing actions fall through to145   * DEFAULT_BINDINGS — `getActiveBindings(settings.shortcuts)` is the146   * canonical lookup. Storing only the deltas keeps the vault payload147   * small and lets default updates land for users who never rebind.148   */149  shortcuts?: Record<string, { key: string; ctrl?: boolean; meta?: boolean; shift?: boolean; alt?: boolean }>;150}151152let settings: VaultSettings = {};153154export function getSettings(): VaultSettings {155  return settings;156}157158export async function ensureSettingsLoaded(): Promise<VaultSettings> {159  await loadSettings();160  return settings;161}162163export async function updateSettings(patch: Partial<VaultSettings>): Promise<void> {164  await loadSettings();165  // LV-7: cheap insurance against prototype pollution if a future caller ever166  // feeds parsed external JSON here (native spread keeps __proto__ an own-167  // property today, but Object.assign-style consumers and JSON revivers have168  // been burned before).169  const safe = Object.fromEntries(170    Object.entries(patch).filter(171      ([k]) => !["__proto__", "constructor", "prototype"].includes(k),172    ),173  ) as Partial<VaultSettings>;174  settings = { ...settings, ...safe };175  await persistSettings();176}177178async function persistSettings(): Promise<void> {179  try {180    await chrome.storage.local.set({ peach_settings: settings });181  } catch (err) {182    throw mapStorageError(err);183  }184}185186function mapStorageError(err: unknown): Error {187  const message = err instanceof Error ? err.message : String(err);188  if (/QUOTA_BYTES|quota/i.test(message)) {189    return new Error("STORAGE_QUOTA_EXCEEDED");190  }191  return err instanceof Error ? err : new Error(message);192}193194async function loadSettings(): Promise<void> {195  const result = await chrome.storage.local.get("peach_settings");196  settings = (result as { peach_settings?: VaultSettings }).peach_settings ?? {};197}198199// Fire-and-forget sync trigger after a committed vault write. Lazy200// dynamic import avoids a circular dependency between vault.ts and201// sync-trigger.ts (the trigger imports paired-devices, which only reads202// storage, but the lazy load also gives us a clean opt-out path in203// tests where sync-trigger has not been initialised).204function fireSyncTrigger(reason: "vault_change"): void {205  void import("./sync-trigger")206    .then((mod) => mod.triggerSyncIfReachable({ reason }))207    .catch(() => {208      /* sync trigger module not available — non-fatal */209    });210}211212// ---------------------------------------------------------------------------213// HIBP breach check (k-anonymity model)214// ---------------------------------------------------------------------------215216export async function checkBreach(password: string): Promise<number> {217  // HIBP k-anonymity API is keyed on SHA-1 prefixes — this is dictated by218  // the public API, not a security choice. Do not migrate to SHA-256 here.219  const encoder = new TextEncoder();220  const hashBuffer = await crypto.subtle.digest("SHA-1", encoder.encode(password));221  const hashHex = Array.from(new Uint8Array(hashBuffer))222    .map((b) => b.toString(16).padStart(2, "0"))223    .join("")224    .toUpperCase();225226  const prefix = hashHex.slice(0, 5);227  const suffix = hashHex.slice(5);228229  try {230    const res = await fetch(`https://api.pwnedpasswords.com/range/${prefix}`);231    if (!res.ok) return 0;232    const text = await res.text();233    const match = text.split("\r\n").find((l) => l.startsWith(suffix));234    return match ? parseInt(match.split(":")[1], 10) : 0;235  } catch {236    return 0; // Network unavailable — don't block237  }238}239240export async function declineCardSaveOffer(last4: string, expMonth: string, expYear: string): Promise<void> {241  const fingerprint = await cardFingerprint(last4, expMonth, expYear);242  await loadSettings();243  settings = {244    ...settings,245    declinedCardFingerprints: [...(settings.declinedCardFingerprints ?? []), fingerprint],246  };247  await persistSettings();248}249250export async function isCardDeclined(last4: string, expMonth: string, expYear: string): Promise<boolean> {251  await loadSettings();252  const fingerprint = await cardFingerprint(last4, expMonth, expYear);253  return (settings.declinedCardFingerprints ?? []).includes(fingerprint);254}255256async function cardFingerprint(last4: string, expMonth: string, expYear: string): Promise<string> {257  const encoder = new TextEncoder();258  const buf = await crypto.subtle.digest("SHA-256", encoder.encode(`${last4}|${expMonth}|${expYear}`));259  return Array.from(new Uint8Array(buf).slice(0, 8))260    .map((b) => b.toString(16).padStart(2, "0"))261    .join("");262}263264function hex(buf: Uint8Array): string {265  return Array.from(buf)266    .map((b) => b.toString(16).padStart(2, "0"))267    .join("");268}269270function unhex(s: string): Uint8Array {271  const out = new Uint8Array(s.length / 2);272  for (let i = 0; i < s.length; i += 2) {273    out[i / 2] = parseInt(s.slice(i, i + 2), 16);274  }275  return out;276}277278// ---------------------------------------------------------------------------279// Root key generation and recovery word wrapping280// ---------------------------------------------------------------------------281282/**283 * Generate a random 32-byte root key and wrap it (AES-256-GCM) under a key284 * derived from the recovery word material via HKDF-SHA256. The wrapped blob285 * fields are stored in slot A alongside the password-wrapped ciphertext. The286 * root key itself is kept in memory for the session so getRecoveryWords() can287 * encode it without re-deriving.288 */289async function generateAndWrapRootKey(): Promise<{290  rawRootKey: string; // hex of the plaintext root key — caller stores in rootKey291  recoverySalt: string;292  wrappedRootKey: string;293  wrappedNonce: string;294  wrappedTag: string;295}> {296  const rootKeyBytes = crypto.getRandomValues(new Uint8Array(32));297298  // Derive the recovery key from the BIP39 words encoding of the root key.299  const words = await rootKeyToWords(rootKeyBytes);300  const wordMaterial = await wordsToRootKeyMaterial(words);301  const recoverySalt = crypto.getRandomValues(new Uint8Array(16));302  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);303304  // Wrap the root key with the recovery key using AES-256-GCM directly.305  // We hex-encode rootKeyBytes so the plaintext is always valid UTF-8.306  const nonce = crypto.getRandomValues(new Uint8Array(12));307  const encoded = new TextEncoder().encode(hex(rootKeyBytes));308  const raw = await crypto.subtle.encrypt(309    {310      name: "AES-GCM",311      iv: nonce,312      additionalData: new Uint8Array([1]).buffer as ArrayBuffer,313      tagLength: 128,314    },315    recoveryKey,316    encoded317  );318  // WebCrypto returns ciphertext || auth tag concatenated; split them.319  const rawU8 = new Uint8Array(raw);320  const ciphertext = hex(rawU8.slice(0, rawU8.length - 16));321  const tag = hex(rawU8.slice(rawU8.length - 16));322323  return {324    rawRootKey: hex(rootKeyBytes),325    recoverySalt: hex(recoverySalt),326    wrappedRootKey: ciphertext,327    wrappedNonce: hex(nonce),328    wrappedTag: tag,329  };330}331332/**333 * Unwrap the root key using recovery words. Called during recovery flow when334 * the user provides their 12-word phrase and a new password.335 * Exported for use by the RECOVER_WITH_WORDS message handler.336 */337export async function unwrapRootKeyForRecovery(338  wrappedRootKey: string,339  wrappedNonce: string,340  wrappedTag: string,341  recoverySaltHex: string,342  words: string[]343): Promise<void> {344  const wordMaterial = await wordsToRootKeyMaterial(words);345  const recoverySalt = unhex(recoverySaltHex);346  const recoveryKey = await deriveRecoveryKey(wordMaterial, recoverySalt);347348  // Decrypt and cache the root key. The root key bytes were hex-encoded before349  // encryption so the ciphertext is valid UTF-8. We inline the AES-GCM decrypt350  // here to avoid passing a CryptoKey to the string-based encrypt/decrypt helpers.351  // Each component is stored separately in the vault slot; decode them individually352  // so the nonce is always exactly the 12 bytes from wrappedNonce.353  const nonceRaw = unhex(wrappedNonce);354  const ciphertextRaw = unhex(wrappedRootKey);355  const tagRaw = unhex(wrappedTag);356  // Combine ciphertext and tag for AES-GCM (expects ciphertext||tag).357  // Use explicit ArrayBuffer allocations to satisfy TS BufferSource constraints.358  const nonceU8 = nonceRaw as unknown as Uint8Array<ArrayBuffer>;359  const combined = new Uint8Array(ciphertextRaw.length + tagRaw.length);360  combined.set(ciphertextRaw, 0);361  combined.set(tagRaw, ciphertextRaw.length);362  const raw = await crypto.subtle.decrypt(363    { name: "AES-GCM", iv: nonceU8, additionalData: new Uint8Array([1]).buffer as ArrayBuffer, tagLength: 128 },364    recoveryKey,365    combined366  );367  const decryptedHex = new TextDecoder().decode(raw);368  rootKey = unhex(decryptedHex);369}370371/**372 * Re-wrap the cached root key under a new derived key (from the user's new373 * password). Called after password change and after recovery re-key so the374 * same recovery words continue to work with the updated password — and so375 * subsequent unlocks can rehydrate rootKey via deriveRootKeyFromDerivedKey.376 *377 * recovery_* fields are intentionally not touched: those wrap rootKey under378 * the recovery words, which do not change when the password changes.379 */380export async function rewrapRootKeyForPassword(381  newDerivedKey: Uint8Array,382  _newSalt: Uint8Array383): Promise<void> {384  if (!rootKey) throw new Error("root key not set");385  const slotA = await loadSlot(SLOT_A);386  if (!slotA) throw new Error("missing slot A");387  const pwRoot = await wrapRootKeyForDerivedKey(newDerivedKey, rootKey);388  await writeSlot(SLOT_A, { ...slotA, ...pwRoot });389}390391/**392 * Change the vault encryption to a new derived key (already derived from393 * the user's new password). Used by RECOVER_WITH_WORDS and changePassword.394 * Caller is responsible for key swap and snapshot.395 */396async function reencryptVaultForNewKey(397  newKey: Uint8Array,398  slotA: StoredVault399): Promise<void> {400  const plaintext = encodePayload(entries, duressConfigured, duressConfigured ? DURESS_SLOT_SIZE : 0);401  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext, "slot_a_v1");402  await writeSlot(SLOT_A, {403    ...slotA,404    nonce,405    ciphertext,406    tag,407  });408}409410/**411 * Full password change with an already-derived key (used after recovery).412 * Unlike changePassword(), this skips re-verification of current password413 * since recovery flow has already validated the user's identity via words.414 */415export async function changePasswordWithNewKey(416  newSalt: Uint8Array,417  newKey: Uint8Array418): Promise<void> {419  const slotA = await loadSlot(SLOT_A);420  if (!slotA) throw new Error("missing slot A");421  // reencryptVaultForNewKey must receive the updated salt so that subsequent422  // UNLOCK calls with the new password can re-derive the correct key.423  await reencryptVaultForNewKey(newKey, { ...slotA, salt: hex(newSalt) });424  if (derivedKey) zeroKey(derivedKey);425  derivedKey = newKey;426  // PCH-01-008: re-escrow the new key (see changePassword). The recovery flow427  // reaches this path, so a post-recovery SW restart would otherwise lock out428  // a user who just reset their password.429  void saveSessionKey(newKey, "real");430  // Refresh pw_root_* under the new key so deriveRootKeyFromDerivedKey works431  // on the next unlock (H2/COR-006). If rootKey is missing — recovery flow432  // should have populated it — fall back silently rather than blowing up the433  // recovery path; migrateToRecovery will rehydrate on the next unlock.434  if (rootKey) {435    const slotAfter = await loadSlot(SLOT_A);436    if (slotAfter) {437      const pwRoot = await wrapRootKeyForDerivedKey(newKey, rootKey);438      await writeSlot(SLOT_A, { ...slotAfter, ...pwRoot });439    }440  }441  if (!duressConfigured) {442    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));443  }444  await stampLocalVaultMutation();445}446447// ---------------------------------------------------------------------------448// Salt generation --- exported so the RECOVER_WITH_WORDS handler can derive449// a new key after password reset without re-importing the crypto module.450// ---------------------------------------------------------------------------451452export { deriveKey, generateSalt };453454export async function loadSlotA(): Promise<StoredVault | null> {455  return loadSlot(SLOT_A);456}457458/**459 * Recover the session root key by AES-GCM-decrypting the pw_root_* fields460 * (a second wrapping of the root key under the password-derived key). This461 * is what makes GET_RECOVERY_WORDS work across lock/unlock cycles without462 * the user re-entering their recovery phrase: the same root key bytes —463 * and therefore the same 12 words — survive every unlock.464 *465 * Returns null when the pw_root fields are absent (pre-v2.12 vault that466 * hasn't been migrated yet) or when decryption fails (wrong derived key,467 * tampered ciphertext). On null the caller should fall through to468 * migrateToRecovery() to populate the fields.469 */470async function deriveRootKeyFromDerivedKey(471  derivedKeyBytes: Uint8Array,472  slotA: StoredVault,473): Promise<Uint8Array | null> {474  if (!slotA.pw_root_ciphertext || !slotA.pw_root_nonce || !slotA.pw_root_tag) {475    return null;476  }477  try {478    const rootHex = await decrypt(479      derivedKeyBytes,480      slotA.pw_root_nonce,481      slotA.pw_root_ciphertext,482      slotA.pw_root_tag,483      "pw_root_v1",484    );485    return unhex(rootHex);486  } catch {487    return null;488  }489}490491// AES-GCM-wrap rootKey under derivedKey for the pw_root_* fields. Returns the492// hex-encoded ciphertext/nonce/tag triple ready to embed in StoredVault.493async function wrapRootKeyForDerivedKey(494  derivedKeyBytes: Uint8Array,495  rootKeyBytes: Uint8Array,496): Promise<{ pw_root_ciphertext: string; pw_root_nonce: string; pw_root_tag: string }> {497  const { nonce, ciphertext, tag } = await encrypt(derivedKeyBytes, hex(rootKeyBytes), "pw_root_v1");498  return {499    pw_root_ciphertext: ciphertext,500    pw_root_nonce: nonce,501    pw_root_tag: tag,502  };503}504505/**506 * For vaults created before v2.12 (no recovery_salt/recovery_wrapped_root_key),507 * generate the root key, wrap it under the recovery words, and persist the508 * recovery escrow into slot A. The caller must show the recovery words to the509 * user before calling this — once called, those words are the vault's recovery510 * path. This runs once, on the first unlock of an upgraded vault.511 */512export async function migrateToRecovery(): Promise<void> {513  if (!derivedKey || vaultMode !== "real") {514    throw new Error("vault must be unlocked in real mode");515  }516  const slotA = await loadSlot(SLOT_A);517  if (!slotA) throw new Error("missing slot A");518  const hasRecoveryEscrow =519    !!slotA.recovery_salt && !!slotA.recovery_wrapped_root_key;520  const hasPwRoot = !!slotA.pw_root_ciphertext && !!slotA.pw_root_nonce;521  if (hasRecoveryEscrow && hasPwRoot) return; // already done522523  // If recovery escrow already exists but pw_root doesn't, we cannot recover524  // the original rootKey without the user's words. The least-surprising path525  // is to rotate to a fresh root key — the user's old recovery words become526  // invalid but we surface peach_recovery_pending so the popup re-presents527  // the words. The unlock-path callers all set that flag.528  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =529    await generateAndWrapRootKey();530  rootKey = unhex(rawRootKey);531532  const pwRoot = await wrapRootKeyForDerivedKey(derivedKey, rootKey);533  const nextSlotA: StoredVault = {534    ...slotA,535    recovery_salt: recoverySalt,536    recovery_wrapped_root_key: wrappedRootKey,537    recovery_nonce: wrappedNonce,538    recovery_tag: wrappedTag,539    ...pwRoot,540  };541  await writeSlot(SLOT_A, nextSlotA);542  await stampLocalVaultMutation();543}544545/**546 * Returns the 12 recovery words for the current session's root key.547 * Must be called while the vault is unlocked. Throws if recovery has not548 * been set up (pre-v2.12 vault that hasn't migrated yet).549 */550export async function getRecoveryWords(): Promise<string[]> {551  if (!rootKey) {552    throw new Error("recovery not set up — vault may need migration");553  }554  return rootKeyToWords(rootKey);555}556557async function loadSlot(key: string): Promise<StoredVault | null> {558  const result = await chrome.storage.local.get(key);559  return (result as Record<string, StoredVault | undefined>)[key] ?? null;560}561562async function writeSlot(key: string, vault: StoredVault): Promise<void> {563  try {564    await chrome.storage.local.set({ [key]: vault });565  } catch (err) {566    throw mapStorageError(err);567  }568}569570// Monotonic clock for per-entry modifiedAt / deletedAt timestamps. Guards the571// sync layer's last-write-wins merge against wall-clock regressions (NTP step,572// manual clock change, DST glitch): a backwards Date.now() must never produce573// an entry timestamp that's <= the entry's existing modifiedAt, or a genuine574// local edit would lose to a stale remote copy. The returned value is always575// strictly greater than both the entry's current modifiedAt and the previous576// call's result (SEV-2-E).577let lastEntryTimestamp = 0;578function monotonicEntryTimestamp(currentModifiedAt?: number): number {579  const base = Math.max(580    Date.now(),581    (currentModifiedAt ?? 0) + 1,582    lastEntryTimestamp + 1,583  );584  lastEntryTimestamp = base;585  return base;586}587588async function stampLocalVaultMutation(): Promise<void> {589  const raw = await chrome.storage.local.get("peach_last_modified_at");590  const previous = Number(591    (raw as Record<string, unknown>).peach_last_modified_at,592  );593  const next = Math.max(594    Date.now(),595    Number.isFinite(previous) ? previous + 1 : 0,596  );597  await chrome.storage.local.set({598    peach_last_modified_at: String(next),599  });600}601602/**603 * One-shot migration from the pre-A-2 schema (peach_vault + peach_decoy_vault604 * + peach_duress flag) to the slot-neutral schema (peach_vault_a +605 * peach_vault_b). Runs at most once per session; safe to call repeatedly.606 */607async function ensureMigrated(): Promise<void> {608  if (migrationRan) return;609  migrationRan = true;610611  const keys = [LEGACY_MAIN, LEGACY_DECOY, LEGACY_DURESS_FLAG, SLOT_A, SLOT_B];612  const raw = await chrome.storage.local.get(keys);613  const slotAExists = raw[SLOT_A] !== undefined;614  const slotBExists = raw[SLOT_B] !== undefined;615  const legacyMain = raw[LEGACY_MAIN] as StoredVault | undefined;616  const legacyDecoy = raw[LEGACY_DECOY] as StoredVault | undefined;617618  // Fresh install with nothing in storage — nothing to do; createVault() sets up both slots.619  if (!slotAExists && !legacyMain) return;620621  // Already migrated.622  if (slotAExists && slotBExists && !legacyMain && !legacyDecoy) return;623624  let didMigrate = false;625  if (legacyMain && !slotAExists) {626    await writeSlot(SLOT_A, legacyMain);627    didMigrate = true;628  }629630  // If slot B isn't set, populate it: use legacy decoy when present, else noise.631  if (!slotBExists) {632    const target = (legacyMain ?? raw[SLOT_A]) as StoredVault | undefined;633    if (legacyDecoy) {634      await writeSlot(SLOT_B, legacyDecoy);635      didMigrate = true;636    } else if (target) {637      await writeSlot(SLOT_B, generateNoiseBlob(target.ciphertext.length / 2));638      didMigrate = true;639    }640  }641642  const toRemove: string[] = [];643  if (legacyMain) toRemove.push(LEGACY_MAIN);644  if (legacyDecoy) toRemove.push(LEGACY_DECOY);645  if (raw[LEGACY_DURESS_FLAG] !== undefined) toRemove.push(LEGACY_DURESS_FLAG);646  if (toRemove.length > 0) {647    await chrome.storage.local.remove(toRemove);648    didMigrate = true;649  }650  if (didMigrate) {651    await stampLocalVaultMutation();652  }653}654655// ---------------------------------------------------------------------------656// Padding + noise helpers657// ---------------------------------------------------------------------------658659/**660 * Pads the plaintext string with trailing spaces so that the encoded byte661 * length is a multiple of PAD_BLOCK. JSON.parse tolerates trailing662 * whitespace after a complete document, so the padding is transparent on663 * decode. Ciphertext length equals plaintext length plus a 16-byte tag.664 */665function padPlaintext(json: string, minBytes = 0): string {666  const encoded = new TextEncoder().encode(json);667  const bucket = Math.max(PAD_BLOCK, Math.ceil(encoded.length / PAD_BLOCK) * PAD_BLOCK);668  // minBytes lets the duress path force a fixed ceiling so both slots match.669  const target = Math.max(bucket, minBytes);670  const padLen = target - encoded.length;671  return json + " ".repeat(padLen);672}673674/**675 * Produces a StoredVault-shaped record filled with CSPRNG bytes. The676 * ciphertext length matches the given byte count. Because GCM ciphertext is677 * pseudo-random, this blob is indistinguishable at rest from a real678 * encrypted vault without the key.679 *680 * crypto.getRandomValues() caps each call at 65536 bytes (the Web Crypto681 * spec hard limit). Chunk the fill so vaults larger than that — common once682 * the user has dozens of entries or any passkey — don't throw.683 */684const CSPRNG_CHUNK = 65_536;685686function fillRandom(buf: Uint8Array): Uint8Array {687  for (let offset = 0; offset < buf.length; offset += CSPRNG_CHUNK) {688    const end = Math.min(offset + CSPRNG_CHUNK, buf.length);689    const chunk = new Uint8Array(end - offset);690    crypto.getRandomValues(chunk);691    buf.set(chunk, offset);692  }693  return buf;694}695696function generateNoiseBlob(ciphertextBytes: number): StoredVault {697  const salt = crypto.getRandomValues(new Uint8Array(16));698  const nonce = crypto.getRandomValues(new Uint8Array(12));699  const cipher = fillRandom(new Uint8Array(ciphertextBytes));700  const tag = crypto.getRandomValues(new Uint8Array(16));701  return {702    version: 1,703    salt: hex(salt),704    nonce: hex(nonce),705    ciphertext: hex(cipher),706    tag: hex(tag),707  };708}709710// ---------------------------------------------------------------------------711// Encrypted plaintext envelope712// ---------------------------------------------------------------------------713714type VaultPayload = {715  entries: VaultEntry[];716  duressConfigured?: boolean;717};718719type DecryptedPayload = {720  entries: VaultEntry[];721  duressConfigured: boolean;722  /**723   * True when at least one entry in the on-disk payload had a legacy shape724   * (pre-v2.11.0 passkey type/flat fields, or pre-A-2 created/updated keys)725   * and got rewritten by normalizeEntry. The unlock path uses this to726   * persist the new shape immediately — without it, the migration would727   * re-run on every unlock and old shape never disappears from disk.728   */729  migratedFromLegacy: boolean;730};731732function encodePayload(entries: VaultEntry[], duress: boolean, minBytes = 0): string {733  const envelope: VaultPayload = { entries, duressConfigured: duress };734  return padPlaintext(JSON.stringify(envelope), minBytes);735}736737function rawNeedsMigration(raw: VaultEntry | LegacyVaultEntry): boolean {738  if ("created" in raw || "updated" in raw) return true;739  const flat = raw as Partial<VaultEntry>;740  if (flat.type === "passkey") return true;741  // Flat rpId without a passkey sub-object means pre-v2.11.0 schema.742  if (flat.rpId !== undefined && flat.passkey === undefined) return true;743  return false;744}745746function decodePayload(plain: string): DecryptedPayload {747  const parsed = JSON.parse(plain) as748    | Array<VaultEntry | LegacyVaultEntry>749    | VaultPayload;750751  const rawEntries = Array.isArray(parsed) ? parsed : parsed.entries ?? [];752  const flatMigrationNeeded = rawEntries.some(rawNeedsMigration);753754  const normalized = rawEntries.map(normalizeEntry);755  const { merged } = mergePasskeyOnlyIntoLogins(normalized);756757  if (Array.isArray(parsed)) {758    return {759      entries: normalized,760      duressConfigured: false,761      migratedFromLegacy: true, // pre-A-2 array shape always needs save762    };763  }764765  return {766    entries: normalized,767    duressConfigured: parsed.duressConfigured === true,768    migratedFromLegacy: flatMigrationNeeded || merged,769  };770}771772/**773 * One-time consolidation pass run alongside the legacy passkey migration.774 *775 * Pre-v2.11.0 the Chrome passkey path always created a fresh `type:"passkey"`776 * entry per registration — so users who tested registration multiple times777 * accumulated several passkey-bearing entries for the same RP, and the778 * migration to the unified shape carried that duplication forward. This779 * pass collapses the duplicates:780 *781 *   1. Group passkey-bearing (and not deleted) entries by `passkey.rpId`.782 *   2. Within each group, the most recently-active credential wins (prefer783 *      lastUsedAt, fall back to passkey.createdAt, fall back to modifiedAt).784 *   3. If a NON-passkey login entry exists for that RP (matched by url or785 *      additionalUrls hostname), attach the winning passkey to it and786 *      soft-delete every passkey-bearing entry in the group.787 *   4. Otherwise, soft-delete every loser in the group, keeping the winner788 *      as a passkey-only entry.789 *790 * Soft delete (deletedAt) rather than splice — the trash flow keeps the791 * record around for retention and the user can restore if the heuristic792 * picked wrong. The `merged` flag goes back to decodePayload so the793 * unlock path persists the consolidated shape immediately.794 */795function mergePasskeyOnlyIntoLogins(entries: VaultEntry[]): { merged: boolean } {796  let merged = false;797  const now = Date.now();798799  const candidates = entries.filter((e) => !e.deletedAt && e.passkey);800  const byRpId = new Map<string, VaultEntry[]>();801  for (const e of candidates) {802    const rpId = e.passkey!.rpId;803    const list = byRpId.get(rpId);804    if (list) list.push(e);805    else byRpId.set(rpId, [e]);806  }807808  for (const [rpId, group] of byRpId) {809    if (group.length === 0) continue;810    group.sort((a, b) => {811      const aT =812        a.passkey!.lastUsedAt ?? a.passkey!.createdAt ?? a.modifiedAt ?? a.createdAt;813      const bT =814        b.passkey!.lastUsedAt ?? b.passkey!.createdAt ?? b.modifiedAt ?? b.createdAt;815      return bT - aT;816    });817    const winner = group[0]!;818819    // Look for a NON-passkey login entry matching this RP. Use the URL820    // hostname rather than a substring search so a "login.foo.com" entry821    // doesn't accidentally absorb a passkey for "foo.com".822    const loginTarget = entries.find((e) => {823      if (e.deletedAt || e.passkey || e.type !== "login") return false;824      if (e.url && extractDomain(e.url) === rpId) return true;825      for (const extra of e.additionalUrls ?? []) {826        if (extractDomain(extra) === rpId) return true;827      }828      return false;829    });830831    if (loginTarget) {832      loginTarget.passkey = winner.passkey;833      loginTarget.modifiedAt = now;834      for (const dup of group) {835        if (!dup.deletedAt) {836          dup.deletedAt = now;837          dup.modifiedAt = now;838        }839      }840      merged = true;841    } else if (group.length > 1) {842      for (let i = 1; i < group.length; i += 1) {843        const dup = group[i]!;844        if (!dup.deletedAt) {845          dup.deletedAt = now;846          dup.modifiedAt = now;847        }848      }849      merged = true;850    }851  }852853  return { merged };854}855856// Validates that a `StoredVault` record on disk has the field shape we expect857// before treating an AES-GCM decrypt success as a clean unlock. AES-GCM only858// authenticates the static VERSION_BYTE AAD, not the `version` field on the859// outer record — a corrupted `version: 42` slot still decrypts cleanly. This860// check surfaces that as `corrupted` (M7/COR-007) so the popup can offer a861// history-restore path instead of falsely reporting "wrong password".862function hasValidVaultSchema(v: unknown): v is StoredVault {863  if (typeof v !== "object" || v === null) return false;864  const r = v as Record<string, unknown>;865  return (866    r.version === 1 &&867    typeof r.salt === "string" &&868    typeof r.nonce === "string" &&869    typeof r.ciphertext === "string" &&870    typeof r.tag === "string"871  );872}873874export type TryDecryptResult =875  | { status: "ok"; payload: DecryptedPayload }876  | { status: "wrong_key" }877  | { status: "corrupted"; reason: string };878879// Exported solely for `unlockFromCodex` below, which needs to verify a880// supplied passphrase against a slotA blob *without* committing anything to881// storage. Keep this internal otherwise — callers crossing the messages.ts882// boundary should always go through `unlock` / `verifyMainPassword` so the883// constant-time invariants in Projects.md §3.4 are not bypassed.884export async function tryDecryptSlot(885  vault: StoredVault,886  key: Uint8Array,887  aadTag?: string,888): Promise<DecryptedPayload | null> {889  const res = await tryDecryptSlotResult(vault, key, aadTag);890  return res.status === "ok" ? res.payload : null;891}892893export async function tryDecryptSlotResult(894  vault: StoredVault,895  key: Uint8Array,896  aadTag?: string,897): Promise<TryDecryptResult> {898  if (!hasValidVaultSchema(vault)) {899    return { status: "corrupted", reason: "invalid_slot_schema" };900  }901  let plain: string;902  try {903    plain = await decrypt(key, vault.nonce, vault.ciphertext, vault.tag, aadTag);904  } catch {905    return { status: "wrong_key" };906  }907  try {908    const parsed = JSON.parse(plain) as unknown;909    if (typeof parsed !== "object" || parsed === null) {910      return { status: "corrupted", reason: "invalid_payload_shape" };911    }912    if (!Array.isArray(parsed) && !Array.isArray((parsed as VaultPayload).entries)) {913      return { status: "corrupted", reason: "invalid_payload_shape" };914    }915    return { status: "ok", payload: decodePayload(plain) };916  } catch {917    return { status: "corrupted", reason: "invalid_payload_json" };918  }919}920921// ---------------------------------------------------------------------------922// Core operations923// ---------------------------------------------------------------------------924925function buildDomainIndexSync(all: VaultEntry[]): void {926  domainIndex = new Map();927928  for (const entry of all) {929    if (entry.deletedAt) continue;930    if (entry.type === "note" || entry.type === "card") continue;931932    // An entry can match multiple sites via `additionalUrls`. Index each933    // hostname so `queryDomain` returns the entry regardless of which alias934    // the user is currently visiting. Dedupe per-entry so a single entry935    // never appears twice in one bucket.936    const domains = new Set<string>();937    const primary = extractDomain(entry.url ?? "");938    if (primary) domains.add(primary);939    for (const extra of entry.additionalUrls ?? []) {940      const d = extractDomain(extra);941      if (d) domains.add(d);942    }943    if (domains.size === 0) continue;944945    for (const domain of domains) {946      const bucket = domainIndex.get(domain) ?? [];947      bucket.push(entry);948      domainIndex.set(domain, bucket);949    }950  }951}952953export async function createVault(password: string): Promise<void> {954  await ensureMigrated();955956  const salt = generateSalt();957  derivedKey = await deriveKey(password, salt);958  entries = [];959  vaultMode = "real";960  duressConfigured = false;961  buildDomainIndexSync(entries);962963  // Generate and wrap the root key for recovery word escrow.964  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =965    await generateAndWrapRootKey();966  rootKey = unhex(rawRootKey);967968  const plaintext = encodePayload(entries, duressConfigured, duressConfigured ? DURESS_SLOT_SIZE : 0);969  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext, "slot_a_v1");970971  // Second wrapping of rootKey under derivedKey so future unlocks can rehydrate972  // it without recovery words (H2/COR-006).973  const pwRoot = await wrapRootKeyForDerivedKey(derivedKey, rootKey);974975  const slotA: StoredVault = {976    version: 1,977    salt: hex(salt),978    nonce,979    ciphertext,980    tag,981    recovery_salt: recoverySalt,982    recovery_wrapped_root_key: wrappedRootKey,983    recovery_nonce: wrappedNonce,984    recovery_tag: wrappedTag,985    ...pwRoot,986  };987  await writeSlot(SLOT_A, slotA);988989  // Matching-size noise blob for slot B, so a fresh install shows two990  // similarly-shaped records regardless of whether duress is ever set up.991  await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));992  await stampLocalVaultMutation();993994  touchAutoLock();995}996997export async function hasVault(): Promise<boolean> {998  await ensureMigrated();999  return (await loadSlot(SLOT_A)) !== null;1000}10011002/**1003 * Returns whether the real vault has recorded that a duress passphrase is1004 * configured. Reliable only after a real-mode unlock — before unlock or in1005 * decoy mode, returns false because we cannot read slot A's plaintext.1006 */1007export async function isDuressEnabled(): Promise<boolean> {1008  return vaultMode === "real" && duressConfigured;1009}10101011export async function setupDuress(duressPassword: string): Promise<void> {1012  if (!derivedKey || vaultMode !== "real") {1013    throw new Error("vault must be unlocked in real mode");1014  }1015  await ensureMigrated();10161017  // Encrypt the decoy payload (empty entries) under the duress key. Pad to1018  // at least the current slot A ciphertext size so the two slots look the1019  // same at rest at setup time.1020  const slotA = await loadSlot(SLOT_A);1021  if (!slotA) throw new Error("missing slot A");10221023  const decoySalt = generateSalt();1024  const decoyKey = await deriveKey(duressPassword, decoySalt);1025  try {1026    // Pad the decoy to the fixed duress ceiling. Slot A is re-encrypted to the1027    // same ceiling by the persistSlotA() call below (duressConfigured is now1028    // true), so both slots are exactly DURESS_SLOT_SIZE+tag at rest and their1029    // lengths cannot reveal that duress is configured (audit MEDIUM).1030    const plaintext = encodePayload([], false, DURESS_SLOT_SIZE);1031    const { nonce, ciphertext, tag } = await encrypt(decoyKey, plaintext, "slot_b_v1");1032    await writeSlot(SLOT_B, {1033      version: 1,1034      salt: hex(decoySalt),1035      nonce,1036      ciphertext,1037      tag,1038    });1039  } finally {1040    zeroKey(decoyKey);1041  }10421043  // Re-encrypt slot A with the duressConfigured flag set to true. No external1044  // flag is written — the bit lives inside the encrypted envelope.1045  duressConfigured = true;1046  await persistSlotA();1047}10481049export async function disableDuress(): Promise<void> {1050  if (!derivedKey || vaultMode !== "real") {1051    throw new Error("vault must be unlocked in real mode");1052  }1053  await ensureMigrated();10541055  // Overwrite slot B with fresh noise that matches slot A's size. The slot1056  // remains occupied so its mere existence does not signal that duress was1057  // ever configured.1058  const slotA = await loadSlot(SLOT_A);1059  if (slotA) {1060    await writeSlot(SLOT_B, generateNoiseBlob(slotA.ciphertext.length / 2));1061  }10621063  duressConfigured = false;1064  await persistSlotA();1065}10661067// L14/COR-015: this function intentionally checks slot A (real vault) only.1068// A duress-mode user attempting changePassword is rejected earlier by the1069// vaultMode === 'decoy' check at changePassword. The real-vs-decoy1070// distinction here is by design — duress users cannot rotate the real1071// password by guessing it, and the asymmetry in error timing is acceptable1072// because duress mode is opt-in and the user knows their own decoy key.1073export async function verifyMainPassword(password: string): Promise<boolean> {1074  await ensureMigrated();1075  const slotA = await loadSlot(SLOT_A);1076  if (!slotA) return false;1077  const key = await deriveKey(password, unhex(slotA.salt));1078  try {1079    return (await tryDecryptSlot(slotA, key, "slot_a_v1")) !== null;1080  } finally {1081    zeroKey(key);1082  }1083}10841085export async function unlock(password: string): Promise<boolean> {1086  // LV-1: the normal path NFC-normalizes inside deriveKey. If that fails and1087  // the supplied password is not already in NFC form, the vault may have been1088  // sealed (pre-LV-1) with the raw, un-normalized bytes — retry with those.1089  if (await attemptUnlockWithDerive(password, deriveKey)) return true;10901091  const nfc = password.normalize("NFC");1092  if (password === nfc) return false; // ASCII/NFC password — no legacy form to try10931094  if (await attemptUnlockWithDerive(password, deriveKeyRaw)) {1095    // Heal: re-seal the real slot under the NFC-normalized key so every1096    // future unlock takes the normal path. Only the real vault is re-sealed;1097    // a non-NFC duress password keeps working through this fallback.1098    if (vaultMode === "real") {1099      try {1100        const newSalt = generateSalt();1101        const newKey = await deriveKey(password, newSalt);1102        await changePasswordWithNewKey(newSalt, newKey);1103      } catch (healErr) {1104        console.warn("[Peach] LV-1 NFC heal failed:", healErr);1105      }1106    }1107    return true;1108  }1109  return false;1110}11111112async function attemptUnlockWithDerive(1113  password: string,1114  derive: typeof deriveKey,1115): Promise<boolean> {1116  await ensureMigrated();11171118  // Load both slots in parallel. Both always exist post-migration; the1119  // parallel load and the parallel derivation below keep wall-clock time1120  // constant regardless of which slot's passphrase (real or decoy) the1121  // caller supplied, so a timing side channel cannot distinguish the1122  // duress path from the real-unlock path (DUR-002).1123  const [slotA, slotB] = await Promise.all([1124    loadSlot(SLOT_A),1125    loadSlot(SLOT_B),1126  ]);11271128  // Derive both keys in parallel, unconditionally.1129  const [keyA, keyB] = await Promise.all([1130    slotA ? derive(password, unhex(slotA.salt)) : Promise.resolve(null),1131    slotB ? derive(password, unhex(slotB.salt)) : Promise.resolve(null),1132  ]);11331134  // Attempt both decrypts in parallel. The Result variant lets us distinguish1135  // 'wrong_key' from 'corrupted' so callers can offer a history-restore path1136  // when a slot's schema is invalid even though AES-GCM authenticated (M7).1137  const [resultA, resultB] = await Promise.all([1138    keyA && slotA1139      ? tryDecryptSlotResult(slotA, keyA, "slot_a_v1")1140      : Promise.resolve({ status: "wrong_key" } as TryDecryptResult),1141    keyB && slotB1142      ? tryDecryptSlotResult(slotB, keyB, "slot_b_v1")1143      : Promise.resolve({ status: "wrong_key" } as TryDecryptResult),1144  ]);1145  const payloadA = resultA.status === "ok" ? resultA.payload : null;1146  const payloadB = resultB.status === "ok" ? resultB.payload : null;11471148  if (payloadA && keyA) {1149    derivedKey = keyA;1150    void saveSessionKey(keyA, "real");1151    vaultMode = "real";1152    entries = payloadA.entries;1153    duressConfigured = payloadA.duressConfigured;1154    buildDomainIndexSync(entries);1155    await loadSettings();1156    touchAutoLock();1157    if (keyB) zeroKey(keyB);11581159    // slotA is non-null when payloadA is truthy (keyA was derived from slotA).1160    if (slotA) {1161      const needsRecoverySetup =1162        !slotA.recovery_salt || !slotA.recovery_wrapped_root_key;1163      if (!needsRecoverySetup) {1164        rootKey = await deriveRootKeyFromDerivedKey(keyA, slotA);1165      }1166      // Pre-v2.12 vault, or a vault whose pw_root_* fields haven't been written1167      // yet: generate the recovery escrow and seed pw_root_* so subsequent1168      // unlocks can rehydrate rootKey without the user's recovery words.1169      // Best-effort — a failure here must not block unlock.1170      if (needsRecoverySetup || !rootKey) {1171        try {1172          await migrateToRecovery();1173          if (needsRecoverySetup) {1174            // String "1" matches the popup's existing peach_recovery_pending1175            // check; surfaced so the user re-confirms recovery words after a1176            // legacy-vault unlock (H2/COR-006).1177            await chrome.storage.local.set({ peach_recovery_pending: '1' });1178          }1179        } catch (migrationErr) {1180          console.warn('[Peach] recovery migration failed:', migrationErr);1181        }1182      }1183    }11841185    // If the payload had a pre-v2.11.0 shape (legacy passkey type, flat1186    // passkey fields, or pre-A-2 created/updated keys), rewrite it now so1187    // the next save uses the new format and migration doesn't re-run forever.1188    if (payloadA.migratedFromLegacy) {1189      await persistSlotA();1190    }1191    return true;1192  }11931194  if (payloadB && keyB) {1195    derivedKey = keyB;1196    void saveSessionKey(keyB, "decoy");1197    vaultMode = "decoy";1198    entries = payloadB.entries;1199    duressConfigured = false;1200    buildDomainIndexSync(entries);1201    await loadSettings();1202    touchAutoLock();1203    if (keyA) zeroKey(keyA);1204    return true;1205  }12061207  if (keyA) zeroKey(keyA);1208  if (keyB) zeroKey(keyB);1209  // Surface corruption (good AES tag, broken schema) distinctly from1210  // wrong-password so the popup can offer history-restore (M7/COR-007).1211  if (resultA.status === "corrupted") {1212    lastUnlockFailureReason = resultA.reason;1213  } else if (resultB.status === "corrupted") {1214    lastUnlockFailureReason = resultB.reason;1215  }1216  return false;1217}12181219// ---------------------------------------------------------------------------1220// MV3 service worker session key — survives SW restarts within a browser session1221// ---------------------------------------------------------------------------12221223// chrome.storage.session is cleared on browser close; survives SW restart.1224// Firefox does not implement it (persistent background script, no restart risk).1225const SESSION_KEY_STORAGE = "peach_session_dk";12261227async function saveSessionKey(key: Uint8Array, mode: "real" | "decoy"): Promise<void> {1228  if (!chrome.storage.session) return;1229  await chrome.storage.session.set({ [SESSION_KEY_STORAGE]: { keyHex: hex(key), mode } });1230}12311232async function clearSessionKey(): Promise<void> {1233  if (!chrome.storage.session) return;1234  await chrome.storage.session.remove(SESSION_KEY_STORAGE);1235}12361237/**1238 * On SW restart, re-derive all in-memory vault state from the session key1239 * stored in chrome.storage.session (set at unlock time, cleared on lock).1240 * Returns true if the vault was restored, false if no session key was found.1241 * No-ops if the vault is already unlocked.1242 */1243export async function restoreFromSession(): Promise<boolean> {1244  if (derivedKey !== null) return true;1245  if (!chrome.storage.session) return false;1246  const stored = await chrome.storage.session.get(SESSION_KEY_STORAGE);1247  const entry = stored[SESSION_KEY_STORAGE] as { keyHex: string; mode: "real" | "decoy" } | undefined;1248  if (!entry?.keyHex) return false;12491250  const key = unhex(entry.keyHex);1251  const slotKey = entry.mode === "real" ? SLOT_A : SLOT_B;1252  const aadTag = entry.mode === "real" ? "slot_a_v1" : "slot_b_v1";1253  const slot = await loadSlot(slotKey);1254  if (!slot) { await clearSessionKey(); return false; }12551256  const result = await tryDecryptSlotResult(slot, key, aadTag);1257  if (result.status !== "ok") { await clearSessionKey(); return false; }12581259  derivedKey = key;1260  vaultMode = entry.mode;1261  entries = result.payload.entries;1262  duressConfigured = entry.mode === "real" ? result.payload.duressConfigured : false;1263  buildDomainIndexSync(entries);1264  await loadSettings();1265  touchAutoLock();12661267  if (entry.mode === "real" && slot.recovery_salt && slot.recovery_wrapped_root_key) {1268    try { rootKey = await deriveRootKeyFromDerivedKey(key, slot); } catch { /* non-fatal */ }1269  }12701271  return true;1272}12731274export function lock(): void {1275  void clearSessionKey();1276  if (derivedKey) {1277    zeroKey(derivedKey);1278    derivedKey = null;1279  }1280  if (rootKey) {1281    rootKey.fill(0);1282    rootKey = null;1283  }12841285  entries = [];1286  domainIndex = new Map();1287  vaultMode = "real";1288  duressConfigured = false;1289  clearAutoLock();1290}12911292export function isUnlocked(): boolean {1293  return derivedKey !== null;1294}12951296// Opens the vault using a pre-derived AES-256 key (skipping Argon2id).1297// Used by Android biometric unlock — the key was derived on the native side.1298export async function unlockWithKey(1299  masterKeyHex: string,1300): Promise<{ ok: true; entries: VaultEntry[] } | { ok: false; error: string }> {1301  try {1302    const masterKeyBytes = unhex(masterKeyHex);1303    const stored = await loadSlot(SLOT_A);1304    if (!stored) return { ok: false, error: "invalid_key" };1305    const payload = await tryDecryptSlot(stored, masterKeyBytes, "slot_a_v1");1306    if (!payload) return { ok: false, error: "invalid_key" };13071308    derivedKey = masterKeyBytes;1309    vaultMode = "real";1310    entries = payload.entries;1311    duressConfigured = payload.duressConfigured;1312    buildDomainIndexSync(entries);1313    await loadSettings();1314    touchAutoLock();13151316    // PCH-01-009: the biometric path skips Argon2id but must still rehydrate the1317    // root key, exactly like attemptUnlockWithDerive and restoreFromSession.1318    // Without it, rootKey stays null after a biometric unlock and chirp sync +1319    // GET_RECOVERY_WORDS report the open vault as locked. Guarded and non-fatal:1320    // a missing/legacy escrow must not fail the unlock itself.1321    if (stored.recovery_salt && stored.recovery_wrapped_root_key) {1322      try {1323        rootKey = await deriveRootKeyFromDerivedKey(masterKeyBytes, stored);1324      } catch { /* non-fatal — unlock still succeeds without rootKey */ }1325    }1326    return { ok: true, entries };1327  } catch {1328    return { ok: false, error: "invalid_key" };1329  }1330}13311332/**1333 * Restore the vault from a parsed Peach Codex payload.1334 *1335 * The supplied passphrase is verified against the Codex's slotA *before* any1336 * storage mutation — wrong passphrases return without touching disk. On1337 * success, the current vault state is snapshotted (reason: "restore") so the1338 * user can revert via Settings -> Vault History, then slotA/slotB are written1339 * and `peach_last_modified_at` is set to the Codex's recorded timestamp. A1340 * regular `unlock()` follows so the rest of the SW sees the standard1341 * post-unlock state.1342 *1343 * Intentionally non-Pro-gated — see peach_codex_v2.md "Pro gating". A Codex1344 * printed by a Pro user must always be restorable, even on a free device.1345 *1346 * Timing is not held constant here (in contrast to `unlock`): the Codex flow1347 * is an explicit user action with a known passphrase, not a passive lock1348 * screen, so the duress-vs-real timing side channel does not apply.1349 */1350export async function unlockFromCodex(params: {1351  slotA: string; // JSON string of StoredVault for slot A1352  slotB: string | null; // JSON string of StoredVault for slot B, or null1353  timestamp: number;1354  masterPassphrase: string;1355}): Promise<1356  | { ok: true; entryCount: number }1357  | { ok: false; reason: "wrong_passphrase" | "invalid_slot_format" }1358> {1359  let parsedSlotA: StoredVault;1360  let parsedSlotB: StoredVault | null = null;1361  try {1362    const rawA = JSON.parse(params.slotA) as unknown;1363    if (!hasValidVaultSchema(rawA)) {1364      return { ok: false, reason: "invalid_slot_format" };1365    }1366    parsedSlotA = rawA;1367    if (params.slotB !== null) {1368      const rawB = JSON.parse(params.slotB) as unknown;1369      if (!hasValidVaultSchema(rawB)) {1370        return { ok: false, reason: "invalid_slot_format" };1371      }1372      parsedSlotB = rawB;1373    }1374  } catch {1375    return { ok: false, reason: "invalid_slot_format" };1376  }13771378  // Verify the passphrase against the supplied slotA WITHOUT writing anything.1379  // If this fails the on-disk vault is left completely untouched.1380  const trialKey = await deriveKey(params.masterPassphrase, unhex(parsedSlotA.salt));1381  const trialPayload = await tryDecryptSlot(parsedSlotA, trialKey, "slot_a_v1");1382  if (!trialPayload) {1383    zeroKey(trialKey);1384    return { ok: false, reason: "wrong_passphrase" };1385  }1386  // Drop the trial key; `unlock()` below derives its own copy. Holding two1387  // copies of the master key in memory at once would be a needless surface.1388  zeroKey(trialKey);13891390  // Snapshot the about-to-be-overwritten on-disk state so the user can revert1391  // a mistaken Codex restore via the Vault History UI. Non-fatal if it fails.1392  await takeVaultSnapshot("restore");13931394  await writeSlot(SLOT_A, parsedSlotA);1395  if (parsedSlotB) {1396    await writeSlot(SLOT_B, parsedSlotB);1397  } else {1398    // Slot B must always exist with ciphertext length matching slot A1399    // (Projects.md §3.2). Codex prints may omit slot B (duress wasn't1400    // configured); regenerate noise to satisfy the invariant.1401    await writeSlot(1402      SLOT_B,1403      generateNoiseBlob(parsedSlotA.ciphertext.length / 2),1404    );1405  }1406  await chrome.storage.local.set({1407    peach_last_modified_at: String(params.timestamp),1408  });14091410  // Standard unlock path so domain index, settings, auto-lock, and1411  // recovery-key state all populate exactly as a normal unlock would.1412  const ok = await unlock(params.masterPassphrase);1413  if (!ok) {1414    // Should be unreachable — we just verified the passphrase against the1415    // same slotA we wrote. Treat as wrong_passphrase to keep the contract1416    // simple if some race or storage anomaly intrudes.1417    return { ok: false, reason: "wrong_passphrase" };1418  }1419  return { ok: true, entryCount: getEntries().length };1420}14211422// Returns the current in-memory derived key as a hex string.1423// Used by the biometric enable flow in Settings to store the key in Android Keystore.1424export function getDerivedKeyHex(): string {1425  if (!derivedKey) throw new Error("vault_not_unlocked");1426  return hex(derivedKey);1427}14281429/**1430 * Returns the vault's stable root key as hex, or null when locked / not yet1431 * populated. The root key is random per-vault, generated at creation, escrowed1432 * under the recovery words, and re-wrapped (not regenerated) on every password1433 * change — so it is INVARIANT across password rotation, unlike getDerivedKeyHex.1434 *1435 * This is the correct input for sync identity derivation (vault_id, the Noise1436 * XX static key, the Ed25519 manifest signing key, and per-entry envelope1437 * keys): paired devices keep the same identity and can still decrypt each1438 * other's entries after either side rotates its master password (SEV-2-B).1439 *1440 * Returns null rather than throwing so sync callers degrade to VAULT_LOCKED1441 * instead of crashing; rootKey is populated on every real-mode unlock via1442 * deriveRootKeyFromDerivedKey / migrateToRecovery.1443 */1444export function getRootKeyHex(): string | null {1445  return rootKey ? hex(rootKey) : null;1446}14471448export function getEntries(): VaultEntry[] {1449  if (!derivedKey) throw new Error("vault_locked");1450  return entries.filter((entry) => !entry.deletedAt);1451}14521453const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;14541455export function getIdentities(): SavedIdentity[] {1456  if (!derivedKey) throw new Error("vault_locked");1457  const active = entries.filter((e) => !e.deletedAt && e.type === "login");14581459  const emailMap = new Map<string, { lastUsed: number; count: number; domain: string }>();1460  const usernameMap = new Map<string, { lastUsed: number; count: number; domain: string }>();14611462  for (const entry of active) {1463    const domain = extractDomain(entry.url ?? "") ?? "";1464    const modifiedAt = entry.modifiedAt ?? entry.createdAt ?? 0;14651466    function tally(map: Map<string, { lastUsed: number; count: number; domain: string }>, v: string) {1467      const existing = map.get(v);1468      if (!existing || modifiedAt > existing.lastUsed) {1469        map.set(v, { lastUsed: modifiedAt, count: (existing?.count ?? 0) + 1, domain });1470      } else {1471        map.set(v, { ...existing, count: existing.count + 1 });1472      }1473    }14741475    const uVal = entry.username?.trim();1476    if (uVal) tally(EMAIL_RE.test(uVal) ? emailMap : usernameMap, uVal);14771478    // Also count the dedicated email field — entries created via the inline1479    // popup new-entry form store email here rather than in username.1480    const eVal = entry.email?.trim();1481    if (eVal) tally(emailMap, eVal);1482  }14831484  const toIdentity = (type: "email" | "username") =>1485    ([value, d]: [string, { lastUsed: number; count: number; domain: string }]): SavedIdentity => ({1486      value, type, lastUsed: d.lastUsed, useCount: d.count, exampleDomain: d.domain || undefined,1487    });14881489  return [1490    ...[...emailMap.entries()].map(toIdentity("email")).sort((a, b) => b.useCount - a.useCount),1491    ...[...usernameMap.entries()].map(toIdentity("username")).sort((a, b) => b.useCount - a.useCount),1492  ];1493}14941495export async function batchAddEntries(drafts: EntryDraft[]): Promise<VaultEntry[]> {1496  if (!derivedKey) throw new Error("vault_locked");14971498  const now = Date.now();1499  const added: VaultEntry[] = [];1500  for (const draft of drafts) {1501    try {1502      const sanitized = sanitizeEntryDraft(draft);1503      const nextEntry: VaultEntry = {1504        ...sanitized,1505        id: crypto.randomUUID(),1506        createdAt: now,1507        modifiedAt: monotonicEntryTimestamp(),1508      };1509      entries.push(nextEntry);1510      added.push(nextEntry);1511    } catch {1512      // Skip invalid drafts silently — caller can see diff between input and output length1513    }1514  }15151516  if (added.length > 0) {1517    await persistEntries();1518  }15191520  return added;1521}15221523/**1524 * Restore a set of entries in full fidelity — preserves id, createdAt,1525 * modifiedAt, and every other field unlike `batchAddEntries` which1526 * assigns fresh identity. Used by restore paths where the backup is the1527 * authoritative source of truth. `mode` controls how entries already in1528 * the vault are treated.1529 */1530export async function restoreEntries(1531  incoming: VaultEntry[],1532  mode: "replace" | "merge"1533): Promise<{ added: number; updated: number; skipped: number }> {1534  if (!derivedKey) throw new Error("vault_locked");15351536  // A replace with zero incoming entries is almost certainly a caller bug1537  // (e.g. an import that silently dropped everything). Refuse rather than1538  // wipe the vault.1539  if (mode === "replace" && incoming.length === 0) {1540    throw new Error("refuse to replace with empty entry set");1541  }15421543  let added = 0;1544  let updated = 0;1545  let skipped = 0;15461547  if (mode === "replace") {1548    entries = [];1549    for (const entry of incoming) {1550      const normalized = sanitizeRestoredEntry(entry);1551      if (!normalized) {1552        skipped += 1;1553        continue;1554      }1555      entries.push(normalized);1556      added += 1;1557    }1558  } else {1559    const byId = new Map<string, VaultEntry>();1560    for (const e of entries) byId.set(e.id, e);15611562    for (const entry of incoming) {1563      const normalized = sanitizeRestoredEntry(entry);1564      if (!normalized) {1565        skipped += 1;1566        continue;1567      }1568      const existing = byId.get(normalized.id);1569      if (!existing) {1570        entries.push(normalized);1571        added += 1;1572      } else if (normalized.modifiedAt > existing.modifiedAt) {1573        const idx = entries.findIndex((e) => e.id === existing.id);1574        if (idx !== -1) {1575          entries[idx] = normalized;1576          updated += 1;1577        }1578      } else {1579        skipped += 1;1580      }1581    }1582  }15831584  await persistEntries("import");1585  return { added, updated, skipped };1586}15871588/**1589 * Validate + normalise a VaultEntry coming from a backup, import, or sync1590 * peer. MV-6: this is a strict field WHITELIST — `normalizeEntry` copies1591 * only known VaultEntry fields (unknown/attacker keys are dropped), and1592 * nested `passkey`/`canary` shapes are rebuilt field-by-field, never1593 * spread. Returns null for values the live vault shouldn't accept.1594 */1595function sanitizeRestoredEntry(entry: VaultEntry): VaultEntry | null {1596  if (!entry || typeof entry !== "object") return null;1597  const title = typeof entry.title === "string" ? entry.title.trim() : "";1598  if (!title) return null;1599  const id = typeof entry.id === "string" && entry.id ? entry.id : crypto.randomUUID();1600  const now = Date.now();1601  const createdAt = typeof entry.createdAt === "number" ? entry.createdAt : now;1602  const modifiedAt = typeof entry.modifiedAt === "number" ? entry.modifiedAt : createdAt;16031604  // normalizeEntry whitelists every known VaultEntry field and runs the1605  // same legacy-passkey migration as the on-disk read path — pre-v2.11.01606  // backups with flat passkey fields still import correctly because the1607  // legacy flat fields are themselves whitelisted VaultEntry members.1608  const normalized = normalizeEntry({1609    ...entry,1610    id,1611    title,1612    type: entry.type ?? "login",1613    createdAt,1614    modifiedAt,1615  });16161617  if (normalized.passkey) {1618    normalized.passkey = sanitizePasskey(normalized.passkey);1619  }1620  if (normalized.canary) {1621    normalized.canary = sanitizeCanary(normalized.canary);1622  }1623  // PCH-01-016: bound tags on the restore path too, so both import funnels agree.1624  normalized.tags = sanitizeTags(normalized.tags);16251626  // Login entries from backup must carry a password (or be passkey-only,1627  // in which case the passkey sub-object replaces the password requirement).1628  if (1629    normalized.type === "login" &&1630    !normalized.passkey &&1631    typeof normalized.password !== "string"1632  ) {1633    return null;1634  }1635  if (entry.passkey && !normalized.passkey) {1636    // Entry claimed a passkey but the shape didn't validate — refuse the1637    // whole entry rather than silently importing a credential-less husk.1638    return null;1639  }16401641  return normalized;1642}16431644/**1645 * Rebuild a PasskeyData object from untrusted input, field by field.1646 * Returns undefined when required fields are missing/mistyped — a rogue1647 * passkey credential must not activate via import (MV-6 attack scenario).1648 */1649function sanitizePasskey(pk: unknown): PasskeyData | undefined {1650  if (!pk || typeof pk !== "object") return undefined;1651  const r = pk as Record<string, unknown>;1652  if (1653    typeof r.rpId !== "string" || !r.rpId ||1654    typeof r.credentialId !== "string" || !r.credentialId ||1655    typeof r.publicKeyCose !== "string" ||1656    typeof r.userHandle !== "string" ||1657    typeof r.userName !== "string"1658  ) {1659    return undefined;1660  }1661  return {1662    rpId: r.rpId,1663    rpName: typeof r.rpName === "string" ? r.rpName : r.rpId,1664    credentialId: r.credentialId,1665    publicKeyCose: r.publicKeyCose,1666    privateKeyJwk:1667      r.privateKeyJwk && typeof r.privateKeyJwk === "object"1668        ? (r.privateKeyJwk as JsonWebKey)1669        : undefined,1670    userHandle: r.userHandle,1671    userName: r.userName,1672    userDisplayName:1673      typeof r.userDisplayName === "string" ? r.userDisplayName : r.userName,1674    algorithm: r.algorithm === -8 ? -8 : -7,1675    signCount: typeof r.signCount === "number" ? r.signCount : 0,1676    aaguid: typeof r.aaguid === "string" ? r.aaguid : "",1677    backupEligible: r.backupEligible === true,1678    backupState: r.backupState === true,1679    lastUsedAt: typeof r.lastUsedAt === "number" ? r.lastUsedAt : undefined,1680    browser: r.browser === "chrome" || r.browser === "firefox" ? r.browser : undefined,1681    transports: Array.isArray(r.transports)1682      ? r.transports.filter((t): t is string => typeof t === "string")1683      : undefined,1684    createdAt: typeof r.createdAt === "number" ? r.createdAt : Date.now(),1685  };1686}16871688/**1689 * Rebuild a Canary object from untrusted input. The webhook must be a1690 * well-formed HTTPS URL — a poisoned canary.webhookUrl is a standing1691 * exfiltration endpoint that the canary subsystem will POST entry titles1692 * to (MV-6 attack scenario). Invalid → the canary is dropped, not the1693 * entry. FIRE_CANARY_PING re-validates with the full SSRF guard at fire1694 * time (isAllowedCanaryWebhookUrl), so this import-time check is the1695 * outer of two gates.1696 */1697function sanitizeCanary(c: unknown): Canary | undefined {1698  if (!c || typeof c !== "object") return undefined;1699  const r = c as Record<string, unknown>;1700  if (typeof r.webhookUrl !== "string") return undefined;1701  let url: URL;1702  try {1703    url = new URL(r.webhookUrl);1704  } catch {1705    return undefined;1706  }1707  if (url.protocol !== "https:") return undefined;1708  return {1709    webhookUrl: url.toString(),1710    label: typeof r.label === "string" ? r.label : undefined,1711    lastPinged: typeof r.lastPinged === "number" ? r.lastPinged : undefined,1712    pingCount: typeof r.pingCount === "number" ? r.pingCount : undefined,1713  };1714}17151716/**1717 * MV-6 / PCH-01-016: tags from imported / batch-added entries are untrusted.1718 * Keep only non-empty strings, cap each at 256 chars, and cap the count at 2551719 * (the same per-entry ceiling the export codec enforces, peachPayload.ts).1720 * Non-array or all-invalid input collapses to undefined.1721 */1722function sanitizeTags(tags: unknown): string[] | undefined {1723  if (!Array.isArray(tags)) return undefined;1724  const out: string[] = [];1725  for (const t of tags) {1726    if (typeof t !== "string") continue;1727    const trimmed = t.trim();1728    if (!trimmed) continue;1729    out.push(trimmed.slice(0, 256));1730    if (out.length >= 255) break;1731  }1732  return out.length > 0 ? out : undefined;1733}17341735// Soft-deletes every non-deleted entry in one call. The literal string1736// argument is a tripwire against accidental calls — any caller that would1737// wipe the vault has to consciously type "REPLACE_ALL". Do not relax this1738// to a boolean.1739export async function clearVaultEntries(1740  confirmation: "REPLACE_ALL"1741): Promise<void> {1742  if (confirmation !== "REPLACE_ALL") {1743    throw new Error("clearVaultEntries requires explicit confirmation");1744  }1745  if (!derivedKey) throw new Error("vault_locked");1746  for (const entry of entries) {1747    if (!entry.deletedAt) {1748      const stamp = monotonicEntryTimestamp(entry.modifiedAt);1749      entry.deletedAt = stamp;1750      entry.modifiedAt = stamp;1751    }1752  }1753  await persistEntries();1754}17551756export async function addEntry(entry: EntryDraft): Promise<VaultEntry> {1757  if (!derivedKey) throw new Error("vault_locked");17581759  const sanitized = sanitizeEntryDraft(entry);1760  const now = Date.now();1761  const stamp = monotonicEntryTimestamp();1762  const nextEntry: VaultEntry = {1763    ...sanitized,1764    id: crypto.randomUUID(),1765    createdAt: now,1766    modifiedAt: stamp,1767    // Stamp passwordChangedAt at creation so age tracking is accurate from1768    // day one. Non-login entries carry the same stamp harmlessly.1769    passwordChangedAt: sanitized.password ? stamp : undefined,1770  };17711772  // Clone-and-replace so a failed persist rolls back the in-memory state1773  // (COR-008). Without this, a quota-full or storage error leaves the1774  // module-level `entries` ahead of what's actually on disk.1775  const rollback = entries;1776  entries = [...entries, nextEntry];1777  try {1778    await persistEntries();1779  } catch (err) {1780    entries = rollback;1781    throw err;1782  }17831784  // Fire-and-forget breach check (only for login entries if breach monitoring is enabled or not set)1785  if (settings.breachMonitoring !== false && nextEntry.type === "login" && nextEntry.password) {1786    checkBreach(nextEntry.password).then(async (count) => {1787      if (count > 0) {1788        const idx = entries.findIndex((e) => e.id === nextEntry.id);1789        if (idx !== -1) {1790          entries[idx] = { ...entries[idx], breach: count, modifiedAt: monotonicEntryTimestamp(entries[idx].modifiedAt) };1791          await persistEntries();1792        }1793      }1794    });1795  }17961797  fireSyncTrigger("vault_change");17981799  return nextEntry;1800}18011802/**1803 * Run the passkey-merge pass on the live in-memory entries and persist if1804 * anything changed. Called immediately after any passkey registration so that1805 * a same-domain TOTP-only entry already in the vault gets consolidated right1806 * away instead of waiting until the next lock/unlock cycle.1807 */1808export async function consolidatePasskeyEntries(): Promise<void> {1809  if (!derivedKey) return;1810  const { merged } = mergePasskeyOnlyIntoLogins(entries);1811  if (merged) {1812    await persistEntries();1813  }1814}18151816export async function updateEntry(1817  id: string,1818  patch: Partial<VaultEntry>1819): Promise<void> {1820  if (!derivedKey) throw new Error("vault_locked");18211822  const idx = entries.findIndex((entry) => entry.id === id);1823  if (idx === -1 || entries[idx].deletedAt) {1824    throw new Error("entry_not_found");1825  }18261827  const current = entries[idx];1828  const sanitizedPatch = sanitizeEntryPatch(patch);1829  // Refresh passwordChangedAt only when the password value actually changes —1830  // a metadata-only update (title, tags, etc.) must not reset the age clock1831  // that drives the Pro health rotation recommendation.1832  const passwordChanged =1833    "password" in patch &&1834    typeof sanitizedPatch.password === "string" &&1835    sanitizedPatch.password !== current.password;1836  const stamp = monotonicEntryTimestamp(current.modifiedAt);1837  // When the password actually changes, push the outgoing one onto the history1838  // (newest first) so the user can reveal/copy/restore it later. We record the1839  // age it carried (passwordChangedAt, falling back to createdAt) rather than1840  // `now`, so the list reflects when each password was last in effect. Only1841  // capture a non-empty prior value — clearing a password leaves no secret to1842  // keep, and we skip an immediate exact duplicate (e.g. restore-then-restore)1843  // to avoid noise.1844  const priorPassword = current.password;1845  const shouldCapture =1846    passwordChanged &&1847    typeof priorPassword === "string" &&1848    priorPassword.length > 0 &&1849    current.passwordHistory?.[0]?.password !== priorPassword;1850  const nextHistory = shouldCapture1851    ? [1852        { password: priorPassword!, changedAt: current.passwordChangedAt ?? current.createdAt },1853        ...(current.passwordHistory ?? []),1854      ].slice(0, PASSWORD_HISTORY_MAX)1855    : current.passwordHistory;1856  entries[idx] = {1857    ...current,1858    ...sanitizedPatch,1859    modifiedAt: stamp,1860    ...(passwordChanged ? { passwordChangedAt: stamp, passwordHistory: nextHistory } : {}),1861  };1862186318641865  // Card entries must retain their card number1866  if (entries[idx].type === "card" && !entries[idx].cardNumber) {1867    entries[idx] = current;1868    throw new Error("card number is required for card entries");1869  }18701871  // Roll the in-memory entry back to its pre-edit state if the disk write1872  // fails (e.g. storage quota). Without this the module-level `entries` array1873  // would hold an edit that never landed on disk, diverging from the vault1874  // until the next unlock silently reverts it (SEV-3-A). Mirrors addEntry.1875  try {1876    await persistEntries();1877  } catch (err) {1878    entries[idx] = current;1879    throw err;1880  }18811882  // Breach check when password changes: clear immediately so old breach doesn't linger,1883  // then async-check the new password and only re-set if it's also found.1884  if ("password" in patch && settings.breachMonitoring !== false && entries[idx].type === "login") {1885    entries[idx] = { ...entries[idx], breach: undefined };1886    await persistEntries();1887    const password = entries[idx].password;1888    if (password) {1889      checkBreach(password).then(async (count) => {1890        const i = entries.findIndex((e) => e.id === id);1891        if (i !== -1 && count > 0) {1892          entries[i] = { ...entries[i], breach: count, modifiedAt: monotonicEntryTimestamp(entries[i].modifiedAt) };1893          await persistEntries();1894        }1895      });1896    }1897  }18981899  fireSyncTrigger("vault_change");1900}19011902export async function deleteEntry(id: string): Promise<void> {1903  if (!derivedKey) throw new Error("vault_locked");19041905  const entry = entries.find((item) => item.id === id);1906  if (!entry || entry.deletedAt) {1907    throw new Error("entry_not_found");1908  }19091910  // Tombstone timestamp must be strictly greater than the entry's last1911  // modification so the deletion wins LWW against any in-flight edit, even1912  // under clock skew (SEV-2-E).1913  const previousModifiedAt = entry.modifiedAt;1914  const stamp = monotonicEntryTimestamp(entry.modifiedAt);1915  entry.deletedAt = stamp;1916  entry.modifiedAt = stamp;1917  // Roll the tombstone back if the disk write fails, so a quota error doesn't1918  // leave the entry deleted in memory but alive on disk (SEV-3-A).1919  try {1920    await persistEntries();1921  } catch (err) {1922    delete entry.deletedAt;1923    entry.modifiedAt = previousModifiedAt;1924    throw err;1925  }1926  fireSyncTrigger("vault_change");1927}19281929export function getDeletedEntries(): VaultEntry[] {1930  if (!derivedKey) return [];1931  return entries.filter((e) => !!e.deletedAt);1932}19331934/**1935 * Returns the vault's monotonic version — the `peach_last_modified_at` value1936 * stamped on every user-initiated write. Remote (sync) writes do NOT advance1937 * this version so a sync round never cascades into a second outbound sync.1938 */1939export async function getVaultVersion(): Promise<number> {1940  try {1941    const raw = await chrome.storage.local.get("peach_last_modified_at");1942    const v = (raw as Record<string, unknown>).peach_last_modified_at;1943    if (typeof v === "number") return v;1944    if (typeof v === "string" && v !== "") {1945      const n = Number.parseInt(v, 10);1946      return Number.isFinite(n) ? n : 0;1947    }1948    return 0;1949  } catch {1950    return 0;1951  }1952}19531954/**1955 * Internal write variants — bypass `fireSyncTrigger` so the sync layer can1956 * apply entries without cascading into another outbound sync round.1957 *1958 * INVARIANT: callers must not pass entries whose IDs collide with existing1959 * active (non-deleted) entries unless the intent is an explicit overwrite.1960 * These functions do not validate field-level constraints (password required,1961 * card number required, etc.) — they are trust-the-caller paths for sync and1962 * test harness use only.1963 */1964export async function addEntryDirect(entry: VaultEntry): Promise<void> {1965  if (!derivedKey) throw new Error("vault_locked");1966  entries = [...entries, entry];1967  await persistEntries("sync_received");1968}19691970export async function updateEntryDirect(patch: VaultEntry): Promise<void> {1971  if (!derivedKey) throw new Error("vault_locked");1972  const idx = entries.findIndex((e) => e.id === patch.id);1973  if (idx === -1) throw new Error("entry_not_found");1974  entries[idx] = { ...entries[idx], ...patch };1975  await persistEntries("sync_received");1976}19771978export async function deleteEntryDirect(id: string, deletedAt: number): Promise<void> {1979  if (!derivedKey) throw new Error("vault_locked");1980  const idx = entries.findIndex((e) => e.id === id);1981  if (idx === -1) throw new Error("entry_not_found");1982  entries[idx] = { ...entries[idx], deletedAt, modifiedAt: deletedAt };1983  await persistEntries("sync_received");1984}19851986/**1987 * Apply a batch of remote entries + tombstones from a chirp-sync peer.1988 * Last-write-wins by `modifiedAt`; a tombstone supersedes a live entry when1989 * `deletedAt > modifiedAt` on the local side. Mutates `entries` directly and1990 * persists with reason `sync_received` — does NOT fire the sync dispatcher, so1991 * applying a sync round cannot cascade into another outbound sync.1992 *1993 * `applied` counts entries that actually changed local state (new add,1994 * overwrite-by-newer, or new tombstone). `received` is the raw payload size1995 * the caller can surface in the sync result UI.1996 */1997export async function applyMergedRemoteEntries(1998  remoteEntries: VaultEntry[],1999  remoteTombstones: Record<string, number>,2000  context: { deviceName?: string } = {},2001): Promise<{ received: number; applied: number; conflicts: number }> {2002  // DESIGN: last-write-wins is applied PER-ENTRY, not per-field. If two devices2003  // concurrently edit different fields of the same entry, the one with the2004  // newer modifiedAt wins the whole entry and the other device's field edits2005  // are dropped silently. This is the deliberate trade-off for a simple,2006  // conflict-free merge — per-field CRDT merge is out of scope. Guidance for2007  // users: edit a given entry on one device at a time.2008  if (!derivedKey) throw new Error("vault_locked");20092010  const received = remoteEntries.length + Object.keys(remoteTombstones).length;2011  let applied = 0;2012  let conflicts = 0;20132014  const byId = new Map<string, VaultEntry>();2015  for (const e of entries) byId.set(e.id, e);20162017  const alerts: SyncAlert[] = [];2018  const deviceName = context.deviceName ?? "paired device";2019  const mergeNow = Date.now();20202021  for (const remote of remoteEntries) {2022    const sanitized = sanitizeRestoredEntry(remote);2023    if (!sanitized) continue;2024    // HV-1a: a far-future remote modifiedAt must not permanently pin a2025    // malicious value against honest local edits. Clamp BEFORE any2026    // comparison against local timestamps.2027    sanitized.modifiedAt = clampSyncTimestamp(sanitized.modifiedAt, mergeNow);2028    if (typeof sanitized.deletedAt === "number") {2029      sanitized.deletedAt = clampSyncTimestamp(sanitized.deletedAt, mergeNow);2030    }2031    const tombstoneTs = remoteTombstones[sanitized.id];2032    if (tombstoneTs !== undefined && clampSyncTimestamp(tombstoneTs, mergeNow) >= sanitized.modifiedAt) {2033      continue;2034    }2035    const local = byId.get(sanitized.id);2036    if (!local) {2037      entries.push(sanitized);2038      byId.set(sanitized.id, sanitized);2039      applied += 1;2040    } else if (sanitized.modifiedAt > local.modifiedAt) {2041      const idx = entries.findIndex((e) => e.id === local.id);2042      if (idx !== -1) {2043        // HV-1b: surface remote password REPLACEMENTS (existing id, password2044        // differs) so a compromised peer can't silently rewrite credentials.2045        if (2046          !local.deletedAt &&2047          typeof sanitized.password === "string" &&2048          typeof local.password === "string" &&2049          sanitized.password !== local.password2050        ) {2051          alerts.push({2052            type: "REMOTE_PASSWORD_CHANGE",2053            entryId: sanitized.id,2054            entryTitle: sanitized.title,2055            deviceName,2056            timestamp: mergeNow,2057          });2058        }2059        entries[idx] = sanitized;2060        byId.set(sanitized.id, sanitized);2061        applied += 1;2062        if (!local.deletedAt) conflicts += 1;2063      }2064    }2065  }20662067  for (const [id, rawDeletedAt] of Object.entries(remoteTombstones)) {2068    if (typeof rawDeletedAt !== "number" || !Number.isFinite(rawDeletedAt)) continue;2069    // HV-1a/1c: clamp the tombstone timestamp before every comparison.2070    const deletedAt = clampSyncTimestamp(rawDeletedAt, mergeNow);2071    const local = byId.get(id);2072    if (!local) continue;2073    if (local.deletedAt && local.deletedAt >= deletedAt) continue;2074    if (deletedAt < local.modifiedAt) continue;2075    const idx = entries.findIndex((e) => e.id === id);2076    if (idx !== -1) {2077      // HV-1b: surface remote deletes of live local entries.2078      if (!local.deletedAt) {2079        alerts.push({2080          type: "REMOTE_DELETE",2081          entryId: id,2082          entryTitle: local.title,2083          deviceName,2084          timestamp: mergeNow,2085        });2086      }2087      entries[idx] = { ...local, deletedAt, modifiedAt: deletedAt };2088      applied += 1;2089    }2090  }20912092  if (applied > 0) {2093    await persistEntries("sync_received");2094    buildDomainIndexSync(entries);2095  }2096  // Queued after the merge committed — an alert must describe a mutation2097  // that actually landed. recordSyncAlerts never throws.2098  await recordSyncAlerts(alerts);2099  return { received, applied, conflicts };2100}21012102export async function restoreEntry(id: string): Promise<void> {2103  if (!derivedKey) throw new Error("vault_locked");2104  const entry = entries.find((e) => e.id === id && e.deletedAt);2105  if (!entry) throw new Error("deleted entry not found");2106  delete entry.deletedAt;2107  entry.modifiedAt = monotonicEntryTimestamp(entry.modifiedAt);2108  await persistEntries();2109  buildDomainIndexSync(entries);2110}21112112export async function purgeEntry(id: string): Promise<void> {2113  if (!derivedKey) throw new Error("vault_locked");2114  const idx = entries.findIndex((e) => e.id === id && e.deletedAt);2115  if (idx === -1) throw new Error("deleted entry not found");2116  entries.splice(idx, 1);2117  await persistEntries();2118}21192120/** Permanently delete entries whose deletedAt is older than retentionMs. 0 = keep forever. */2121export async function purgeExpiredTrash(retentionMs: number): Promise<number> {2122  if (!derivedKey || retentionMs === 0) return 0;2123  const cutoff = Date.now() - retentionMs;2124  const before = entries.length;2125  entries = entries.filter((e) => !(e.deletedAt && e.deletedAt < cutoff));2126  const purged = before - entries.length;2127  if (purged > 0) await persistEntries();2128  return purged;2129}21302131export function queryDomain(domain: string): VaultEntry[] {2132  return domainIndex.get(domain) ?? [];2133}21342135export function getEntryById(id: string): VaultEntry | undefined {2136  return entries.find((entry) => entry.id === id && !entry.deletedAt);2137}21382139async function persistSlotA(2140  reason: VaultSnapshot["reason"] = "local_save",2141): Promise<StoredVault> {2142  if (!derivedKey) throw new Error("vault_locked");21432144  const slotA = await loadSlot(SLOT_A);2145  if (!slotA) throw new Error("missing slot A");21462147  // Snapshot the about-to-be-overwritten state BEFORE we mutate disk. Failures2148  // are swallowed by takeVaultSnapshot — history must never block a save.2149  await takeVaultSnapshot(reason);21502151  const plaintext = encodePayload(entries, duressConfigured, duressConfigured ? DURESS_SLOT_SIZE : 0);2152  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext, "slot_a_v1");21532154  // Preserve existing recovery escrow fields.2155  const nextSlotA: StoredVault = {2156    ...slotA,2157    nonce,2158    ciphertext,2159    tag,2160    // recovery_salt and recovery_wrapped_root_key are already set from loadSlot2161  };2162  await writeSlot(SLOT_A, nextSlotA);21632164  // Slot A (the real vault) is now committed. Wrap every later write so a2165  // mid-persist failure (e.g. a storage error after slot A landed) restores the2166  // prior slot A before rethrowing. Without this, a "failed" save that already2167  // wrote slot A leaves disk ahead of the in-memory rollback in addEntry/2168  // updateEntry/deleteEntry — the entry stays silently on disk and resurfaces2169  // as a duplicate on the next unlock (observed under storage-quota pressure).2170  try {2171    // When duress is not configured, keep slot B size-matched with slot A by2172    // regenerating its noise. When duress IS configured, leave slot B alone —2173    // we don't have the decoy key to re-encrypt it. This is a known trade-off.2174    if (!duressConfigured) {2175      await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));2176    }2177    // Only advance the vault version for locally-originated writes. Entries2178    // applied from a sync peer ("sync_received") must NOT bump the counter —2179    // otherwise applying a remote round makes us look newer than the peer and2180    // triggers a redundant outbound sync, defeating the no-op optimisation2181    // and risking a ping-pong between two devices (SEV-2-C).2182    if (reason !== "sync_received") {2183      await stampLocalVaultMutation();2184    }2185  } catch (err) {2186    // Best-effort restore — the prior slot A is smaller-or-equal, so this write2187    // fits even when the failure was a quota overflow.2188    await writeSlot(SLOT_A, slotA).catch(() => {});2189    throw err;2190  }21912192  return nextSlotA;2193}21942195async function persistSlotB(): Promise<void> {2196  if (!derivedKey) throw new Error("vault_locked");21972198  const slotB = await loadSlot(SLOT_B);2199  if (!slotB) throw new Error("missing slot B");22002201  // Decoy plaintext always reports duressConfigured=false — the decoy user2202  // sees a clean slate. persistSlotB only runs in decoy mode, which only2203  // exists once duress is configured, so pad to the fixed duress ceiling to2204  // keep slot B's length equal to slot A's (audit MEDIUM — deniability).2205  const plaintext = encodePayload(entries, false, DURESS_SLOT_SIZE);2206  const { nonce, ciphertext, tag } = await encrypt(derivedKey, plaintext, "slot_b_v1");22072208  await writeSlot(SLOT_B, { ...slotB, nonce, ciphertext, tag });2209  // Symmetric with persistSlotA: if the version stamp fails after the decoy2210  // slot committed, restore the prior slot B so a failed decoy-mode save can't2211  // leave disk ahead of the in-memory rollback.2212  try {2213    await stampLocalVaultMutation();2214  } catch (err) {2215    await writeSlot(SLOT_B, slotB).catch(() => {});2216    throw err;2217  }2218}22192220async function persistEntries(2221  reason: VaultSnapshot["reason"] = "local_save",2222): Promise<void> {2223  if (!derivedKey) throw new Error("vault_locked");22242225  if (vaultMode === "decoy") {2226    await persistSlotB();2227  } else {2228    await persistSlotA(reason);2229  }2230  buildDomainIndexSync(entries);2231}22322233function cleanAdditionalUrls(values: string[] | undefined): string[] | undefined {2234  if (!Array.isArray(values)) return undefined;2235  const seen = new Set<string>();2236  const cleaned: string[] = [];2237  for (const value of values) {2238    const trimmed = typeof value === "string" ? value.trim() : "";2239    if (!trimmed) continue;2240    const key = trimmed.toLowerCase();2241    if (seen.has(key)) continue;2242    seen.add(key);2243    cleaned.push(trimmed);2244  }2245  return cleaned.length > 0 ? cleaned : undefined;2246}22472248function sanitizeEntryDraft(entry: EntryDraft): EntryDraft {2249  const title = entry.title.trim();2250  // Accept legacy `type:"passkey"` drafts but rewrite to "login" — the new2251  // model is "any entry, optionally with a passkey sub-object." Imports and2252  // any in-flight callers from before v2.11.0 keep working without throwing.2253  let type: VaultEntry["type"] = entry.type ?? "login";2254  if (type === "passkey") type = "login";22552256  if (!title) throw new Error("title is required");22572258  if (entry.passkey) {2259    const p = entry.passkey;2260    if (2261      !p.rpId ||2262      !p.credentialId ||2263      !p.publicKeyCose ||2264      !p.userHandle ||2265      !p.userName ||2266      (p.algorithm !== -7 && p.algorithm !== -8)2267    ) {2268      throw new Error("invalid passkey credential");2269    }2270  }2271  if (type === "card") {2272    if (!entry.cardholderName?.trim() || !entry.cardNumber?.trim()) {2273      throw new Error("cardholder name and card number are required");2274    }2275  }22762277  return {2278    type,2279    title,2280    username: type === "login" ? entry.username?.trim() ?? "" : undefined,2281    email: type === "login" ? entry.email?.trim() || undefined : undefined,2282    password: type === "login" ? entry.password : undefined,2283    url: type === "login" ? entry.url?.trim() ?? "" : undefined,2284    additionalUrls: type === "login" ? cleanAdditionalUrls(entry.additionalUrls) : undefined,2285    totp: type === "login" ? entry.totp?.trim() || undefined : undefined,2286    notes: entry.notes?.trim() || undefined,2287    breach: type === "login" ? entry.breach : undefined,2288    // PCH-01-016: the batch-add path was bypassing the MV-6 canary/tags2289    // whitelist applied on the RESTORE_ENTRIES path. Run both sanitizers here.2290    canary: type === "login" ? sanitizeCanary(entry.canary) : undefined,2291    tags: sanitizeTags(entry.tags),2292    passkey: entry.passkey,2293    cardholderName: type === "card" ? entry.cardholderName?.trim() : undefined,2294    cardNumber: type === "card" ? entry.cardNumber?.trim() : undefined,2295    expMonth: type === "card" ? entry.expMonth?.trim() : undefined,2296    expYear: type === "card" ? entry.expYear?.trim() : undefined,2297    cvc: type === "card" ? entry.cvc?.trim() : undefined,2298    cardNickname: type === "card" ? entry.cardNickname?.trim() || undefined : undefined,2299    network: type === "card" ? entry.network : undefined,2300    last4: type === "card" ? entry.last4?.trim() : undefined,2301  };2302}23032304function sanitizeEntryPatch(patch: Partial<VaultEntry>): Partial<VaultEntry> {2305  const nextPatch: Partial<VaultEntry> = {};23062307  if ("type" in patch) {2308    nextPatch.type = patch.type;2309  }23102311  if ("title" in patch) {2312    const title = patch.title?.trim() ?? "";2313    if (!title) throw new Error("title is required");2314    nextPatch.title = title;2315  }23162317  if ("url" in patch) {2318    nextPatch.url = patch.url?.trim() ?? "";2319  }23202321  if ("additionalUrls" in patch) {2322    const incoming = Array.isArray(patch.additionalUrls) ? patch.additionalUrls : [];2323    const seen = new Set<string>();2324    const cleaned: string[] = [];2325    for (const value of incoming) {2326      const trimmed = typeof value === "string" ? value.trim() : "";2327      if (!trimmed) continue;2328      const key = trimmed.toLowerCase();2329      if (seen.has(key)) continue;2330      seen.add(key);2331      cleaned.push(trimmed);2332    }2333    nextPatch.additionalUrls = cleaned.length > 0 ? cleaned : undefined;2334  }23352336  if ("username" in patch) {2337    nextPatch.username = patch.username?.trim() ?? "";2338  }23392340  if ("email" in patch) {2341    nextPatch.email = patch.email?.trim() || undefined;2342  }23432344  if ("password" in patch) {2345    const password = patch.password ?? "";2346    // Allow clearing a password (e.g. on passkey-only entries)2347    nextPatch.password = password || undefined;2348  }23492350  if ("totp" in patch) {2351    nextPatch.totp = patch.totp?.trim() || undefined;2352  }23532354  if ("notes" in patch) {2355    nextPatch.notes = patch.notes?.trim() || undefined;2356  }23572358  if ("breach" in patch) {2359    nextPatch.breach = patch.breach;2360  }23612362  if ("canary" in patch) {2363    nextPatch.canary = patch.canary;2364  }23652366  if ("passkey" in patch) {2367    // Passkey sub-object replaces all the legacy flat passkey fields. A2368    // patch with `passkey: undefined` removes the credential from the entry2369    // (used by "remove passkey" without deleting the whole entry).2370    nextPatch.passkey = patch.passkey;2371  }23722373  if ("tags" in patch) {2374    nextPatch.tags = patch.tags;2375  }23762377  if ("cardholderName" in patch) {2378    nextPatch.cardholderName = patch.cardholderName?.trim() || undefined;2379  }2380  if ("cardNumber" in patch) {2381    const n = patch.cardNumber ?? "";2382    nextPatch.cardNumber = n || undefined;2383  }2384  if ("expMonth" in patch) {2385    nextPatch.expMonth = patch.expMonth?.trim() || undefined;2386  }2387  if ("expYear" in patch) {2388    nextPatch.expYear = patch.expYear?.trim() || undefined;2389  }2390  if ("cvc" in patch) {2391    nextPatch.cvc = patch.cvc?.trim() || undefined;2392  }2393  if ("cardNickname" in patch) {2394    nextPatch.cardNickname = patch.cardNickname?.trim() || undefined;2395  }2396  if ("network" in patch) {2397    nextPatch.network = patch.network;2398  }2399  if ("last4" in patch) {2400    nextPatch.last4 = patch.last4?.trim() || undefined;2401  }24022403  return nextPatch;2404}24052406function normalizeEntry(entry: VaultEntry | LegacyVaultEntry): VaultEntry {2407  const createdAt = "createdAt" in entry ? entry.createdAt : entry.created;2408  const modifiedAt =2409    "modifiedAt" in entry ? entry.modifiedAt : entry.updated;24102411  const base: VaultEntry = {2412    id: entry.id,2413    type: "type" in entry ? entry.type : "login",2414    title: entry.title,2415    username: entry.username,2416    email: "email" in entry ? entry.email : undefined,2417    password: entry.password,2418    url: entry.url,2419    additionalUrls:2420      "additionalUrls" in entry && Array.isArray(entry.additionalUrls)2421        ? entry.additionalUrls2422        : undefined,2423    totp: entry.totp,2424    notes: entry.notes,2425    breach: entry.breach,2426    // Preserve the password-age clock across lock/unlock. Without this the2427    // field is dropped on every decode and the advanced-health "aged password"2428    // signal silently resets to createdAt on each unlock (SEV-2-A).2429    passwordChangedAt:2430      "passwordChangedAt" in entry ? entry.passwordChangedAt : undefined,2431    // Preserve past-password history across lock/unlock. normalizeEntry is a2432    // field whitelist, so an unlisted field is silently dropped on every decode2433    // (same class as the passwordChangedAt SEV-2-A above).2434    passwordHistory:2435      "passwordHistory" in entry && Array.isArray(entry.passwordHistory)2436        ? entry.passwordHistory2437        : undefined,2438    canary: "canary" in entry ? entry.canary : undefined,2439    passkey: "passkey" in entry ? entry.passkey : undefined,2440    createdAt,2441    modifiedAt,2442    deletedAt: entry.deletedAt,2443    tags: "tags" in entry ? entry.tags : undefined,2444    cardholderName: "cardholderName" in entry ? entry.cardholderName : undefined,2445    cardNumber: "cardNumber" in entry ? entry.cardNumber : undefined,2446    expMonth: "expMonth" in entry ? entry.expMonth : undefined,2447    expYear: "expYear" in entry ? entry.expYear : undefined,2448    cvc: "cvc" in entry ? entry.cvc : undefined,2449    cardNickname: "cardNickname" in entry ? entry.cardNickname : undefined,2450    network: "network" in entry ? entry.network : undefined,2451    last4: "last4" in entry ? entry.last4 : undefined,2452  };24532454  return migrateLegacyPasskey(base, entry);2455}24562457/**2458 * v2.11.0 read migration. Pre-v2.11.0 passkey entries used `type:"passkey"`2459 * with flat top-level fields (rpId, credentialId, etc.). This converts them2460 * to the new shape — `type:"login"` with a `passkey` sub-object — so all2461 * downstream consumers can rely on the new model. The result is rewritten2462 * to disk on next vault save (hasLegacyShape detects the un-migrated case).2463 */2464function migrateLegacyPasskey(2465  base: VaultEntry,2466  raw: VaultEntry | LegacyVaultEntry2467): VaultEntry {2468  if (base.passkey) return base;2469  const flat = raw as Partial<VaultEntry>;2470  const isPasskeyType = base.type === "passkey";2471  const hasFlatPasskeyFields =2472    typeof flat.rpId === "string" &&2473    typeof flat.credentialId === "string" &&2474    typeof flat.publicKeyCose === "string" &&2475    typeof flat.userHandle === "string" &&2476    typeof flat.userName === "string";2477  if (!isPasskeyType && !hasFlatPasskeyFields) return base;2478  if (!hasFlatPasskeyFields) return base;24792480  const algorithm: -7 | -8 = flat.algorithm === -8 ? -8 : -7;2481  base.passkey = {2482    rpId: flat.rpId!,2483    rpName: flat.rpName ?? flat.rpId!,2484    credentialId: flat.credentialId!,2485    publicKeyCose: flat.publicKeyCose!,2486    privateKeyJwk: flat.privateKeyJwk,2487    userHandle: flat.userHandle!,2488    userName: flat.userName!,2489    userDisplayName: flat.userDisplayName ?? flat.userName!,2490    algorithm,2491    signCount: flat.signCount ?? 0,2492    aaguid: flat.aaguid ?? "",2493    backupEligible: flat.backupEligible === true,2494    backupState: flat.backupState === true,2495    lastUsedAt: flat.lastUsedAt,2496    browser: flat.browser,2497    transports: flat.transports,2498    createdAt: base.createdAt,2499  };2500  // Promote to "login" so the unified Entry view renders. Username gets2501  // populated from the passkey's userName so the entry is searchable in the2502  // existing autofill flow without a separate code path.2503  if (isPasskeyType) base.type = "login";2504  if (!base.username) base.username = flat.userName;2505  if (!base.url) base.url = `https://${flat.rpId}`;2506  return base;2507}250825092510// ---------------------------------------------------------------------------2511// Deduplication2512// ---------------------------------------------------------------------------25132514function entryDupeKey(e: VaultEntry): string {2515  if (e.type === "note") return `note|${e.title.toLowerCase()}`;2516  // Passkey-bearing entries dedupe by the credential's RP + user. Two logins2517  // on different sites that happen to share a username are still distinct;2518  // two passkey credentials for the same RP+user are duplicates regardless2519  // of any login fields they may also carry.2520  if (e.passkey) {2521    return `passkey|${e.passkey.rpId.toLowerCase()}|${e.passkey.userName.toLowerCase()}`;2522  }2523  return `login|${e.title.toLowerCase()}|${(e.url ?? "").toLowerCase()}|${(e.username ?? "").toLowerCase()}`;2524}25252526/** Returns the number of duplicate entries (those that would be removed). */2527export function countDuplicates(): number {2528  const best = new Map<string, VaultEntry>();2529  let dupeCount = 0;2530  for (const e of entries.filter((e) => !e.deletedAt)) {2531    const key = entryDupeKey(e);2532    const existing = best.get(key);2533    if (!existing) {2534      best.set(key, e);2535    } else {2536      dupeCount++;2537      // Keep the more recently modified one in the map2538      if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {2539        best.set(key, e);2540      }2541    }2542  }2543  return dupeCount;2544}25452546/**2547 * Soft-deletes duplicate entries, keeping the most recently modified copy of2548 * each group. Returns the number of entries moved to trash.2549 */2550export async function deduplicateEntries(): Promise<number> {2551  if (!derivedKey) throw new Error("vault_locked");2552  // Dedup reshapes slot B permanently under the decoy key, which is2553  // forensically distinguishable from a freshly-seeded decoy vault. Refuse.2554  if (vaultMode === "decoy") {2555    throw new Error("deduplicate disabled in decoy mode");2556  }25572558  const best = new Map<string, VaultEntry>();2559  for (const e of entries.filter((en) => !en.deletedAt)) {2560    const key = entryDupeKey(e);2561    const existing = best.get(key);2562    if (!existing) {2563      best.set(key, e);2564    } else if ((e.modifiedAt ?? e.createdAt ?? 0) > (existing.modifiedAt ?? existing.createdAt ?? 0)) {2565      best.set(key, e);2566    }2567  }25682569  const keepIds = new Set(Array.from(best.values()).map((e) => e.id));2570  let removed = 0;2571  entries = entries.map((e) => {2572    if (!e.deletedAt && !keepIds.has(e.id)) {2573      removed++;2574      return { ...e, deletedAt: monotonicEntryTimestamp(e.modifiedAt) };2575    }2576    return e;2577  });25782579  if (removed > 0) await persistEntries();2580  return removed;2581}25822583// ---------------------------------------------------------------------------2584// Vault history — snapshot BEFORE every vault write so the saved blob is2585// what is about to be lost, not what was just written. Capped at 5. The2586// stored blobs are AES-256-GCM ciphertext under the master password, so2587// history adds zero security surface.2588// ---------------------------------------------------------------------------25892590const HISTORY_KEY = "peach_vault_history";2591const HISTORY_MAX = 5;25922593/**2594 * Snapshot the CURRENT on-disk vault state into history. Call this BEFORE2595 * overwriting peach_vault_a so the saved blob captures the about-to-be-lost2596 * state. Identical-to-most-recent blobs are skipped to avoid filling history2597 * with no-op writes.2598 */2599export async function takeVaultSnapshot(2600  reason: VaultSnapshot["reason"],2601): Promise<void> {2602  try {2603    const raw = await chrome.storage.local.get([SLOT_A, SLOT_B, HISTORY_KEY]);2604    const slotA = (raw as Record<string, StoredVault | undefined>)[SLOT_A];2605    const slotB = (raw as Record<string, StoredVault | undefined>)[SLOT_B];26062607    // Nothing to snapshot if the real slot doesn't exist yet (fresh install,2608    // pre-createVault). createVault skips snapshots intentionally — there is2609    // no prior state to preserve.2610    if (!slotA) return;26112612    const vault_a = JSON.stringify(slotA);2613    const vault_b = JSON.stringify(slotB ?? slotA);26142615    // Best-effort entry count: only meaningful when the in-memory state is2616    // unlocked AND in real mode. Decoy mode would leak the decoy entry2617    // count; locked mode has no entries to count. Both fall through to null.2618    let entryCount: number | null = null;2619    if (derivedKey && vaultMode === "real") {2620      try {2621        entryCount = entries.filter((e) => !e.deletedAt).length;2622      } catch {2623        entryCount = null;2624      }2625    }26262627    const existingRaw = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];2628    const history: VaultHistory = existingRaw && Array.isArray(existingRaw.snapshots)2629      ? existingRaw2630      : { snapshots: [] };26312632    // Deduplication: if the most recent snapshot has byte-identical blobs,2633    // skip — saves the same vault N times in a row would otherwise burn the2634    // entire history window with no recovery value.2635    const latest = history.snapshots[0];2636    if (latest && latest.vault_a === vault_a && latest.vault_b === vault_b) {2637      return;2638    }26392640    const snapshot: VaultSnapshot = {2641      id: crypto.randomUUID(),2642      savedAt: Date.now(),2643      reason,2644      vault_a,2645      vault_b,2646      entryCount,2647    };26482649    history.snapshots = [snapshot, ...history.snapshots].slice(0, HISTORY_MAX);2650    await chrome.storage.local.set({ [HISTORY_KEY]: history });2651  } catch (err) {2652    // Non-fatal — history must never block a save. A failed snapshot just2653    // means the user loses one rung of the safety ladder for this write.2654    // M4/QUA-006: log so the failure is visible; if it looks like a quota2655    // problem, notify the popup so the user can prune history themselves.2656    console.warn('[Peach] takeVaultSnapshot failed:', err);2657    if (String(err).includes('QUOTA') || String(err).includes('quota')) {2658      try {2659        await chrome.runtime.sendMessage({ type: 'VAULT_STORAGE_QUOTA_WARNING' });2660      } catch {2661        // popup may be closed; surfaced via console.warn above2662      }2663    }2664  }2665}26662667/** Wipe all vault history. Called by Panic / DESTROY_VAULT. */2668export async function clearVaultHistory(): Promise<void> {2669  try {2670    await chrome.storage.local.set({ [HISTORY_KEY]: { snapshots: [] } satisfies VaultHistory });2671  } catch {2672    // Non-fatal — DESTROY_VAULT also runs storage.local.clear().2673  }2674}26752676/** Read all snapshots, newest first, for the Settings vault history view. */2677export async function getVaultHistory(): Promise<VaultSnapshot[]> {2678  const raw = await chrome.storage.local.get(HISTORY_KEY);2679  const history = (raw as Record<string, VaultHistory | undefined>)[HISTORY_KEY];2680  if (!history || !Array.isArray(history.snapshots)) return [];2681  return history.snapshots;2682}26832684/**2685 * Replace the on-disk vault with the chosen snapshot's blobs and lock the2686 * vault. The current state is itself snapshotted with reason 'restore' first2687 * so the restore is reversible. After this returns, the user must unlock2688 * with the password that matches the restored blob.2689 */2690export async function restoreVaultSnapshot(2691  snapshotId: string,2692): Promise<{ ok: true } | { ok: false; error: string }> {2693  const history = await getVaultHistory();2694  const snapshot = history.find((s) => s.id === snapshotId);2695  if (!snapshot) return { ok: false, error: "snapshot_not_found" };26962697  // Snapshot the about-to-be-replaced current state so the restore is2698  // reversible. takeVaultSnapshot is no-op when the most recent entry is2699  // byte-identical, so a restore-of-a-restore stays clean.2700  await takeVaultSnapshot("restore");27012702  let restoredA: StoredVault;2703  let restoredB: StoredVault;2704  try {2705    restoredA = JSON.parse(snapshot.vault_a) as StoredVault;2706    restoredB = JSON.parse(snapshot.vault_b) as StoredVault;2707  } catch {2708    return { ok: false, error: "snapshot_corrupt" };2709  }27102711  await writeSlot(SLOT_A, restoredA);2712  await writeSlot(SLOT_B, restoredB);2713  await stampLocalVaultMutation();27142715  // Lock so the user must prove they can decrypt the restored blob. If they2716  // restored a pre-password-change snapshot they discover it immediately at2717  // the lock screen rather than silently holding an inaccessible vault.2718  lock();27192720  return { ok: true };2721}27222723/**2724 * Rotate the vault's sync identity by generating a FRESH ROOT KEY and2725 * re-wrapping everything under it (MV-8). Because every sync credential —2726 * vault_id, the Noise XX static keypair, the Ed25519 manifest signing key,2727 * and the per-entry envelope keys — derives from the root key, rotation2728 * makes a lost/stolen device's retained key material useless. All paired2729 * devices must re-pair afterwards.2730 *2731 * CONSEQUENCE THE UI MUST SURFACE: the 12 recovery words also derive from2732 * the root key, so rotation INVALIDATES the old recovery phrase. The flow2733 * sets peach_recovery_pending so the popup re-presents the new words for2734 * confirmation, same as the migrateToRecovery rotate path.2735 */2736export async function rotateSyncIdentity(): Promise<{ ok: boolean; error?: string }> {2737  if (!derivedKey || vaultMode !== "real") {2738    return { ok: false, error: "vault_locked" };2739  }2740  const slotA = await loadSlot(SLOT_A);2741  if (!slotA) return { ok: false, error: "missing_slot_a" };27422743  // 1. Fresh root key + recovery escrow (new words).2744  const { rawRootKey, recoverySalt, wrappedRootKey, wrappedNonce, wrappedTag } =2745    await generateAndWrapRootKey();27462747  // 2. Re-wrap the new root key under the current password-derived key.2748  const newRootKey = unhex(rawRootKey);2749  const pwRoot = await wrapRootKeyForDerivedKey(derivedKey, newRootKey);27502751  // 3. Snapshot, then single atomic slot write — both escrow wrappings of2752  //    the new root key land together (same transactional rule as2753  //    changePassword). Entries are untouched: they are sealed under the2754  //    password-derived key, not the root key.2755  await takeVaultSnapshot("local_save");2756  const nextSlotA: StoredVault = {2757    ...slotA,2758    recovery_salt: recoverySalt,2759    recovery_wrapped_root_key: wrappedRootKey,2760    recovery_nonce: wrappedNonce,2761    recovery_tag: wrappedTag,2762    ...pwRoot,2763  };2764  await writeSlot(SLOT_A, nextSlotA);27652766  // 4. Swap in memory only after disk committed.2767  if (rootKey) zeroKey(rootKey);2768  rootKey = newRootKey;27692770  // 5. Old peers' identities derive from the dead root key — drop them all.2771  const { clearPairedDevices } = await import("./paired-devices");2772  await clearPairedDevices();27732774  // 6. Force re-presentation of the NEW recovery words on next popup view.2775  await setRecoveryConfirmPending(); // same flag the create-vault flow uses27762777  await stampLocalVaultMutation();2778  return { ok: true };2779}27802781/**2782 * Change the master password. Re-encrypts slot A with a fresh salt + new2783 * derived key. Slot B (duress or noise) is left untouched when duress is2784 * configured — it uses an independent key derived from the duress password —2785 * and regenerated as noise when duress is not configured.2786 */2787export async function changePassword(2788  currentPassword: string,2789  newPassword: string2790): Promise<boolean> {2791  if (!derivedKey || vaultMode !== "real") return false;27922793  // Verify current password before allowing any change2794  const valid = await verifyMainPassword(currentPassword);2795  if (!valid) return false;27962797  const slotA = await loadSlot(SLOT_A);2798  if (!slotA) return false;2799  if (!rootKey) return false;28002801  // Derive fresh key from new password + new random salt2802  const newSalt = generateSalt();2803  const newKey = await deriveKey(newPassword, newSalt);28042805  // Re-encrypt the in-memory entries with the new key2806  const plaintext = encodePayload(entries, duressConfigured, duressConfigured ? DURESS_SLOT_SIZE : 0);2807  const { nonce, ciphertext, tag } = await encrypt(newKey, plaintext, "slot_a_v1");28082809  // Pre-wrap root key under the new derived key. Recovery fields wrap rootKey2810  // under recovery words and remain untouched — recovery phrase keeps working2811  // across password changes (H4/COR-002).2812  const pwRoot = await wrapRootKeyForDerivedKey(newKey, rootKey);28132814  const finalSlotA: StoredVault = {2815    ...slotA,2816    salt: hex(newSalt),2817    nonce,2818    ciphertext,2819    tag,2820    ...pwRoot,2821  };28222823  // Snapshot the OLD-key blob before overwriting it. If the user later2824  // restores from this snapshot they will need the OLD password — the2825  // post-restore lock surfaces that immediately.2826  await takeVaultSnapshot("local_save");28272828  // Single atomic write: ciphertext, salt, and pw_root_* fields all land in2829  // one storage transaction. An SW kill before this point leaves the old2830  // slot intact; after this point everything tied to the new password is2831  // already on disk together (M6/COR-004).2832  await writeSlot(SLOT_A, finalSlotA);28332834  // Swap the in-memory key synchronously before any further await. If we2835  // threw between the Slot A write and this swap, the next persistEntries()2836  // would overwrite Slot A with old-key ciphertext, locking the user out.2837  zeroKey(derivedKey);2838  derivedKey = newKey;28392840  // PCH-01-008: the MV3 session escrow still holds the OLD key. Without this,2841  // the next service-worker restore decrypts the new-key Slot A with the stale2842  // key, fails tryDecryptSlotResult, and silently drops the user to a lock2843  // screen. Re-escrow the new key so restore survives a password change.2844  void saveSessionKey(newKey, "real");28452846  // Regenerate noise for slot B when duress is not in use. Runs after the2847  // key swap so a failure here is a disposable noise regeneration, not a2848  // lockout.2849  if (!duressConfigured) {2850    await writeSlot(SLOT_B, generateNoiseBlob(ciphertext.length / 2));2851  }2852  await stampLocalVaultMutation();28532854  return true;2855}