Improves dependency checking security

Replaces `child_process.exec` with `child_process.spawn` for both asynchronous and synchronous dependency checks.
This change mitigates potential command injection vulnerabilities by passing arguments separately, enhancing the security of the application.
This commit is contained in:
AlexKucera committed 2025-09-03 22:33:29 +02:00
1 parent fcb62d5025
commit f00bc88bb2
1 file changed
+35 -10
+35 -10
View File
@@ -47,16 +47,41 @@ export class DependencyChecker {
executablePath: string, executablePath: string,
versionCommand: string, versionCommand: string,
versionRegex: RegExp versionRegex: RegExp
): Promise<DependencyInfo> { ): Promise<DependencyInfo> {
const { exec } = require('child_process'); const { spawn } = require('child_process');
const { promisify } = require('util');
const execAsync = promisify(exec);
try { try {
const { stdout } = await execAsync(`${executablePath} ${versionCommand}`, { // Use spawn instead of exec for security - arguments passed separately
const stdout = await new Promise<string>((resolve, reject) => {
const checkProcess = spawn(executablePath, versionCommand.split(' '), {
stdio: ['pipe', 'pipe', 'pipe'],
env: this.getAugmentedEnv() env: this.getAugmentedEnv()
}); });
let output = '';
let error = '';
checkProcess.stdout?.on('data', (data) => {
output += data.toString();
});
checkProcess.stderr?.on('data', (data) => {
error += data.toString();
});
checkProcess.on('close', (code) => {
if (code === 0) {
resolve(output);
} else {
reject(new Error(`Command failed with code ${code}: ${error}`));
}
});
checkProcess.on('error', (err) => {
reject(new Error(`Failed to spawn process: ${err.message}`));
});
});
const match = stdout.match(versionRegex); const match = stdout.match(versionRegex);
const version = match ? match[1] : null; const version = match ? match[1] : null;
@@ -74,24 +99,24 @@ export class DependencyChecker {
executablePath executablePath
}; };
} }
} }
/** /**
* Check if a dependency is available (synchronous version for startup) * Check if a dependency is available (synchronous version for startup)
*/ */
private static checkDependencySync(executablePath: string, versionCommand: string): boolean { private static checkDependencySync(executablePath: string, versionCommand: string): boolean {
const { execSync } = require('child_process'); const { spawnSync } = require('child_process');
try { try {
execSync(`${executablePath} ${versionCommand}`, { const result = spawnSync(executablePath, versionCommand.split(' '), {
encoding: 'utf8', encoding: 'utf8',
env: this.getAugmentedEnv() env: this.getAugmentedEnv()
}); });
return true; return result.status === 0;
} catch { } catch {
return false; return false;
} }
} }
/** /**
* Comprehensive dependency check (async) - returns detailed information * Comprehensive dependency check (async) - returns detailed information