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:
1 file changed
+32
-7
@@ -48,15 +48,40 @@ export class DependencyChecker {
|
||||
versionCommand: string,
|
||||
versionRegex: RegExp
|
||||
): Promise<DependencyInfo> {
|
||||
const { exec } = require('child_process');
|
||||
const { promisify } = require('util');
|
||||
const execAsync = promisify(exec);
|
||||
const { spawn } = require('child_process');
|
||||
|
||||
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()
|
||||
});
|
||||
|
||||
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 version = match ? match[1] : null;
|
||||
|
||||
@@ -80,14 +105,14 @@ export class DependencyChecker {
|
||||
* Check if a dependency is available (synchronous version for startup)
|
||||
*/
|
||||
private static checkDependencySync(executablePath: string, versionCommand: string): boolean {
|
||||
const { execSync } = require('child_process');
|
||||
const { spawnSync } = require('child_process');
|
||||
|
||||
try {
|
||||
execSync(`${executablePath} ${versionCommand}`, {
|
||||
const result = spawnSync(executablePath, versionCommand.split(' '), {
|
||||
encoding: 'utf8',
|
||||
env: this.getAugmentedEnv()
|
||||
});
|
||||
return true;
|
||||
return result.status === 0;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user