security: Fix command injection vulnerability in statusline.cjs

- Add input validation for command parameter
- Check for dangerous shell metacharacters
- Allow only safe command patterns
This commit is contained in:
fr4iser
2026-02-28 20:40:05 +01:00
parent 5db55fdd70
commit 4cb01fd482

View File

@@ -47,8 +47,27 @@ const c = {
};
// Safe execSync with strict timeout (returns empty string on failure)
// Validates command to prevent command injection
function safeExec(cmd, timeoutMs = 2000) {
try {
// Validate command to prevent command injection
// Only allow commands that match safe patterns (no shell metacharacters)
if (typeof cmd !== 'string') {
return '';
}
// Check for dangerous shell metacharacters that could allow injection
const dangerousChars = /[;&|`$(){}[\]<>'"\\]/;
if (dangerousChars.test(cmd)) {
// If dangerous chars found, only allow if it's a known safe pattern
// Allow 'sh -c' with single-quoted script (already escaped)
const safeShPattern = /^sh\s+-c\s+'[^']*'$/;
if (!safeShPattern.test(cmd)) {
console.warn('safeExec: Command contains potentially dangerous characters');
return '';
}
}
return execSync(cmd, {
encoding: 'utf-8',
timeout: timeoutMs,