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