XssPhtmlTemplateTest.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Test\Php;
  7. use Magento\Framework\App\Utility\Files;
  8. use Magento\TestFramework\Utility\XssOutputValidator;
  9. use Magento\Framework\Component\ComponentRegistrar;
  10. /**
  11. * Find not escaped output in phtml templates
  12. */
  13. class XssPhtmlTemplateTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @return void
  17. */
  18. public function testXssSensitiveOutput()
  19. {
  20. $invoker = new \Magento\Framework\App\Utility\AggregateInvoker($this);
  21. $xssOutputValidator = new XssOutputValidator();
  22. $invoker(
  23. /**
  24. * Static test will cover the following cases:
  25. *
  26. * 1. /\* @noEscape \*\/ before output. Output doesn't require escaping. Test is green.
  27. * 2. Methods which contains "html" in their names (e.g. echo $object->{suffix}Html{postfix}() ).
  28. * Data is ready for the HTML output. Test is green.
  29. * 3. AbstractBlock methods escapeHtml, escapeUrl, escapeQuote, escapeXssInUrl are allowed. Test is green.
  30. * 4. Type casting and php function count() are allowed
  31. * (e.g. echo (int)$var, echo (float)$var, echo (bool)$var, echo count($var)). Test is green.
  32. * 5. Output in single quotes (e.g. echo 'some text'). Test is green.
  33. * 6. Output in double quotes without variables (e.g. echo "some text"). Test is green.
  34. * 7. Other of p.1-6. Output is not escaped. Test is red.
  35. *
  36. * @param string $file
  37. */
  38. function ($file) use ($xssOutputValidator) {
  39. $lines = $xssOutputValidator->getLinesWithXssSensitiveOutput($file);
  40. $this->assertEmpty(
  41. $lines,
  42. "Potentially XSS vulnerability. " .
  43. "Please verify that output is escaped at lines " . $lines
  44. );
  45. },
  46. Files::init()->getPhtmlFiles()
  47. );
  48. }
  49. /**
  50. * @return void
  51. */
  52. public function testAbsenceOfEscapeNotVerifiedAnnotationInRefinedModules()
  53. {
  54. $componentRegistrar = new ComponentRegistrar();
  55. $exemptModules = [];
  56. foreach (array_diff(scandir(__DIR__ . '/_files/whitelist/exempt_modules'), ['..', '.']) as $file) {
  57. $exemptModules = array_merge(
  58. $exemptModules,
  59. include(__DIR__ . '/_files/whitelist/exempt_modules/' . $file)
  60. );
  61. }
  62. $result = "";
  63. foreach ($componentRegistrar->getPaths(ComponentRegistrar::MODULE) as $moduleName => $modulePath) {
  64. if (in_array($moduleName, $exemptModules)) {
  65. continue;
  66. }
  67. foreach (Files::init()->getFiles([$modulePath], '*.phtml') as $file) {
  68. $fileContents = file_get_contents($file);
  69. $pattern = "/\\/* @escapeNotVerified \\*\\/ echo (?!__).+/";
  70. $instances = preg_grep($pattern, explode("\n", $fileContents));
  71. if (!empty($instances)) {
  72. foreach (array_keys($instances) as $line) {
  73. $result .= $file . ':' . ($line + 1) . "\n";
  74. }
  75. }
  76. }
  77. }
  78. $this->assertEmpty(
  79. $result,
  80. "@escapeNotVerified annotation detected.\n" .
  81. "Please use the correct escape strategy and remove annotation at:\n" . $result
  82. );
  83. }
  84. }