File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
5051function 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 = / ^ s h \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 ,
You can’t perform that action at this time.
0 commit comments