XmlScanner.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheet\Reader\Security;
  3. use PhpOffice\PhpSpreadsheet\Reader;
  4. class XmlScanner
  5. {
  6. /**
  7. * Identifies whether the thread-safe libxmlDisableEntityLoader() function is available.
  8. *
  9. * @var bool
  10. */
  11. private $libxmlDisableEntityLoader = false;
  12. /**
  13. * String used to identify risky xml elements.
  14. *
  15. * @var string
  16. */
  17. private $pattern;
  18. private $callback;
  19. private function __construct($pattern = '<!DOCTYPE')
  20. {
  21. $this->pattern = $pattern;
  22. $this->libxmlDisableEntityLoader = $this->identifyLibxmlDisableEntityLoaderAvailability();
  23. }
  24. public static function getInstance(Reader\IReader $reader)
  25. {
  26. switch (true) {
  27. case $reader instanceof Reader\Html:
  28. return new self('<!ENTITY');
  29. case $reader instanceof Reader\Xlsx:
  30. case $reader instanceof Reader\Xml:
  31. case $reader instanceof Reader\Ods:
  32. case $reader instanceof Reader\Gnumeric:
  33. return new self('<!DOCTYPE');
  34. default:
  35. return new self('<!DOCTYPE');
  36. }
  37. }
  38. private function identifyLibxmlDisableEntityLoaderAvailability()
  39. {
  40. if (PHP_MAJOR_VERSION == 7) {
  41. switch (PHP_MINOR_VERSION) {
  42. case 2:
  43. return PHP_RELEASE_VERSION >= 1;
  44. case 1:
  45. return PHP_RELEASE_VERSION >= 13;
  46. case 0:
  47. return PHP_RELEASE_VERSION >= 27;
  48. }
  49. return true;
  50. }
  51. return false;
  52. }
  53. public function setAdditionalCallback(callable $callback)
  54. {
  55. $this->callback = $callback;
  56. }
  57. /**
  58. * Scan the XML for use of <!ENTITY to prevent XXE/XEE attacks.
  59. *
  60. * @param mixed $xml
  61. *
  62. * @throws Reader\Exception
  63. *
  64. * @return string
  65. */
  66. public function scan($xml)
  67. {
  68. if ($this->libxmlDisableEntityLoader) {
  69. $previousLibxmlDisableEntityLoaderValue = libxml_disable_entity_loader(true);
  70. }
  71. $pattern = '/encoding="(.*?)"/';
  72. $result = preg_match($pattern, $xml, $matches);
  73. $charset = $result ? $matches[1] : 'UTF-8';
  74. if ($charset !== 'UTF-8') {
  75. $xml = mb_convert_encoding($xml, 'UTF-8', $charset);
  76. }
  77. // Don't rely purely on libxml_disable_entity_loader()
  78. $pattern = '/\\0?' . implode('\\0?', str_split($this->pattern)) . '\\0?/';
  79. try {
  80. if (preg_match($pattern, $xml)) {
  81. throw new Reader\Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
  82. }
  83. if ($this->callback !== null && is_callable($this->callback)) {
  84. $xml = call_user_func($this->callback, $xml);
  85. }
  86. } finally {
  87. if (isset($previousLibxmlDisableEntityLoaderValue)) {
  88. libxml_disable_entity_loader($previousLibxmlDisableEntityLoaderValue);
  89. }
  90. }
  91. return $xml;
  92. }
  93. /**
  94. * Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks.
  95. *
  96. * @param string $filestream
  97. *
  98. * @throws Reader\Exception
  99. *
  100. * @return string
  101. */
  102. public function scanFile($filestream)
  103. {
  104. return $this->scan(file_get_contents($filestream));
  105. }
  106. }