|
| 1 | +import * as assert from "node:assert"; |
| 2 | +import { exec } from "node:child_process"; |
| 3 | +import { readdir, readFile, rm, writeFile } from "node:fs/promises"; |
| 4 | +import * as path from "node:path"; |
| 5 | +import { promisify } from "node:util"; |
| 6 | +import * as vscode from "vscode"; |
| 7 | +import { MiseService } from "../../miseService"; |
| 8 | + |
| 9 | +const execAsync = promisify(exec); |
| 10 | + |
| 11 | +/** |
| 12 | + * Task and tool names come from the repository configuration, so a repository |
| 13 | + * that gets opened in vscode fully controls them. None of them may ever reach a |
| 14 | + * shell as anything other than a single literal argument. |
| 15 | + * |
| 16 | + * Every name in the fixture carries a payload creating a `pwned-*` file in the |
| 17 | + * workspace: the assertions below check that no such file is ever created, |
| 18 | + * while the task itself still runs and prints its marker. |
| 19 | + */ |
| 20 | +suite("Command Injection Test Suite", function () { |
| 21 | + this.timeout(60_000); |
| 22 | + |
| 23 | + const TASK_DQ = 'inj-dq"; touch pwned-dq #'; |
| 24 | + const TASK_SQ = "inj-sq'; touch pwned-sq #"; |
| 25 | + const TASK_SUB = "inj-sub$(touch pwned-sub)`touch pwned-bt`"; |
| 26 | + const TASK_SPACES = "inj spaces & touch pwned-amp | name"; |
| 27 | + |
| 28 | + const INJECTING_TASKS = [ |
| 29 | + { name: TASK_DQ, marker: "marker-dq" }, |
| 30 | + { name: TASK_SQ, marker: "marker-sq" }, |
| 31 | + { name: TASK_SUB, marker: "marker-sub" }, |
| 32 | + { name: TASK_SPACES, marker: "marker-spaces" }, |
| 33 | + ]; |
| 34 | + |
| 35 | + let workspaceRoot: string; |
| 36 | + let miseService: MiseService; |
| 37 | + |
| 38 | + // the service only reads workspaceState from the context |
| 39 | + const fakeContext = { |
| 40 | + workspaceState: { get: () => undefined }, |
| 41 | + } as unknown as vscode.ExtensionContext; |
| 42 | + |
| 43 | + const listPwnedFiles = async () => |
| 44 | + (await readdir(workspaceRoot)).filter((name) => name.startsWith("pwned")); |
| 45 | + |
| 46 | + const assertNoInjection = async (context: string) => { |
| 47 | + assert.deepEqual( |
| 48 | + await listPwnedFiles(), |
| 49 | + [], |
| 50 | + `${context} executed an injected command`, |
| 51 | + ); |
| 52 | + }; |
| 53 | + |
| 54 | + suiteSetup(async () => { |
| 55 | + workspaceRoot = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? ""; |
| 56 | + assert.ok(workspaceRoot, "Workspace root should be available"); |
| 57 | + |
| 58 | + miseService = new MiseService(fakeContext); |
| 59 | + await miseService.initializeMisePath(); |
| 60 | + assert.ok( |
| 61 | + miseService.getMiseBinaryPath(), |
| 62 | + "mise binary should be resolved", |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + teardown(async () => { |
| 67 | + for (const name of await listPwnedFiles()) { |
| 68 | + await rm(path.join(workspaceRoot, name), { force: true }); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + test("the fixture exposes the task names an attacker would use", async () => { |
| 73 | + const tasks = await miseService.getTasks(); |
| 74 | + |
| 75 | + assert.deepEqual( |
| 76 | + tasks.map((task) => task.name).sort(), |
| 77 | + [TASK_SPACES, TASK_DQ, TASK_SQ, TASK_SUB, "echo-ok"].sort(), |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + test("getTaskInfo resolves hostile task names instead of running them", async () => { |
| 82 | + for (const { name } of INJECTING_TASKS) { |
| 83 | + const taskInfo = await miseService.getTaskInfo(name); |
| 84 | + |
| 85 | + assert.ok(taskInfo, `task info should be returned for ${name}`); |
| 86 | + assert.equal(taskInfo?.name, name); |
| 87 | + await assertNoInjection(`getTaskInfo(${name})`); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + test("the command sent to the terminal runs the task and nothing else", async function () { |
| 92 | + if (process.platform === "win32") { |
| 93 | + // the generated command targets powershell, sh cannot evaluate it |
| 94 | + this.skip(); |
| 95 | + } |
| 96 | + |
| 97 | + for (const { name, marker } of INJECTING_TASKS) { |
| 98 | + const command = miseService.createMiseCommand(["run", name]); |
| 99 | + assert.ok(command, "a command should be built"); |
| 100 | + |
| 101 | + const { stdout } = await execAsync(command as string, { |
| 102 | + cwd: workspaceRoot, |
| 103 | + }); |
| 104 | + |
| 105 | + assert.ok( |
| 106 | + stdout.includes(marker), |
| 107 | + `expected ${marker} in the output of ${name}, got: ${stdout}`, |
| 108 | + ); |
| 109 | + await assertNoInjection(`run '${name}'`); |
| 110 | + } |
| 111 | + }); |
| 112 | + |
| 113 | + test("task arguments are passed through without reaching the shell", async function () { |
| 114 | + if (process.platform === "win32") { |
| 115 | + this.skip(); |
| 116 | + } |
| 117 | + |
| 118 | + const command = miseService.createMiseCommand([ |
| 119 | + "run", |
| 120 | + "echo-ok", |
| 121 | + "--", |
| 122 | + "$(touch pwned-arg)", |
| 123 | + ]); |
| 124 | + assert.ok(command, "a command should be built"); |
| 125 | + |
| 126 | + const { stdout } = await execAsync(command as string, { |
| 127 | + cwd: workspaceRoot, |
| 128 | + }); |
| 129 | + |
| 130 | + assert.ok(stdout.includes("marker-ok"), `unexpected output: ${stdout}`); |
| 131 | + await assertNoInjection("run with an injected argument"); |
| 132 | + }); |
| 133 | + |
| 134 | + test("tasks executed through the vscode tasks api stay a single argument", async () => { |
| 135 | + const tasks = await vscode.tasks.fetchTasks({ type: "mise" }); |
| 136 | + |
| 137 | + for (const { name } of INJECTING_TASKS) { |
| 138 | + const task = tasks.find((t) => t.name === name); |
| 139 | + assert.ok(task, `task ${name} should be provided to vscode`); |
| 140 | + |
| 141 | + const exitCode = await runVsCodeTask(task as vscode.Task); |
| 142 | + |
| 143 | + assert.equal(exitCode, 0, `task ${name} should succeed`); |
| 144 | + await assertNoInjection(`vscode task '${name}'`); |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + test("tool actions running in a terminal quote their arguments", async () => { |
| 149 | + // mise exits non-zero on the unknown setting, the point is that the |
| 150 | + // payload in the name must not be executed by the task shell |
| 151 | + await miseService.runMiseToolActionInConsole( |
| 152 | + ["settings", "get", "x; touch pwned-console #"], |
| 153 | + "injection-test", |
| 154 | + ); |
| 155 | + |
| 156 | + await assertNoInjection("runMiseToolActionInConsole"); |
| 157 | + }); |
| 158 | + |
| 159 | + test("tool lookups do not execute hostile tool names", async () => { |
| 160 | + const toolName = "x; touch pwned-tool #"; |
| 161 | + |
| 162 | + assert.equal(await miseService.which(toolName), undefined); |
| 163 | + await assertNoInjection("which"); |
| 164 | + |
| 165 | + await miseService.binPaths(toolName).catch(() => []); |
| 166 | + await assertNoInjection("binPaths"); |
| 167 | + |
| 168 | + // mise rejects the unknown setting instead of the payload being run |
| 169 | + await assert.rejects(() => |
| 170 | + miseService.getSetting("x; touch pwned-setting #"), |
| 171 | + ); |
| 172 | + await assertNoInjection("getSetting"); |
| 173 | + }); |
| 174 | + |
| 175 | + test("environment variables are written verbatim, without being evaluated", async () => { |
| 176 | + const envFilePath = path.join(workspaceRoot, "mise.injection.toml"); |
| 177 | + await writeFile(envFilePath, ""); |
| 178 | + |
| 179 | + try { |
| 180 | + await miseService.miseSetEnv({ |
| 181 | + filePath: envFilePath, |
| 182 | + name: "INJECTED", |
| 183 | + value: '$(touch pwned-env) "; touch pwned-env2 #', |
| 184 | + }); |
| 185 | + |
| 186 | + const content = await readFile(envFilePath, "utf8"); |
| 187 | + assert.ok( |
| 188 | + content.includes("touch pwned-env"), |
| 189 | + `the value should be stored as written, got: ${content}`, |
| 190 | + ); |
| 191 | + await assertNoInjection("miseSetEnv"); |
| 192 | + } finally { |
| 193 | + await rm(envFilePath, { force: true }); |
| 194 | + } |
| 195 | + }); |
| 196 | +}); |
| 197 | + |
| 198 | +function runVsCodeTask(task: vscode.Task): Promise<number | undefined> { |
| 199 | + return new Promise<number | undefined>((resolve, reject) => { |
| 200 | + const disposable = vscode.tasks.onDidEndTaskProcess((event) => { |
| 201 | + if (event.execution.task === task) { |
| 202 | + disposable.dispose(); |
| 203 | + resolve(event.exitCode); |
| 204 | + } |
| 205 | + }); |
| 206 | + |
| 207 | + vscode.tasks.executeTask(task).then(undefined, (error) => { |
| 208 | + disposable.dispose(); |
| 209 | + reject(error); |
| 210 | + }); |
| 211 | + }); |
| 212 | +} |
0 commit comments