|
1 | 1 | import { afterEach, describe, expect, it } from "vitest"; |
2 | | -import { allowAllEnv } from "../../../src/index.js"; |
| 2 | +import { allowAllEnv, allowAllChildProcess } from "../../../src/index.js"; |
3 | 3 | import { createTestNodeRuntime } from "../../test-utils.js"; |
4 | | -import type { NodeRuntime } from "../../../src/index.js"; |
| 4 | +import type { NodeRuntime, CommandExecutor } from "../../../src/index.js"; |
| 5 | +import type { SpawnedProcess } from "../../../src/types.js"; |
5 | 6 |
|
6 | 7 | type CapturedConsoleEvent = { |
7 | 8 | channel: "stdout" | "stderr"; |
@@ -104,4 +105,79 @@ describe("runtime driver specific: node env leakage", () => { |
104 | 105 | expect(parsed.home).toBe(process.env.HOME ?? "/root"); |
105 | 106 | expect(parsed.marker).toBe("env-positive-control"); |
106 | 107 | }); |
| 108 | + |
| 109 | + // --------------------------------------------------------------- |
| 110 | + // Dangerous env var filtering for child process spawn |
| 111 | + // --------------------------------------------------------------- |
| 112 | + |
| 113 | + function createCapturingExecutor() { |
| 114 | + const calls: { command: string; args: string[]; env?: Record<string, string> }[] = []; |
| 115 | + const executor: CommandExecutor = { |
| 116 | + spawn(command, args, options): SpawnedProcess { |
| 117 | + calls.push({ command, args, env: options?.env }); |
| 118 | + return { |
| 119 | + writeStdin: () => {}, |
| 120 | + closeStdin: () => {}, |
| 121 | + kill: () => {}, |
| 122 | + wait: () => Promise.resolve(0), |
| 123 | + }; |
| 124 | + }, |
| 125 | + }; |
| 126 | + return { executor, calls }; |
| 127 | + } |
| 128 | + |
| 129 | + it("strips LD_PRELOAD from child process spawn env", async () => { |
| 130 | + const { executor, calls } = createCapturingExecutor(); |
| 131 | + proc = createTestNodeRuntime({ |
| 132 | + permissions: { ...allowAllChildProcess }, |
| 133 | + commandExecutor: executor, |
| 134 | + }); |
| 135 | + await proc.run(` |
| 136 | + const cp = require('child_process'); |
| 137 | + cp.spawnSync('echo', ['hi'], { |
| 138 | + env: { LD_PRELOAD: '/evil/lib.so', SAFE_VAR: 'ok' }, |
| 139 | + }); |
| 140 | + `); |
| 141 | + expect(calls.length).toBe(1); |
| 142 | + expect(calls[0].env).toBeDefined(); |
| 143 | + expect(calls[0].env!.LD_PRELOAD).toBeUndefined(); |
| 144 | + expect(calls[0].env!.SAFE_VAR).toBe("ok"); |
| 145 | + }); |
| 146 | + |
| 147 | + it("strips NODE_OPTIONS from child process spawn env", async () => { |
| 148 | + const { executor, calls } = createCapturingExecutor(); |
| 149 | + proc = createTestNodeRuntime({ |
| 150 | + permissions: { ...allowAllChildProcess }, |
| 151 | + commandExecutor: executor, |
| 152 | + }); |
| 153 | + await proc.run(` |
| 154 | + const cp = require('child_process'); |
| 155 | + cp.spawnSync('echo', ['hi'], { |
| 156 | + env: { NODE_OPTIONS: '--require=evil.js', PATH: '/usr/bin' }, |
| 157 | + }); |
| 158 | + `); |
| 159 | + expect(calls.length).toBe(1); |
| 160 | + expect(calls[0].env!.NODE_OPTIONS).toBeUndefined(); |
| 161 | + expect(calls[0].env!.PATH).toBe("/usr/bin"); |
| 162 | + }); |
| 163 | + |
| 164 | + it("normal env vars pass through child process spawn correctly", async () => { |
| 165 | + const { executor, calls } = createCapturingExecutor(); |
| 166 | + proc = createTestNodeRuntime({ |
| 167 | + permissions: { ...allowAllChildProcess }, |
| 168 | + commandExecutor: executor, |
| 169 | + }); |
| 170 | + await proc.run(` |
| 171 | + const cp = require('child_process'); |
| 172 | + cp.spawnSync('echo', ['hi'], { |
| 173 | + env: { PATH: '/usr/bin', HOME: '/home/test', CUSTOM: 'value' }, |
| 174 | + }); |
| 175 | + `); |
| 176 | + expect(calls.length).toBe(1); |
| 177 | + expect(calls[0].env).toEqual({ |
| 178 | + PATH: "/usr/bin", |
| 179 | + HOME: "/home/test", |
| 180 | + CUSTOM: "value", |
| 181 | + }); |
| 182 | + }); |
107 | 183 | }); |
0 commit comments