storage-access.ts

1.5 KB · raw

1/**2 * PB-26-203: the session-key escrow is only as trustworthy as the boundary3 * around chrome.storage.session. This initializer positively establishes4 * trusted-only access on both storage areas before any session restore runs.5 * Where the browser cannot prove that state, the caller must disable the6 * escrow (fail closed) instead of trusting an unproven boundary.7 */8export type StorageHardening = {9  localHardened: boolean;10  sessionHardened: boolean;11  sessionEscrowAvailable: boolean;12};1314type AccessLevelArea = {15  setAccessLevel?: (options: { accessLevel: "TRUSTED_CONTEXTS" }) => Promise<void>;16};1718async function setTrustedContexts(area: unknown): Promise<boolean> {19  const typed = area as AccessLevelArea | undefined;20  if (typeof typed?.setAccessLevel !== "function") return false;21  try {22    await typed.setAccessLevel({ accessLevel: "TRUSTED_CONTEXTS" });23    return true;24  } catch {25    // Optional browser hardening must never prevent the extension from26    // starting; the message policy remains the primary security boundary.27    return false;28  }29}3031export async function hardenExtensionStorage(): Promise<StorageHardening> {32  const localHardened = await setTrustedContexts(chrome.storage?.local);33  const session = chrome.storage?.session;34  // No session area (older Firefox): no escrow exists to protect, so there35  // is nothing to harden — but escrow is still reported unavailable.36  const sessionHardened = session ? await setTrustedContexts(session) : false;37  return { localHardened, sessionHardened, sessionEscrowAvailable: sessionHardened };38}