|
| 1 | +import { BuildTarget } from "@trigger.dev/core/v3"; |
| 2 | +import { BuildManifest } from "@trigger.dev/core/v3/schemas"; |
| 3 | +import { BuildContext, BuildExtension } from "@trigger.dev/core/v3/build"; |
| 4 | +import { dirname, resolve, join } from "node:path"; |
| 5 | +import { readFileSync } from "node:fs"; |
| 6 | +import { createRequire } from "node:module"; |
| 7 | +import { readPackageJSON } from "pkg-types"; |
| 8 | + |
| 9 | +export type SecureExecOptions = { |
| 10 | + /** |
| 11 | + * Packages available inside the sandbox at runtime. |
| 12 | + * |
| 13 | + * These are `require()`'d inside the V8 isolate at runtime — the bundler |
| 14 | + * never sees them statically. They are marked external and installed as |
| 15 | + * deploy dependencies. |
| 16 | + * |
| 17 | + * @example |
| 18 | + * ```ts |
| 19 | + * secureExec({ packages: ["jszip", "lodash"] }) |
| 20 | + * ``` |
| 21 | + */ |
| 22 | + packages?: string[]; |
| 23 | +}; |
| 24 | + |
| 25 | +/** |
| 26 | + * Build extension for [secure-exec](https://secureexec.dev) — run untrusted |
| 27 | + * JavaScript/TypeScript in V8 isolates with configurable permissions. |
| 28 | + * |
| 29 | + * Handles the esbuild workarounds needed for secure-exec's runtime |
| 30 | + * `require.resolve` calls, native binaries, and module-scope resolution. |
| 31 | + * |
| 32 | + * @example |
| 33 | + * ```ts |
| 34 | + * import { secureExec } from "@trigger.dev/build/extensions/secureExec"; |
| 35 | + * |
| 36 | + * export default defineConfig({ |
| 37 | + * build: { |
| 38 | + * extensions: [secureExec()], |
| 39 | + * }, |
| 40 | + * }); |
| 41 | + * ``` |
| 42 | + */ |
| 43 | +export function secureExec(options?: SecureExecOptions): BuildExtension { |
| 44 | + return new SecureExecExtension(options ?? {}); |
| 45 | +} |
| 46 | + |
| 47 | +class SecureExecExtension implements BuildExtension { |
| 48 | + public readonly name = "SecureExecExtension"; |
| 49 | + |
| 50 | + private userPackages: string[]; |
| 51 | + |
| 52 | + constructor(options: SecureExecOptions) { |
| 53 | + this.userPackages = options.packages ?? []; |
| 54 | + } |
| 55 | + |
| 56 | + externalsForTarget(_target: BuildTarget) { |
| 57 | + return [ |
| 58 | + // esbuild must not be bundled — it locates its native binary via a |
| 59 | + // relative path from its JS API entry point. secure-exec uses esbuild |
| 60 | + // at runtime to bundle polyfills for sandbox code. |
| 61 | + "esbuild", |
| 62 | + // User-specified packages are require()'d inside the V8 sandbox at |
| 63 | + // runtime — the bundler never sees them statically. |
| 64 | + ...this.userPackages, |
| 65 | + ]; |
| 66 | + } |
| 67 | + |
| 68 | + onBuildStart(context: BuildContext) { |
| 69 | + context.logger.debug(`Adding ${this.name} esbuild plugins`); |
| 70 | + |
| 71 | + // Plugin 1: Replace node-stdlib-browser with pre-resolved paths. |
| 72 | + // |
| 73 | + // Trigger's ESM shim anchors require.resolve() to the chunk path, so |
| 74 | + // node-stdlib-browser's runtime require.resolve("./mock/empty.js") breaks. |
| 75 | + // Fix: load the real node-stdlib-browser at build time (where require.resolve |
| 76 | + // works), capture the resolved path map, and inline it as a static export. |
| 77 | + const workingDir = context.workingDir; |
| 78 | + context.registerPlugin({ |
| 79 | + name: "secure-exec-stdlib-resolver", |
| 80 | + setup(build) { |
| 81 | + build.onResolve({ filter: /^node-stdlib-browser$/ }, () => ({ |
| 82 | + path: "node-stdlib-browser", |
| 83 | + namespace: "secure-exec-nsb-resolved", |
| 84 | + })); |
| 85 | + build.onLoad({ filter: /.*/, namespace: "secure-exec-nsb-resolved" }, () => { |
| 86 | + const buildRequire = createRequire(join(workingDir, "package.json")); |
| 87 | + const resolved = buildRequire("node-stdlib-browser"); |
| 88 | + return { |
| 89 | + contents: `export default ${JSON.stringify(resolved)};`, |
| 90 | + loader: "js", |
| 91 | + }; |
| 92 | + }); |
| 93 | + }, |
| 94 | + }); |
| 95 | + |
| 96 | + // Plugin 2: Inline bridge.js at build time. |
| 97 | + // |
| 98 | + // bridge-loader.js in @secure-exec/node(js) uses __dirname and |
| 99 | + // require.resolve("@secure-exec/core") at module scope to locate |
| 100 | + // dist/bridge.js on disk. This fails in Trigger's bundled output. |
| 101 | + // Fix: read bridge.js content at build time and inline it as a |
| 102 | + // string literal so no runtime filesystem resolution is needed. |
| 103 | + // |
| 104 | + context.registerPlugin({ |
| 105 | + name: "secure-exec-bridge-inline", |
| 106 | + setup(build) { |
| 107 | + build.onLoad( |
| 108 | + { filter: /[\\/]@secure-exec[\\/]node[\\/]dist[\\/]bridge-loader\.js$/ }, |
| 109 | + (args) => { |
| 110 | + try { |
| 111 | + const buildRequire = createRequire(args.path); |
| 112 | + const coreEntry = buildRequire.resolve("@secure-exec/core"); |
| 113 | + const coreRoot = resolve(dirname(coreEntry), ".."); |
| 114 | + const bridgeCode = readFileSync(join(coreRoot, "dist", "bridge.js"), "utf8"); |
| 115 | + |
| 116 | + return { |
| 117 | + contents: [ |
| 118 | + `import { getIsolateRuntimeSource } from "@secure-exec/core";`, |
| 119 | + `const bridgeCodeCache = ${JSON.stringify(bridgeCode)};`, |
| 120 | + `export function getRawBridgeCode() { return bridgeCodeCache; }`, |
| 121 | + `export function getBridgeAttachCode() { return getIsolateRuntimeSource("bridgeAttach"); }`, |
| 122 | + ].join("\n"), |
| 123 | + loader: "js", |
| 124 | + }; |
| 125 | + } catch { |
| 126 | + // If we can't inline the bridge, let the normal loader handle it. |
| 127 | + return undefined; |
| 128 | + } |
| 129 | + } |
| 130 | + ); |
| 131 | + }, |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + async onBuildComplete(context: BuildContext, _manifest: BuildManifest) { |
| 136 | + if (context.target === "dev") { |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + context.logger.debug(`Adding ${this.name} deploy dependencies`); |
| 141 | + |
| 142 | + const dependencies: Record<string, string> = {}; |
| 143 | + |
| 144 | + // Resolve versions for user-specified sandbox packages |
| 145 | + for (const pkg of this.userPackages) { |
| 146 | + try { |
| 147 | + const modulePath = await context.resolvePath(pkg); |
| 148 | + if (!modulePath) { |
| 149 | + dependencies[pkg] = "latest"; |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + const packageJSON = await readPackageJSON(dirname(modulePath)); |
| 154 | + dependencies[pkg] = packageJSON.version ?? "latest"; |
| 155 | + } catch { |
| 156 | + context.logger.warn( |
| 157 | + `Could not resolve version for sandbox package ${pkg}, defaulting to latest` |
| 158 | + ); |
| 159 | + dependencies[pkg] = "latest"; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + context.addLayer({ |
| 164 | + id: "secureExec", |
| 165 | + dependencies, |
| 166 | + image: { |
| 167 | + // isolated-vm requires native compilation tools |
| 168 | + pkgs: ["python3", "make", "g++"], |
| 169 | + }, |
| 170 | + }); |
| 171 | + } |
| 172 | +} |
0 commit comments