types.ts

11.1 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}165166167export interface PendingPasskeyRequest {168  requestId: number;169  type: "create" | "get";170  rpId: string;171  rpName: string;172  userName: string;173  userDisplayName?: string;174  /**175   * Firefox path: when present, the popup performs the navigator.credentials176   * call itself rather than completing a chrome.webAuthenticationProxy177   * request. Absent for Chrome (the existing webAuthenticationProxy flow).178   */179  browser?: "chrome" | "firefox";180  /** Firefox create: base64url challenge minted by the popup-context flow. */181  challenge?: string;182  /** Firefox create: base64url user handle. Generated if absent. */183  userHandle?: string;184}185186export interface PasskeyProviderState {187  supported: boolean;188  enabled: boolean;189  attached: boolean;190  browserLabel: string;191  statusLabel: string;192  conflict?: string;193}194195export interface StoredVault {196  version: 1; // AAD for AES-GCM --- prevents downgrade197  salt: string; // hex --- 16 bytes, CSPRNG, stored not secret198  nonce: string; // hex --- 12 bytes, CSPRNG, fresh per write199  ciphertext: string; // hex --- AES-256-GCM output200  tag: string; // hex --- 16-byte GCM auth tag201}202203// Background-bound message types — sent from popup/content scripts,204// handled in src/background/messages.ts. Everything in this group must205// have a matching case in that file's switch statement.206export type MessageType =207  | "UNLOCK"208  | "LOCK"209  | "IS_UNLOCKED"210  | "HAS_VAULT"211  | "GET_ENTRIES"212  | "ADD_ENTRY"213  | "BATCH_ADD_ENTRIES"214  | "RESTORE_ENTRIES"215  | "CLEAR_VAULT_ENTRIES"216  | "UPDATE_ENTRY"217  | "ADD_ADDITIONAL_URL"218  | "DELETE_ENTRY"219  | "AUTOFILL_QUERY"220  | "COPY"221  | "CREATE_VAULT"222  | "GET_ENTRY_FOR_FILL"223  | "CHECK_BREACH"224  | "CHECK_ALL_BREACHES"225  | "GET_SETTINGS"226  | "UPDATE_SETTINGS"227  | "FIRE_CANARY_PING"228  | "ACTIVATE_LICENSE"229  | "GET_LICENSE"230  | "GET_LICENSE_KEY"231  | "IS_DURESS_ENABLED"232  | "SETUP_DURESS"233  | "DISABLE_DURESS"234  | "VERIFY_MAIN_PASSWORD"235  | "CHANGE_PASSWORD"236  | "GET_SITE_PASSWORD_POLICY"237  | "GET_SYNC_STATE"238  | "TRIGGER_SYNC"239  | "START_PAIRING"240  | "CANCEL_PAIRING"241  | "ACCEPT_PAIRING"242  | "REMOVE_PAIRED_DEVICE"243  | "REMOVE_TRUSTED_NETWORK"244  | "MARK_NETWORK_TRUSTED"245  | "GET_PENDING_PASSKEY"246  | "APPROVE_PASSKEY"247  | "DENY_PASSKEY"248  | "DELETE_PASSKEY"249  | "GET_PASSKEYS"250  | "GET_PASSKEY_PROVIDER_STATE"251  | "SET_PASSKEY_PROVIDER_ENABLED"252  | "GET_DELETED_ENTRIES"253  | "RESTORE_ENTRY"254  | "PURGE_ENTRY"255  | "PURGE_EXPIRED_TRASH"256  | "LIST_VAULT_SNAPSHOTS"257  | "RESTORE_VAULT_SNAPSHOT"258  | "COUNT_DUPLICATES"259  | "DEDUPLICATE_ENTRIES"260  | "RECORD_MANUAL_SUMMON"261  | "DECLINE_CARD_SAVE"262  | "IS_CARD_DECLINED"263  | "SUBMIT_FEEDBACK"264  | "GET_VAULT_STATUS"265  | "GET_ADVANCED_HEALTH_REPORT"266  | "INITIATE_PASSWORD_CHANGE"267  | "DEACTIVATE_LICENSE"268  | "GET_IDENTITIES"269  | "DESTROY_VAULT"270  | "INITIATE_FIREFOX_PASSKEY_CREATE"271  | "COMPLETE_FIREFOX_PASSKEY_CREATE"272  | "PASSKEY_OFFER_DECLINE"273  | "GET_PASSKEY_OFFER_DECLINED"274  | "GET_PASSKEY_FOR_DOMAIN"275  // Content-script-bound message types — sent BY the background TO content276  // scripts. They never reach messages.ts's switch. Kept in this union so277  // existing call sites type-check; do not add cases for them in the278  // background handler.279  | "PING"            // background → content: liveness probe280  | "SUMMON_AUTOFILL" // background → content: open the in-page palette281  | "VAULT_LOCKED"    // background → content/popup: vault just locked282  | "VAULT_UNLOCKED"  // background → content/popup: vault just unlocked283  | "PRO_GATE_TRIGGERED" // background → popup: a Pro feature was attempted on free284  | "PASSKEY_PRO_GATE" // background → content: show passkey Pro gate in inline popup285  | "OPEN_PRO_GATE" // content → background: open popup and navigate to License section286  | "SHOULD_PLAY_CELEBRATION"287  | "MARK_CELEBRATION_PLAYED";288289export type ProFeature =290  | "sync"291  | "passkeys"292  | "advanced_health"293  | "smart_password_change"294  | "ios_app";295296/**297 * License export shape included in PeachScript and encrypted JSON backups.298 * `celebrationPlayed` is intentionally absent — a new device deserves the moment.299 */300export interface PeachLicenseExport {301  key: string;302  activatedAt?: number;303}304305export interface AdvancedHealthEntryReport {306  id: string;307  title: string;308  domain: string;309  url?: string;310  score: number;311  ageDays: number;312  reasons: string[];313  factors: {314    breached: boolean;315    breachCount: number;316    reused: boolean;317    aged: boolean;318    weak: boolean;319    missingTotpOnSensitive: boolean;320  };321}322323export interface AdvancedHealthReport {324  vaultScore: number;325  generatedAt: number;326  entryCount: number;327  reusedGroups: number;328  agedCount: number;329  remediation: AdvancedHealthEntryReport[];330}331332export const ACCENT_COLORS = [333  { name: "peach",    hex: "#F49C70" },334  { name: "rose",     hex: "#FF8AA6" },335  { name: "plum",     hex: "#D597F2" },336  { name: "lavender", hex: "#A6A8FF" },337  { name: "sky",      hex: "#5BC8EE" },338  { name: "mint",     hex: "#5EE0B0" },339  { name: "sage",     hex: "#B5D870" },340  { name: "butter",   hex: "#FFD45A" },341  { name: "sand",     hex: "#EDBC85" },342  { name: "clay",     hex: "#F08660" },343  { name: "slate",    hex: "#80BEDE" },344  { name: "stone",    hex: "#C8B8A8" },345] as const;346347export type AccentColorName = typeof ACCENT_COLORS[number]["name"];348349export interface SavedIdentity {350  value: string;351  type: "email" | "username";352  lastUsed: number;353  useCount: number;354  exampleDomain?: string;355}356357export interface PasswordPolicy {358  minLength?: number;359  maxLength?: number;360  requiresUppercase?: boolean;361  requiresLowercase?: boolean;362  requiresNumbers?: boolean;363  requiresSymbols?: boolean;364  forbidsSymbols?: boolean;365  allowedSymbols?: string;366  /** Characters the site explicitly disallows. Filtered from every pool before generation. */367  forbiddenChars?: string;368}369370export interface Message {371  type: MessageType;372  payload?: unknown;373}374375export interface MessageResponse {376  ok: boolean;377  data?: unknown;378  error?: string;379  unlocked?: boolean;380}