import { timingSafeEqual } from "node:crypto"; /** Per-launch desktop HTTP capability. This is not an OS-user malware * boundary and is not an OAuth credential. Browser-only mode is unchanged. */ export function desktopAPIAuthorized( headers: Headers, env: Record = process.env, ): boolean { const expected = env.LH_DESKTOP_API_SECRET; if (env.LH_DESKTOP_SERVER !== "1" && !expected) return true; if (!expected || expected.length < 64) return false; const supplied = headers.get("x-learnharness-desktop"); if (!supplied) return false; const actual = Buffer.from(supplied); const wanted = Buffer.from(expected); return actual.length === wanted.length && timingSafeEqual(actual, wanted); }