File size: 12,514 Bytes
fcd8223 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | /** Tests system.run allowlist planning, output truncation, and argv resolution. */
import { spawn } from "node:child_process";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { expectDefined } from "@openclaw/normalization-core";
import { describe, expect, it } from "vitest";
import { resolveExecApprovalsFromFile, type ExecCommandSegment } from "../infra/exec-approvals.js";
import { planShellAuthorization } from "../infra/exec-authorization-plan.js";
import { buildAuthorizedShellCommandFromPlan } from "../infra/exec-authorization-render.js";
import {
evaluateSystemRunAllowlist,
resolveSystemRunExecArgv,
} from "./invoke-system-run-allowlist.js";
function resolveWindowsShellExecArgv(segment: ExecCommandSegment) {
return resolveSystemRunExecArgv({
plannedAllowlistArgv: undefined,
argv: ["powershell.exe", "-Command", "safe --version"],
security: "allowlist",
isWindows: true,
policy: {
approvedByAsk: false,
analysisOk: true,
allowlistSatisfied: true,
},
shellCommand: "safe --version",
segments: [segment],
segmentSatisfiedBy: ["allowlist"],
authorizationPlan: undefined,
});
}
function runExecutable(params: {
argv: string[];
cwd: string;
env: NodeJS.ProcessEnv;
}): Promise<{ exitCode: number | null; stdout: string }> {
return new Promise((resolve, reject) => {
const child = spawn(
expectDefined(params.argv[0], "params.argv[0] test invariant"),
params.argv.slice(1),
{
cwd: params.cwd,
env: params.env,
stdio: ["ignore", "pipe", "pipe"],
windowsHide: true,
},
);
const stdout: Buffer[] = [];
child.stdout.on("data", (chunk: Buffer) => stdout.push(chunk));
child.once("error", reject);
child.once("close", (exitCode) => {
resolve({ exitCode, stdout: Buffer.concat(stdout).toString("utf8") });
});
});
}
describe("resolveSystemRunExecArgv", () => {
it("pins Windows shell execution to the resolved allowlisted executable", async () => {
const trustedExecutable = "C:\\trusted-bin\\safe.exe";
const result = await resolveWindowsShellExecArgv({
raw: "safe --version",
argv: ["safe", "--version"],
resolution: {
kind: "command",
execution: {
kind: "executable",
rawExecutable: "safe",
resolvedPath: trustedExecutable,
executableName: "safe.exe",
},
policy: {
kind: "executable",
rawExecutable: "safe",
resolvedPath: trustedExecutable,
executableName: "safe.exe",
},
},
});
expect(result).toEqual([trustedExecutable, "--version"]);
});
it("preserves unresolved Windows shell argv authorized by a bare wildcard", async () => {
const result = await resolveWindowsShellExecArgv({
raw: "safe --version",
argv: ["safe", "--version"],
resolution: null,
});
expect(result).toEqual(["safe", "--version"]);
});
it("fails closed for Windows opaque shell transports before inner argv rewrite", async () => {
const trustedExecutable = "C:\\trusted-bin\\safe-tool.exe";
const result = await resolveSystemRunExecArgv({
plannedAllowlistArgv: undefined,
argv: ["nu.exe", "--commands", "safe-tool arg"],
security: "allowlist",
isWindows: true,
policy: {
approvedByAsk: false,
analysisOk: true,
allowlistSatisfied: true,
},
shellCommand: "safe-tool arg",
segments: [
{
raw: "safe-tool arg",
argv: ["safe-tool", "arg"],
resolution: {
kind: "command",
execution: {
kind: "executable",
rawExecutable: "safe-tool",
resolvedPath: trustedExecutable,
executableName: "safe-tool.exe",
},
policy: {
kind: "executable",
rawExecutable: "safe-tool",
resolvedPath: trustedExecutable,
executableName: "safe-tool.exe",
},
},
},
],
segmentSatisfiedBy: ["allowlist"],
authorizationPlan: undefined,
});
expect(result).toBeNull();
});
it("fails closed when the Windows shell execution plan is blocked", async () => {
const result = await resolveWindowsShellExecArgv({
raw: "safe --version",
argv: ["safe", "--version"],
resolution: {
kind: "command",
policyBlocked: true,
execution: {
kind: "executable",
rawExecutable: "safe",
executableName: "safe",
},
policy: {
kind: "executable",
rawExecutable: "safe",
executableName: "safe",
},
},
});
expect(result).toBeNull();
});
it.runIf(process.platform === "win32")(
"executes the allowlisted path instead of a workspace shadow executable",
async () => {
const fixtureRoot = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-windows-shadow-"));
const trustedBin = path.join(fixtureRoot, "trusted-bin");
const workspace = path.join(fixtureRoot, "workspace");
fs.mkdirSync(trustedBin);
fs.mkdirSync(workspace);
const trustedExecutable = path.join(trustedBin, "safe.exe");
const shadowExecutable = path.join(workspace, "safe.exe");
const systemRoot = process.env.SystemRoot ?? process.env.WINDIR ?? "C:\\Windows";
fs.copyFileSync(path.join(systemRoot, "System32", "cmd.exe"), trustedExecutable);
fs.copyFileSync(path.join(systemRoot, "System32", "where.exe"), shadowExecutable);
const env = {
...process.env,
PATH: `${trustedBin}${path.delimiter}${process.env.PATH ?? ""}`,
PATHEXT: process.env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD",
};
const shellCommand = "safe /d /s /c echo TRUSTED_EXECUTABLE";
const commandTail = ["/d", "/s", "/c", "echo", "TRUSTED_EXECUTABLE"];
try {
const bareResult = await runExecutable({
argv: ["safe", ...commandTail],
cwd: workspace,
env,
});
expect(bareResult.exitCode).not.toBe(0);
expect(bareResult.stdout).not.toContain("TRUSTED_EXECUTABLE");
const approvals = resolveExecApprovalsFromFile({
agentId: "main",
file: {
version: 1,
defaults: { security: "allowlist", ask: "off", askFallback: "deny" },
agents: { main: { allowlist: [{ pattern: trustedExecutable }] } },
},
});
const analysis = await evaluateSystemRunAllowlist({
shellCommand,
argv: ["powershell.exe", "-Command", shellCommand],
approvals,
security: "allowlist",
safeBins: new Set(),
safeBinProfiles: {},
trustedSafeBinDirs: new Set(),
cwd: workspace,
env,
skillBins: [],
autoAllowSkills: false,
});
expect(analysis.analysisOk).toBe(true);
expect(analysis.allowlistSatisfied).toBe(true);
const execArgv = await resolveSystemRunExecArgv({
plannedAllowlistArgv: undefined,
argv: ["powershell.exe", "-Command", shellCommand],
security: "allowlist",
isWindows: true,
policy: {
approvedByAsk: false,
analysisOk: analysis.analysisOk,
allowlistSatisfied: analysis.allowlistSatisfied,
},
shellCommand,
segments: analysis.segments,
segmentSatisfiedBy: analysis.segmentSatisfiedBy,
authorizationPlan: analysis.authorizationPlan,
});
expect(execArgv?.[0]).toBe(fs.realpathSync(trustedExecutable));
const fixedResult = await runExecutable({ argv: execArgv ?? [], cwd: workspace, env });
expect(fixedResult.exitCode).toBe(0);
expect(fixedResult.stdout).toContain("TRUSTED_EXECUTABLE");
} finally {
fs.rmSync(fixtureRoot, { recursive: true, force: true });
}
},
);
it.runIf(process.platform !== "win32")(
"fails closed when shell rewriting has no authorization plan",
async () => {
const result = await resolveSystemRunExecArgv({
plannedAllowlistArgv: undefined,
argv: ["/bin/sh", "-lc", "head -c 16"],
security: "allowlist",
isWindows: false,
policy: {
approvedByAsk: false,
analysisOk: true,
allowlistSatisfied: true,
},
shellCommand: "head -c 16",
segments: [],
segmentSatisfiedBy: ["safeBins"],
authorizationPlan: undefined,
});
expect(result).toBeNull();
},
);
it.runIf(process.platform !== "win32")(
"returns rebuilt shell argv when the authorization plan supports rewriting",
async () => {
const env = { PATH: "/usr/bin:/bin" };
const authorizationPlan = await planShellAuthorization({
command: "head -c 16",
env,
platform: process.platform,
});
expect(authorizationPlan.ok).toBe(true);
if (!authorizationPlan.ok) {
throw new Error(authorizationPlan.reason);
}
const segmentSatisfiedBy: ["safeBins"] = ["safeBins"];
const expectedCommand = buildAuthorizedShellCommandFromPlan({
plan: authorizationPlan,
mode: "safeBins",
segmentSatisfiedBy,
});
expect(expectedCommand.ok).toBe(true);
if (!expectedCommand.ok) {
throw new Error(expectedCommand.reason);
}
const result = await resolveSystemRunExecArgv({
plannedAllowlistArgv: undefined,
argv: ["/bin/sh", "-lc", "head -c 16"],
security: "allowlist",
isWindows: false,
policy: {
approvedByAsk: false,
analysisOk: true,
allowlistSatisfied: true,
},
shellCommand: "head -c 16",
segments: authorizationPlan.groups.flatMap((group) =>
group.candidates.map((candidate) => candidate.sourceSegment),
),
segmentSatisfiedBy,
authorizationPlan,
});
expect(result).not.toBeNull();
expect(result?.[0]).toBe("/bin/sh");
expect(result?.[2]).toBe(expectedCommand.command);
},
);
it.runIf(process.platform !== "win32")(
"fails closed instead of rewriting opaque shell transports",
async () => {
const env = { PATH: "/usr/bin:/bin" };
const authorizationPlan = await planShellAuthorization({
command: "head -c 16",
env,
platform: process.platform,
});
expect(authorizationPlan.ok).toBe(true);
if (!authorizationPlan.ok) {
throw new Error(authorizationPlan.reason);
}
const result = await resolveSystemRunExecArgv({
plannedAllowlistArgv: undefined,
argv: ["nu", "--commands", "head -c 16"],
security: "allowlist",
isWindows: false,
policy: {
approvedByAsk: false,
analysisOk: true,
allowlistSatisfied: true,
},
shellCommand: "head -c 16",
segments: authorizationPlan.groups.flatMap((group) =>
group.candidates.map((candidate) => candidate.sourceSegment),
),
segmentSatisfiedBy: ["safeBins"],
authorizationPlan,
});
expect(result).toBeNull();
},
);
it.runIf(process.platform !== "win32")(
"fails closed when opaque shell transports use inner allowlist authorization",
async () => {
const env = { PATH: "/usr/bin:/bin" };
const authorizationPlan = await planShellAuthorization({
command: "head -c 16",
env,
platform: process.platform,
});
expect(authorizationPlan.ok).toBe(true);
if (!authorizationPlan.ok) {
throw new Error(authorizationPlan.reason);
}
const result = await resolveSystemRunExecArgv({
plannedAllowlistArgv: undefined,
argv: ["nu", "--commands", "head -c 16"],
security: "allowlist",
isWindows: false,
policy: {
approvedByAsk: false,
analysisOk: true,
allowlistSatisfied: true,
},
shellCommand: "head -c 16",
segments: authorizationPlan.groups.flatMap((group) =>
group.candidates.map((candidate) => candidate.sourceSegment),
),
segmentSatisfiedBy: ["allowlist"],
authorizationPlan,
});
expect(result).toBeNull();
},
);
});
|