|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +/* eslint-env node */ |
| 8 | + |
| 9 | +import { spawnSync } from 'node:child_process'; |
| 10 | +import path from 'node:path'; |
| 11 | +import fs from 'node:fs'; |
| 12 | +import os from 'node:os'; |
| 13 | +import { fileURLToPath } from 'node:url'; |
| 14 | + |
| 15 | +const __filename = fileURLToPath(import.meta.url); |
| 16 | +const __dirname = path.dirname(__filename); |
| 17 | + |
| 18 | +/** |
| 19 | + * Compiles the GeminiSandbox C# helper on Windows. |
| 20 | + * This is used to provide native restricted token sandboxing. |
| 21 | + */ |
| 22 | +function compileWindowsSandbox() { |
| 23 | + if (os.platform() !== 'win32') { |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + const srcHelperPath = path.resolve( |
| 28 | + __dirname, |
| 29 | + '../src/services/scripts/GeminiSandbox.exe', |
| 30 | + ); |
| 31 | + const distHelperPath = path.resolve( |
| 32 | + __dirname, |
| 33 | + '../dist/src/services/scripts/GeminiSandbox.exe', |
| 34 | + ); |
| 35 | + const sourcePath = path.resolve( |
| 36 | + __dirname, |
| 37 | + '../src/services/scripts/GeminiSandbox.cs', |
| 38 | + ); |
| 39 | + |
| 40 | + if (!fs.existsSync(sourcePath)) { |
| 41 | + console.error(`Sandbox source not found at ${sourcePath}`); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // Ensure directories exist |
| 46 | + [srcHelperPath, distHelperPath].forEach((p) => { |
| 47 | + const dir = path.dirname(p); |
| 48 | + if (!fs.existsSync(dir)) { |
| 49 | + fs.mkdirSync(dir, { recursive: true }); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + // Find csc.exe (C# Compiler) which is built into Windows .NET Framework |
| 54 | + const systemRoot = process.env['SystemRoot'] || 'C:\\Windows'; |
| 55 | + const cscPaths = [ |
| 56 | + 'csc.exe', // Try in PATH first |
| 57 | + path.join( |
| 58 | + systemRoot, |
| 59 | + 'Microsoft.NET', |
| 60 | + 'Framework64', |
| 61 | + 'v4.0.30319', |
| 62 | + 'csc.exe', |
| 63 | + ), |
| 64 | + path.join( |
| 65 | + systemRoot, |
| 66 | + 'Microsoft.NET', |
| 67 | + 'Framework', |
| 68 | + 'v4.0.30319', |
| 69 | + 'csc.exe', |
| 70 | + ), |
| 71 | + ]; |
| 72 | + |
| 73 | + let csc = undefined; |
| 74 | + for (const p of cscPaths) { |
| 75 | + if (p === 'csc.exe') { |
| 76 | + const result = spawnSync('where', ['csc.exe'], { stdio: 'ignore' }); |
| 77 | + if (result.status === 0) { |
| 78 | + csc = 'csc.exe'; |
| 79 | + break; |
| 80 | + } |
| 81 | + } else if (fs.existsSync(p)) { |
| 82 | + csc = p; |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if (!csc) { |
| 88 | + console.warn( |
| 89 | + 'Windows C# compiler (csc.exe) not found. Native sandboxing will attempt to compile on first run.', |
| 90 | + ); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + console.log(`Compiling native Windows sandbox helper...`); |
| 95 | + // Compile to src |
| 96 | + let result = spawnSync( |
| 97 | + csc, |
| 98 | + [`/out:${srcHelperPath}`, '/optimize', sourcePath], |
| 99 | + { |
| 100 | + stdio: 'inherit', |
| 101 | + }, |
| 102 | + ); |
| 103 | + |
| 104 | + if (result.status === 0) { |
| 105 | + console.log('Successfully compiled GeminiSandbox.exe to src'); |
| 106 | + // Copy to dist if dist exists |
| 107 | + const distDir = path.resolve(__dirname, '../dist'); |
| 108 | + if (fs.existsSync(distDir)) { |
| 109 | + const distScriptsDir = path.dirname(distHelperPath); |
| 110 | + if (!fs.existsSync(distScriptsDir)) { |
| 111 | + fs.mkdirSync(distScriptsDir, { recursive: true }); |
| 112 | + } |
| 113 | + fs.copyFileSync(srcHelperPath, distHelperPath); |
| 114 | + console.log('Successfully copied GeminiSandbox.exe to dist'); |
| 115 | + } |
| 116 | + } else { |
| 117 | + console.error('Failed to compile Windows sandbox helper.'); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +compileWindowsSandbox(); |
0 commit comments