Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
phpcs-security-audit/Security/Sniffs/BadFunctions/SystemExecFunctionsSniff.php /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
58 lines (48 sloc)
1.82 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace PHPCS_SecurityAudit\Security\Sniffs\BadFunctions; | |
| use PHP_CodeSniffer\Sniffs\Sniff; | |
| use PHP_CodeSniffer\Files\File; | |
| class SystemExecFunctionsSniff implements Sniff { | |
| /** | |
| * Returns the token types that this sniff is interested in. | |
| * | |
| * @return array(int) | |
| */ | |
| public function register() { | |
| return array(T_STRING); | |
| } | |
| /** | |
| * Processes the tokens that this sniff is interested in. | |
| * | |
| * @param File $phpcsFile The file where the token was found. | |
| * @param int $stackPtr The position in the stack where | |
| * the token was found. | |
| * | |
| * @return void | |
| */ | |
| public function process(File $phpcsFile, $stackPtr) { | |
| $utils = \PHPCS_SecurityAudit\Security\Sniffs\UtilsFactory::getInstance(); | |
| $tokens = $phpcsFile->getTokens(); | |
| if (in_array($tokens[$stackPtr]['content'], $utils::getSystemexecFunctions())) { | |
| if ($tokens[$stackPtr - 1]['code'] == T_OBJECT_OPERATOR) { | |
| return; | |
| } | |
| $opener = $phpcsFile->findNext(T_OPEN_PARENTHESIS, $stackPtr, null, false, null, true); | |
| if (!$opener) { | |
| return; | |
| } | |
| $closer = $tokens[$opener]['parenthesis_closer']; | |
| $s = $stackPtr + 1; | |
| $s = $phpcsFile->findNext(array_merge(\PHP_CodeSniffer\Util\Tokens::$emptyTokens, \PHP_CodeSniffer\Util\Tokens::$bracketTokens, \PHPCS_SecurityAudit\Security\Sniffs\Utils::$staticTokens, array(T_STRING_CONCAT)), $s, $closer, true); | |
| if ($s) { | |
| $msg = 'System program execution function ' . $tokens[$stackPtr]['content'] . '() detected with dynamic parameter'; | |
| if ($utils::is_token_user_input($tokens[$s])) { | |
| $phpcsFile->addError($msg . ' directly from user input', $stackPtr, 'ErrSystemExec'); | |
| } else { | |
| $phpcsFile->addWarning($msg, $stackPtr, 'WarnSystemExec'); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| ?> |