types.ts

13.7 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  password?: string; // only for login entries70  url?: string; // only for login entries71  /**72   * Additional sites this entry should match for autofill. Login entries only.73   * Each value is a full origin or URL (e.g. `https://login.foo.com`).74   * The vault domain index keys against the hostname of every entry in this75   * list, in addition to `url`.76   */77  additionalUrls?: string[];78  totp?: string; // base32 seed, stored encrypted — only for login entries79  notes?: string;80  breach?: number; // HIBP exposure count; 0 or undefined = clean — only for login entries81  /**82   * When the password field was last changed. Set on first-write of `password`83   * and refreshed on every subsequent change. Defaults to `createdAt` when84   * legacy entries are read by the advanced health analyser.85   */86  passwordChangedAt?: number;87  canary?: Canary;88  createdAt: number; // Date.now()89  modifiedAt: number;90  deletedAt?: number;91  tags?: string[];92  /** Passkey credential attached to this entry, if any. */93  passkey?: PasskeyData;94  // ── Legacy passkey flat fields (pre-v2.11.0). Populated by vault read95  // migration so that the old import/sanitize paths keep typing; new code96  // should always read `entry.passkey?.*` instead. The migration drops these97  // on next vault save. Do not introduce new readers of these fields.98  rpId?: string;99  rpName?: string;100  credentialId?: string;101  publicKeyCose?: string;102  privateKeyJwk?: JsonWebKey;103  userHandle?: string;104  userName?: string;105  userDisplayName?: string;106  algorithm?: -7 | -8;107  signCount?: number;108  aaguid?: string;109  backupEligible?: boolean;110  backupState?: boolean;111  lastUsedAt?: number;112  browser?: "chrome" | "firefox";113  transports?: string[];114  // Card entry fields115  cardholderName?: string;116  cardNumber?: string;       // full PAN, stored encrypted in vault117  expMonth?: string;118  expYear?: string;119  cvc?: string;120  cardNickname?: string;121  network?: CardNetwork;122  last4?: string;123}124125/**126 * Returns true when an entry has a passkey credential attached. After v2.11.0127 * migration this is the canonical check; pre-migration entries with a flat128 * `rpId`/`credentialId` shape get rewritten by the vault read pass before129 * any consumer sees them.130 */131export function hasPasskey(entry: VaultEntry): boolean {132  return entry.passkey !== undefined && entry.passkey.credentialId.length > 0;133}134135/**136 * Returns true when an entry is "passkey-only" — has a passkey credential137 * but no password to fill into a form. Login fields hide for these entries138 * and the inline autofill selector filters them out (signing in via the139 * passkey is a separate WebAuthn flow). Username may still be populated140 * (the migration mirrors passkey.userName onto the entry's username so the141 * domain-index/search keep working) and is not part of this discriminator.142 */143export function isPasskeyOnly(entry: VaultEntry): boolean {144  return hasPasskey(entry) && !entry.password;145}146147export interface LegacyVaultEntry {148  id: string;149  title: string;150  username: string;151  password: string;152  url: string;153  totp?: string;154  notes?: string;155  breach?: number;156  created: number;157  updated: number;158  deletedAt?: number;159}160161export interface CardEntry extends VaultEntry {162  type: "card";163  cardholderName: string;164  cardNumber: string;165  expMonth: string;166  expYear: string;167  cvc: string;168}169170171export interface PendingPasskeyRequest {172  requestId: number;173  type: "create" | "get";174  rpId: string;175  rpName: string;176  userName: string;177  userDisplayName?: string;178  /**179   * Firefox path: when present, the popup performs the navigator.credentials180   * call itself rather than completing a chrome.webAuthenticationProxy181   * request. Absent for Chrome (the existing webAuthenticationProxy flow).182   */183  browser?: "chrome" | "firefox";184  /** Firefox create: base64url challenge minted by the popup-context flow. */185  challenge?: string;186  /** Firefox create: base64url user handle. Generated if absent. */187  userHandle?: string;188}189190export interface PasskeyProviderState {191  supported: boolean;192  enabled: boolean;193  attached: boolean;194  browserLabel: string;195  statusLabel: string;196  conflict?: string;197}198199export interface StoredVault {200  version: 1; // AAD for AES-GCM --- prevents downgrade201  salt: string; // hex --- 16 bytes, CSPRNG, stored not secret202  nonce: string; // hex --- 12 bytes, CSPRNG, fresh per write203  ciphertext: string; // hex --- AES-256-GCM output204  tag: string; // hex --- 16-byte GCM auth tag205  // Recovery escrow: root key wrapped under recovery words via AES-256-GCM.206  // Absent on vaults created before v2.12 (migrateToRecovery adds them on first unlock).207  recovery_salt?: string; // hex --- 16 bytes, CSPRNG, HKDF salt for recovery key208  recovery_wrapped_root_key?: string; // hex --- AES-256-GCM ciphertext of 32-byte root key209  recovery_nonce?: string; // hex --- 12 bytes, fresh nonce for the above210  recovery_tag?: string; // hex --- 16-byte GCM auth tag for the above211}212213// Background-bound message types — sent from popup/content scripts,214// handled in src/background/messages.ts. Everything in this group must215// have a matching case in that file's switch statement.216export type MessageType =217  | "UNLOCK"218  | "LOCK"219  | "IS_UNLOCKED"220  | "HAS_VAULT"221  | "GET_ENTRIES"222  | "ADD_ENTRY"223  | "BATCH_ADD_ENTRIES"224  | "RESTORE_ENTRIES"225  | "CLEAR_VAULT_ENTRIES"226  | "UPDATE_ENTRY"227  | "ADD_ADDITIONAL_URL"228  | "DELETE_ENTRY"229  | "AUTOFILL_QUERY"230  | "COPY"231  | "CREATE_VAULT"232  | "GET_ENTRY_FOR_FILL"233  | "CHECK_BREACH"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  | "TRIGGER_SYNC"249  | "START_PAIRING"250  | "CANCEL_PAIRING"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_PEER_DISCOVERY"260  | "STOP_PEER_DISCOVERY"261  | "INITIATE_PAIR_WITH_PEER"262  | "START_PAIRING_SERVER"263  | "STOP_PAIRING_SERVER"264  | "CONNECT_WITH_CODE"265  | "GET_RECENT_PAIRING"266  | "GET_PENDING_PASSKEY"267  | "APPROVE_PASSKEY"268  | "DENY_PASSKEY"269  | "DELETE_PASSKEY"270  | "GET_PASSKEYS"271  | "GET_PASSKEY_PROVIDER_STATE"272  | "SET_PASSKEY_PROVIDER_ENABLED"273  | "GET_DELETED_ENTRIES"274  | "RESTORE_ENTRY"275  | "PURGE_ENTRY"276  | "PURGE_EXPIRED_TRASH"277  | "GET_VAULT_HISTORY"278  | "RESTORE_VAULT_SNAPSHOT"279  | "COUNT_DUPLICATES"280  | "DEDUPLICATE_ENTRIES"281  | "RECORD_MANUAL_SUMMON"282  | "DECLINE_CARD_SAVE"283  | "IS_CARD_DECLINED"284  | "SUBMIT_FEEDBACK"285  | "GET_VAULT_STATUS"286  | "GET_ADVANCED_HEALTH_REPORT"287  | "INITIATE_PASSWORD_CHANGE"288  | "DEACTIVATE_LICENSE"289  | "GET_IDENTITIES"290  | "DESTROY_VAULT"291  | "INITIATE_FIREFOX_PASSKEY_CREATE"292  | "COMPLETE_FIREFOX_PASSKEY_CREATE"293  | "PASSKEY_OFFER_DECLINE"294  | "GET_PASSKEY_OFFER_DECLINED"295  | "GET_PASSKEY_FOR_DOMAIN"296  // Firefox content-script proxy path (v2.12.0). Sent FROM the page-world297  // monkey-patch through the extension-world relay TO the background. See298  // src/shared/passkey-flags.ts for removal conditions.299  | "PASSKEY_HAS_CREDENTIALS"300  | "PASSKEY_CREATE"301  | "PASSKEY_GET"302  // Android / iOS message types — activate only when Capacitor.isNativePlatform() is true.303  | "UNLOCK_WITH_KEY"304  | "GET_DERIVED_KEY"305  // Recovery word messages306  | "VALIDATE_RECOVERY_WORDS"307  | "RECOVER_WITH_WORDS"308  | "GET_RECOVERY_WORDS"309  | "SCAN_PAIRING_QR"310  | "GET_LOCAL_IP"311  | "GET_IDENTITY_SUGGESTIONS"312  | "POPULATE_INLINE_VAULT"313  // Content-script-bound message types — sent BY the background TO content314  // scripts. They never reach messages.ts's switch. Kept in this union so315  // existing call sites type-check; do not add cases for them in the316  // background handler.317  | "PING"            // background → content: liveness probe318  | "SUMMON_AUTOFILL" // background → content: open the in-page palette319  | "VAULT_LOCKED"    // background → content/popup: vault just locked320  | "VAULT_UNLOCKED"  // background → content/popup: vault just unlocked321  | "PRO_GATE_TRIGGERED" // background → popup: a Pro feature was attempted on free322  | "PASSKEY_PRO_GATE" // background → content: show passkey Pro gate in inline popup323  | "OPEN_PRO_GATE" // content → background: open popup and navigate to License section324  | "SHOULD_PLAY_CELEBRATION"325  | "MARK_CELEBRATION_PLAYED";326327export type ProFeature =328  | "sync"329  | "passkeys"330  | "advanced_health"331  | "smart_password_change"332  | "ios_app";333334/**335 * License export shape included in PeachScript and encrypted JSON backups.336 * `celebrationPlayed` is intentionally absent — a new device deserves the moment.337 */338export interface PeachLicenseExport {339  key: string;340  activatedAt?: number;341}342343export interface AdvancedHealthEntryReport {344  id: string;345  title: string;346  domain: string;347  url?: string;348  score: number;349  ageDays: number;350  reasons: string[];351  factors: {352    breached: boolean;353    breachCount: number;354    reused: boolean;355    aged: boolean;356    weak: boolean;357    missingTotpOnSensitive: boolean;358  };359}360361export interface AdvancedHealthReport {362  vaultScore: number;363  generatedAt: number;364  entryCount: number;365  reusedGroups: number;366  agedCount: number;367  remediation: AdvancedHealthEntryReport[];368}369370export const ACCENT_COLORS = [371  { name: "peach",    hex: "#F49C70" },372  { name: "rose",     hex: "#FF8AA6" },373  { name: "plum",     hex: "#D597F2" },374  { name: "lavender", hex: "#A6A8FF" },375  { name: "sky",      hex: "#5BC8EE" },376  { name: "mint",     hex: "#5EE0B0" },377  { name: "sage",     hex: "#B5D870" },378  { name: "butter",   hex: "#FFD45A" },379  { name: "sand",     hex: "#EDBC85" },380  { name: "clay",     hex: "#F08660" },381  { name: "slate",    hex: "#80BEDE" },382  { name: "stone",    hex: "#C8B8A8" },383] as const;384385export type AccentColorName = typeof ACCENT_COLORS[number]["name"];386387export interface SavedIdentity {388  value: string;389  type: "email" | "username";390  lastUsed: number;391  useCount: number;392  exampleDomain?: string;393}394395export interface PasswordPolicy {396  minLength?: number;397  maxLength?: number;398  requiresUppercase?: boolean;399  requiresLowercase?: boolean;400  requiresNumbers?: boolean;401  requiresSymbols?: boolean;402  forbidsSymbols?: boolean;403  allowedSymbols?: string;404  /** Characters the site explicitly disallows. Filtered from every pool before generation. */405  forbiddenChars?: string;406}407408/**409 * A single vault history snapshot. The `vault_a` and `vault_b` fields are the410 * raw encrypted blobs from chrome.storage.local — already AES-256-GCM411 * protected by the master password, so storing them in history adds no412 * security surface.413 *414 * Snapshots are taken BEFORE every vault write so the saved blob represents415 * what is about to be lost, not what was just written. The reason field416 * documents the trigger so the UI can render "saved" / "synced" / "imported"417 * / "restored" labels next to each row.418 */419export interface VaultSnapshot {420  id: string;421  savedAt: number;422  reason: "local_save" | "sync_received" | "import" | "restore";423  vault_a: string;424  vault_b: string;425  /** Active entry count when the snapshot was taken; null if vault was locked. */426  entryCount: number | null;427}428429export interface VaultHistory {430  /** Newest first; capped at HISTORY_MAX (10). */431  snapshots: VaultSnapshot[];432}433434export interface Message {435  type: MessageType;436  payload?: unknown;437}438439export interface MessageResponse {440  ok: boolean;441  data?: unknown;442  error?: string;443  unlocked?: boolean;444}