Security.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Xml
  17. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Xml_SecurityScan
  24. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Zend_Xml_Security
  28. {
  29. const ENTITY_DETECT = 'Detected use of ENTITY in XML, disabled to prevent XXE/XEE attacks';
  30. /**
  31. * Heuristic scan to detect entity in XML
  32. *
  33. * @param string $xml
  34. * @throws Zend_Xml_Exception
  35. */
  36. protected static function heuristicScan($xml)
  37. {
  38. if (strpos($xml, '<!ENTITY') !== false) {
  39. require_once 'Exception.php';
  40. throw new Zend_Xml_Exception(self::ENTITY_DETECT);
  41. }
  42. }
  43. /**
  44. * Scan XML string for potential XXE and XEE attacks
  45. *
  46. * @param string $xml
  47. * @param DomDocument $dom
  48. * @throws Zend_Xml_Exception
  49. * @return SimpleXMLElement|DomDocument|boolean
  50. */
  51. public static function scan($xml, DOMDocument $dom = null)
  52. {
  53. // If running with PHP-FPM we perform an heuristic scan
  54. // We cannot use libxml_disable_entity_loader because of this bug
  55. // @see https://bugs.php.net/bug.php?id=64938
  56. if (self::isPhpFpm()) {
  57. self::heuristicScan($xml);
  58. }
  59. if (null === $dom) {
  60. $simpleXml = true;
  61. $dom = new DOMDocument();
  62. }
  63. if (!self::isPhpFpm()) {
  64. $loadEntities = libxml_disable_entity_loader(true);
  65. $useInternalXmlErrors = libxml_use_internal_errors(true);
  66. }
  67. // Load XML with network access disabled (LIBXML_NONET)
  68. // error disabled with @ for PHP-FPM scenario
  69. set_error_handler(function ($errno, $errstr) {
  70. if (substr_count($errstr, 'DOMDocument::loadXML()') > 0) {
  71. return true;
  72. }
  73. return false;
  74. }, E_WARNING);
  75. $result = $dom->loadXml($xml, LIBXML_NONET);
  76. restore_error_handler();
  77. if (!$result) {
  78. // Entity load to previous setting
  79. if (!self::isPhpFpm()) {
  80. libxml_disable_entity_loader($loadEntities);
  81. libxml_use_internal_errors($useInternalXmlErrors);
  82. }
  83. return false;
  84. }
  85. // Scan for potential XEE attacks using ENTITY, if not PHP-FPM
  86. if (!self::isPhpFpm()) {
  87. foreach ($dom->childNodes as $child) {
  88. if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
  89. if ($child->entities->length > 0) {
  90. require_once 'Exception.php';
  91. throw new Zend_Xml_Exception(self::ENTITY_DETECT);
  92. }
  93. }
  94. }
  95. }
  96. // Entity load to previous setting
  97. if (!self::isPhpFpm()) {
  98. libxml_disable_entity_loader($loadEntities);
  99. libxml_use_internal_errors($useInternalXmlErrors);
  100. }
  101. if (isset($simpleXml)) {
  102. $result = simplexml_import_dom($dom);
  103. if (!$result instanceof SimpleXMLElement) {
  104. return false;
  105. }
  106. return $result;
  107. }
  108. return $dom;
  109. }
  110. /**
  111. * Scan XML file for potential XXE/XEE attacks
  112. *
  113. * @param string $file
  114. * @param DOMDocument $dom
  115. * @throws Zend_Xml_Exception
  116. * @return SimpleXMLElement|DomDocument
  117. */
  118. public static function scanFile($file, DOMDocument $dom = null)
  119. {
  120. if (!file_exists($file)) {
  121. require_once 'Exception.php';
  122. throw new Zend_Xml_Exception(
  123. "The file $file specified doesn't exist"
  124. );
  125. }
  126. return self::scan(file_get_contents($file), $dom);
  127. }
  128. /**
  129. * Return true if PHP is running with PHP-FPM
  130. *
  131. * @return boolean
  132. */
  133. public static function isPhpFpm()
  134. {
  135. if (substr(php_sapi_name(), 0, 3) === 'fpm') {
  136. return true;
  137. }
  138. return false;
  139. }
  140. }