ExecutableRegExSniff.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sniffs\Security;
  7. use PHP_CodeSniffer\Sniffs\Sniff;
  8. use PHP_CodeSniffer\Files\File;
  9. use PHP_CodeSniffer\Util\Tokens;
  10. /**
  11. * Detects executable regular expressions.
  12. *
  13. * Example: echo preg_replace('|^(.*)$|ei', '"\1"', 'get_input');
  14. */
  15. class ExecutableRegExSniff implements Sniff
  16. {
  17. /**
  18. * String representation of error.
  19. *
  20. * @var string
  21. */
  22. // @codingStandardsIgnoreLine
  23. protected $errorMessage = "Possible executable regular expression in %s. Make sure that the pattern doesn't contain 'e' modifier";
  24. /**
  25. * Error violation code.
  26. *
  27. * @var string
  28. */
  29. protected $errorCode = 'PossibleExecutableRegEx';
  30. /**
  31. * Observed function.
  32. *
  33. * @var array
  34. */
  35. protected $function = 'preg_replace';
  36. /**
  37. * List of ignored tokens.
  38. *
  39. * @var array
  40. */
  41. protected $ignoreTokens = [
  42. T_DOUBLE_COLON,
  43. T_OBJECT_OPERATOR,
  44. T_FUNCTION,
  45. T_CONST,
  46. T_CLASS,
  47. ];
  48. /**
  49. * @inheritdoc
  50. */
  51. public function register()
  52. {
  53. return [T_STRING];
  54. }
  55. /**
  56. * @inheritdoc
  57. */
  58. public function process(File $phpcsFile, $stackPtr)
  59. {
  60. $tokens = $phpcsFile->getTokens();
  61. if ($tokens[$stackPtr]['content'] !== $this->function) {
  62. return;
  63. }
  64. $prevToken = $phpcsFile->findPrevious(T_WHITESPACE, $stackPtr - 1, null, true);
  65. if (in_array($tokens[$prevToken]['code'], $this->ignoreTokens)) {
  66. return;
  67. }
  68. $nextToken = $phpcsFile->findNext([T_WHITESPACE, T_OPEN_PARENTHESIS], $stackPtr + 1, null, true);
  69. if (in_array($tokens[$nextToken]['code'], Tokens::$stringTokens)
  70. && preg_match('/[#\/|~\}\)][imsxADSUXJu]*e[imsxADSUXJu]*.$/', $tokens[$nextToken]['content'])
  71. ) {
  72. $phpcsFile->addError(
  73. $this->errorMessage,
  74. $stackPtr,
  75. $this->errorCode,
  76. [$tokens[$stackPtr]['content']]
  77. );
  78. }
  79. }
  80. }