Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4cb01fd

Browse files
committed
security: Fix command injection vulnerability in statusline.cjs
- Add input validation for command parameter - Check for dangerous shell metacharacters - Allow only safe command patterns
1 parent 5db55fd commit 4cb01fd

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

.claude/helpers/statusline.cjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,27 @@ const c = {
4747
};
4848

4949
// Safe execSync with strict timeout (returns empty string on failure)
50+
// Validates command to prevent command injection
5051
function safeExec(cmd, timeoutMs = 2000) {
5152
try {
53+
// Validate command to prevent command injection
54+
// Only allow commands that match safe patterns (no shell metacharacters)
55+
if (typeof cmd !== 'string') {
56+
return '';
57+
}
58+
59+
// Check for dangerous shell metacharacters that could allow injection
60+
const dangerousChars = /[;&|`$(){}[\]<>'"\\]/;
61+
if (dangerousChars.test(cmd)) {
62+
// If dangerous chars found, only allow if it's a known safe pattern
63+
// Allow 'sh -c' with single-quoted script (already escaped)
64+
const safeShPattern = /^sh\s+-c\s+'[^']*'$/;
65+
if (!safeShPattern.test(cmd)) {
66+
console.warn('safeExec: Command contains potentially dangerous characters');
67+
return '';
68+
}
69+
}
70+
5271
return execSync(cmd, {
5372
encoding: 'utf-8',
5473
timeout: timeoutMs,

0 commit comments

Comments
 (0)