clipboard.ts

6.7 KB · raw

1// Clipboard write + reliable auto-clear — audit fix HV-2.2//3// The pre-fix implementation scheduled the clear with a bare `setTimeout`,4// which does not survive MV3 service-worker eviction, and performed the5// clear via `executeScript` into the *active tab*, which fails on6// restricted pages (chrome://, web store) — both failure modes left the7// secret on the OS clipboard indefinitely with the error swallowed.8//9// Now:10//   - The clear is scheduled with chrome.alarms (survives worker restarts).11//   - A best-effort setTimeout still runs as a fast path for sub-30s12//     settings (chrome.alarms resolution floor is 0.5 min); the alarm is13//     the guaranteed backstop.14//   - The clear itself goes through an offscreen document15//     (offscreen.html/offscreen.ts) — a stable extension-owned surface —16//     with executeScript into the active tab as the Firefox fallback17//     (Firefox MV2/MV3 has no chrome.offscreen).18//   - A SHA-256 hash of the copied value (never the plaintext) is parked in19//     chrome.storage.session so a restarted worker knows a clear is owed.2021import { getSettings } from "./vault";2223const DEFAULT_CLEAR_SECONDS = 30;24export const CLIPBOARD_CLEAR_ALARM = "peach-clipboard-clear-current";25const CLEAR_HASH_KEY = "clipboard-clear-hash";26// chrome.alarms cannot fire sooner than 30s in stable Chrome. For shorter27// user settings the setTimeout fast path handles the early clear and the28// alarm remains as the eviction-proof backstop.29const MIN_ALARM_MINUTES = 0.5;3031let clipboardClearTimer: ReturnType<typeof setTimeout> | null = null;3233async function writeToClipboard(text: string): Promise<void> {34  // Prefer chrome.scripting.executeScript which reliably works in MV3 service workers35  // by running in the context of the active tab page, falling back to direct write.36  try {37    const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });38    if (tab?.id) {39      await chrome.scripting.executeScript({40        target: { tabId: tab.id },41        func: (value: string) => navigator.clipboard.writeText(value),42        args: [text],43      });44      return;45    }46  } catch {47    // Fall through to direct write48  }49  // Fallback: direct navigator.clipboard (may fail if service worker is backgrounded)50  await navigator.clipboard.writeText(text);51}5253async function hashForVerification(value: string): Promise<string> {54  const digest = await crypto.subtle.digest(55    "SHA-256",56    new TextEncoder().encode(value),57  );58  return Array.from(new Uint8Array(digest))59    .map((b) => b.toString(16).padStart(2, "0"))60    .join("");61}6263export async function copyToClipboard(64  text: string,65  _label: string,66  alreadyWritten = false,67): Promise<void> {68  if (clipboardClearTimer) {69    clearTimeout(clipboardClearTimer);70    clipboardClearTimer = null;71  }72  // A second copy supersedes the first: cancel the previous alarm so the73  // old timer can't clear the new value early.74  await chrome.alarms.clear(CLIPBOARD_CLEAR_ALARM).catch(() => false);7576  // The popup/content caller may have already written the clipboard from a77  // focused document with a fresh user gesture — that path is more reliable78  // than the SW's `executeScript` into the active tab. When that happens, we79  // only need to schedule the auto-clear here.80  if (!alreadyWritten) {81    await writeToClipboard(text);82  }8384  const settingsSeconds = getSettings().clipboardClearSeconds;85  const clearAfterMs =86    (typeof settingsSeconds === "number" && settingsSeconds > 087      ? settingsSeconds88      : DEFAULT_CLEAR_SECONDS) * 1000;8990  // Park a hash (never plaintext) so a cold-started worker knows a clear is91  // owed. storage.session is memory-backed and cleared on browser exit.92  try {93    const hash = await hashForVerification(text);94    await chrome.storage.session.set({ [CLEAR_HASH_KEY]: hash });95  } catch {96    // Hash bookkeeping is best-effort; the alarm still fires without it.97  }9899  // Eviction-proof backstop (HV-2a). delayInMinutes floors at 0.5 in stable100  // Chrome; for shorter settings the setTimeout below clears earlier and the101  // alarm's clear of an already-empty clipboard is harmless.102  await chrome.alarms.create(CLIPBOARD_CLEAR_ALARM, {103    delayInMinutes: Math.max(clearAfterMs / 60_000, MIN_ALARM_MINUTES),104  });105106  // Fast path while the worker stays alive — honors sub-30s settings.107  clipboardClearTimer = setTimeout(async () => {108    clipboardClearTimer = null;109    await clearClipboardNow();110  }, clearAfterMs);111}112113/**114 * Clear the clipboard through the most reliable available surface and tear115 * down the bookkeeping. Called by the setTimeout fast path and by the116 * onAlarm backstop (background entry point).117 */118export async function clearClipboardNow(): Promise<void> {119  let cleared = false;120121  // Chrome: offscreen document — works with no tab and on restricted pages.122  if (chrome.offscreen?.createDocument) {123    try {124      await chrome.offscreen.createDocument({125        // crxjs emits the offscreen page preserving its source path under the126        // Chrome dist root (see vite.config.ts offscreen input). Keep this URL127        // aligned with that emitted location.128        url: "src/background/offscreen.html",129        reasons: [chrome.offscreen.Reason.CLIPBOARD],130        justification: "Clear sensitive clipboard content after timeout",131      });132    } catch {133      // Already exists — fine, message it directly.134    }135    try {136      const res = (await chrome.runtime.sendMessage({137        type: "CLEAR_CLIPBOARD",138      })) as { ok?: boolean } | undefined;139      cleared = res?.ok === true;140    } catch {141      cleared = false;142    }143    // Close the offscreen document so it doesn't linger; ignore failures.144    try {145      await chrome.offscreen.closeDocument();146    } catch {147      /* not open / race — fine */148    }149  }150151  // Firefox (no chrome.offscreen) or offscreen failure: active-tab path.152  if (!cleared) {153    try {154      await writeToClipboard("");155      cleared = true;156    } catch {157      cleared = false;158    }159  }160161  if (cleared) {162    await chrome.storage.session.remove(CLEAR_HASH_KEY).catch(() => undefined);163    await chrome.alarms.clear(CLIPBOARD_CLEAR_ALARM).catch(() => false);164  } else {165    // Could not clear (e.g. only a chrome:// tab focused on Firefox).166    // Re-arm a short retry alarm rather than swallowing the failure —167    // the secret must not silently persist.168    await chrome.alarms.create(CLIPBOARD_CLEAR_ALARM, {169      delayInMinutes: MIN_ALARM_MINUTES,170    });171  }172}173174/**175 * Wire the eviction-proof clear. Must be called during the worker's initial176 * synchronous evaluation (same rule as the onMessage listener) — from177 * entry.ts start().178 */179export function setupClipboardClearAlarm(): void {180  chrome.alarms.onAlarm.addListener((alarm) => {181    if (alarm.name !== CLIPBOARD_CLEAR_ALARM) return;182    void clearClipboardNow();183  });184}