clean install

This commit is contained in:
matze committed 2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 files changed
+3555714

No files matched your search

@@ -0,0 +1,67 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionCalls\CheckPlainSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionCalls;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionCall;
/**
* Check that check_plain() is not used on literal strings.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class CheckPlainSniff extends FunctionCall
{
/**
* Returns an array of function names this test wants to listen for.
*
* @return array<string>
*/
public function registerFunctionNames()
{
return ['check_plain'];
}//end registerFunctionNames()
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
* @param int $openBracket The position of the opening
* parenthesis in the stack.
* @param int $closeBracket The position of the closing
* parenthesis in the stack.
*
* @return void
*/
public function processFunctionCall(
File $phpcsFile,
$stackPtr,
$openBracket,
$closeBracket
) {
$tokens = $phpcsFile->getTokens();
$argument = $this->getArgument(1);
if ($argument['start'] === $argument['end'] && $tokens[$argument['start']]['code'] === T_CONSTANT_ENCAPSED_STRING) {
$warning = 'Do not use check_plain() on string literals, because they cannot contain user provided text';
$phpcsFile->addWarning($warning, $argument['start'], 'CheckPlainLiteral');
}
}//end processFunctionCall()
}//end class
@@ -0,0 +1,73 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionCalls\CurlSslVerifierSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionCalls;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionCall;
/**
* Make sure that CURLOPT_SSL_VERIFYPEER is not disabled, since that is a
* security issue.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class CurlSslVerifierSniff extends FunctionCall
{
/**
* Returns an array of function names this test wants to listen for.
*
* @return array<string>
*/
public function registerFunctionNames()
{
return ['curl_setopt'];
}//end registerFunctionNames()
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
* @param int $openBracket The position of the opening
* parenthesis in the stack.
* @param int $closeBracket The position of the closing
* parenthesis in the stack.
*
* @return void
*/
public function processFunctionCall(
File $phpcsFile,
$stackPtr,
$openBracket,
$closeBracket
) {
$tokens = $phpcsFile->getTokens();
$option = $this->getArgument(2);
if ($tokens[$option['start']]['content'] !== 'CURLOPT_SSL_VERIFYPEER') {
return;
}
$value = $this->getArgument(3);
if ($tokens[$value['start']]['content'] === 'FALSE' || $tokens[$value['start']]['content'] === '0') {
$warning = 'Potential security problem: SSL peer verification must not be disabled';
$phpcsFile->addWarning($warning, $value['start'], 'SslPeerVerificationDisabled');
}
}//end processFunctionCall()
}//end class
@@ -0,0 +1,83 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionCalls\DbQuerySniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionCalls;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionCall;
use DrupalPractice\Project;
/**
* Check that UPDATE/DELETE queries are not used in db_query() in Drupal 7.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class DbQuerySniff extends FunctionCall
{
/**
* Returns an array of function names this test wants to listen for.
*
* @return array<string>
*/
public function registerFunctionNames()
{
return ['db_query'];
}//end registerFunctionNames()
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
* @param int $openBracket The position of the opening
* parenthesis in the stack.
* @param int $closeBracket The position of the closing
* parenthesis in the stack.
*
* @return void|int
*/
public function processFunctionCall(
File $phpcsFile,
$stackPtr,
$openBracket,
$closeBracket
) {
// This check only applies to Drupal 7, not Drupal 6.
if (Project::getCoreVersion($phpcsFile) !== 7) {
return ($phpcsFile->numTokens + 1);
}
$tokens = $phpcsFile->getTokens();
$argument = $this->getArgument(1);
$queryStart = '';
for ($start = $argument['start']; $tokens[$start]['code'] === T_CONSTANT_ENCAPSED_STRING && empty($queryStart) === true; $start++) {
// Remove quote and white space from the beginning.
$queryStart = trim(substr($tokens[$start]['content'], 1));
// Just look at the first word.
$parts = explode(' ', $queryStart);
$queryStart = $parts[0];
if (in_array(strtoupper($queryStart), ['INSERT', 'UPDATE', 'DELETE', 'TRUNCATE']) === true) {
$warning = 'Do not use %s queries with db_query(), use %s instead';
$phpcsFile->addWarning($warning, $start, 'DbQuery', [$queryStart, 'db_'.strtolower($queryStart).'()']);
}
}
}//end processFunctionCall()
}//end class
@@ -0,0 +1,70 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionCalls\DbSelectBracesSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionCalls;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionCall;
/**
* Check that db_select() calls do not use {} braces for the table name.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class DbSelectBracesSniff extends FunctionCall
{
/**
* Returns an array of function names this test wants to listen for.
*
* @return array<string>
*/
public function registerFunctionNames()
{
return ['db_select'];
}//end registerFunctionNames()
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
* @param int $openBracket The position of the opening
* parenthesis in the stack.
* @param int $closeBracket The position of the closing
* parenthesis in the stack.
*
* @return void
*/
public function processFunctionCall(
File $phpcsFile,
$stackPtr,
$openBracket,
$closeBracket
) {
$tokens = $phpcsFile->getTokens();
$argument = $this->getArgument(1);
if ($argument !== false && $tokens[$argument['start']]['code'] === T_CONSTANT_ENCAPSED_STRING
&& strpos($tokens[$argument['start']]['content'], '{') !== false
) {
$warning = 'Do not use {} curly brackets in db_select() table names';
$phpcsFile->addWarning($warning, $argument['start'], 'DbSelectBrace');
}
}//end processFunctionCall()
}//end class
@@ -0,0 +1,89 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionCalls\DefaultValueSanitizeSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionCalls;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionCall;
use PHP_CodeSniffer\Util\Tokens;
/**
* Check that sanitization functions such as check_plain() are not used on Form
* API #default_value elements.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class DefaultValueSanitizeSniff extends FunctionCall
{
/**
* Returns an array of function names this test wants to listen for.
*
* @return array<string>
*/
public function registerFunctionNames()
{
return [
'check_markup',
'check_plain',
'check_url',
'filter_xss',
'filter_xss_admin',
];
}//end registerFunctionNames()
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
* @param int $openBracket The position of the opening
* parenthesis in the stack.
* @param int $closeBracket The position of the closing
* parenthesis in the stack.
*
* @return void
*/
public function processFunctionCall(
File $phpcsFile,
$stackPtr,
$openBracket,
$closeBracket
) {
$tokens = $phpcsFile->getTokens();
// We assume that the sequence '#default_value' => check_plain(...) is
// wrong because the Form API already sanitizes #default_value.
$arrow = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($stackPtr - 1), null, true);
if ($arrow === false || $tokens[$arrow]['code'] !== T_DOUBLE_ARROW) {
return;
}
$arrayKey = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($arrow - 1), null, true);
if ($arrayKey === false
|| $tokens[$arrayKey]['code'] !== T_CONSTANT_ENCAPSED_STRING
|| substr($tokens[$arrayKey]['content'], 1, -1) !== '#default_value'
) {
return;
}
$warning = 'Do not use the %s() sanitization function on Form API #default_value elements, they get escaped automatically';
$data = [$tokens[$stackPtr]['content']];
$phpcsFile->addWarning($warning, $stackPtr, 'DefaultValue', $data);
}//end processFunctionCall()
}//end class
@@ -0,0 +1,70 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionCalls\FormErrorTSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionCalls;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionCall;
/**
* Verifies that messages passed to form_set_error() run through t().
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class FormErrorTSniff extends FunctionCall
{
/**
* Returns an array of function names this test wants to listen for.
*
* @return array<string>
*/
public function registerFunctionNames()
{
return [
'form_set_error',
'form_error',
];
}//end registerFunctionNames()
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
* @param int $openBracket The position of the opening
* parenthesis in the stack.
* @param int $closeBracket The position of the closing
* parenthesis in the stack.
*
* @return void
*/
public function processFunctionCall(
File $phpcsFile,
$stackPtr,
$openBracket,
$closeBracket
) {
$tokens = $phpcsFile->getTokens();
$argument = $this->getArgument(2);
if ($argument !== false && $tokens[$argument['start']]['code'] === T_CONSTANT_ENCAPSED_STRING) {
$warning = 'Form error messages are user facing text and must run through t() for translation';
$phpcsFile->addWarning($warning, $argument['start'], 'ErrorMessage');
}
}//end processFunctionCall()
}//end class
@@ -0,0 +1,103 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionCalls\InsecureUnserializeSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionCalls;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionCall;
/**
* Check that unserialize() limits classes that may be unserialized.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class InsecureUnserializeSniff extends FunctionCall
{
/**
* Returns an array of function names this test wants to listen for.
*
* @return array<string>
*/
public function registerFunctionNames()
{
return ['unserialize'];
}//end registerFunctionNames()
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
* @param int $openBracket The position of the opening
* parenthesis in the stack.
* @param int $closeBracket The position of the closing
* parenthesis in the stack.
*
* @return void
*/
public function processFunctionCall(
File $phpcsFile,
$stackPtr,
$openBracket,
$closeBracket
) {
$tokens = $phpcsFile->getTokens();
$argument = $this->getArgument(2);
if ($argument === false) {
$this->fail($phpcsFile, $closeBracket);
return;
}
$allowedClassesKeyStart = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $argument['start'], $argument['end'], false, '\'allowed_classes\'');
if ($allowedClassesKeyStart === false) {
$allowedClassesKeyStart = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $argument['start'], $argument['end'], false, '"allowed_classes"');
}
if ($allowedClassesKeyStart === false) {
$this->fail($phpcsFile, $argument['end']);
return;
}
$allowedClassesArrow = $phpcsFile->findNext(T_DOUBLE_ARROW, $allowedClassesKeyStart, $argument['end'], false);
if ($allowedClassesArrow === false) {
$this->fail($phpcsFile, $argument['end']);
return;
}
$allowedClassesValue = $phpcsFile->findNext(T_WHITESPACE, ($allowedClassesArrow + 1), $argument['end'], true);
if ($tokens[$allowedClassesValue]['code'] === T_TRUE) {
$this->fail($phpcsFile, $allowedClassesValue);
}
}//end processFunctionCall()
/**
* Record a violation of the standard.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $position The stack position of the violation.
*
* @return void
*/
protected function fail(File $phpcsFile, int $position)
{
$phpcsFile->addError('unserialize() is insecure unless allowed classes are limited. Use a safe format like JSON or use the allowed_classes option.', $position, 'InsecureUnserialize');
}//end fail()
}//end class
@@ -0,0 +1,67 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionCalls\Drupal_Sniffs_FunctionCalls_LCheckPlainSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionCalls;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionCall;
/**
* The first argument of the l() function should not be check_plain()'ed.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class LCheckPlainSniff extends FunctionCall
{
/**
* Returns an array of function names this test wants to listen for.
*
* @return array<string>
*/
public function registerFunctionNames()
{
return ['l'];
}//end registerFunctionNames()
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
* @param int $openBracket The position of the opening
* parenthesis in the stack.
* @param int $closeBracket The position of the closing
* parenthesis in the stack.
*
* @return void
*/
public function processFunctionCall(
File $phpcsFile,
$stackPtr,
$openBracket,
$closeBracket
) {
$tokens = $phpcsFile->getTokens();
$argument = $this->getArgument(1);
if ($tokens[$argument['start']]['content'] === 'check_plain') {
$warning = 'Do not use check_plain() on the first argument of l(), because l() will sanitize it for you by default';
$phpcsFile->addWarning($warning, $argument['start'], 'LCheckPlain');
}
}//end processFunctionCall()
}//end class
@@ -0,0 +1,67 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionCalls\MessageTSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionCalls;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionCall;
/**
* Verifies that messages passed to drupal_set_message() run through t().
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class MessageTSniff extends FunctionCall
{
/**
* Returns an array of function names this test wants to listen for.
*
* @return array<string>
*/
public function registerFunctionNames()
{
return ['drupal_set_message'];
}//end registerFunctionNames()
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
* @param int $openBracket The position of the opening
* parenthesis in the stack.
* @param int $closeBracket The position of the closing
* parenthesis in the stack.
*
* @return void
*/
public function processFunctionCall(
File $phpcsFile,
$stackPtr,
$openBracket,
$closeBracket
) {
$tokens = $phpcsFile->getTokens();
$argument = $this->getArgument(1);
if ($argument !== false && $tokens[$argument['start']]['code'] === T_CONSTANT_ENCAPSED_STRING) {
$warning = 'Messages are user facing text and must run through t() for translation';
$phpcsFile->addWarning($warning, $argument['start'], 'ErrorMessage');
}
}//end processFunctionCall()
}//end class
@@ -0,0 +1,93 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionCalls\TCheckPlainSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionCalls;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionCall;
/**
* Check that "@" and "%" placeholders in t()/watchdog() are not escaped twice
* with check_plain().
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class TCheckPlainSniff extends FunctionCall
{
/**
* Returns an array of function names this test wants to listen for.
*
* @return array<string>
*/
public function registerFunctionNames()
{
return [
't',
'watchdog',
];
}//end registerFunctionNames()
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
* @param int $openBracket The position of the opening
* parenthesis in the stack.
* @param int $closeBracket The position of the closing
* parenthesis in the stack.
*
* @return void
*/
public function processFunctionCall(
File $phpcsFile,
$stackPtr,
$openBracket,
$closeBracket
) {
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['content'] === 't') {
$argument = $this->getArgument(2);
} else {
// For watchdog() the placeholders are in the third argument.
$argument = $this->getArgument(3);
}
if ($argument === false) {
return;
}
if ($tokens[$argument['start']]['code'] !== T_ARRAY) {
return;
}
$checkPlain = $argument['start'];
while (($checkPlain = $phpcsFile->findNext(T_STRING, ($checkPlain + 1), $tokens[$argument['start']]['parenthesis_closer'])) !== false) {
if ($tokens[$checkPlain]['content'] === 'check_plain') {
// The check_plain() could be embedded with string concatenation,
// which we want to allow.
$previous = $phpcsFile->findPrevious(T_WHITESPACE, ($checkPlain - 1), $argument['start'], true);
if ($previous === false || $tokens[$previous]['code'] !== T_STRING_CONCAT) {
$warning = 'The extra check_plain() is not necessary for placeholders, "@" and "%" will automatically run check_plain()';
$phpcsFile->addWarning($warning, $checkPlain, 'CheckPlain');
}
}
}
}//end processFunctionCall()
}//end class
@@ -0,0 +1,67 @@
<?php
/**
* ThemeSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionCalls;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionCall;
/**
* \DrupalPractice\Sniffs\FunctionCalls\Checks that theme functions are not directly called.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class ThemeSniff extends FunctionCall
{
/**
* List of functions starting with "theme_" that don't generate theme output.
*
* @var array<string>
*/
protected $reservedFunctions = [
'theme_get_registry',
'theme_get_setting',
'theme_render_template',
'theme_enable',
'theme_disable',
'theme_get_suggestions',
];
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$functionName = $tokens[$stackPtr]['content'];
if (strpos($functionName, 'theme_') !== 0
|| in_array($functionName, $this->reservedFunctions) === true
|| $this->isFunctionCall($phpcsFile, $stackPtr) === false
) {
return;
}
$themeName = substr($functionName, 6);
$warning = "Do not call theme functions directly, use theme('%s', ...) instead";
$phpcsFile->addWarning($warning, $stackPtr, 'ThemeFunctionDirect', [$themeName]);
}//end process()
}//end class
@@ -0,0 +1,80 @@
<?php
/**
* \DrupalPractice\Sniffs\FunctionCalls\VariableSetSanitizeSniff
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
namespace DrupalPractice\Sniffs\FunctionCalls;
use PHP_CodeSniffer\Files\File;
use Drupal\Sniffs\Semantics\FunctionCall;
/**
* Check that variable_set() calls do not run check_plain() or other
* sanitization functions on the value.
*
* @category PHP
* @package PHP_CodeSniffer
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class VariableSetSanitizeSniff extends FunctionCall
{
/**
* Returns an array of function names this test wants to listen for.
*
* @return array<string>
*/
public function registerFunctionNames()
{
return ['variable_set'];
}//end registerFunctionNames()
/**
* Processes this function call.
*
* @param \PHP_CodeSniffer\Files\File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the function call in
* the stack.
* @param int $openBracket The position of the opening
* parenthesis in the stack.
* @param int $closeBracket The position of the closing
* parenthesis in the stack.
*
* @return void
*/
public function processFunctionCall(
File $phpcsFile,
$stackPtr,
$openBracket,
$closeBracket
) {
$tokens = $phpcsFile->getTokens();
$argument = $this->getArgument(2);
if ($argument !== false && in_array(
$tokens[$argument['start']]['content'],
[
'check_markup',
'check_plain',
'check_url',
'filter_xss',
'filter_xss_admin',
]
) === true
) {
$warning = 'Do not use the %s() sanitization function when writing values to the database, use it on output to HTML instead';
$data = [$tokens[$argument['start']]['content']];
$phpcsFile->addWarning($warning, $argument['start'], 'VariableSet', $data);
}
}//end processFunctionCall()
}//end class