types.ts

14.4 KB · raw

1/**2 * types.ts — cross-boundary types and the `MessageType` union.3 *4 * `MessageType` mixes background-bound and content-script-bound messages.5 * Background-bound types have a case in `src/background/messages.ts`.6 * Content-script-bound types (PING, SUMMON_AUTOFILL, VAULT_LOCKED,7 * RESTART_AUTO_LOCK, etc.) are sent *to* content scripts or the popup and8 * have no handler in messages.ts by design — that absence is not a missing9 * handler. Add new cross-boundary types here, not inline in call sites.10 */1112export interface ManualSummonRecord {13  firstSummonedAt: number;14  summonCount: number;15  lastSummonedAt: number;16}1718export interface Canary {19  webhookUrl: string;20  label?: string;21  lastPinged?: number;22  pingCount?: number;23}2425export type CardNetwork = "visa" | "mastercard" | "amex" | "discover" | "unknown";2627/**28 * Passkey credential data attached to a VaultEntry. New in v2.11.0 — passkeys29 * are no longer a separate entry type; instead they hang off any login entry.30 * A passkey-only entry is a `type:"login"` entry with no username/password31 * but a `passkey` sub-object set. A login that ALSO has a passkey is just a32 * normal login with `passkey` populated.33 *34 * privateKeyJwk is present only on Chrome-created passkeys, where Peach is35 * the authenticator. Firefox-created passkeys delegate to the platform36 * authenticator, so the field is absent — `browser` distinguishes the two.37 */38export interface PasskeyData {39  rpId: string;40  rpName: string;41  credentialId: string;42  publicKeyCose: string;43  privateKeyJwk?: JsonWebKey;44  userHandle: string;45  userName: string;46  userDisplayName: string;47  algorithm: -7 | -8;48  signCount: number;49  aaguid: string;50  backupEligible: boolean;51  backupState: boolean;52  lastUsedAt?: number;53  browser?: "chrome" | "firefox";54  transports?: string[];55  /** When the credential was first registered with Peach. */56  createdAt: number;57}5859export interface VaultEntry {60  id: string; // crypto.randomUUID()61  /**62   * Entry type. As of v2.11.0 passkeys are stored on `type:"login"` entries63   * via the `passkey` sub-object — `"passkey"` here is a legacy value still64   * accepted on read but rewritten to `"login"` on next vault save.65   */66  type?: "login" | "note" | "passkey" | "card";67  title: string;68  username?: string; // only for login entries69  email?: string; // separate email address — for sites that require both a username and an email70  password?: string; // only for login entries71  url?: string; // only for login entries72  /**73   * Additional sites this entry should match for autofill. Login entries only.74   * Each value is a full origin or URL (e.g. `https://login.foo.com`).75   * The vault domain index keys against the hostname of every entry in this76   * list, in addition to `url`.77   */78  additionalUrls?: string[];79  totp?: string; // base32 seed, stored encrypted — only for login entries80  notes?: string;81  breach?: number; // HIBP exposure count; 0 or undefined = clean — only for login entries82  /**83   * When the password field was last changed. Set on first-write of `password`84   * and refreshed on every subsequent change. Defaults to `createdAt` when85   * legacy entries are read by the advanced health analyser.86   */87  passwordChangedAt?: number;88  canary?: Canary;89  createdAt: number; // Date.now()90  modifiedAt: number;91  deletedAt?: number;92  tags?: string[];93  /** Passkey credential attached to this entry, if any. */94  passkey?: PasskeyData;95  // ── Legacy passkey flat fields (pre-v2.11.0). Populated by vault read96  // migration so that the old import/sanitize paths keep typing; new code97  // should always read `entry.passkey?.*` instead. The migration drops these98  // on next vault save. Do not introduce new readers of these fields.99  rpId?: string;100  rpName?: string;101  credentialId?: string;102  publicKeyCose?: string;103  privateKeyJwk?: JsonWebKey;104  userHandle?: string;105  userName?: string;106  userDisplayName?: string;107  algorithm?: -7 | -8;108  signCount?: number;109  aaguid?: string;110  backupEligible?: boolean;111  backupState?: boolean;112  lastUsedAt?: number;113  browser?: "chrome" | "firefox";114  transports?: string[];115  // Card entry fields116  cardholderName?: string;117  cardNumber?: string;       // full PAN, stored encrypted in vault118  expMonth?: string;119  expYear?: string;120  cvc?: string;121  cardNickname?: string;122  network?: CardNetwork;123  last4?: string;124}125126/**127 * Returns true when an entry has a passkey credential attached. After v2.11.0128 * migration this is the canonical check; pre-migration entries with a flat129 * `rpId`/`credentialId` shape get rewritten by the vault read pass before130 * any consumer sees them.131 */132export function hasPasskey(entry: VaultEntry): boolean {133  return entry.passkey !== undefined && entry.passkey.credentialId.length > 0;134}135136/**137 * Returns true when an entry is "passkey-only" — has a passkey credential138 * but no password to fill into a form. Login fields hide for these entries139 * and the inline autofill selector filters them out (signing in via the140 * passkey is a separate WebAuthn flow). Username may still be populated141 * (the migration mirrors passkey.userName onto the entry's username so the142 * domain-index/search keep working) and is not part of this discriminator.143 */144export function isPasskeyOnly(entry: VaultEntry): boolean {145  return hasPasskey(entry) && !entry.password;146}147148export interface LegacyVaultEntry {149  id: string;150  title: string;151  username: string;152  password: string;153  url: string;154  totp?: string;155  notes?: string;156  breach?: number;157  created: number;158  updated: number;159  deletedAt?: number;160}161162export interface CardEntry extends VaultEntry {163  type: "card";164  cardholderName: string;165  cardNumber: string;166  expMonth: string;167  expYear: string;168  cvc: string;169}170171172export interface PendingPasskeyRequest {173  requestId: number;174  type: "create" | "get";175  rpId: string;176  rpName: string;177  userName: string;178  userDisplayName?: string;179  /**180   * Firefox path: when present, the popup performs the navigator.credentials181   * call itself rather than completing a chrome.webAuthenticationProxy182   * request. Absent for Chrome (the existing webAuthenticationProxy flow).183   */184  browser?: "chrome" | "firefox";185  /** Firefox create: base64url challenge minted by the popup-context flow. */186  challenge?: string;187  /** Firefox create: base64url user handle. Generated if absent. */188  userHandle?: string;189}190191export interface PasskeyProviderState {192  supported: boolean;193  enabled: boolean;194  attached: boolean;195  browserLabel: string;196  statusLabel: string;197  conflict?: string;198}199200export interface StoredVault {201  version: 1; // AAD for AES-GCM --- prevents downgrade202  salt: string; // hex --- 16 bytes, CSPRNG, stored not secret203  nonce: string; // hex --- 12 bytes, CSPRNG, fresh per write204  ciphertext: string; // hex --- AES-256-GCM output205  tag: string; // hex --- 16-byte GCM auth tag206  // Recovery escrow: root key wrapped under recovery words via AES-256-GCM.207  // Absent on vaults created before v2.12 (migrateToRecovery adds them on first unlock).208  recovery_salt?: string; // hex --- 16 bytes, CSPRNG, HKDF salt for recovery key209  recovery_wrapped_root_key?: string; // hex --- AES-256-GCM ciphertext of 32-byte root key210  recovery_nonce?: string; // hex --- 12 bytes, fresh nonce for the above211  recovery_tag?: string; // hex --- 16-byte GCM auth tag for the above212}213214// Background-bound message types — sent from popup/content scripts,215// handled in src/background/messages.ts. Everything in this group must216// have a matching case in that file's switch statement.217export type MessageType =218  | "UNLOCK"219  | "LOCK"220  | "IS_UNLOCKED"221  | "HAS_VAULT"222  | "GET_ENTRIES"223  | "ADD_ENTRY"224  | "BATCH_ADD_ENTRIES"225  | "RESTORE_ENTRIES"226  | "CLEAR_VAULT_ENTRIES"227  | "UPDATE_ENTRY"228  | "ADD_ADDITIONAL_URL"229  | "DELETE_ENTRY"230  | "AUTOFILL_QUERY"231  | "COPY"232  | "CREATE_VAULT"233  | "GET_ENTRY_FOR_FILL"234  | "CHECK_ALL_BREACHES"235  | "GET_SETTINGS"236  | "UPDATE_SETTINGS"237  | "FIRE_CANARY_PING"238  | "ACTIVATE_LICENSE"239  | "GET_LICENSE"240  | "GET_LICENSE_KEY"241  | "IS_DURESS_ENABLED"242  | "SETUP_DURESS"243  | "DISABLE_DURESS"244  | "VERIFY_MAIN_PASSWORD"245  | "CHANGE_PASSWORD"246  | "GET_SITE_PASSWORD_POLICY"247  | "GET_SYNC_STATE"248  | "GET_PAIRED_DEVICES"249  | "GET_SYNC_STATUS"250  | "TRIGGER_SYNC"251  | "ACCEPT_PAIRING"252  | "REMOVE_PAIRED_DEVICE"253  | "REMOVE_TRUSTED_NETWORK"254  | "MARK_NETWORK_TRUSTED"255  | "RESET_SYNC_IDENTITY"256  | "GET_INCOMING_PAIRING_REQUEST"257  | "RESPOND_INCOMING_PAIRING"258  | "DEBUG_INJECT_INCOMING_PAIRING"259  | "START_PAIRING_SERVER"260  | "STOP_PAIRING_SERVER"261  | "GET_RECENT_PAIRING"262  | "GET_PENDING_PASSKEY"263  | "APPROVE_PASSKEY"264  | "DENY_PASSKEY"265  | "GET_PASSKEY_PROVIDER_STATE"266  | "SET_PASSKEY_PROVIDER_ENABLED"267  | "GET_DELETED_ENTRIES"268  | "RESTORE_ENTRY"269  | "PURGE_ENTRY"270  | "PURGE_EXPIRED_TRASH"271  | "GET_VAULT_HISTORY"272  | "RESTORE_VAULT_SNAPSHOT"273  | "COUNT_DUPLICATES"274  | "DEDUPLICATE_ENTRIES"275  | "RECORD_MANUAL_SUMMON"276  | "DECLINE_CARD_SAVE"277  | "IS_CARD_DECLINED"278  | "SUBMIT_FEEDBACK"279  | "GET_VAULT_STATUS"280  | "GET_ADVANCED_HEALTH_REPORT"281  | "INITIATE_PASSWORD_CHANGE"282  | "DEACTIVATE_LICENSE"283  | "GET_IDENTITIES"284  | "DESTROY_VAULT"285  | "INITIATE_FIREFOX_PASSKEY_CREATE"286  | "COMPLETE_FIREFOX_PASSKEY_CREATE"287  | "PASSKEY_OFFER_DECLINE"288  | "GET_PASSKEY_OFFER_DECLINED"289  | "GET_PASSKEY_FOR_DOMAIN"290  // Firefox content-script proxy path (v2.12.0). Sent FROM the page-world291  // monkey-patch through the extension-world relay TO the background. See292  // src/shared/passkey-flags.ts for removal conditions.293  | "PASSKEY_HAS_CREDENTIALS"294  | "PASSKEY_CREATE"295  | "PASSKEY_GET"296  // Android / iOS message types — activate only when Capacitor.isNativePlatform() is true.297  | "UNLOCK_WITH_KEY"298  | "GET_DERIVED_KEY"299  // Recovery word messages300  | "VALIDATE_RECOVERY_WORDS"301  | "RECOVER_WITH_WORDS"302  | "GET_RECOVERY_WORDS"303  | "SCAN_PAIRING_QR"304  | "SYNC_SESSION_STARTED"305  | "GET_LOCAL_IP"306  | "GET_IDENTITY_SUGGESTIONS"307  | "TOUCH_AUTO_LOCK"308  | "POPULATE_INLINE_VAULT"309  | "TEST_LOCAL_WEBSOCKET"310  | "TEST_SUBNET_DETECTION"311  | "TEST_SUBNET_SCAN"312  | "TEST_WS_LOCAL"313  | "TEST_SUBNET_DETECT"314  | "SAVE_NOTE"315  | "DELETE_NOTE"316  | "ML_INIT"317  | "ML_PREDICT_BATCH"318  // Content-script-bound message types — sent BY the background TO content319  // scripts. They never reach messages.ts's switch. Kept in this union so320  // existing call sites type-check; do not add cases for them in the321  // background handler.322  | "PING"            // background → content: liveness probe323  | "SUMMON_AUTOFILL" // background → content: open the in-page palette324  | "VAULT_LOCKED"    // background → content/popup: vault just locked325  | "VAULT_UNLOCKED"  // background → content/popup: vault just unlocked326  | "PRO_GATE_TRIGGERED" // background → popup: a Pro feature was attempted on free327  | "PASSKEY_PRO_GATE" // background → content: show passkey Pro gate in inline popup328  | "OPEN_PRO_GATE" // content → background: open popup and navigate to License section329  | "SHOW_PASSKEY_TOAST"    // background → content tab: show passkey auth toast330  | "DISMISS_PASSKEY_TOAST" // background → content tab: dismiss passkey auth toast331  | "PASSKEY_AUTH_RESPONSE" // content → background: user approved or denied the toast332  | "SHOULD_PLAY_CELEBRATION"333  | "MARK_CELEBRATION_PLAYED"334  // Peach Codex (v2.42.0). Generation runs in the popup — the SW only335  // sources vault data and applies the restored slots.336  | "GET_CODEX_VAULT_DATA"337  | "ESTIMATE_CODEX_PAGES"338  | "RESTORE_FROM_CODEX";339340export type ProFeature =341  | "sync"342  | "passkeys"343  | "advanced_health"344  | "smart_password_change"345  | "ios_app"346  | "codex";347348/**349 * License export shape included in PeachScript and encrypted JSON backups.350 * `celebrationPlayed` is intentionally absent — a new device deserves the moment.351 */352export interface PeachLicenseExport {353  key: string;354  activatedAt?: number;355}356357export interface AdvancedHealthEntryReport {358  id: string;359  title: string;360  domain: string;361  url?: string;362  score: number;363  ageDays: number;364  reasons: string[];365  factors: {366    breached: boolean;367    breachCount: number;368    reused: boolean;369    aged: boolean;370    weak: boolean;371    missingTotpOnSensitive: boolean;372  };373}374375export interface AdvancedHealthReport {376  vaultScore: number;377  generatedAt: number;378  entryCount: number;379  reusedGroups: number;380  agedCount: number;381  remediation: AdvancedHealthEntryReport[];382}383384export const ACCENT_COLORS = [385  { name: "peach",    hex: "#F49C70" },386  { name: "rose",     hex: "#FF8AA6" },387  { name: "plum",     hex: "#D597F2" },388  { name: "lavender", hex: "#A6A8FF" },389  { name: "sky",      hex: "#5BC8EE" },390  { name: "mint",     hex: "#5EE0B0" },391  { name: "sage",     hex: "#B5D870" },392  { name: "butter",   hex: "#FFD45A" },393  { name: "sand",     hex: "#EDBC85" },394  { name: "clay",     hex: "#F08660" },395  { name: "slate",    hex: "#80BEDE" },396  { name: "stone",    hex: "#C8B8A8" },397] as const;398399export type AccentColorName = typeof ACCENT_COLORS[number]["name"];400401export interface SavedIdentity {402  value: string;403  type: "email" | "username";404  lastUsed: number;405  useCount: number;406  exampleDomain?: string;407}408409export interface PasswordPolicy {410  minLength?: number;411  maxLength?: number;412  requiresUppercase?: boolean;413  requiresLowercase?: boolean;414  requiresNumbers?: boolean;415  requiresSymbols?: boolean;416  forbidsSymbols?: boolean;417  allowedSymbols?: string;418  /** Characters the site explicitly disallows. Filtered from every pool before generation. */419  forbiddenChars?: string;420}421422/**423 * A single vault history snapshot. The `vault_a` and `vault_b` fields are the424 * raw encrypted blobs from chrome.storage.local — already AES-256-GCM425 * protected by the master password, so storing them in history adds no426 * security surface.427 *428 * Snapshots are taken BEFORE every vault write so the saved blob represents429 * what is about to be lost, not what was just written. The reason field430 * documents the trigger so the UI can render "saved" / "synced" / "imported"431 * / "restored" labels next to each row.432 */433export interface VaultSnapshot {434  id: string;435  savedAt: number;436  reason: "local_save" | "sync_received" | "import" | "restore";437  vault_a: string;438  vault_b: string;439  /** Active entry count when the snapshot was taken; null if vault was locked. */440  entryCount: number | null;441}442443export interface VaultHistory {444  /** Newest first; capped at HISTORY_MAX (10). */445  snapshots: VaultSnapshot[];446}447448export interface Message {449  type: MessageType;450  payload?: unknown;451}452453export interface MessageResponse {454  ok: boolean;455  data?: unknown;456  error?: string;457  unlocked?: boolean;458}