summaryrefslogtreecommitdiffstats
path: root/vendor
diff options
context:
space:
mode:
authorBernhard Posselt <dev@bernhard-posselt.com>2015-11-12 17:20:17 +0100
committerBernhard Posselt <dev@bernhard-posselt.com>2015-11-12 17:20:17 +0100
commitec700890c0484250edb4796b197d490df99daa42 (patch)
tree55c3fd03d8b60970097c907264e8b51cd28c937e /vendor
parentb575fec0ecedd20e7178b4e01da48ca2e997243c (diff)
update picofeed
Diffstat (limited to 'vendor')
-rw-r--r--vendor/composer/autoload_namespaces.php1
-rw-r--r--vendor/composer/installed.json58
-rw-r--r--vendor/fguillot/picofeed/lib/PicoFeed/Parser/XmlEntityException.php12
-rw-r--r--vendor/fguillot/picofeed/lib/PicoFeed/Parser/XmlParser.php108
-rw-r--r--vendor/fguillot/picofeed/lib/PicoFeed/Rules/neustadt-ticker.de.php10
-rw-r--r--vendor/zendframework/zendxml/.gitignore5
-rw-r--r--vendor/zendframework/zendxml/.travis.yml23
-rw-r--r--vendor/zendframework/zendxml/LICENSE.md12
-rw-r--r--vendor/zendframework/zendxml/README.md50
-rw-r--r--vendor/zendframework/zendxml/composer.json40
-rw-r--r--vendor/zendframework/zendxml/library/ZendXml/Exception/ExceptionInterface.php14
-rw-r--r--vendor/zendframework/zendxml/library/ZendXml/Exception/InvalidArgumentException.php17
-rw-r--r--vendor/zendframework/zendxml/library/ZendXml/Exception/RuntimeException.php17
-rw-r--r--vendor/zendframework/zendxml/library/ZendXml/Security.php374
-rw-r--r--vendor/zendframework/zendxml/tests/Bootstrap.php92
-rw-r--r--vendor/zendframework/zendxml/tests/ZendXmlTest/MultibyteTest.php125
-rw-r--r--vendor/zendframework/zendxml/tests/ZendXmlTest/SecurityTest.php135
-rwxr-xr-xvendor/zendframework/zendxml/tests/phpunit.xml.dist27
18 files changed, 1032 insertions, 88 deletions
diff --git a/vendor/composer/autoload_namespaces.php b/vendor/composer/autoload_namespaces.php
index 761f12c54..50cc89c0f 100644
--- a/vendor/composer/autoload_namespaces.php
+++ b/vendor/composer/autoload_namespaces.php
@@ -6,6 +6,7 @@ $vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);
return array(
+ 'ZendXml\\' => array($vendorDir . '/zendframework/zendxml/library'),
'PicoFeed' => array($vendorDir . '/fguillot/picofeed/lib'),
'HTMLPurifier' => array($vendorDir . '/ezyang/htmlpurifier/library'),
);
diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json
index 4e9deca41..c50f9a496 100644
--- a/vendor/composer/installed.json
+++ b/vendor/composer/installed.json
@@ -156,18 +156,65 @@
]
},
{
+ "name": "zendframework/zendxml",
+ "version": "1.0.1",
+ "version_normalized": "1.0.1.0",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/zendframework/ZendXml.git",
+ "reference": "54edb3875aba5b45f02824f65f311c9fb2743a38"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/zendframework/ZendXml/zipball/54edb3875aba5b45f02824f65f311c9fb2743a38",
+ "reference": "54edb3875aba5b45f02824f65f311c9fb2743a38",
+ "shasum": ""
+ },
+ "require": {
+ "php": ">=5.3.3"
+ },
+ "require-dev": {
+ "phpunit/phpunit": "~3.7",
+ "squizlabs/php_codesniffer": "~1.5"
+ },
+ "time": "2015-08-03 14:50:10",
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.0-dev"
+ }
+ },
+ "installation-source": "dist",
+ "autoload": {
+ "psr-0": {
+ "ZendXml\\": "library/"
+ }
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "BSD-3-Clause"
+ ],
+ "description": "Utility library for XML usage, best practices, and security in PHP",
+ "homepage": "http://packages.zendframework.com/",
+ "keywords": [
+ "security",
+ "xml",
+ "zf2"
+ ]
+ },
+ {
"name": "fguillot/picofeed",
"version": "dev-master",
"version_normalized": "9999999-dev",
"source": {
"type": "git",
"url": "https://github.com/fguillot/picoFeed.git",
- "reference": "ee91bcfd4be93d38ae5f870723c652a9d795c75f"
+ "reference": "27475c23f969274788decf61e4ec4bc9fde9e124"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/fguillot/picoFeed/zipball/ee91bcfd4be93d38ae5f870723c652a9d795c75f",
- "reference": "ee91bcfd4be93d38ae5f870723c652a9d795c75f",
+ "url": "https://api.github.com/repos/fguillot/picoFeed/zipball/27475c23f969274788decf61e4ec4bc9fde9e124",
+ "reference": "27475c23f969274788decf61e4ec4bc9fde9e124",
"shasum": ""
},
"require": {
@@ -176,12 +223,13 @@
"ext-libxml": "*",
"ext-simplexml": "*",
"ext-xml": "*",
- "php": ">=5.3.0"
+ "php": ">=5.3.0",
+ "zendframework/zendxml": "^1.0"
},
"suggest": {
"ext-curl": "PicoFeed will use cURL if present"
},
- "time": "2015-10-23 21:59:31",
+ "time": "2015-11-11 14:19:37",
"bin": [
"picofeed"
],
diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/XmlEntityException.php b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/XmlEntityException.php
new file mode 100644
index 000000000..f3f914d52
--- /dev/null
+++ b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/XmlEntityException.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace PicoFeed\Parser;
+
+/**
+ * XmlEntityException Exception.
+ *
+ * @author Bernhard Posselt
+ */
+class XmlEntityException extends MalformedXmlException
+{
+}
diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/XmlParser.php b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/XmlParser.php
index ea04a476f..7c8ebc602 100644
--- a/vendor/fguillot/picofeed/lib/PicoFeed/Parser/XmlParser.php
+++ b/vendor/fguillot/picofeed/lib/PicoFeed/Parser/XmlParser.php
@@ -2,9 +2,11 @@
namespace PicoFeed\Parser;
-use Closure;
use DomDocument;
use SimpleXmlElement;
+use Exception;
+
+use ZendXml\Security;
/**
* XML parser class.
@@ -26,64 +28,7 @@ class XmlParser
*/
public static function getSimpleXml($input)
{
- $dom = self::getDomDocument($input);
-
- if ($dom !== false) {
- $simplexml = simplexml_import_dom($dom);
-
- if (!$simplexml instanceof SimpleXmlElement) {
- return false;
- }
-
- return $simplexml;
- }
-
- return false;
- }
-
- /**
- * Scan the input for XXE attacks.
- *
- * @param string $input Unsafe input
- * @param Closure $callback Callback called to build the dom.
- * Must be an instance of DomDocument and receives the input as argument
- *
- * @return bool|DomDocument False if an XXE attack was discovered,
- * otherwise the return of the callback
- */
- private static function scanInput($input, Closure $callback)
- {
- $isRunningFpm = substr(php_sapi_name(), 0, 3) === 'fpm';
-
- if ($isRunningFpm) {
-
- // If running with PHP-FPM and an entity is detected we refuse to parse the feed
- // @see https://bugs.php.net/bug.php?id=64938
- if (strpos($input, '<!ENTITY') !== false) {
- return false;
- }
- } else {
- $entityLoaderDisabled = libxml_disable_entity_loader(true);
- }
-
- libxml_use_internal_errors(true);
-
- $dom = $callback($input);
-
- // Scan for potential XEE attacks using ENTITY
- foreach ($dom->childNodes as $child) {
- if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
- if ($child->entities->length > 0) {
- return false;
- }
- }
- }
-
- if ($isRunningFpm === false) {
- libxml_disable_entity_loader($entityLoaderDisabled);
- }
-
- return $dom;
+ return self::scan($input);
}
/**
@@ -101,12 +46,7 @@ class XmlParser
return false;
}
- $dom = self::scanInput($input, function ($in) {
- $dom = new DomDocument();
- $dom->loadXml($in, LIBXML_NONET);
-
- return $dom;
- });
+ $dom = self::scan($input, new DOMDocument());
// The document is empty, there is probably some parsing errors
if ($dom && $dom->childNodes->length === 0) {
@@ -117,6 +57,22 @@ class XmlParser
}
/**
+ * Small wrapper around ZendXml to turn their exceptions into picoFeed
+ * exceptions
+ * @param $input the xml to load
+ * @param $dom pass in a dom document or use null/omit if simpleXml should
+ * be used
+ */
+ private static function scan($input, $dom=null)
+ {
+ try {
+ return Security::scan($input, $dom);
+ } catch(\ZendXml\Exception\RuntimeException $e) {
+ throw new XmlEntityException($e->getMessage());
+ }
+ }
+
+ /**
* Load HTML document by using a DomDocument instance or return false on failure.
*
* @static
@@ -127,27 +83,21 @@ class XmlParser
*/
public static function getHtmlDocument($input)
{
+ $dom = new DomDocument();
+
if (empty($input)) {
- return new DomDocument();
+ return $dom;
}
- if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
- $callback = function ($in) {
- $dom = new DomDocument();
- $dom->loadHTML($in, LIBXML_NONET);
+ libxml_use_internal_errors(true);
- return $dom;
- };
+ if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
+ $dom->loadHTML($input, LIBXML_NONET);
} else {
- $callback = function ($in) {
- $dom = new DomDocument();
- $dom->loadHTML($in);
-
- return $dom;
- };
+ $dom->loadHTML($input);
}
- return self::scanInput($input, $callback);
+ return $dom;
}
/**
diff --git a/vendor/fguillot/picofeed/lib/PicoFeed/Rules/neustadt-ticker.de.php b/vendor/fguillot/picofeed/lib/PicoFeed/Rules/neustadt-ticker.de.php
index d17ed9192..60d9bfa51 100644
--- a/vendor/fguillot/picofeed/lib/PicoFeed/Rules/neustadt-ticker.de.php
+++ b/vendor/fguillot/picofeed/lib/PicoFeed/Rules/neustadt-ticker.de.php
@@ -3,11 +3,13 @@
return array(
'grabber' => array(
'%.*%' => array(
- 'test_url' => 'http://www.neustadt-ticker.de/36480/aktuell/nachrichten/buergerbuero-neustadt-ab-heute-wieder-geoeffnet',
- 'body' => array('//div[contains(@class,"article")]/div[@class="PostContent" and *[not(contains(@class, "navigation"))]]'),
+ 'test_url' => 'http://www.neustadt-ticker.de/41302/alltag/kultur/demo-auf-der-boehmischen',
+ 'body' => array(
+ '//div[@class="entry-content"]',
+ ),
'strip' => array(
- '//*[@id="wp_rp_first"]',
- '//*[@class="yarpp-related"]',
+ '//*[contains(@class, "sharedaddy")]',
+ '//*[contains(@class, "yarpp-related")]',
),
),
),
diff --git a/vendor/zendframework/zendxml/.gitignore b/vendor/zendframework/zendxml/.gitignore
new file mode 100644
index 000000000..0a4f6e27f
--- /dev/null
+++ b/vendor/zendframework/zendxml/.gitignore
@@ -0,0 +1,5 @@
+composer.lock
+vendor
+.buildpath
+.project
+.settings
diff --git a/vendor/zendframework/zendxml/.travis.yml b/vendor/zendframework/zendxml/.travis.yml
new file mode 100644
index 000000000..967d999ba
--- /dev/null
+++ b/vendor/zendframework/zendxml/.travis.yml
@@ -0,0 +1,23 @@
+language: php
+php:
+ - 5.3
+ - 5.4
+ - 5.5
+ - 5.6
+ - hhvm
+
+matrix:
+ allow_failures:
+ - php: hhvm
+
+before_script:
+ - composer self-update
+ - composer install --dev
+
+script:
+ - ./vendor/bin/phpunit -c ./tests
+ - ./vendor/bin/phpcs --standard=PSR2 --ignore=tests/Bootstrap.php library tests
+
+notifications:
+ irc: "irc.freenode.org#zftalk.dev"
+ email: false
diff --git a/vendor/zendframework/zendxml/LICENSE.md b/vendor/zendframework/zendxml/LICENSE.md
new file mode 100644
index 000000000..141d3a2dd
--- /dev/null
+++ b/vendor/zendframework/zendxml/LICENSE.md
@@ -0,0 +1,12 @@
+Copyright (c) 2014-2015, Zend Technologies USA, Inc.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
+
+- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+
+- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+
+- Neither the name of Zend Technologies USA, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/vendor/zendframework/zendxml/README.md b/vendor/zendframework/zendxml/README.md
new file mode 100644
index 000000000..2c67008da
--- /dev/null
+++ b/vendor/zendframework/zendxml/README.md
@@ -0,0 +1,50 @@
+ZendXml
+=======
+
+An utility component for XML usage and best practices in PHP
+
+Installation
+------------
+
+You can install using:
+
+```
+curl -s https://getcomposer.org/installer | php
+php composer.phar install
+```
+
+Notice that this library doesn't have any external dependencies, the usage of composer is for autoloading and standard purpose.
+
+
+ZendXml\Security
+----------------
+
+This is a security component to prevent [XML eXternal Entity](https://www.owasp.org/index.php/XML_External_Entity_%28XXE%29_Processing) (XXE) and [XML Entity Expansion](http://projects.webappsec.org/w/page/13247002/XML%20Entity%20Expansion) (XEE) attacks on XML documents.
+
+The XXE attack is prevented disabling the load of external entities in the libxml library used by PHP, using the function [libxml_disable_entity_loader](http://www.php.net/manual/en/function.libxml-disable-entity-loader.php).
+
+The XEE attack is prevented looking inside the XML document for ENTITY usage. If the XML document uses ENTITY the library throw an Exception.
+
+We have two static methods to scan and load XML document from a string (scan) and from a file (scanFile). You can decide to get a SimpleXMLElement or DOMDocument as result, using the following use cases:
+
+```php
+use ZendXml\Security as XmlSecurity;
+
+$xml = <<<XML
+<?xml version="1.0"?>
+<results>
+ <result>test</result>
+</results>
+XML;
+
+// SimpleXML use case
+$simplexml = XmlSecurity::scan($xml);
+printf ("SimpleXMLElement: %s\n", ($simplexml instanceof \SimpleXMLElement) ? 'yes' : 'no');
+
+// DOMDocument use case
+$dom = new \DOMDocument('1.0');
+$dom = XmlSecurity::scan($xml, $dom);
+printf ("DOMDocument: %s\n", ($dom instanceof \DOMDocument) ? 'yes' : 'no');
+```
+
+
diff --git a/vendor/zendframework/zendxml/composer.json b/vendor/zendframework/zendxml/composer.json
new file mode 100644
index 000000000..139f1e23b
--- /dev/null
+++ b/vendor/zendframework/zendxml/composer.json
@@ -0,0 +1,40 @@
+{
+ "name": "zendframework/zendxml",
+ "description": "Utility library for XML usage, best practices, and security in PHP",
+ "type": "library",
+ "license": "BSD-3-Clause",
+ "keywords": [
+ "zf2",
+ "xml",
+ "security"
+ ],
+ "homepage": "http://packages.zendframework.com/",
+ "autoload": {
+ "psr-0": {
+ "ZendXml\\": "library/"
+ }
+ },
+ "autoload-dev": {
+ "psr-4": {
+ "ZendTest\\Xml\\": "tests/ZendXmlTest/"
+ }
+ },
+ "repositories": [
+ {
+ "type": "composer",
+ "url": "http://packages.zendframework.com/"
+ }
+ ],
+ "require": {
+ "php": ">=5.3.3"
+ },
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.0-dev"
+ }
+ },
+ "require-dev": {
+ "phpunit/phpunit": "~3.7",
+ "squizlabs/php_codesniffer": "~1.5"
+ }
+}
diff --git a/vendor/zendframework/zendxml/library/ZendXml/Exception/ExceptionInterface.php b/vendor/zendframework/zendxml/library/ZendXml/Exception/ExceptionInterface.php
new file mode 100644
index 000000000..c55eb9034
--- /dev/null
+++ b/vendor/zendframework/zendxml/library/ZendXml/Exception/ExceptionInterface.php
@@ -0,0 +1,14 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+namespace ZendXml\Exception;
+
+interface ExceptionInterface
+{
+}
diff --git a/vendor/zendframework/zendxml/library/ZendXml/Exception/InvalidArgumentException.php b/vendor/zendframework/zendxml/library/ZendXml/Exception/InvalidArgumentException.php
new file mode 100644
index 000000000..0fef6b298
--- /dev/null
+++ b/vendor/zendframework/zendxml/library/ZendXml/Exception/InvalidArgumentException.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+namespace ZendXml\Exception;
+
+/**
+ * Invalid argument exception
+ */
+class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
+{
+}
diff --git a/vendor/zendframework/zendxml/library/ZendXml/Exception/RuntimeException.php b/vendor/zendframework/zendxml/library/ZendXml/Exception/RuntimeException.php
new file mode 100644
index 000000000..b730da4ff
--- /dev/null
+++ b/vendor/zendframework/zendxml/library/ZendXml/Exception/RuntimeException.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+namespace ZendXml\Exception;
+
+/**
+ * Runtime exception
+ */
+class RuntimeException extends \RuntimeException implements ExceptionInterface
+{
+}
diff --git a/vendor/zendframework/zendxml/library/ZendXml/Security.php b/vendor/zendframework/zendxml/library/ZendXml/Security.php
new file mode 100644
index 000000000..892e2ab14
--- /dev/null
+++ b/vendor/zendframework/zendxml/library/ZendXml/Security.php
@@ -0,0 +1,374 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+namespace ZendXml;
+
+use DOMDocument;
+use SimpleXMLElement;
+
+class Security
+{
+ const ENTITY_DETECT = 'Detected use of ENTITY in XML, disabled to prevent XXE/XEE attacks';
+
+ /**
+ * Heuristic scan to detect entity in XML
+ *
+ * @param string $xml
+ * @throws Exception\RuntimeException If entity expansion or external entity declaration was discovered.
+ */
+ protected static function heuristicScan($xml)
+ {
+ foreach (self::getEntityComparison($xml) as $compare) {
+ if (strpos($xml, $compare) !== false) {
+ throw new Exception\RuntimeException(self::ENTITY_DETECT);
+ }
+ }
+ }
+
+ /**
+ * Scan XML string for potential XXE and XEE attacks
+ *
+ * @param string $xml
+ * @param DomDocument $dom
+ * @throws Exception\RuntimeException
+ * @return SimpleXMLElement|DomDocument|boolean
+ */
+ public static function scan($xml, DOMDocument $dom = null)
+ {
+ // If running with PHP-FPM we perform an heuristic scan
+ // We cannot use libxml_disable_entity_loader because of this bug
+ // @see https://bugs.php.net/bug.php?id=64938
+ if (self::isPhpFpm()) {
+ self::heuristicScan($xml);
+ }
+
+ if (null === $dom) {
+ $simpleXml = true;
+ $dom = new DOMDocument();
+ }
+
+ if (!self::isPhpFpm()) {
+ $loadEntities = libxml_disable_entity_loader(true);
+ $useInternalXmlErrors = libxml_use_internal_errors(true);
+ }
+
+ // Load XML with network access disabled (LIBXML_NONET)
+ // error disabled with @ for PHP-FPM scenario
+ set_error_handler(function ($errno, $errstr) {
+ if (substr_count($errstr, 'DOMDocument::loadXML()') > 0) {
+ return true;
+ }
+ return false;
+ }, E_WARNING);
+ $result = $dom->loadXml($xml, LIBXML_NONET);
+ restore_error_handler();
+
+ if (!$result) {
+ // Entity load to previous setting
+ if (!self::isPhpFpm()) {
+ libxml_disable_entity_loader($loadEntities);
+ libxml_use_internal_errors($useInternalXmlErrors);
+ }
+ return false;
+ }
+
+ // Scan for potential XEE attacks using ENTITY, if not PHP-FPM
+ if (!self::isPhpFpm()) {
+ foreach ($dom->childNodes as $child) {
+ if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
+ if ($child->entities->length > 0) {
+ throw new Exception\RuntimeException(self::ENTITY_DETECT);
+ }
+ }
+ }
+ }
+
+ // Entity load to previous setting
+ if (!self::isPhpFpm()) {
+ libxml_disable_entity_loader($loadEntities);
+ libxml_use_internal_errors($useInternalXmlErrors);
+ }
+
+ if (isset($simpleXml)) {
+ $result = simplexml_import_dom($dom);
+ if (!$result instanceof SimpleXMLElement) {
+ return false;
+ }
+ return $result;
+ }
+ return $dom;
+ }
+
+ /**
+ * Scan XML file for potential XXE/XEE attacks
+ *
+ * @param string $file
+ * @param DOMDocument $dom
+ * @throws Exception\InvalidArgumentException
+ * @return SimpleXMLElement|DomDocument
+ */
+ public static function scanFile($file, DOMDocument $dom = null)
+ {
+ if (!file_exists($file)) {
+ throw new Exception\InvalidArgumentException(
+ "The file $file specified doesn't exist"
+ );
+ }
+ return self::scan(file_get_contents($file), $dom);
+ }
+
+ /**
+ * Return true if PHP is running with PHP-FPM
+ *
+ * This method is mainly used to determine whether or not heuristic checks
+ * (vs libxml checks) should be made, due to threading issues in libxml;
+ * under php-fpm, threading becomes a concern.
+ *
+ * However, PHP versions 5.5.22+ and 5.6.6+ contain a patch to the
+ * libxml support in PHP that makes the libxml checks viable; in such
+ * versions, this method will return false to enforce those checks, which
+ * are more strict and accurate than the heuristic checks.
+ *
+ * @return boolean
+ */
+ public static function isPhpFpm()
+ {
+ $isVulnerableVersion = (
+ version_compare(PHP_VERSION, '5.5.22', 'lt')
+ || (
+ version_compare(PHP_VERSION, '5.6', 'gte')
+ && version_compare(PHP_VERSION, '5.6.6', 'lt')
+ )
+ );
+
+ if (substr(php_sapi_name(), 0, 3) === 'fpm' && $isVulnerableVersion) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Determine and return the string(s) to use for the <!ENTITY comparison.
+ *
+ * @param string $xml
+ * @return string[]
+ */
+ protected static function getEntityComparison($xml)
+ {
+ $encodingMap = self::getAsciiEncodingMap();
+ return array_map(function ($encoding) use ($encodingMap) {
+ $generator = isset($encodingMap[$encoding]) ? $encodingMap[$encoding] : $encodingMap['UTF-8'];
+ return $generator('<!ENTITY');
+ }, self::detectXmlEncoding($xml, self::detectStringEncoding($xml)));
+ }
+
+ /**
+ * Determine the string encoding.
+ *
+ * Determines string encoding from either a detected BOM or a
+ * heuristic.
+ *
+ * @param string $xml
+ * @return string File encoding
+ */
+ protected static function detectStringEncoding($xml)
+ {
+ return self::detectBom($xml) ?: self::detectXmlStringEncoding($xml);
+ }
+
+ /**
+ * Attempt to match a known BOM.
+ *
+ * Iterates through the return of getBomMap(), comparing the initial bytes
+ * of the provided string to the BOM of each; if a match is determined,
+ * it returns the encoding.
+ *
+ * @param string $string
+ * @return false|string Returns encoding on success.
+ */
+ protected static function detectBom($string)
+ {
+ foreach (self::getBomMap() as $criteria) {
+ if (0 === strncmp($string, $criteria['bom'], $criteria['length'])) {
+ return $criteria['encoding'];
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Attempt to detect the string encoding of an XML string.
+ *
+ * @param string $xml
+ * @return string Encoding
+ */
+ protected static function detectXmlStringEncoding($xml)
+ {
+ foreach (self::getAsciiEncodingMap() as $encoding => $generator) {
+ $prefix = $generator('<' . '?xml');
+ if (0 === strncmp($xml, $prefix, strlen($prefix))) {
+ return $encoding;
+ }
+ }
+
+ // Fallback
+ return 'UTF-8';
+ }
+
+ /**
+ * Attempt to detect the specified XML encoding.
+ *
+ * Using the file's encoding, determines if an "encoding" attribute is
+ * present and well-formed in the XML declaration; if so, it returns a
+ * list with both the ASCII representation of that declaration and the
+ * original file encoding.
+ *
+ * If not, a list containing only the provided file encoding is returned.
+ *
+ * @param string $xml
+ * @param string $fileEncoding
+ * @return string[] Potential XML encodings
+ */
+ protected static function detectXmlEncoding($xml, $fileEncoding)
+ {
+ $encodingMap = self::getAsciiEncodingMap();
+ $generator = $encodingMap[$fileEncoding];
+ $encAttr = $generator('encoding="');
+ $quote = $generator('"');
+ $close = $generator('>');
+
+ $closePos = strpos($xml, $close);
+ if (false === $closePos) {
+ return array($fileEncoding);
+ }
+
+ $encPos = strpos($xml, $encAttr);
+ if (false === $encPos
+ || $encPos > $closePos
+ ) {
+ return array($fileEncoding);
+ }
+
+ $encPos += strlen($encAttr);
+ $quotePos = strpos($xml, $quote, $encPos);
+ if (false === $quotePos) {
+ return array($fileEncoding);
+ }
+
+ $encoding = self::substr($xml, $encPos, $quotePos);
+ return array(
+ // Following line works because we're only supporting 8-bit safe encodings at this time.
+ str_replace('\0', '', $encoding), // detected encoding
+ $fileEncoding, // file encoding
+ );
+ }
+
+ /**
+ * Return a list of BOM maps.
+ *
+ * Returns a list of common encoding -> BOM maps, along with the character
+ * length to compare against.
+ *
+ * @link https://en.wikipedia.org/wiki/Byte_order_mark
+ * @return array
+ */
+ protected static function getBomMap()
+ {
+ return array(
+ array(
+ 'encoding' => 'UTF-32BE',
+ 'bom' => pack('CCCC', 0x00, 0x00, 0xfe, 0xff),
+ 'length' => 4,
+ ),
+ array(
+ 'encoding' => 'UTF-32LE',
+ 'bom' => pack('CCCC', 0xff, 0xfe, 0x00, 0x00),
+ 'length' => 4,
+ ),
+ array(
+ 'encoding' => 'GB-18030',
+ 'bom' => pack('CCCC', 0x84, 0x31, 0x95, 0x33),
+ 'length' => 4,
+ ),