Security.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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-2015 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-2015 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. * @param integer $errno
  45. * @param string $errstr
  46. * @param string $errfile
  47. * @param integer $errline
  48. * @return bool
  49. */
  50. public static function loadXmlErrorHandler($errno, $errstr, $errfile, $errline)
  51. {
  52. if (substr_count($errstr, 'DOMDocument::loadXML()') > 0) {
  53. return true;
  54. }
  55. return false;
  56. }
  57. /**
  58. * Scan XML string for potential XXE and XEE attacks
  59. *
  60. * @param string $xml
  61. * @param DomDocument $dom
  62. * @throws Zend_Xml_Exception
  63. * @return SimpleXMLElement|DomDocument|boolean
  64. */
  65. public static function scan($xml, DOMDocument $dom = null)
  66. {
  67. // If running with PHP-FPM we perform an heuristic scan
  68. // We cannot use libxml_disable_entity_loader because of this bug
  69. // @see https://bugs.php.net/bug.php?id=64938
  70. if (self::isPhpFpm()) {
  71. self::heuristicScan($xml);
  72. }
  73. if (null === $dom) {
  74. $simpleXml = true;
  75. $dom = new DOMDocument();
  76. }
  77. if (!self::isPhpFpm()) {
  78. $loadEntities = libxml_disable_entity_loader(true);
  79. $useInternalXmlErrors = libxml_use_internal_errors(true);
  80. }
  81. // Load XML with network access disabled (LIBXML_NONET)
  82. // error disabled with @ for PHP-FPM scenario
  83. set_error_handler(array('Zend_Xml_Security', 'loadXmlErrorHandler'), E_WARNING);
  84. $result = $dom->loadXml($xml, LIBXML_NONET);
  85. restore_error_handler();
  86. // Entity load to previous setting
  87. if (!self::isPhpFpm()) {
  88. libxml_disable_entity_loader($loadEntities);
  89. libxml_use_internal_errors($useInternalXmlErrors);
  90. }
  91. if (!$result) {
  92. return false;
  93. }
  94. // Scan for potential XEE attacks using ENTITY, if not PHP-FPM
  95. if (!self::isPhpFpm()) {
  96. foreach ($dom->childNodes as $child) {
  97. if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
  98. if ($child->entities->length > 0) {
  99. require_once 'Exception.php';
  100. throw new Zend_Xml_Exception(self::ENTITY_DETECT);
  101. }
  102. }
  103. }
  104. }
  105. if (isset($simpleXml)) {
  106. $result = simplexml_import_dom($dom);
  107. if (!$result instanceof SimpleXMLElement) {
  108. return false;
  109. }
  110. return $result;
  111. }
  112. return $dom;
  113. }
  114. /**
  115. * Scan XML file for potential XXE/XEE attacks
  116. *
  117. * @param string $file
  118. * @param DOMDocument $dom
  119. * @throws Zend_Xml_Exception
  120. * @return SimpleXMLElement|DomDocument
  121. */
  122. public static function scanFile($file, DOMDocument $dom = null)
  123. {
  124. if (!file_exists($file)) {
  125. require_once 'Exception.php';
  126. throw new Zend_Xml_Exception(
  127. "The file $file specified doesn't exist"
  128. );
  129. }
  130. return self::scan(file_get_contents($file), $dom);
  131. }
  132. /**
  133. * Return true if PHP is running with PHP-FPM
  134. *
  135. * @return boolean
  136. */
  137. public static function isPhpFpm()
  138. {
  139. if (substr(php_sapi_name(), 0, 3) === 'fpm') {
  140. return true;
  141. }
  142. return false;
  143. }
  144. }