Skip to content

Fix deprecated libxml_disable_entity_loader #37763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ public function testLoadEmptyXmlFile()
// test for issue https://github.com/symfony/symfony/issues/9731
public function testLoadWrongEmptyXMLWithErrorHandler()
{
$originalDisableEntities = libxml_disable_entity_loader(false);
if (LIBXML_VERSION < 20900) {
$originalDisableEntities = libxml_disable_entity_loader(false);
}
$errorReporting = error_reporting(-1);

set_error_handler(function ($errno, $errstr) {
Expand All @@ -219,12 +221,13 @@ public function testLoadWrongEmptyXMLWithErrorHandler()
error_reporting($errorReporting);
}

$disableEntities = libxml_disable_entity_loader(true);
libxml_disable_entity_loader($disableEntities);

libxml_disable_entity_loader($originalDisableEntities);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
libxml_disable_entity_loader($disableEntities);

$this->assertFalse($disableEntities);
libxml_disable_entity_loader($originalDisableEntities);
$this->assertFalse($disableEntities);
}

// should not throw an exception
XmlUtils::loadFile(__DIR__.'/../Fixtures/Util/valid.xml', __DIR__.'/../Fixtures/Util/schema.xsd');
Expand Down
12 changes: 9 additions & 3 deletions src/Symfony/Component/Config/Util/XmlUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,27 @@ public static function parse($content, $schemaOrCallable = null)
}

$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
}
libxml_clear_errors();

$dom = new \DOMDocument();
$dom->validateOnParse = true;
if (!$dom->loadXML($content, LIBXML_NONET | (\defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0))) {
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}

throw new XmlParsingException(implode("\n", static::getXmlErrors($internalErrors)));
}

$dom->normalizeDocument();

libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}

foreach ($dom->childNodes as $child) {
if (XML_DOCUMENT_TYPE_NODE === $child->nodeType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,13 @@ public function validateSchema(\DOMDocument $dom)
EOF
;

$disableEntities = libxml_disable_entity_loader(false);
$valid = @$dom->schemaValidateSource($source);
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(false);
$valid = @$dom->schemaValidateSource($source);
libxml_disable_entity_loader($disableEntities);
} else {
$valid = @$dom->schemaValidateSource($source);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this change: in previous code, the entity_loader were enabled, but the flag LIBXML_NOENT is not supported by schemaValidateSource.

Copy link
Member Author

@jderusse jderusse Aug 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

foreach ($tmpfiles as $tmpfile) {
@unlink($tmpfile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,17 @@ public function testParseFile()

public function testLoadWithExternalEntitiesDisabled()
{
$disableEntities = libxml_disable_entity_loader(true);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
}

$containerBuilder = new ContainerBuilder();
$loader = new XmlFileLoader($containerBuilder, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('services2.xml');

libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}

$this->assertGreaterThan(0, $containerBuilder->getParameterBag()->all(), 'Parameters can be read from the config file.');
}
Expand Down
16 changes: 12 additions & 4 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ public function addContent($content, $type = null)
public function addHtmlContent($content, $charset = 'UTF-8')
{
$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
}

$dom = new \DOMDocument('1.0', $charset);
$dom->validateOnParse = true;
Expand All @@ -203,7 +205,9 @@ public function addHtmlContent($content, $charset = 'UTF-8')
}

libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}

$this->addDocument($dom);

Expand Down Expand Up @@ -246,7 +250,9 @@ public function addXmlContent($content, $charset = 'UTF-8', $options = LIBXML_NO
}

$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
}

$dom = new \DOMDocument('1.0', $charset);
$dom->validateOnParse = true;
Expand All @@ -256,7 +262,9 @@ public function addXmlContent($content, $charset = 'UTF-8', $options = LIBXML_NO
}

libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}

$this->addDocument($dom);

Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,18 @@ public function decode($data, $format, array $context = [])
}

$internalErrors = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
}
libxml_clear_errors();

$dom = new \DOMDocument();
$dom->loadXML($data, $this->loadOptions);

libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}

if ($error = libxml_get_last_error()) {
libxml_clear_errors();
Expand Down
12 changes: 7 additions & 5 deletions src/Symfony/Component/Translation/Loader/XliffFileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,18 @@ private function validateSchema($file, \DOMDocument $dom, $schema)
{
$internalErrors = libxml_use_internal_errors(true);

$disableEntities = libxml_disable_entity_loader(false);

if (!@$dom->schemaValidateSource($schema)) {
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(false);
$isValid = @$dom->schemaValidateSource($schema);
libxml_disable_entity_loader($disableEntities);
} else {
$isValid = @$dom->schemaValidateSource($schema);
}

if (!$isValid) {
throw new InvalidResourceException(sprintf('Invalid resource provided: "%s"; Errors: ', $file).implode("\n", $this->getXmlErrors($internalErrors)));
}

libxml_disable_entity_loader($disableEntities);

$dom->normalizeDocument();

libxml_clear_errors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ public function testLoadWithInternalErrorsEnabled()

public function testLoadWithExternalEntitiesDisabled()
{
$disableEntities = libxml_disable_entity_loader(true);
if (LIBXML_VERSION < 20900) {
$disableEntities = libxml_disable_entity_loader(true);
}

$loader = new XliffFileLoader();
$resource = __DIR__.'/../fixtures/resources.xlf';
$catalogue = $loader->load($resource, 'en', 'domain1');

libxml_disable_entity_loader($disableEntities);
if (LIBXML_VERSION < 20900) {
libxml_disable_entity_loader($disableEntities);
}

$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals([new FileResource($resource)], $catalogue->getResources());
Expand Down