Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
fc5cb28
fix(core): read a backslash inside single quotes as literal when spli…
TianYuan1024 Sep 13, 2026
dd80fb9
Merge remote-tracking branch 'origin/main' into fix/permission-single…
TianYuan1024 Sep 13, 2026
a122d7a
fix(core): keep the escape inside bash's ANSI-C quotes when splitting
TianYuan1024 Sep 13, 2026
06b3591
Merge remote-tracking branch 'origin/main' into fix/permission-single…
TianYuan1024 Sep 13, 2026
0974d13
test(core): cover mixed quote forms on one line when splitting commands
TianYuan1024 Sep 13, 2026
29ddd0a
Merge remote-tracking branch 'origin/main' into fix/permission-single…
TianYuan1024 Sep 13, 2026
2306f10
fix(core): elide a line continuation before deciding the quote form
TianYuan1024 Sep 14, 2026
e1d00ca
Merge remote-tracking branch 'origin/main' into fix/permission-single…
TianYuan1024 Sep 14, 2026
d2d63af
docs(core): correct why the continuation branch and ANSI-C quoting work
TianYuan1024 Sep 14, 2026
8af09af
Merge remote-tracking branch 'origin/main' into fix/permission-single…
TianYuan1024 Sep 14, 2026
413ea00
Merge remote-tracking branch 'origin/main' into fix/permission-single…
TianYuan1024 Sep 15, 2026
550a71b
fix(core): keep every boundary either backslash reading finds when sp…
TianYuan1024 Sep 15, 2026
f56192f
refactor(core): condense splitter comments and quote-handling tests
TianYuan1024 Sep 15, 2026
ee3a811
perf(core): skip the second scan when the command has no backslash
TianYuan1024 Sep 16, 2026
3eb322a
Merge remote-tracking branch 'origin/main' into fix/permission-single…
TianYuan1024 Sep 16, 2026
68d4df3
fix(core): trim only the whitespace bash's lexer discards when splitting
TianYuan1024 Sep 17, 2026
32e35ca
Merge remote-tracking branch 'origin/main' into fix/permission-single…
TianYuan1024 Sep 17, 2026
2d85029
Merge remote-tracking branch 'origin/main' into fix/permission-single…
TianYuan1024 Sep 19, 2026
16b0f00
Merge remote-tracking branch 'origin/main' into fix/permission-single…
TianYuan1024 Sep 19, 2026
f0bc4fc
test(core): pin the one-character reach of a pending `$`
TianYuan1024 Sep 19, 2026
e10f3c2
test(core): pin the false deny this split declares as its tradeoff
TianYuan1024 Sep 20, 2026
683e06b
Merge remote-tracking branch 'origin/main' into fix/permission-single…
TianYuan1024 Sep 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions packages/core/src/permissions/permission-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,72 @@ describe('splitCompoundCommand', () => {
expect(splitCompoundCommand('echo a \\&& b')).toEqual(['echo a \\&', 'b']);
});

// A backslash is literal inside single quotes, so `'a\'` is a CLOSED string
// and the operator after it is real structure. Reading the backslash as an
// escape swallowed the closing quote, held the scanner inside the quote to
// the end of input, and returned the whole line as one segment — so an
// `echo` allow rule authorised whatever followed. `bash -x` runs each of
// these as two commands.
it.each([
["echo 'a\\' ; touch /tmp/x", ["echo 'a\\'", 'touch /tmp/x']],
Comment thread
This conversation was marked as resolved.
Resolved
["echo 'a\\' && touch /tmp/x", ["echo 'a\\'", 'touch /tmp/x']],
["echo 'a\\' | sh", ["echo 'a\\'", 'sh']],
])(
'treats a backslash inside single quotes as literal in %s',
async (command, parts) => {
expect(splitCompoundCommand(command)).toEqual(parts);
},
);

