Skip to content

Commit 029305e

Browse files
committed
Code that uses shell_exec() and exec() now escapes cmds and args in case PHPCS is being used in a web service
1 parent b7c84a0 commit 029305e

File tree

9 files changed

+23
-11
lines changed

9 files changed

+23
-11
lines changed

CodeSniffer/Fixer.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ public function generateDiff($filePath=null, $colors=true)
266266

267267
// We must use something like shell_exec() because whitespace at the end
268268
// of lines is critical to diff files.
269-
$cmd = "diff -u -L\"$filename\" -LPHP_CodeSniffer \"$filename\" \"$tempName\"";
269+
$filename = escapeshellarg($filename);
270+
$cmd = "diff -u -L$filename -LPHP_CodeSniffer $filename \"$tempName\"";
271+
270272
$diff = shell_exec($cmd);
271273

272274
fclose($fixedFile);

CodeSniffer/Standards/Generic/Sniffs/Debug/CSSLintSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6666
return;
6767
}
6868

69-
$cmd = $csslintPath.' '.escapeshellarg($fileName);
69+
$cmd = escapeshellcmd($csslintPath).' '.escapeshellarg($fileName).' 2>&1';
7070
exec($cmd, $output, $retval);
7171

7272
if (is_array($output) === false) {

CodeSniffer/Standards/Generic/Sniffs/Debug/ClosureLinterSniff.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
8383
return;
8484
}
8585

86-
$cmd = "$lintPath --nosummary --notime --unix_mode \"$fileName\"";
87-
$msg = exec($cmd, $output, $retval);
86+
$lintPath = escapeshellcmd($lintPath);
87+
$cmd = '$lintPath --nosummary --notime --unix_mode '.escapeshellarg($fileName);
88+
$msg = exec($cmd, $output, $retval);
8889

8990
if (is_array($output) === false) {
9091
return;

CodeSniffer/Standards/Generic/Sniffs/Debug/JSHintSniff.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7070
return;
7171
}
7272

73-
$cmd = "$rhinoPath \"$jshintPath\" \"$fileName\"";
73+
$rhinoPath = escapeshellcmd($rhinoPath);
74+
$jshintPath = escapeshellcmd($jshintPath);
75+
76+
$cmd = "$rhinoPath \"$jshintPath\" ".escapeshellarg($fileName);
7477
$msg = exec($cmd, $output, $retval);
7578

7679
if (is_array($output) === true) {

CodeSniffer/Standards/Generic/Sniffs/PHP/SyntaxSniff.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
7373
}
7474
}
7575

76-
$fileName = $phpcsFile->getFilename();
76+
$fileName = escapeshellarg($phpcsFile->getFilename());
7777
if (defined('HHVM_VERSION') === false) {
78-
$cmd = $this->_phpPath." -l -d error_prepend_string='' \"$fileName\" 2>&1";
78+
$cmd = escapeshellcmd($this->_phpPath)." -l -d error_prepend_string='' $fileName 2>&1";
7979
} else {
80-
$cmd = $this->_phpPath." -l \"$fileName\" 2>&1";
80+
$cmd = escapeshellcmd($this->_phpPath)." -l $fileName 2>&1";
8181
}
8282

8383
$output = shell_exec($cmd);

CodeSniffer/Standards/Squiz/Sniffs/Debug/JSLintSniff.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6868
return;
6969
}
7070

71-
$cmd = "$rhinoPath \"$jslintPath\" \"$fileName\"";
71+
$rhinoPath = escapeshellcmd($rhinoPath);
72+
$jslintPath = escapeshellcmd($jslintPath);
73+
74+
$cmd = "$rhinoPath \"$jslintPath\" ".escapeshellarg($fileName);
7275
$msg = exec($cmd, $output, $retval);
7376

7477
if (is_array($output) === true) {

CodeSniffer/Standards/Squiz/Sniffs/Debug/JavaScriptLintSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6666
return;
6767
}
6868

69-
$cmd = '"'.$jslPath.'" -nologo -nofilelisting -nocontext -nosummary -output-format __LINE__:__ERROR__ -process "'.$fileName.'"';
69+
$cmd = '"'.escapeshellcmd($jslPath).'" -nologo -nofilelisting -nocontext -nosummary -output-format __LINE__:__ERROR__ -process '.escapeshellarg($fileName);
7070
$msg = exec($cmd, $output, $retval);
7171

7272
// Variable $exitCode is the last line of $output if no error occurs, on

CodeSniffer/Standards/Zend/Sniffs/Debug/CodeAnalyzerSniff.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
6363
// In the command, 2>&1 is important because the code analyzer sends its
6464
// findings to stderr. $output normally contains only stdout, so using 2>&1
6565
// will pipe even stderr to stdout.
66-
$cmd = $analyzerPath.' '.$fileName.' 2>&1';
66+
$cmd = escapeshellcmd($analyzerPath).' '.escapeshellarg($fileName).' 2>&1';
6767

6868
// There is the possibility to pass "--ide" as an option to the analyzer.
6969
// This would result in an output format which would be easier to parse.

package.xml

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
2727
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
2828
<notes>
2929
- The PHP-supplied T_COALESCE_EQUAL token has been replicated for PHP versions before 7.2
30+
- Code that uses shell_exec() and exec() now escapes cmds and args in case PHPCS is being used in a web service
31+
-- This changes saves having to do filename and config validation before passing content to PHPCS
32+
-- Thanks to Klaus Purer for reporting this
3033
- PEAR.Functions.FunctionDeclaration now reports an error for blank lines found inside a function declaration
3134
- PEAR.Functions.FunctionDeclaration no longer reports indent errors for blank lines in a function declaration
3235
- Squiz.Functions.MultiLineFunctionDeclaration no longer reports errors for blank lines in a function declaration

0 commit comments

Comments
 (0)