MultibyteTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_Security
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Xml_SecurityTest::main');
  24. }
  25. /**
  26. * @see Zend_Xml_Security
  27. */
  28. require_once 'Zend/Xml/Security.php';
  29. require_once 'Zend/Xml/Exception.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Xml_Security
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Xml
  37. * @group ZF2015-06
  38. */
  39. class Zend_Xml_MultibyteTest extends PHPUnit_Framework_TestCase
  40. {
  41. public static function main()
  42. {
  43. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  44. $result = PHPUnit_TextUI_TestRunner::run($suite);
  45. }
  46. public function multibyteEncodings()
  47. {
  48. return array(
  49. 'UTF-16LE' => array('UTF-16LE', pack('CC', 0xff, 0xfe), 3),
  50. 'UTF-16BE' => array('UTF-16BE', pack('CC', 0xfe, 0xff), 3),
  51. 'UTF-32LE' => array('UTF-32LE', pack('CCCC', 0xff, 0xfe, 0x00, 0x00), 4),
  52. 'UTF-32BE' => array('UTF-32BE', pack('CCCC', 0x00, 0x00, 0xfe, 0xff), 4),
  53. );
  54. }
  55. public function getXmlWithXXE()
  56. {
  57. return <<<XML
  58. <?xml version="1.0" encoding="{ENCODING}"?>
  59. <!DOCTYPE methodCall [
  60. <!ENTITY pocdata SYSTEM "file:///etc/passwd">
  61. ]>
  62. <methodCall>
  63. <methodName>retrieved: &pocdata;</methodName>
  64. </methodCall>
  65. XML;
  66. }
  67. /**
  68. * Invoke Zend_Xml_Security::heuristicScan with the provided XML.
  69. *
  70. * @param string $xml
  71. * @return void
  72. * @throws Zend_Xml_Exception
  73. */
  74. public function invokeHeuristicScan($xml)
  75. {
  76. $r = new ReflectionMethod('Zend_Xml_Security', 'heuristicScan');
  77. $r->setAccessible(true);
  78. return $r->invoke(null, $xml);
  79. }
  80. /**
  81. * @dataProvider multibyteEncodings
  82. * @group heuristicDetection
  83. */
  84. public function testDetectsMultibyteXXEVectorsUnderFPMWithEncodedStringMissingBOM($encoding, $bom, $bomLength)
  85. {
  86. $xml = $this->getXmlWithXXE();
  87. $xml = str_replace('{ENCODING}', $encoding, $xml);
  88. $xml = iconv('UTF-8', $encoding, $xml);
  89. $this->assertNotSame(0, strncmp($xml, $bom, $bomLength));
  90. $this->setExpectedException('Zend_Xml_Exception', 'ENTITY');
  91. $this->invokeHeuristicScan($xml);
  92. }
  93. /**
  94. * @dataProvider multibyteEncodings
  95. */
  96. public function testDetectsMultibyteXXEVectorsUnderFPMWithEncodedStringUsingBOM($encoding, $bom)
  97. {
  98. $xml = $this->getXmlWithXXE();
  99. $xml = str_replace('{ENCODING}', $encoding, $xml);
  100. $orig = iconv('UTF-8', $encoding, $xml);
  101. $xml = $bom . $orig;
  102. $this->setExpectedException('Zend_Xml_Exception', 'ENTITY');
  103. $this->invokeHeuristicScan($xml);
  104. }
  105. public function getXmlWithoutXXE()
  106. {
  107. return <<<XML
  108. <?xml version="1.0" encoding="{ENCODING}"?>
  109. <methodCall>
  110. <methodName>retrieved: &pocdata;</methodName>
  111. </methodCall>
  112. XML;
  113. }
  114. /**
  115. * @dataProvider multibyteEncodings
  116. */
  117. public function testDoesNotFlagValidMultibyteXmlAsInvalidUnderFPM($encoding)
  118. {
  119. $xml = $this->getXmlWithoutXXE();
  120. $xml = str_replace('{ENCODING}', $encoding, $xml);
  121. $xml = iconv('UTF-8', $encoding, $xml);
  122. try {
  123. $result = $this->invokeHeuristicScan($xml);
  124. $this->assertNull($result);
  125. } catch (Exception $e) {
  126. $this->fail('Security scan raised exception when it should not have');
  127. }
  128. }
  129. /**
  130. * @dataProvider multibyteEncodings
  131. * @group mixedEncoding
  132. */
  133. public function testDetectsXXEWhenXMLDocumentEncodingDiffersFromFileEncoding($encoding, $bom)
  134. {
  135. $xml = $this->getXmlWithXXE();
  136. $xml = str_replace('{ENCODING}', 'UTF-8', $xml);
  137. $xml = iconv('UTF-8', $encoding, $xml);
  138. $xml = $bom . $xml;
  139. $this->setExpectedException('Zend_Xml_Exception', 'ENTITY');
  140. $this->invokeHeuristicScan($xml);
  141. }
  142. }
  143. if (PHPUnit_MAIN_METHOD == "Zend_Xml_MultibyteTest::main") {
  144. Zend_Xml_MultibyteTest::main();
  145. }