Skip to content

Commit

Permalink
Code that uses shell_exec() and exec() now escapes cmds and args in c…
Browse files Browse the repository at this point in the history
…ase PHPCS is being used in a web service
  • Loading branch information
gsherwood committed Feb 26, 2017
1 parent b7c84a0 commit 029305e
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CodeSniffer/Fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ public function generateDiff($filePath=null, $colors=true)

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

$diff = shell_exec($cmd);

fclose($fixedFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
return;
}

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

if (is_array($output) === false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
return;
}

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

if (is_array($output) === false) {
return;
Expand Down
5 changes: 4 additions & 1 deletion CodeSniffer/Standards/Generic/Sniffs/Debug/JSHintSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
return;
}

$cmd = "$rhinoPath \"$jshintPath\" \"$fileName\"";
$rhinoPath = escapeshellcmd($rhinoPath);
$jshintPath = escapeshellcmd($jshintPath);

$cmd = "$rhinoPath \"$jshintPath\" ".escapeshellarg($fileName);
$msg = exec($cmd, $output, $retval);

if (is_array($output) === true) {
Expand Down
6 changes: 3 additions & 3 deletions CodeSniffer/Standards/Generic/Sniffs/PHP/SyntaxSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
}
}

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

$output = shell_exec($cmd);
Expand Down
5 changes: 4 additions & 1 deletion CodeSniffer/Standards/Squiz/Sniffs/Debug/JSLintSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
return;
}

$cmd = "$rhinoPath \"$jslintPath\" \"$fileName\"";
$rhinoPath = escapeshellcmd($rhinoPath);
$jslintPath = escapeshellcmd($jslintPath);

$cmd = "$rhinoPath \"$jslintPath\" ".escapeshellarg($fileName);
$msg = exec($cmd, $output, $retval);

if (is_array($output) === true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
return;
}

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

// Variable $exitCode is the last line of $output if no error occurs, on
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
// In the command, 2>&1 is important because the code analyzer sends its
// findings to stderr. $output normally contains only stdout, so using 2>&1
// will pipe even stderr to stdout.
$cmd = $analyzerPath.' '.$fileName.' 2>&1';
$cmd = escapeshellcmd($analyzerPath).' '.escapeshellarg($fileName).' 2>&1';

// There is the possibility to pass "--ide" as an option to the analyzer.
// This would result in an output format which would be easier to parse.
Expand Down
3 changes: 3 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
<license uri="https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt">BSD 3-Clause License</license>
<notes>
- The PHP-supplied T_COALESCE_EQUAL token has been replicated for PHP versions before 7.2
- Code that uses shell_exec() and exec() now escapes cmds and args in case PHPCS is being used in a web service
-- This changes saves having to do filename and config validation before passing content to PHPCS
-- Thanks to Klaus Purer for reporting this
- PEAR.Functions.FunctionDeclaration now reports an error for blank lines found inside a function declaration
- PEAR.Functions.FunctionDeclaration no longer reports indent errors for blank lines in a function declaration
- Squiz.Functions.MultiLineFunctionDeclaration no longer reports errors for blank lines in a function declaration
Expand Down

0 comments on commit 029305e

Please sign in to comment.