Skip to content

Commit 0fcf830

Browse files
MiyaMiya
authored andcommitted
fix(exec): splice shell line continuations before allowlist analysis
1 parent 6bc0dc8 commit 0fcf830

2 files changed

Lines changed: 94 additions & 8 deletions

File tree

‎src/infra/exec-approvals-allowlist.ts‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,43 @@ function hasShellLineContinuation(command: string): boolean {
4747
return /\\(?:\r\n|\n|\r)/.test(command);
4848
}
4949

50+
function spliceShellLineContinuations(command: string): string {
51+
let result = "";
52+
let quote: "single" | "double" | null = null;
53+
54+
for (let index = 0; index < command.length; index += 1) {
55+
const char = command[index];
56+
const next = command[index + 1];
57+
58+
if (char === "\\" && quote !== "single") {
59+
if (next === "\r" || next === "\n") {
60+
index += next === "\r" && command[index + 2] === "\n" ? 2 : 1;
61+
continue;
62+
}
63+
if (quote === "double" && next && ['"', "`", "$", "\\"].includes(next)) {
64+
result += char + next;
65+
index += 1;
66+
continue;
67+
}
68+
if (quote === null && next) {
69+
result += char + next;
70+
index += 1;
71+
continue;
72+
}
73+
}
74+
75+
if (char === "'" && quote !== "double") {
76+
quote = quote === "single" ? null : "single";
77+
} else if (char === '"' && quote !== "single") {
78+
quote = quote === "double" ? null : "double";
79+
}
80+
81+
result += char;
82+
}
83+
84+
return result;
85+
}
86+
5087
export function normalizeSafeBins(entries?: readonly string[]): Set<string> {
5188
if (!Array.isArray(entries)) {
5289
return new Set();
@@ -1092,18 +1129,21 @@ export function evaluateShellAllowlist(
10921129
segmentSatisfiedBy: [],
10931130
});
10941131

1095-
// Keep allowlist analysis conservative: line-continuation semantics are shell-dependent
1096-
// and can rewrite token boundaries at runtime.
1097-
if (hasShellLineContinuation(params.command)) {
1098-
return analysisFailure();
1099-
}
1132+
// Splice backslash-newline line continuations before analysis so that
1133+
// multi-line commands (e.g. `git \<newline> commit`) are evaluated against
1134+
// the allowlist rather than unconditionally rejected. Commands with dangerous
1135+
// constructs embedded in continuations (e.g. `"$\<newline>(id)"`) still fail
1136+
// at the parser level, preserving the existing safety contract.
1137+
const command = hasShellLineContinuation(params.command)
1138+
? spliceShellLineContinuations(params.command)
1139+
: params.command;
11001140

11011141
const chainParts = isWindowsPlatform(params.platform)
11021142
? null
1103-
: splitCommandChainWithOperators(params.command);
1143+
: splitCommandChainWithOperators(command);
11041144
if (!chainParts) {
11051145
const analysis = analyzeShellCommand({
1106-
command: params.command,
1146+
command,
11071147
cwd: params.cwd,
11081148
env: params.env,
11091149
platform: params.platform,

‎src/infra/exec-approvals-analysis.test.ts‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,9 @@ describe("exec approvals shell analysis", () => {
698698
},
699699
);
700700

701-
it("fails allowlist analysis for shell line continuations", () => {
701+
it("fails allowlist analysis when a spliced line continuation produces an unsafe construct", () => {
702+
// After splicing `\<newline>`, this becomes `echo "ok $(id -u)"` which
703+
// the parser rejects as an unsupported shell substitution.
702704
const result = evaluateShellAllowlist({
703705
command: 'echo "ok $\\\n(id -u)"',
704706
allowlist: [{ pattern: "/usr/bin/echo" }],
@@ -709,6 +711,50 @@ describe("exec approvals shell analysis", () => {
709711
expect(result.allowlistSatisfied).toBe(false);
710712
});
711713

714+
it("satisfies allowlist for safe backslash-newline line continuations", () => {
715+
const result = evaluateShellAllowlist({
716+
command: "/usr/bin/git \\\n commit \\\n -m 'msg'",
717+
allowlist: [{ pattern: "/usr/bin/git" }],
718+
safeBins: new Set(),
719+
cwd: "/tmp",
720+
});
721+
expect(result.analysisOk).toBe(true);
722+
expect(result.allowlistSatisfied).toBe(true);
723+
});
724+
725+
it("does not splice backslash-newline inside single quotes", () => {
726+
const result = evaluateShellAllowlist({
727+
command: "/usr/bin/git commit -m 'fix: foo\\\nbar'",
728+
allowlist: [{ pattern: "/usr/bin/git" }],
729+
safeBins: new Set(),
730+
cwd: "/tmp",
731+
});
732+
expect(result.analysisOk).toBe(true);
733+
expect(result.allowlistSatisfied).toBe(true);
734+
});
735+
736+
it("marks analysisOk true but allowlistSatisfied false for unapproved binary after splicing", () => {
737+
const result = evaluateShellAllowlist({
738+
command: "/usr/bin/curl \\\n https://example.com",
739+
allowlist: [],
740+
safeBins: new Set(),
741+
cwd: "/tmp",
742+
});
743+
expect(result.analysisOk).toBe(true);
744+
expect(result.allowlistSatisfied).toBe(false);
745+
});
746+
747+
it("evaluates chain operators exposed by line continuation splicing", () => {
748+
const result = evaluateShellAllowlist({
749+
command: "/usr/bin/echo ok \\\n&& /usr/bin/rm -rf /",
750+
allowlist: [{ pattern: "/usr/bin/echo" }],
751+
safeBins: new Set(),
752+
cwd: "/tmp",
753+
});
754+
expect(result.analysisOk).toBe(true);
755+
expect(result.allowlistSatisfied).toBe(false);
756+
});
757+
712758
it("satisfies allowlist when bare * wildcard is present", () => {
713759
const dir = makeTempDir();
714760
const binPath = path.join(dir, "mybin");

0 commit comments

Comments
 (0)