示例工程 39-secrets-safestorage

39-secrets-safestorage/app/shared/ipc.ts

typescript · 2.2 KB

export const CH = {
  snapshot: "vault:snapshot",
  add: "vault:add-secret",
  reveal: "vault:reveal-secret",
  rotate: "vault:rotate",
  tamper: "vault:tamper",
  reload: "vault:reload",
  roundtrip: "vault:roundtrip",
  refresh: "vault:refresh",
} as const;

export type EventRow = {
  at: number;
  kind: string;
  detail: string;
};

export type StorageInfo = {
  status: "initializing" | "available" | "unavailable";
  backend: string;
  mock: boolean;
  error: string;
  wrappedKeyBytes: number;
};

export type SecretRow = {
  id: string;
  name: string;
  createdAt: string;
  updatedAt: string;
  valid: boolean;
  bytes: number;
};

export type VaultInfo = {
  path: string;
  keyId: number;
  count: number;
  bytes: number;
  leak: boolean;
  error: string;
  secrets: SecretRow[];
};

export type RoundtripResult = {
  plaintextLength: number;
  cipherBytes: number;
  cipherHead: string;
  decryptedEqual: boolean;
  shouldReEncrypt: boolean;
  tamperError: string;
  at: number;
};

export type Snapshot = {
  platform: string;
  electronVersion: string;
  nodeVersion: string;
  storage: StorageInfo;
  vault: VaultInfo;
  roundtrip: RoundtripResult | null;
  events: EventRow[];
};

export type RevealResponse = {
  snapshot: Snapshot;
  value: string;
  error: string;
};

export function assertAddSecretReq(
  value: unknown,
): asserts value is { name: string; secret: string } {
  if (typeof value !== "object" || value === null) {
    throw new Error("BAD_SECRET_REQ");
  }
  const req = value as { name?: unknown; secret?: unknown };
  if (
    typeof req.name !== "string" ||
    req.name.trim().length === 0 ||
    req.name.length > 60
  ) {
    throw new Error("BAD_SECRET_NAME");
  }
  if (
    typeof req.secret !== "string" ||
    req.secret.length === 0 ||
    req.secret.length > 4096
  ) {
    throw new Error("BAD_SECRET_VALUE");
  }
}

export function assertSecretIdReq(
  value: unknown,
): asserts value is { id: string } {
  if (typeof value !== "object" || value === null) {
    throw new Error("BAD_SECRET_ID_REQ");
  }
  const id = (value as { id?: unknown }).id;
  if (typeof id !== "string" || !/^[A-Za-z0-9_-]{1,64}$/.test(id)) {
    throw new Error("BAD_SECRET_ID");
  }
}