it('still escapes a backslash inside double quotes', async () => {
// In double quotes a backslash does escape, so `"a\"` keeps the string
// open across the `;` and the whole thing is one command, matching bash.
expect(splitCompoundCommand('echo "a\\" ; touch /tmp/x"')).toEqual([
'echo "a\\" ; touch /tmp/x"',
]);
});

// The other half of the rule. `$'…'` is bash's ANSI-C quoting, where the
// backslash IS an escape, so `$'a\''` ends at its second quote and the
// operator after it still separates two commands. Reading these as plain
// single quotes swallows the real closing quote instead and glues the line
// back into one segment — the same bypass, entered from the other side.
it.each([
["echo $'a\\'' ; touch /tmp/x", ["echo $'a\\''", 'touch /tmp/x']],
["echo $'a\\'' && touch /tmp/x", ["echo $'a\\''", 'touch /tmp/x']],
["echo $'a\\'' | cat", ["echo $'a\\''", 'cat']],
])(
'escapes a backslash inside ANSI-C quotes in %s',
async (command, parts) => {
expect(splitCompoundCommand(command)).toEqual(parts);
},
);

// A `$` only opens ANSI-C quoting when it is itself unquoted, unescaped and
// not already spent: bash reads all three of these as plain `'…'` strings,
// so the backslash is literal and the quote closes. Treating them as ANSI-C
// re-opens the plain-quote bypass through a different door.
it.each([
["echo \\$'a\\' ; touch /tmp/x", ["echo \\$'a\\'", 'touch /tmp/x']],
Comment thread
This conversation was marked as resolved.
Resolved
["echo $$'a\\' ; touch /tmp/x", ["echo $$'a\\'", 'touch /tmp/x']],
['echo "$"\'a\\\' ; touch /tmp/x', ['echo "$"\'a\\\'', 'touch /tmp/x']],
])('reads %s as a plain single-quoted string', async (command, parts) => {
expect(splitCompoundCommand(command)).toEqual(parts);
});

// Which form a string has is decided independently for each string, so the
// two can be mixed on one line in either order, and `$$$'…'` is the PID
// expansion followed by a real ANSI-C string. Characterisation of shapes the
// cases above do not reach; bash runs every line here as two commands.
it.each([
["echo $'a\\'' 'c\\' ; echo B", ["echo $'a\\'' 'c\\'", 'echo B']],
["echo 'a' $'b\\'' ; echo B", ["echo 'a' $'b\\''", 'echo B']],
["echo $$$'a\\'' ; echo B", ["echo $$$'a\\''", 'echo B']],
["echo $'\\'' ; echo B", ["echo $'\\''", 'echo B']],
])('tracks the quote form per string in %s', async (command, parts) => {
expect(splitCompoundCommand(command)).toEqual(parts);
});

