PageRenderTime 44ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Zend/Xml/Security.php

https://gitlab.com/blingbang2016/shop
PHP | 478 lines | 295 code | 30 blank | 153 comment | 21 complexity | 7366a7b9e2dab5551b557f49d900d6a1 MD5 | raw file
  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 If entity expansion or external entity declaration was discovered.
  35. */
  36. protected static function heuristicScan($xml)
  37. {
  38. foreach (self::getEntityComparison($xml) as $compare) {
  39. if (strpos($xml, $compare) !== false) {
  40. throw new Zend_Xml_Exception(self::ENTITY_DETECT);
  41. }
  42. }
  43. }
  44. /**
  45. * @param integer $errno
  46. * @param string $errstr
  47. * @param string $errfile
  48. * @param integer $errline
  49. * @return bool
  50. */
  51. public static function loadXmlErrorHandler($errno, $errstr, $errfile, $errline)
  52. {
  53. if (substr_count($errstr, 'DOMDocument::loadXML()') > 0) {
  54. return true;
  55. }
  56. return false;
  57. }
  58. /**
  59. * Scan XML string for potential XXE and XEE attacks
  60. *
  61. * @param string $xml
  62. * @param DomDocument $dom
  63. * @throws Zend_Xml_Exception
  64. * @return SimpleXMLElement|DomDocument|boolean
  65. */
  66. public static function scan($xml, DOMDocument $dom = null)
  67. {
  68. // If running with PHP-FPM we perform an heuristic scan
  69. // We cannot use libxml_disable_entity_loader because of this bug
  70. // @see https://bugs.php.net/bug.php?id=64938
  71. if (self::isPhpFpm()) {
  72. self::heuristicScan($xml);
  73. }
  74. if (null === $dom) {
  75. $simpleXml = true;
  76. $dom = new DOMDocument();
  77. }
  78. if (!self::isPhpFpm()) {
  79. $loadEntities = libxml_disable_entity_loader(true);
  80. $useInternalXmlErrors = libxml_use_internal_errors(true);
  81. }
  82. // Load XML with network access disabled (LIBXML_NONET)
  83. // error disabled with @ for PHP-FPM scenario
  84. set_error_handler(array('Zend_Xml_Security', 'loadXmlErrorHandler'), E_WARNING);
  85. $result = $dom->loadXml($xml, LIBXML_NONET);
  86. restore_error_handler();
  87. if (!$result) {
  88. // Entity load to previous setting
  89. if (!self::isPhpFpm()) {
  90. libxml_disable_entity_loader($loadEntities);
  91. libxml_use_internal_errors($useInternalXmlErrors);
  92. }
  93. return false;
  94. }
  95. // Scan for potential XEE attacks using ENTITY, if not PHP-FPM
  96. if (!self::isPhpFpm()) {
  97. foreach ($dom->childNodes as $child) {
  98. if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
  99. if ($child->entities->length > 0) {
  100. libxml_disable_entity_loader($loadEntities);
  101. libxml_use_internal_errors($useInternalXmlErrors);
  102. #require_once 'Exception.php';
  103. throw new Zend_Xml_Exception(self::ENTITY_DETECT);
  104. }
  105. }
  106. }
  107. }
  108. // Entity load to previous setting
  109. if (!self::isPhpFpm()) {
  110. libxml_disable_entity_loader($loadEntities);
  111. libxml_use_internal_errors($useInternalXmlErrors);
  112. }
  113. if (isset($simpleXml)) {
  114. $result = simplexml_import_dom($dom);
  115. if (!$result instanceof SimpleXMLElement) {
  116. return false;
  117. }
  118. return $result;
  119. }
  120. return $dom;
  121. }
  122. /**
  123. * Scan XML file for potential XXE/XEE attacks
  124. *
  125. * @param string $file
  126. * @param DOMDocument $dom
  127. * @throws Zend_Xml_Exception
  128. * @return SimpleXMLElement|DomDocument
  129. */
  130. public static function scanFile($file, DOMDocument $dom = null)
  131. {
  132. if (!file_exists($file)) {
  133. #require_once 'Exception.php';
  134. throw new Zend_Xml_Exception(
  135. "The file $file specified doesn't exist"
  136. );
  137. }
  138. return self::scan(file_get_contents($file), $dom);
  139. }
  140. /**
  141. * Return true if PHP is running with PHP-FPM
  142. *
  143. * This method is mainly used to determine whether or not heuristic checks
  144. * (vs libxml checks) should be made, due to threading issues in libxml;
  145. * under php-fpm, threading becomes a concern.
  146. *
  147. * @return boolean
  148. */
  149. public static function isPhpFpm()
  150. {
  151. if (substr(php_sapi_name(), 0, 3) === 'fpm') {
  152. return true;
  153. }
  154. return false;
  155. }
  156. /**
  157. * Determine and return the string(s) to use for the <!ENTITY comparison.
  158. *
  159. * @param string $xml
  160. * @return string[]
  161. */
  162. protected static function getEntityComparison($xml)
  163. {
  164. $encodingMap = self::getAsciiEncodingMap();
  165. return array_map(
  166. array(__CLASS__, 'generateEntityComparison'),
  167. self::detectXmlEncoding($xml, self::detectStringEncoding($xml))
  168. );
  169. }
  170. /**
  171. * Determine the string encoding.
  172. *
  173. * Determines string encoding from either a detected BOM or a
  174. * heuristic.
  175. *
  176. * @param string $xml
  177. * @return string File encoding
  178. */
  179. protected static function detectStringEncoding($xml)
  180. {
  181. $encoding = self::detectBom($xml);
  182. return ($encoding) ? $encoding : self::detectXmlStringEncoding($xml);
  183. }
  184. /**
  185. * Attempt to match a known BOM.
  186. *
  187. * Iterates through the return of getBomMap(), comparing the initial bytes
  188. * of the provided string to the BOM of each; if a match is determined,
  189. * it returns the encoding.
  190. *
  191. * @param string $string
  192. * @return false|string Returns encoding on success.
  193. */
  194. protected static function detectBom($string)
  195. {
  196. foreach (self::getBomMap() as $criteria) {
  197. if (0 === strncmp($string, $criteria['bom'], $criteria['length'])) {
  198. return $criteria['encoding'];
  199. }
  200. }
  201. return false;
  202. }
  203. /**
  204. * Attempt to detect the string encoding of an XML string.
  205. *
  206. * @param string $xml
  207. * @return string Encoding
  208. */
  209. protected static function detectXmlStringEncoding($xml)
  210. {
  211. foreach (self::getAsciiEncodingMap() as $encoding => $generator) {
  212. $prefix = call_user_func($generator, '<' . '?xml');
  213. if (0 === strncmp($xml, $prefix, strlen($prefix))) {
  214. return $encoding;
  215. }
  216. }
  217. // Fallback
  218. return 'UTF-8';
  219. }
  220. /**
  221. * Attempt to detect the specified XML encoding.
  222. *
  223. * Using the file's encoding, determines if an "encoding" attribute is
  224. * present and well-formed in the XML declaration; if so, it returns a
  225. * list with both the ASCII representation of that declaration and the
  226. * original file encoding.
  227. *
  228. * If not, a list containing only the provided file encoding is returned.
  229. *
  230. * @param string $xml
  231. * @param string $fileEncoding
  232. * @return string[] Potential XML encodings
  233. */
  234. protected static function detectXmlEncoding($xml, $fileEncoding)
  235. {
  236. $encodingMap = self::getAsciiEncodingMap();
  237. $generator = $encodingMap[$fileEncoding];
  238. $encAttr = call_user_func($generator, 'encoding="');
  239. $quote = call_user_func($generator, '"');
  240. $close = call_user_func($generator, '>');
  241. $closePos = strpos($xml, $close);
  242. if (false === $closePos) {
  243. return array($fileEncoding);
  244. }
  245. $encPos = strpos($xml, $encAttr);
  246. if (false === $encPos
  247. || $encPos > $closePos
  248. ) {
  249. return array($fileEncoding);
  250. }
  251. $encPos += strlen($encAttr);
  252. $quotePos = strpos($xml, $quote, $encPos);
  253. if (false === $quotePos) {
  254. return array($fileEncoding);
  255. }
  256. $encoding = self::substr($xml, $encPos, $quotePos);
  257. return array(
  258. // Following line works because we're only supporting 8-bit safe encodings at this time.
  259. str_replace('\0', '', $encoding), // detected encoding
  260. $fileEncoding, // file encoding
  261. );
  262. }
  263. /**
  264. * Return a list of BOM maps.
  265. *
  266. * Returns a list of common encoding -> BOM maps, along with the character
  267. * length to compare against.
  268. *
  269. * @link https://en.wikipedia.org/wiki/Byte_order_mark
  270. * @return array
  271. */
  272. protected static function getBomMap()
  273. {
  274. return array(
  275. array(
  276. 'encoding' => 'UTF-32BE',
  277. 'bom' => pack('CCCC', 0x00, 0x00, 0xfe, 0xff),
  278. 'length' => 4,
  279. ),
  280. array(
  281. 'encoding' => 'UTF-32LE',
  282. 'bom' => pack('CCCC', 0xff, 0xfe, 0x00, 0x00),
  283. 'length' => 4,
  284. ),
  285. array(
  286. 'encoding' => 'GB-18030',
  287. 'bom' => pack('CCCC', 0x84, 0x31, 0x95, 0x33),
  288. 'length' => 4,
  289. ),
  290. array(
  291. 'encoding' => 'UTF-16BE',
  292. 'bom' => pack('CC', 0xfe, 0xff),
  293. 'length' => 2,
  294. ),
  295. array(
  296. 'encoding' => 'UTF-16LE',
  297. 'bom' => pack('CC', 0xff, 0xfe),
  298. 'length' => 2,
  299. ),
  300. array(
  301. 'encoding' => 'UTF-8',
  302. 'bom' => pack('CCC', 0xef, 0xbb, 0xbf),
  303. 'length' => 3,
  304. ),
  305. );
  306. }
  307. /**
  308. * Return a map of encoding => generator pairs.
  309. *
  310. * Returns a map of encoding => generator pairs, where the generator is a
  311. * callable that accepts a string and returns the appropriate byte order
  312. * sequence of that string for the encoding.
  313. *
  314. * @return array
  315. */
  316. protected static function getAsciiEncodingMap()
  317. {
  318. return array(
  319. 'UTF-32BE' => array(__CLASS__, 'encodeToUTF32BE'),
  320. 'UTF-32LE' => array(__CLASS__, 'encodeToUTF32LE'),
  321. 'UTF-32odd1' => array(__CLASS__, 'encodeToUTF32odd1'),
  322. 'UTF-32odd2' => array(__CLASS__, 'encodeToUTF32odd2'),
  323. 'UTF-16BE' => array(__CLASS__, 'encodeToUTF16BE'),
  324. 'UTF-16LE' => array(__CLASS__, 'encodeToUTF16LE'),
  325. 'UTF-8' => array(__CLASS__, 'encodeToUTF8'),
  326. 'GB-18030' => array(__CLASS__, 'encodeToUTF8'),
  327. );
  328. }
  329. /**
  330. * Binary-safe substr.
  331. *
  332. * substr() is not binary-safe; this method loops by character to ensure
  333. * multi-byte characters are aggregated correctly.
  334. *
  335. * @param string $string
  336. * @param int $start
  337. * @param int $end
  338. * @return string
  339. */
  340. protected static function substr($string, $start, $end)
  341. {
  342. $substr = '';
  343. for ($i = $start; $i < $end; $i += 1) {
  344. $substr .= $string[$i];
  345. }
  346. return $substr;
  347. }
  348. /**
  349. * Generate an entity comparison based on the given encoding.
  350. *
  351. * This patch is internal only, and public only so it can be used as a
  352. * callable to pass to array_map.
  353. *
  354. * @internal
  355. * @param string $encoding
  356. * @return string
  357. */
  358. public static function generateEntityComparison($encoding)
  359. {
  360. $encodingMap = self::getAsciiEncodingMap();
  361. $generator = isset($encodingMap[$encoding]) ? $encodingMap[$encoding] : $encodingMap['UTF-8'];
  362. return call_user_func($generator, '<!ENTITY');
  363. }
  364. /**
  365. * Encode an ASCII string to UTF-32BE
  366. *
  367. * @internal
  368. * @param string $ascii
  369. * @return string
  370. */
  371. public static function encodeToUTF32BE($ascii)
  372. {
  373. return preg_replace('/(.)/', "\0\0\0\\1", $ascii);
  374. }
  375. /**
  376. * Encode an ASCII string to UTF-32LE
  377. *
  378. * @internal
  379. * @param string $ascii
  380. * @return string
  381. */
  382. public static function encodeToUTF32LE($ascii)
  383. {
  384. return preg_replace('/(.)/', "\\1\0\0\0", $ascii);
  385. }
  386. /**
  387. * Encode an ASCII string to UTF-32odd1
  388. *
  389. * @internal
  390. * @param string $ascii
  391. * @return string
  392. */
  393. public static function encodeToUTF32odd1($ascii)
  394. {
  395. return preg_replace('/(.)/', "\0\\1\0\0", $ascii);
  396. }
  397. /**
  398. * Encode an ASCII string to UTF-32odd2
  399. *
  400. * @internal
  401. * @param string $ascii
  402. * @return string
  403. */
  404. public static function encodeToUTF32odd2($ascii)
  405. {
  406. return preg_replace('/(.)/', "\0\0\\1\0", $ascii);
  407. }
  408. /**
  409. * Encode an ASCII string to UTF-16BE
  410. *
  411. * @internal
  412. * @param string $ascii
  413. * @return string
  414. */
  415. public static function encodeToUTF16BE($ascii)
  416. {
  417. return preg_replace('/(.)/', "\0\\1", $ascii);
  418. }
  419. /**
  420. * Encode an ASCII string to UTF-16LE
  421. *
  422. * @internal
  423. * @param string $ascii
  424. * @return string
  425. */
  426. public static function encodeToUTF16LE($ascii)
  427. {
  428. return preg_replace('/(.)/', "\\1\0", $ascii);
  429. }
  430. /**
  431. * Encode an ASCII string to UTF-8
  432. *
  433. * @internal
  434. * @param string $ascii
  435. * @return string
  436. */
  437. public static function encodeToUTF8($ascii)
  438. {
  439. return $ascii;
  440. }
  441. }