diff options
| author | Jerop Kipruto <[email protected]> | 2025-06-30 11:42:35 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-30 15:42:35 +0000 |
| commit | f3849627fccd610d3701575a7cf711ea0ef7b065 (patch) | |
| tree | 7415a62fd164d76d24d56f6281a02dceb9c87dda /packages/core/src/tools/shell.test.ts | |
| parent | 770f862832dfef477705bee69bd2a84397d105a8 (diff) | |
feat(shell): Enable prefix matching for flexible command validation (#2653)
Diffstat (limited to 'packages/core/src/tools/shell.test.ts')
| -rw-r--r-- | packages/core/src/tools/shell.test.ts | 167 |
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); + }); }); |
