Open History to see the last commit
1import * as NodeEvents from "node:events";
2import * as NodeFS from "node:fs";
3import * as NodeFSP from "node:fs/promises";
4import * as NodeTimers from "node:timers";
5import * as NodeURL from "node:url";
6import * as NodeVM from "node:vm";
7import { parse } from "acorn";
8
9const expectedDesktopBridgeApis = [
10 "getClientPlatform",
11 "getLocalEnvironmentBootstraps",
12 "pickFolder",
13];
14const clerkPasskeysGlobal = "__clerk_internal_electron_passkeys";
15const preloadExecutionTimeoutMs = 1_000;
16const desktopPackage = JSON.parse(
17 NodeFS.readFileSync(new URL("../package.json", import.meta.url), "utf8"),
18);
19const electronVersion = desktopPackage.dependencies.electron;
20
21const isSyntaxNode = (value) =>
22 typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
23
24const inspectBundle = (source) => {
25 const runtimeImports = [];
26 const visit = (node) => {
27 if (node.type === "ImportExpression") {
28 throw new Error("Desktop preload bundle contains a dynamic import() call");
29 }
30
31 if (node.type === "CallExpression" && node.callee.type === "Identifier") {
32 if (node.callee.name === "require") {
33 const [argument] = node.arguments;
34 if (node.arguments.length !== 1 || argument?.type !== "Literal") {
35 throw new Error("Desktop preload bundle contains a dynamic require() call");
36 }
37 if (typeof argument.value !== "string") {
38 throw new Error("Desktop preload bundle contains a dynamic require() call");
39 }
40 runtimeImports.push(argument.value);
41 }
42 }
43
44 for (const child of Object.values(node)) {
45 if (Array.isArray(child)) {
46 for (const item of child) {
47 if (isSyntaxNode(item)) visit(item);
48 }
49 } else if (isSyntaxNode(child)) {
50 visit(child);
51 }
52 }
53 };
54
55 visit(parse(source, { ecmaVersion: "latest", sourceType: "script" }));
56 return runtimeImports;
57};
58
59const createSandboxModules = (exposedGlobals) => {
60 const ipcRenderer = {
61 invoke: () => Promise.resolve(undefined),
62 on: () => undefined,
63 removeListener: () => undefined,
64 sendSync: () => undefined,
65 };
66 const electron = {
67 contextBridge: {
68 exposeInMainWorld: (name, api) => exposedGlobals.set(name, api),
69 },
70 ipcRenderer,
71 };
72
73 return new Map([
74 ["electron", electron],
75 ["electron/common", electron],
76 ["electron/renderer", electron],
77 ["events", NodeEvents.default],
78 ["node:events", NodeEvents.default],
79 ["timers", NodeTimers.default],
80 ["node:timers", NodeTimers.default],
81 ["url", NodeURL.default],
82 ["node:url", NodeURL.default],
83 ]);
84};
85
86const executeBundle = (source, sandboxModules) => {
87 const sandboxProcess = {
88 contextIsolated: true,
89 // oxlint-disable-next-line t3code/no-global-process-runtime -- This standalone CI verifier supplies the preload's host platform without loading Effect.
90 platform: process.platform,
91 versions: { electron: electronVersion },
92 };
93 const requireSandboxModule = (moduleName) => {
94 if (!sandboxModules.has(moduleName)) {
95 throw new Error(
96 `Unsupported sandbox module requested during preload execution: ${moduleName}`,
97 );
98 }
99 return sandboxModules.get(moduleName);
100 };
101
102 NodeVM.runInNewContext(
103 source,
104 {
105 process: sandboxProcess,
106 require: requireSandboxModule,
107 },
108 {
109 filename: "desktop-preload.cjs",
110 timeout: preloadExecutionTimeoutMs,
111 },
112 );
113};
114
115export const verifyPreloadBundle = (source) => {
116 const runtimeImports = inspectBundle(source);
117 const exposedGlobals = new Map();
118 const sandboxModules = createSandboxModules(exposedGlobals);
119 const unsupportedImports = [...new Set(runtimeImports)]
120 .filter((moduleName) => !sandboxModules.has(moduleName))
121 .toSorted();
122
123 if (unsupportedImports.length > 0) {
124 throw new Error(
125 `Desktop preload bundle contains unsupported sandbox imports: ${unsupportedImports.join(", ")}`,
126 );
127 }
128
129 executeBundle(source, sandboxModules);
130
131 const desktopBridge = exposedGlobals.get("desktopBridge");
132 const missingApis = expectedDesktopBridgeApis.filter(
133 (api) => typeof desktopBridge?.[api] !== "function",
134 );
135 if (!exposedGlobals.has("desktopBridge")) missingApis.unshift("desktopBridge exposure");
136 if (!exposedGlobals.has(clerkPasskeysGlobal)) missingApis.push(`${clerkPasskeysGlobal} exposure`);
137
138 if (missingApis.length > 0) {
139 throw new Error(`Desktop preload bundle is missing executable APIs: ${missingApis.join(", ")}`);
140 }
141};
142
143if (process.argv[1] && NodeURL.pathToFileURL(process.argv[1]).href === import.meta.url) {
144 const preloadUrl = new URL("../dist-electron/preload.cjs", import.meta.url);
145 const source = await NodeFSP.readFile(preloadUrl, "utf8");
146 verifyPreloadBundle(source);
147}