|
| 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 [email protected] 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 | + |
| 23 | +/** |
| 24 | + * @category Zend |
| 25 | + * @package Zend_Xml_SecurityScan |
| 26 | + * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) |
| 27 | + * @license http://framework.zend.com/license/new-bsd New BSD License |
| 28 | + */ |
| 29 | +class Zend_Xml_Security |
| 30 | +{ |
| 31 | + const ENTITY_DETECT = 'Detected use of ENTITY in XML, disabled to prevent XXE/XEE attacks'; |
| 32 | + |
| 33 | + /** |
| 34 | + * Heuristic scan to detect entity in XML |
| 35 | + * |
| 36 | + * @param string $xml |
| 37 | + * @throws Zend_Xml_Exception |
| 38 | + */ |
| 39 | + protected static function heuristicScan($xml) |
| 40 | + { |
| 41 | + if (strpos($xml, '<!ENTITY') !== false) { |
| 42 | + #require_once 'Exception.php'; |
| 43 | + throw new Zend_Xml_Exception(self::ENTITY_DETECT); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @param integer $errno |
| 49 | + * @param string $errstr |
| 50 | + * @param string $errfile |
| 51 | + * @param integer $errline |
| 52 | + * @return bool |
| 53 | + */ |
| 54 | + public static function loadXmlErrorHandler($errno, $errstr, $errfile, $errline) |
| 55 | + { |
| 56 | + if (substr_count($errstr, 'DOMDocument::loadXML()') > 0) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Scan XML string for potential XXE and XEE attacks |
| 64 | + * |
| 65 | + * @param string $xml |
| 66 | + * @param DomDocument $dom |
| 67 | + * @throws Zend_Xml_Exception |
| 68 | + * @return SimpleXMLElement|DomDocument|boolean |
| 69 | + */ |
| 70 | + public static function scan($xml, DOMDocument $dom = null) |
| 71 | + { |
| 72 | + // If running with PHP-FPM we perform an heuristic scan |
| 73 | + // We cannot use libxml_disable_entity_loader because of this bug |
| 74 | + // @see https://bugs.php.net/bug.php?id=64938 |
| 75 | + if (self::isPhpFpm()) { |
| 76 | + self::heuristicScan($xml); |
| 77 | + } |
| 78 | + |
| 79 | + if (null === $dom) { |
| 80 | + $simpleXml = true; |
| 81 | + $dom = new DOMDocument(); |
| 82 | + } |
| 83 | + |
| 84 | + if (!self::isPhpFpm()) { |
| 85 | + $loadEntities = libxml_disable_entity_loader(true); |
| 86 | + $useInternalXmlErrors = libxml_use_internal_errors(true); |
| 87 | + } |
| 88 | + |
| 89 | + // Load XML with network access disabled (LIBXML_NONET) |
| 90 | + // error disabled with @ for PHP-FPM scenario |
| 91 | + set_error_handler(array('Zend_Xml_Security', 'loadXmlErrorHandler'), E_WARNING); |
| 92 | + |
| 93 | + $result = $dom->loadXml($xml, LIBXML_NONET); |
| 94 | + restore_error_handler(); |
| 95 | + |
| 96 | + if (!$result) { |
| 97 | + // Entity load to previous setting |
| 98 | + if (!self::isPhpFpm()) { |
| 99 | + libxml_disable_entity_loader($loadEntities); |
| 100 | + libxml_use_internal_errors($useInternalXmlErrors); |
| 101 | + } |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + // Scan for potential XEE attacks using ENTITY, if not PHP-FPM |
| 106 | + if (!self::isPhpFpm()) { |
| 107 | + foreach ($dom->childNodes as $child) { |
| 108 | + if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) { |
| 109 | + if ($child->entities->length > 0) { |
| 110 | + #require_once 'Exception.php'; |
| 111 | + throw new Zend_Xml_Exception(self::ENTITY_DETECT); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Entity load to previous setting |
| 118 | + if (!self::isPhpFpm()) { |
| 119 | + libxml_disable_entity_loader($loadEntities); |
| 120 | + libxml_use_internal_errors($useInternalXmlErrors); |
| 121 | + } |
| 122 | + |
| 123 | + if (isset($simpleXml)) { |
| 124 | + $result = simplexml_import_dom($dom); |
| 125 | + if (!$result instanceof SimpleXMLElement) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + return $result; |
| 129 | + } |
| 130 | + return $dom; |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Scan XML file for potential XXE/XEE attacks |
| 135 | + * |
| 136 | + * @param string $file |
| 137 | + * @param DOMDocument $dom |
| 138 | + * @throws Zend_Xml_Exception |
| 139 | + * @return SimpleXMLElement|DomDocument |
| 140 | + */ |
| 141 | + public static function scanFile($file, DOMDocument $dom = null) |
| 142 | + { |
| 143 | + if (!file_exists($file)) { |
| 144 | + #require_once 'Exception.php'; |
| 145 | + throw new Zend_Xml_Exception( |
| 146 | + "The file $file specified doesn't exist" |
| 147 | + ); |
| 148 | + } |
| 149 | + return self::scan(file_get_contents($file), $dom); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Return true if PHP is running with PHP-FPM |
| 154 | + * |
| 155 | + * @return boolean |
| 156 | + */ |
| 157 | + public static function isPhpFpm() |
| 158 | + { |
| 159 | + if (substr(php_sapi_name(), 0, 3) === 'fpm') { |
| 160 | + return true; |
| 161 | + } |
| 162 | + return false; |
| 163 | + } |
| 164 | +} |
0 commit comments