|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +function parseOptionalInteger(value) { |
| 5 | + if (value == null || value === '') { |
| 6 | + return null; |
| 7 | + } |
| 8 | + |
| 9 | + const parsed = Number.parseInt(String(value), 10); |
| 10 | + return Number.isFinite(parsed) ? parsed : null; |
| 11 | +} |
| 12 | + |
| 13 | +function readBooleanEnv(name, fallback = false) { |
| 14 | + const value = process.env[name]; |
| 15 | + if (value == null) { |
| 16 | + return fallback; |
| 17 | + } |
| 18 | + |
| 19 | + const normalized = String(value).trim().toLowerCase(); |
| 20 | + return normalized === '1' || normalized === 'true' || normalized === 'yes' || normalized === 'on'; |
| 21 | +} |
| 22 | + |
| 23 | +function detectExecutionSandbox() { |
| 24 | + const sandboxRequired = readBooleanEnv('EXEC_REQUIRE_SANDBOX', false); |
| 25 | + const allowUnsandboxedRoot = readBooleanEnv('EXEC_ALLOW_UNSANDBOXED_ROOT', false); |
| 26 | + |
| 27 | + if (process.platform === 'win32' || typeof process.getuid !== 'function') { |
| 28 | + return { |
| 29 | + enabled: false, |
| 30 | + allowed: !sandboxRequired, |
| 31 | + uid: null, |
| 32 | + gid: null, |
| 33 | + reason: 'POSIX uid/gid privilege dropping is unavailable on this platform', |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + if (process.getuid() !== 0) { |
| 38 | + return { |
| 39 | + enabled: false, |
| 40 | + allowed: !sandboxRequired, |
| 41 | + uid: null, |
| 42 | + gid: null, |
| 43 | + reason: 'Server is not running as root, so execution cannot be dropped to a separate OS user', |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + const uid = parseOptionalInteger(process.env.EXEC_SANDBOX_UID); |
| 48 | + const gid = parseOptionalInteger(process.env.EXEC_SANDBOX_GID); |
| 49 | + if (uid == null || gid == null) { |
| 50 | + const allowed = sandboxRequired ? false : allowUnsandboxedRoot; |
| 51 | + return { |
| 52 | + enabled: false, |
| 53 | + allowed, |
| 54 | + uid: null, |
| 55 | + gid: null, |
| 56 | + reason: allowed |
| 57 | + ? 'Server is running as root without a dedicated execution user' |
| 58 | + : 'Server is running as root without a dedicated execution user. Configure EXEC_SANDBOX_UID and EXEC_SANDBOX_GID, or set EXEC_ALLOW_UNSANDBOXED_ROOT=1 to override', |
| 59 | + }; |
| 60 | + } |
| 61 | + |
| 62 | + return { |
| 63 | + enabled: true, |
| 64 | + allowed: true, |
| 65 | + uid, |
| 66 | + gid, |
| 67 | + reason: `Execution runs as uid ${uid}, gid ${gid}`, |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +const executionSandbox = detectExecutionSandbox(); |
| 72 | + |
| 73 | +if (executionSandbox.enabled) { |
| 74 | + console.log(`[exec] Sandbox enabled - ${executionSandbox.reason}`); |
| 75 | +} else if (executionSandbox.allowed) { |
| 76 | + console.warn(`[exec] WARNING: execution is not sandboxed - ${executionSandbox.reason}`); |
| 77 | +} else { |
| 78 | + console.error(`[exec] Execution disabled - ${executionSandbox.reason}`); |
| 79 | +} |
| 80 | + |
| 81 | +function applyOwnership(targetPath, uid, gid) { |
| 82 | + const stats = fs.lstatSync(targetPath); |
| 83 | + if (stats.isSymbolicLink()) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + fs.chownSync(targetPath, uid, gid); |
| 88 | + if (stats.isDirectory()) { |
| 89 | + fs.chmodSync(targetPath, 0o700); |
| 90 | + for (const entry of fs.readdirSync(targetPath, { withFileTypes: true })) { |
| 91 | + applyOwnership(path.join(targetPath, entry.name), uid, gid); |
| 92 | + } |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + fs.chmodSync(targetPath, 0o600); |
| 97 | +} |
| 98 | + |
| 99 | +function prepareExecutionWorkspace(tmpDir) { |
| 100 | + if (!executionSandbox.enabled) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + applyOwnership(tmpDir, executionSandbox.uid, executionSandbox.gid); |
| 105 | +} |
| 106 | + |
| 107 | +function buildExecutionEnv(tmpDir) { |
| 108 | + if (process.platform === 'win32') { |
| 109 | + return { |
| 110 | + ...process.env, |
| 111 | + HOME: tmpDir, |
| 112 | + USERPROFILE: tmpDir, |
| 113 | + TMPDIR: tmpDir, |
| 114 | + TEMP: tmpDir, |
| 115 | + TMP: tmpDir, |
| 116 | + }; |
| 117 | + } |
| 118 | + |
| 119 | + const env = { |
| 120 | + PATH: process.env.PATH || '', |
| 121 | + HOME: tmpDir, |
| 122 | + TMPDIR: tmpDir, |
| 123 | + TEMP: tmpDir, |
| 124 | + TMP: tmpDir, |
| 125 | + LANG: process.env.LANG || 'C.UTF-8', |
| 126 | + LC_ALL: process.env.LC_ALL || process.env.LANG || 'C.UTF-8', |
| 127 | + }; |
| 128 | + |
| 129 | + if (process.env.JAVA_HOME) { |
| 130 | + env.JAVA_HOME = process.env.JAVA_HOME; |
| 131 | + } |
| 132 | + |
| 133 | + return env; |
| 134 | +} |
| 135 | + |
| 136 | +function getExecutionSpawnOptions(tmpDir) { |
| 137 | + const spawnOptions = { |
| 138 | + env: buildExecutionEnv(tmpDir), |
| 139 | + windowsHide: true, |
| 140 | + }; |
| 141 | + |
| 142 | + if (executionSandbox.enabled) { |
| 143 | + spawnOptions.uid = executionSandbox.uid; |
| 144 | + spawnOptions.gid = executionSandbox.gid; |
| 145 | + } |
| 146 | + |
| 147 | + return spawnOptions; |
| 148 | +} |
| 149 | + |
| 150 | +function isExecutionAllowed() { |
| 151 | + return executionSandbox.allowed; |
| 152 | +} |
| 153 | + |
| 154 | +function isExecutionSandboxed() { |
| 155 | + return executionSandbox.enabled; |
| 156 | +} |
| 157 | + |
| 158 | +function getExecutionSandboxStatus() { |
| 159 | + if (executionSandbox.enabled) { |
| 160 | + return `Sandboxed - ${executionSandbox.reason}`; |
| 161 | + } |
| 162 | + |
| 163 | + if (!executionSandbox.allowed) { |
| 164 | + return `Disabled - ${executionSandbox.reason}`; |
| 165 | + } |
| 166 | + |
| 167 | + return `Unsandboxed - ${executionSandbox.reason}`; |
| 168 | +} |
| 169 | + |
| 170 | +module.exports = { |
| 171 | + getExecutionSpawnOptions, |
| 172 | + getExecutionSandboxStatus, |
| 173 | + isExecutionAllowed, |
| 174 | + isExecutionSandboxed, |
| 175 | + prepareExecutionWorkspace, |
| 176 | +}; |
0 commit comments