summaryrefslogtreecommitdiff
path: root/packages/core/src/tools/shell.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/core/src/tools/shell.test.ts')
-rw-r--r--packages/core/src/tools/shell.test.ts167
1 files changed, 167 insertions, 0 deletions
diff --git a/packages/core/src/tools/shell.test.ts b/packages/core/src/tools/shell.test.ts
index 2cbd0ff4..936c1f77 100644
--- a/packages/core/src/tools/shell.test.ts
+++ b/packages/core/src/tools/shell.test.ts
@@ -168,4 +168,171 @@ describe('ShellTool', () => {
const isAllowed = shellTool.isCommandAllowed('rm -rf /');
expect(isAllowed).toBe(false);
});
+
+ it('should allow a command that starts with an allowed command prefix', async () => {
+ const config = {
+ getCoreTools: () => ['ShellTool(gh issue edit)'],
+ getExcludeTools: () => [],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed(
+ 'gh issue edit 1 --add-label "kind/feature"',
+ );
+ expect(isAllowed).toBe(true);
+ });
+
+ it('should allow a command that starts with an allowed command prefix using the public-facing name', async () => {
+ const config = {
+ getCoreTools: () => ['run_shell_command(gh issue edit)'],
+ getExcludeTools: () => [],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed(
+ 'gh issue edit 1 --add-label "kind/feature"',
+ );
+ expect(isAllowed).toBe(true);
+ });
+
+ it('should not allow a command that starts with an allowed command prefix but is chained with another command', async () => {
+ const config = {
+ getCoreTools: () => ['run_shell_command(gh issue edit)'],
+ getExcludeTools: () => [],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('gh issue edit&&rm -rf /');
+ expect(isAllowed).toBe(false);
+ });
+
+ it('should not allow a command that is a prefix of an allowed command', async () => {
+ const config = {
+ getCoreTools: () => ['run_shell_command(gh issue edit)'],
+ getExcludeTools: () => [],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('gh issue');
+ expect(isAllowed).toBe(false);
+ });
+
+ it('should not allow a command that is a prefix of a blocked command', async () => {
+ const config = {
+ getCoreTools: () => [],
+ getExcludeTools: () => ['run_shell_command(gh issue edit)'],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('gh issue');
+ expect(isAllowed).toBe(true);
+ });
+
+ it('should not allow a command that is chained with a pipe', async () => {
+ const config = {
+ getCoreTools: () => ['run_shell_command(gh issue list)'],
+ getExcludeTools: () => [],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('gh issue list | rm -rf /');
+ expect(isAllowed).toBe(false);
+ });
+
+ it('should not allow a command that is chained with a semicolon', async () => {
+ const config = {
+ getCoreTools: () => ['run_shell_command(gh issue list)'],
+ getExcludeTools: () => [],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('gh issue list; rm -rf /');
+ expect(isAllowed).toBe(false);
+ });
+
+ it('should block a chained command if any part is blocked', async () => {
+ const config = {
+ getCoreTools: () => ['run_shell_command(echo "hello")'],
+ getExcludeTools: () => ['run_shell_command(rm)'],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('echo "hello" && rm -rf /');
+ expect(isAllowed).toBe(false);
+ });
+
+ it('should block a command if its prefix is on the blocklist, even if the command itself is on the allowlist', async () => {
+ const config = {
+ getCoreTools: () => ['run_shell_command(git push)'],
+ getExcludeTools: () => ['run_shell_command(git)'],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('git push');
+ expect(isAllowed).toBe(false);
+ });
+
+ it('should be case-sensitive in its matching', async () => {
+ const config = {
+ getCoreTools: () => ['run_shell_command(echo)'],
+ getExcludeTools: () => [],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('ECHO "hello"');
+ expect(isAllowed).toBe(false);
+ });
+
+ it('should correctly handle commands with extra whitespace around chaining operators', async () => {
+ const config = {
+ getCoreTools: () => ['run_shell_command(ls -l)'],
+ getExcludeTools: () => ['run_shell_command(rm)'],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('ls -l ; rm -rf /');
+ expect(isAllowed).toBe(false);
+ });
+
+ it('should allow a chained command if all parts are allowed', async () => {
+ const config = {
+ getCoreTools: () => [
+ 'run_shell_command(echo)',
+ 'run_shell_command(ls -l)',
+ ],
+ getExcludeTools: () => [],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('echo "hello" && ls -l');
+ expect(isAllowed).toBe(true);
+ });
+
+ it('should block a command with command substitution using backticks', async () => {
+ const config = {
+ getCoreTools: () => ['run_shell_command(echo)'],
+ getExcludeTools: () => [],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('echo `rm -rf /`');
+ expect(isAllowed).toBe(false);
+ });
+
+ it('should block a command with command substitution using $()', async () => {
+ const config = {
+ getCoreTools: () => ['run_shell_command(echo)'],
+ getExcludeTools: () => [],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('echo $(rm -rf /)');
+ expect(isAllowed).toBe(false);
+ });
+
+ it('should allow a command with I/O redirection', async () => {
+ const config = {
+ getCoreTools: () => ['run_shell_command(echo)'],
+ getExcludeTools: () => [],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('echo "hello" > file.txt');
+ expect(isAllowed).toBe(true);
+ });
+
+ it('should not allow a command that is chained with a double pipe', async () => {
+ const config = {
+ getCoreTools: () => ['run_shell_command(gh issue list)'],
+ getExcludeTools: () => [],
+ } as unknown as Config;
+ const shellTool = new ShellTool(config);
+ const isAllowed = shellTool.isCommandAllowed('gh issue list || rm -rf /');
+ expect(isAllowed).toBe(false);
+ });
});