1+ import * as NodeEvents from "node:events" ;
2+ import * as NodeFS from "node:fs" ;
13import * as NodeFSP from "node:fs/promises" ;
4+ import * as NodeTimers from "node:timers" ;
25import * as NodeURL from "node:url" ;
6+ import * as NodeVM from "node:vm" ;
37import { parse } from "acorn" ;
48
59const expectedDesktopBridgeApis = [
@@ -8,30 +12,17 @@ const expectedDesktopBridgeApis = [
812 "pickFolder" ,
913] ;
1014const clerkPasskeysGlobal = "__clerk_internal_electron_passkeys" ;
15+ const preloadExecutionTimeoutMs = 1_000 ;
16+ const desktopPackage = JSON . parse (
17+ NodeFS . readFileSync ( new URL ( "../package.json" , import . meta. url ) , "utf8" ) ,
18+ ) ;
19+ const electronVersion = desktopPackage . dependencies . electron ;
1120
1221const isSyntaxNode = ( value ) =>
1322 typeof value === "object" && value !== null && "type" in value && typeof value . type === "string" ;
1423
15- const readStaticName = ( node ) => {
16- if ( node . type === "Identifier" ) return node . name ;
17- if ( node . type === "Literal" && typeof node . value === "string" ) return node . value ;
18- return undefined ;
19- } ;
20-
21- const readMemberName = ( node ) => {
22- if ( node . type !== "MemberExpression" ) return undefined ;
23- if ( ! node . computed && node . property . type === "Identifier" ) return node . property . name ;
24- return readStaticName ( node . property ) ;
25- } ;
26-
27- const isContextBridge = ( node ) =>
28- ( node . type === "Identifier" && node . name === "contextBridge" ) ||
29- ( node . type === "MemberExpression" && readMemberName ( node ) === "contextBridge" ) ;
30-
3124const inspectBundle = ( source ) => {
3225 const runtimeImports = [ ] ;
33- const exposedGlobals = new Set ( ) ;
34- const desktopBridgeApis = new Set ( ) ;
3526 const visit = ( node ) => {
3627 if ( node . type === "ImportExpression" ) {
3728 throw new Error ( "Desktop preload bundle contains a dynamic import() call" ) ;
@@ -50,25 +41,6 @@ const inspectBundle = (source) => {
5041 }
5142 }
5243
53- if (
54- node . type === "CallExpression" &&
55- node . callee . type === "MemberExpression" &&
56- readMemberName ( node . callee ) === "exposeInMainWorld" &&
57- isContextBridge ( node . callee . object )
58- ) {
59- const [ globalName , api ] = node . arguments ;
60- const name = globalName === undefined ? undefined : readStaticName ( globalName ) ;
61- if ( name !== undefined ) exposedGlobals . add ( name ) ;
62-
63- if ( name === "desktopBridge" && api ?. type === "ObjectExpression" ) {
64- for ( const property of api . properties ) {
65- if ( property . type !== "Property" ) continue ;
66- const propertyName = readStaticName ( property . key ) ;
67- if ( propertyName !== undefined ) desktopBridgeApis . add ( propertyName ) ;
68- }
69- }
70- }
71-
7244 for ( const child of Object . values ( node ) ) {
7345 if ( Array . isArray ( child ) ) {
7446 for ( const item of child ) {
@@ -81,20 +53,69 @@ const inspectBundle = (source) => {
8153 } ;
8254
8355 visit ( parse ( source , { ecmaVersion : "latest" , sourceType : "script" } ) ) ;
84- return { desktopBridgeApis , exposedGlobals , runtimeImports } ;
56+ return runtimeImports ;
8557} ;
8658
87- export const verifyPreloadBundle = ( source ) => {
88- const { desktopBridgeApis, exposedGlobals, runtimeImports } = inspectBundle ( source ) ;
89- const missingApis = expectedDesktopBridgeApis . filter ( ( api ) => ! desktopBridgeApis . has ( api ) ) ;
90- if ( ! exposedGlobals . has ( "desktopBridge" ) ) missingApis . unshift ( "desktopBridge exposure" ) ;
91- if ( ! exposedGlobals . has ( clerkPasskeysGlobal ) ) missingApis . push ( `${ clerkPasskeysGlobal } exposure` ) ;
59+ const 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+ } ;
9272
93- if ( missingApis . length > 0 ) {
94- throw new Error ( `Desktop preload bundle is missing executable APIs: ${ missingApis . join ( ", " ) } ` ) ;
95- }
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+
86+ const 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+ } ;
96114
97- const sandboxModules = new Set ( [ "electron" , "events" , "timers" , "url" ] ) ;
115+ export const verifyPreloadBundle = ( source ) => {
116+ const runtimeImports = inspectBundle ( source ) ;
117+ const exposedGlobals = new Map ( ) ;
118+ const sandboxModules = createSandboxModules ( exposedGlobals ) ;
98119 const unsupportedImports = [ ...new Set ( runtimeImports ) ]
99120 . filter ( ( moduleName ) => ! sandboxModules . has ( moduleName ) )
100121 . toSorted ( ) ;
@@ -104,6 +125,19 @@ export const verifyPreloadBundle = (source) => {
104125 `Desktop preload bundle contains unsupported sandbox imports: ${ unsupportedImports . join ( ", " ) } ` ,
105126 ) ;
106127 }
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+ }
107141} ;
108142
109143if ( process . argv [ 1 ] && NodeURL . pathToFileURL ( process . argv [ 1 ] ) . href === import . meta. url ) {
0 commit comments