Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
25 changes: 25 additions & 0 deletions packages/core/src/permissions/permission-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,31 @@ describe('splitCompoundCommand', () => {
},
);

// #11851: bash only treats space, tab and newline as word separators. `\r`,
// `\v`, `\f` and `\u00a0` are ordinary word characters to bash, so in
// `echo x >\r& rm …` the redirection target is the `\r` and the `&` is the
// async operator — bash runs two commands. When the scan skipped those four
// as if they were whitespace, both halves stayed in one segment and the
// first command's allow rule covered the second.
it.each([
['echo x >\r& rm -rf /tmp/x'],
['echo x >\v& rm -rf /tmp/x'],
['echo x >\f& rm -rf /tmp/x'],
['echo x >\u00a0& rm -rf /tmp/x'],
// Spaced variant: real separators around the non-IFS character.
['echo x > \r & rm -rf /tmp/x'],
])(
'splits %s, where a non-IFS "whitespace" sits between the > and the &',
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
async (command) => {
// Segments are trimmed, and `String.prototype.trim` also strips these
// characters, so the first segment ends at the bare `>`.
expect(splitCompoundCommand(command)).toEqual([
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
'echo x >',
'rm -rf /tmp/x',
]);
},
);

// Over-correction guard: the longer operators must keep winning over the
// bare `&`, so these two pass both before and after the change.
it.each([
Expand Down
16 changes: 15 additions & 1 deletion packages/core/src/permissions/rule-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,15 @@ export function buildHumanReadableRuleLabel(rules: string[]): string {
*/
const SHELL_OPERATORS = ['&&', '||', ';;', '|&', '|', ';', '&', '\n'];

/**
* The characters bash treats as word separators (its default `IFS`): space,
* tab and newline. JavaScript's `\s` also matches `\r`, `\v`, `\f`,
* `\u00a0` and other Unicode whitespace, none of which bash treats as
* separators — bash takes such a character as part of the neighbouring word
* instead.
*/
const BASH_WORD_SEPARATORS = [' ', '\t', '\n'];
Comment thread
yiliang114 marked this conversation as resolved.
Resolved

/**
* Count the consecutive backslashes immediately before `index`.
*
Expand Down Expand Up @@ -844,14 +853,19 @@ function precedingBackslashCount(command: string, index: number): number {
* the `echo` and then run the `rm`. Reading that `\>` as a redirection would
* keep both halves in one segment and let the `echo`'s allow rule cover the
* `rm`.
*
* The scan skips only {@link BASH_WORD_SEPARATORS}: with `\s` it also skipped
* `\r`/`\v`/`\f`/`\u00a0`, which bash treats as ordinary word characters, so
* `echo x >\r& rm -rf /` collapsed into one segment whose `echo x` allow rule
* covered the `rm` (#11851).
*/
function isAsyncOperator(command: string, index: number): boolean {
if (command[index + 1] === '>') {
return false;
}
for (let j = index - 1; j >= 0; j--) {
const ch = command[j]!;
if (/\s/.test(ch)) {
if (BASH_WORD_SEPARATORS.includes(ch)) {
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
continue;
}
if (ch === '>' || ch === '<') {
Expand Down
Loading