SecurityTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. */
  38. class Zend_Xml_SecurityTest extends PHPUnit_Framework_TestCase
  39. {
  40. public static function main()
  41. {
  42. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  43. $result = PHPUnit_TextUI_TestRunner::run($suite);
  44. }
  45. public function testScanForXEE()
  46. {
  47. $xml = <<<XML
  48. <?xml version="1.0"?>
  49. <!DOCTYPE results [<!ENTITY harmless "completely harmless">]>
  50. <results>
  51. <result>This result is &harmless;</result>
  52. </results>
  53. XML;
  54. $this->setExpectedException('Zend_Xml_Exception');
  55. $result = Zend_Xml_Security::scan($xml);
  56. }
  57. public function testScanForXXE()
  58. {
  59. $file = tempnam(sys_get_temp_dir(), 'Zend_XML_Security');
  60. file_put_contents($file, 'This is a remote content!');
  61. $xml = <<<XML
  62. <?xml version="1.0"?>
  63. <!DOCTYPE root
  64. [
  65. <!ENTITY foo SYSTEM "file://$file">
  66. ]>
  67. <results>
  68. <result>&foo;</result>
  69. </results>
  70. XML;
  71. try {
  72. $result = Zend_Xml_Security::scan($xml);
  73. } catch (Zend_Xml_Exception $e) {
  74. unlink($file);
  75. return;
  76. }
  77. $this->fail('An expected exception has not been raised.');
  78. }
  79. public function testScanSimpleXmlResult()
  80. {
  81. $result = Zend_Xml_Security::scan($this->_getXml());
  82. $this->assertTrue($result instanceof SimpleXMLElement);
  83. $this->assertEquals((string) $result->result, 'test');
  84. }
  85. public function testScanDom()
  86. {
  87. $dom = new DOMDocument('1.0');
  88. $result = Zend_Xml_Security::scan($this->_getXml(), $dom);
  89. $this->assertTrue($result instanceof DOMDocument);
  90. $node = $result->getElementsByTagName('result')->item(0);
  91. $this->assertEquals($node->nodeValue, 'test');
  92. }
  93. public function testScanInvalidXml()
  94. {
  95. $xml = <<<XML
  96. <foo>test</bar>
  97. XML;
  98. $result = Zend_XML_Security::scan($xml);
  99. $this->assertFalse($result);
  100. }
  101. public function testScanInvalidXmlDom()
  102. {
  103. $xml = <<<XML
  104. <foo>test</bar>
  105. XML;
  106. $dom = new DOMDocument('1.0');
  107. $result = Zend_XML_Security::scan($xml, $dom);
  108. $this->assertFalse($result);
  109. }
  110. public function testScanFile()
  111. {
  112. $file = tempnam(sys_get_temp_dir(), 'Zend_XML_Security');
  113. file_put_contents($file, $this->_getXml());
  114. $result = Zend_Xml_Security::scanFile($file);
  115. $this->assertTrue($result instanceof SimpleXMLElement);
  116. $this->assertEquals((string) $result->result, 'test');
  117. unlink($file);
  118. }
  119. public function testScanXmlWithDTD()
  120. {
  121. $xml = <<<XML
  122. <?xml version="1.0"?>
  123. <!DOCTYPE results [
  124. <!ELEMENT results (result+)>
  125. <!ELEMENT result (#PCDATA)>
  126. ]>
  127. <results>
  128. <result>test</result>
  129. </results>
  130. XML;
  131. $dom = new DOMDocument('1.0');
  132. $result = Zend_Xml_Security::scan($xml, $dom);
  133. $this->assertTrue($result instanceof DOMDocument);
  134. $this->assertTrue($result->validate());
  135. }
  136. protected function _getXml()
  137. {
  138. return <<<XML
  139. <?xml version="1.0"?>
  140. <results>
  141. <result>test</result>
  142. </results>
  143. XML;
  144. }
  145. }
  146. if (PHPUnit_MAIN_METHOD == "Zend_Xml_SecurityTest::main") {
  147. Zend_Xml_SecurityTest::main();
  148. }