|
1 |
| -import { executeSync } from '../../lib/dependencies/sub-process'; |
| 1 | +import { executeSync, execute } from '../../lib/dependencies/sub-process'; |
2 | 2 |
|
3 | 3 | describe('Test sub-process.ts', () => {
|
4 | 4 | it('test restoring proxy setting in executeSync()', async () => {
|
@@ -89,4 +89,44 @@ describe('Test sub-process.ts', () => {
|
89 | 89 | );
|
90 | 90 | expect(output.stdout.toString().trim()).toEqual(expectedNoProxy);
|
91 | 91 | });
|
| 92 | + |
| 93 | + describe('Security: Command injection protection', () => { |
| 94 | + it('should prevent command injection in executeSync()', () => { |
| 95 | + // Test that malicious command strings are treated as literal filenames (not executed) |
| 96 | + const maliciousCommand = 'python3; echo injected'; |
| 97 | + const result = executeSync(maliciousCommand, ['--version']); |
| 98 | + |
| 99 | + // Should fail with ENOENT because 'python3; echo injected' is not a valid executable |
| 100 | + expect(result.status).not.toBe(0); |
| 101 | + expect((result.error as any)?.code).toBe('ENOENT'); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should prevent command injection in execute()', async () => { |
| 105 | + // Test that malicious command strings are treated as literal filenames (not executed) |
| 106 | + const maliciousCommand = 'python3; whoami; echo injected'; |
| 107 | + |
| 108 | + try { |
| 109 | + await execute(maliciousCommand, ['--version']); |
| 110 | + fail('Expected execute() to reject with an error'); |
| 111 | + } catch (error: any) { |
| 112 | + // Should fail with ENOENT because the malicious command is treated as a literal filename |
| 113 | + expect(error.code).toBe('ENOENT'); |
| 114 | + expect(error.syscall).toBe(`spawn ${maliciousCommand}`); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + it('should execute legitimate commands normally', async () => { |
| 119 | + // Verify that normal commands still work correctly |
| 120 | + const result = await execute('python3', ['--version']); |
| 121 | + expect(result).toMatch(/Python \d+\.\d+\.\d+/); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should handle arguments with special characters safely', async () => { |
| 125 | + // Verify that special characters in arguments don't enable injection |
| 126 | + const result = await execute('python3', ['--version', '; echo injected']); |
| 127 | + // Should only show Python version, not execute the injected command |
| 128 | + expect(result).toMatch(/Python \d+\.\d+\.\d+/); |
| 129 | + expect(result).not.toMatch(/injected/); |
| 130 | + }); |
| 131 | + }); |
92 | 132 | });
|
0 commit comments