it('trims whitespace around sub-commands', async () => {
expect(splitCompoundCommand(' git status && rm -rf / ')).toEqual([
'git status',
Expand Down Expand Up @@ -2283,6 +2349,70 @@ describe('PermissionManager', () => {
).toBe('deny');
});

// A trailing backslash inside single quotes is literal in bash, so these
// are two commands and the second one is not covered by the echo rule.
// Reading that backslash as an escape kept the whole line in one segment,
// and the echo rule then authorised the command after the operator.
it('an allow rule does not reach past a single-quoted backslash', async () => {
pm = new PermissionManager(
makeConfig({ permissionsAllow: ['Bash(echo *)'] }),
);
pm.initialize();
expect(
await pm.evaluate({
toolName: 'run_shell_command',
command: "echo 'a\\' ; rm -rf /tmp/x",
}),
).toBe('ask');
});

it('a deny rule still applies past a single-quoted backslash', async () => {
pm = new PermissionManager(
makeConfig({
permissionsAllow: ['Bash(echo *)'],
permissionsDeny: ['Bash(rm *)'],
}),
);
pm.initialize();
expect(
await pm.evaluate({
toolName: 'run_shell_command',
command: "echo 'a\\' ; rm -rf /tmp/x",
}),
).toBe('deny');
});

// `$'a\''` is a complete ANSI-C word, so bash runs these as two commands
// just as it does the plain-quote carrier above.
it('an allow rule does not reach past an ANSI-C quoted carrier', async () => {
pm = new PermissionManager(
makeConfig({ permissionsAllow: ['Bash(echo *)'] }),
);
pm.initialize();
expect(
await pm.evaluate({
toolName: 'run_shell_command',
command: "echo $'a\\'' ; rm -rf /tmp/x",
}),
).toBe('ask');
});

it('a deny rule still applies past an ANSI-C quoted carrier', async () => {
pm = new PermissionManager(
makeConfig({
permissionsAllow: ['Bash(echo *)'],
permissionsDeny: ['Bash(rm *)'],
}),
);
pm.initialize();
expect(
await pm.evaluate({
toolName: 'run_shell_command',
command: "echo $'a\\'' ; rm -rf /tmp/x",
}),
).toBe('deny');
});

it('|| compound: all allowed → allow', async () => {
pm = new PermissionManager(
makeConfig({
Expand Down
28 changes: 27 additions & 1 deletion packages/core/src/permissions/rule-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,13 @@ export function splitCompoundCommandSegments(
const segments: CompoundCommandSegment[] = [];
let inSingle = false;
let inDouble = false;
// Whether the open single-quoted string is bash's ANSI-C form, `$'…'`, in
// which a backslash escapes — the opposite of a plain `'…'`, where it is a
// literal character.
let inAnsiC = false;
Comment thread
TianYuan1024 marked this conversation as resolved.
Resolved
// A `$` still able to introduce `$'…'`: unquoted, unescaped, and not
// already spent as the second half of the `$$` PID expansion.
let dollarPending = false;
let escaped = false;
let lastSplit = 0;
// Nesting depth of `$(( … ))` / `(( … ))`. Inside arithmetic a bare `&` is
Expand All @@ -872,16 +879,29 @@ export function splitCompoundCommandSegments(

for (let i = 0; i < command.length; i++) {
const ch = command[i]!;
// Consumed by this character: only a `$` immediately before an opening
// quote introduces `$'…'`.
const ansiCIntroducer: boolean = dollarPending;
dollarPending = false;
Comment thread
This conversation was marked as resolved.
Resolved

if (escaped) {
escaped = false;
continue;
}
if (ch === '\\') {
// A backslash is a literal character inside a plain `'…'` string and an
// escape everywhere else, ANSI-C `$'…'` included. Without the plain-quote
// exception, `echo 'a\' ; rm -rf x` reads the closing quote as escaped,
// stays inside the quote to the end of the input, and returns the whole
// line as one segment — so an `echo` allow rule ends up authorising the
// `rm`. Applying that exception to `$'…'` as well loses the same line the
// other way round: there the backslash really does escape, so `$'a\''`
// ends at its second quote and the operator after it still splits.
Comment thread
This conversation was marked as resolved.
Resolved
if (ch === '\\' && !(inSingle && !inAnsiC)) {
Comment thread
TianYuan1024 marked this conversation as resolved.
Resolved
Comment thread
TianYuan1024 marked this conversation as resolved.
Resolved
escaped = true;
continue;
}
if (ch === "'" && !inDouble) {
inAnsiC = inSingle ? false : ansiCIntroducer;
Comment thread
This conversation was marked as resolved.
Resolved
inSingle = !inSingle;
continue;
}
Expand All @@ -892,6 +912,12 @@ export function splitCompoundCommandSegments(
if (inSingle || inDouble) {
continue;
}
if (ch === '$') {
// `$$` expands to the PID and spends both characters, so the second one
// cannot open an ANSI-C string; `\$` and `"$"` never reach here.
dollarPending = !ansiCIntroducer;
continue;
}

if (ch === '(' && command[i + 1] === '(') {
arithmeticDepth++;
Expand Down
Loading