types.ts

11.9 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 but no137 * username or password. Such entries render with login fields hidden.138 */139export function isPasskeyOnly(entry: VaultEntry): boolean {140  return hasPasskey(entry) && !entry.username && !entry.password;141}142143export interface LegacyVaultEntry {144  id: string;145  title: string;146  username: string;147  password: string;148  url: string;149  totp?: string;150  notes?: string;151  breach?: number;152  created: number;153  updated: number;154  deletedAt?: number;155}156157export interface CardEntry extends VaultEntry {158  type: "card";159  cardholderName: string;160  cardNumber: string;161  expMonth: string;162  expYear: string;163  cvc: string;164}165166export interface PasskeyEntry extends VaultEntry {167  type: "passkey";168  rpId: string;169  rpName: string;170  credentialId: string;171  publicKeyCose: string;172  /**173   * Encrypted vault holds the private key only for Chrome-created passkeys,174   * where Peach is the authenticator. Firefox-created passkeys delegate to175   * the platform authenticator, so this field is absent — `browser`176   * distinguishes the two paths.177   */178  privateKeyJwk?: JsonWebKey;179  userHandle: string;180  userName: string;181  userDisplayName: string;182  algorithm: -7 | -8;183  signCount: number;184  aaguid: string;185  backupEligible: boolean;186  backupState: boolean;187  lastUsedAt?: number;188  // Optional on read for pre-v2.10.0 entries; new entries always set it.189  browser?: "chrome" | "firefox";190  transports?: string[];191}192193export interface PendingPasskeyRequest {194  requestId: number;195  type: "create" | "get";196  rpId: string;197  rpName: string;198  userName: string;199  userDisplayName?: string;200  /**201   * Firefox path: when present, the popup performs the navigator.credentials202   * call itself rather than completing a chrome.webAuthenticationProxy203   * request. Absent for Chrome (the existing webAuthenticationProxy flow).204   */205  browser?: "chrome" | "firefox";206  /** Firefox create: base64url challenge minted by the popup-context flow. */207  challenge?: string;208  /** Firefox create: base64url user handle. Generated if absent. */209  userHandle?: string;210}211212export interface PasskeyProviderState {213  supported: boolean;214  enabled: boolean;215  attached: boolean;216  browserLabel: string;217  statusLabel: string;218  conflict?: string;219}220221export interface StoredVault {222  version: 1; // AAD for AES-GCM --- prevents downgrade223  salt: string; // hex --- 16 bytes, CSPRNG, stored not secret224  nonce: string; // hex --- 12 bytes, CSPRNG, fresh per write225  ciphertext: string; // hex --- AES-256-GCM output226  tag: string; // hex --- 16-byte GCM auth tag227}228229// Background-bound message types — sent from popup/content scripts,230// handled in src/background/messages.ts. Everything in this group must231// have a matching case in that file's switch statement.232export type MessageType =233  | "UNLOCK"234  | "LOCK"235  | "IS_UNLOCKED"236  | "HAS_VAULT"237  | "GET_ENTRIES"238  | "ADD_ENTRY"239  | "BATCH_ADD_ENTRIES"240  | "RESTORE_ENTRIES"241  | "CLEAR_VAULT_ENTRIES"242  | "UPDATE_ENTRY"243  | "ADD_ADDITIONAL_URL"244  | "DELETE_ENTRY"245  | "AUTOFILL_QUERY"246  | "COPY"247  | "CREATE_VAULT"248  | "GET_ENTRY_FOR_FILL"249  | "CHECK_BREACH"250  | "CHECK_ALL_BREACHES"251  | "GET_SETTINGS"252  | "UPDATE_SETTINGS"253  | "FIRE_CANARY_PING"254  | "ACTIVATE_LICENSE"255  | "GET_LICENSE"256  | "GET_LICENSE_KEY"257  | "IS_DURESS_ENABLED"258  | "SETUP_DURESS"259  | "DISABLE_DURESS"260  | "VERIFY_MAIN_PASSWORD"261  | "CHANGE_PASSWORD"262  | "GET_SITE_PASSWORD_POLICY"263  | "GET_SYNC_STATE"264  | "TRIGGER_SYNC"265  | "START_PAIRING"266  | "CANCEL_PAIRING"267  | "ACCEPT_PAIRING"268  | "REMOVE_PAIRED_DEVICE"269  | "REMOVE_TRUSTED_NETWORK"270  | "MARK_NETWORK_TRUSTED"271  | "GET_PENDING_PASSKEY"272  | "APPROVE_PASSKEY"273  | "DENY_PASSKEY"274  | "DELETE_PASSKEY"275  | "GET_PASSKEYS"276  | "GET_PASSKEY_PROVIDER_STATE"277  | "SET_PASSKEY_PROVIDER_ENABLED"278  | "GET_DELETED_ENTRIES"279  | "RESTORE_ENTRY"280  | "PURGE_ENTRY"281  | "PURGE_EXPIRED_TRASH"282  | "LIST_VAULT_SNAPSHOTS"283  | "RESTORE_VAULT_SNAPSHOT"284  | "COUNT_DUPLICATES"285  | "DEDUPLICATE_ENTRIES"286  | "RECORD_MANUAL_SUMMON"287  | "DECLINE_CARD_SAVE"288  | "IS_CARD_DECLINED"289  | "SUBMIT_FEEDBACK"290  | "GET_VAULT_STATUS"291  | "GET_ADVANCED_HEALTH_REPORT"292  | "INITIATE_PASSWORD_CHANGE"293  | "DEACTIVATE_LICENSE"294  | "GET_IDENTITIES"295  | "DESTROY_VAULT"296  | "INITIATE_FIREFOX_PASSKEY_CREATE"297  | "COMPLETE_FIREFOX_PASSKEY_CREATE"298  | "PASSKEY_OFFER_DECLINE"299  | "GET_PASSKEY_OFFER_DECLINED"300  | "GET_PASSKEY_FOR_DOMAIN"301  // Content-script-bound message types — sent BY the background TO content302  // scripts. They never reach messages.ts's switch. Kept in this union so303  // existing call sites type-check; do not add cases for them in the304  // background handler.305  | "PING"            // background → content: liveness probe306  | "SUMMON_AUTOFILL" // background → content: open the in-page palette307  | "VAULT_LOCKED"    // background → content/popup: vault just locked308  | "VAULT_UNLOCKED"  // background → content/popup: vault just unlocked309  | "PRO_GATE_TRIGGERED" // background → popup: a Pro feature was attempted on free310  | "PASSKEY_PRO_GATE" // background → content: show passkey Pro gate in inline popup311  | "OPEN_PRO_GATE" // content → background: open popup and navigate to License section312  | "SHOULD_PLAY_CELEBRATION"313  | "MARK_CELEBRATION_PLAYED";314315export type ProFeature =316  | "sync"317  | "passkeys"318  | "advanced_health"319  | "smart_password_change"320  | "ios_app";321322/**323 * License export shape included in PeachScript and encrypted JSON backups.324 * `celebrationPlayed` is intentionally absent — a new device deserves the moment.325 */326export interface PeachLicenseExport {327  key: string;328  activatedAt?: number;329}330331export interface AdvancedHealthEntryReport {332  id: string;333  title: string;334  domain: string;335  url?: string;336  score: number;337  ageDays: number;338  reasons: string[];339  factors: {340    breached: boolean;341    breachCount: number;342    reused: boolean;343    aged: boolean;344    weak: boolean;345    missingTotpOnSensitive: boolean;346  };347}348349export interface AdvancedHealthReport {350  vaultScore: number;351  generatedAt: number;352  entryCount: number;353  reusedGroups: number;354  agedCount: number;355  remediation: AdvancedHealthEntryReport[];356}357358export const ACCENT_COLORS = [359  { name: "peach",    hex: "#F49C70" },360  { name: "rose",     hex: "#FF8AA6" },361  { name: "plum",     hex: "#D597F2" },362  { name: "lavender", hex: "#A6A8FF" },363  { name: "sky",      hex: "#5BC8EE" },364  { name: "mint",     hex: "#5EE0B0" },365  { name: "sage",     hex: "#B5D870" },366  { name: "butter",   hex: "#FFD45A" },367  { name: "sand",     hex: "#EDBC85" },368  { name: "clay",     hex: "#F08660" },369  { name: "slate",    hex: "#80BEDE" },370  { name: "stone",    hex: "#C8B8A8" },371] as const;372373export type AccentColorName = typeof ACCENT_COLORS[number]["name"];374375export interface SavedIdentity {376  value: string;377  type: "email" | "username";378  lastUsed: number;379  useCount: number;380  exampleDomain?: string;381}382383export interface PasswordPolicy {384  minLength?: number;385  maxLength?: number;386  requiresUppercase?: boolean;387  requiresLowercase?: boolean;388  requiresNumbers?: boolean;389  requiresSymbols?: boolean;390  forbidsSymbols?: boolean;391  allowedSymbols?: string;392  /** Characters the site explicitly disallows. Filtered from every pool before generation. */393  forbiddenChars?: string;394}395396export interface Message {397  type: MessageType;398  payload?: unknown;399}400401export interface MessageResponse {402  ok: boolean;403  data?: unknown;404  error?: string;405  unlocked?: boolean;406}