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
+61
-36
@@ -43,55 +43,80 @@ export class DependencyChecker {
|
|||||||
* Check if a dependency is available and get its version
|
* Check if a dependency is available and get its version
|
||||||
*/
|
*/
|
||||||
private static async checkDependency(
|
private static async checkDependency(
|
||||||
name: string,
|
name: string,
|
||||||
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()
|
||||||
});
|
});
|
||||||
|
|
||||||
const match = stdout.match(versionRegex);
|
let output = '';
|
||||||
const version = match ? match[1] : null;
|
let error = '';
|
||||||
|
|
||||||
return {
|
checkProcess.stdout?.on('data', (data) => {
|
||||||
name,
|
output += data.toString();
|
||||||
version,
|
});
|
||||||
isAvailable: true,
|
|
||||||
executablePath
|
checkProcess.stderr?.on('data', (data) => {
|
||||||
};
|
error += data.toString();
|
||||||
} catch (error) {
|
});
|
||||||
return {
|
|
||||||
name,
|
checkProcess.on('close', (code) => {
|
||||||
version: null,
|
if (code === 0) {
|
||||||
isAvailable: false,
|
resolve(output);
|
||||||
executablePath
|
} 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;
|
||||||
|
|
||||||
|
return {
|
||||||
|
name,
|
||||||
|
version,
|
||||||
|
isAvailable: true,
|
||||||
|
executablePath
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
name,
|
||||||
|
version: null,
|
||||||
|
isAvailable: false,
|
||||||
|
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
|
||||||
|
|||||||
Reference in new issue
Block a user