fix: harden exec allowlist parsing

This commit is contained in:
Peter Steinberger
2026-02-02 16:53:09 -08:00
parent fff59da962
commit d1ecb46076
3 changed files with 77 additions and 0 deletions

View File

@@ -131,6 +131,36 @@ describe("exec approvals shell parsing", () => {
expect(res.ok).toBe(true);
expect(res.segments[0]?.argv).toEqual(["/bin/echo", "ok"]);
});
it("rejects command substitution inside double quotes", () => {
const res = analyzeShellCommand({ command: 'echo "output: $(whoami)"' });
expect(res.ok).toBe(false);
expect(res.reason).toBe("unsupported shell token: $()");
});
it("rejects backticks inside double quotes", () => {
const res = analyzeShellCommand({ command: 'echo "output: `id`"' });
expect(res.ok).toBe(false);
expect(res.reason).toBe("unsupported shell token: `");
});
it("rejects command substitution outside quotes", () => {
const res = analyzeShellCommand({ command: "echo $(whoami)" });
expect(res.ok).toBe(false);
expect(res.reason).toBe("unsupported shell token: $()");
});
it("allows escaped command substitution inside double quotes", () => {
const res = analyzeShellCommand({ command: 'echo "output: \\$(whoami)"' });
expect(res.ok).toBe(true);
expect(res.segments[0]?.argv[0]).toBe("echo");
});
it("allows command substitution syntax inside single quotes", () => {
const res = analyzeShellCommand({ command: "echo 'output: $(whoami)'" });
expect(res.ok).toBe(true);
expect(res.segments[0]?.argv[0]).toBe("echo");
});
});
describe("exec approvals shell allowlist (chained commands)", () => {
@@ -185,6 +215,18 @@ describe("exec approvals shell allowlist (chained commands)", () => {
expect(result.analysisOk).toBe(true);
expect(result.allowlistSatisfied).toBe(true);
});
it("respects escaped quotes when splitting chains", () => {
const allowlist: ExecAllowlistEntry[] = [{ pattern: "/usr/bin/echo" }];
const result = evaluateShellAllowlist({
command: '/usr/bin/echo "foo\\" && bar"',
allowlist,
safeBins: new Set(),
cwd: "/tmp",
});
expect(result.analysisOk).toBe(true);
expect(result.allowlistSatisfied).toBe(true);
});
});
describe("exec approvals safe bins", () => {