|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpOffice\Math\Reader\Security; |
| 4 | + |
| 5 | +use PhpOffice\Math\Exception\SecurityException; |
| 6 | + |
| 7 | +class XmlScanner |
| 8 | +{ |
| 9 | + public static function getInstance(): self |
| 10 | + { |
| 11 | + return new self(); |
| 12 | + } |
| 13 | + |
| 14 | + /** |
| 15 | + * Scan the XML for use of <!ENTITY to prevent XXE/XEE attacks. |
| 16 | + */ |
| 17 | + public function scan(string $xml): string |
| 18 | + { |
| 19 | + // Don't rely purely on libxml_disable_entity_loader() |
| 20 | + $patternDoctype = '/\0*' . implode('\0*', static::mb_str_split('<!DOCTYPE', 1, 'UTF-8')) . '\0*/'; |
| 21 | + $patternDoctypeMath = '/\0*' . implode('\0*', static::mb_str_split('<!DOCTYPE math', 1, 'UTF-8')) . '\0*/'; |
| 22 | + |
| 23 | + if (preg_match($patternDoctype, $xml) && !preg_match($patternDoctypeMath, $xml)) { |
| 24 | + throw new SecurityException('Detected use of ENTITY in XML, loading aborted to prevent XXE/XEE attacks'); |
| 25 | + } |
| 26 | + |
| 27 | + return $xml; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @param string $string |
| 32 | + * @param integer $split_length |
| 33 | + * @param string|null $encoding |
| 34 | + * @return array|bool |
| 35 | + */ |
| 36 | + public static function mb_str_split(string $string, $split_length = 1, ?string $encoding = null) |
| 37 | + { |
| 38 | + if (extension_loaded('mbstring')) { |
| 39 | + if (function_exists('mb_str_split')) { |
| 40 | + return mb_str_split($string, $split_length, $encoding); |
| 41 | + } |
| 42 | + } |
| 43 | + if (null !== $string && !\is_scalar($string) && !(\is_object($string) && method_exists($string, '__toString'))) { |
| 44 | + trigger_error('mb_str_split() expects parameter 1 to be string, '.\gettype($string).' given', \E_USER_WARNING); |
| 45 | + |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + if (1 > $split_length = (int) $split_length) { |
| 50 | + trigger_error('The length of each segment must be greater than zero', \E_USER_WARNING); |
| 51 | + |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + if (null === $encoding) { |
| 56 | + $encoding = mb_internal_encoding(); |
| 57 | + } |
| 58 | + |
| 59 | + if ('UTF-8' === $encoding || \in_array(strtoupper($encoding), ['UTF-8', 'UTF8'], true)) { |
| 60 | + return preg_split("/(.{{$split_length}})/u", $string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY); |
| 61 | + } |
| 62 | + |
| 63 | + $result = []; |
| 64 | + $length = mb_strlen($string, $encoding); |
| 65 | + |
| 66 | + for ($i = 0; $i < $length; $i += $split_length) { |
| 67 | + $result[] = mb_substr($string, $i, $split_length, $encoding); |
| 68 | + } |
| 69 | + |
| 70 | + return $result; |
| 71 | + } |
| 72 | +} |
0 commit comments