bug #18915 [DependencyInjection] force enabling the external XML entity loaders (xabbuh)

This PR was merged into the 2.7 branch.

Discussion
----------

[DependencyInjection] force enabling the external XML entity loaders

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18876, #18908
| License       | MIT
| Doc PR        |

Commits
-------

12b5509 force enabling the external XML entity loaders
This commit is contained in:
Fabien Potencier 2016-06-13 08:25:24 +02:00
commit 3f192dccdc
4 changed files with 35 additions and 1 deletions

View File

@ -315,7 +315,6 @@ class XmlFileLoader extends FileLoader
foreach ($definitions as $id => $def) { foreach ($definitions as $id => $def) {
list($domElement, $file, $wild) = $def; list($domElement, $file, $wild) = $def;
if (null !== $definition = $this->parseDefinition($domElement, $file)) { if (null !== $definition = $this->parseDefinition($domElement, $file)) {
$this->container->setDefinition($id, $definition); $this->container->setDefinition($id, $definition);
} }
@ -485,7 +484,9 @@ $imports
EOF EOF
; ;
$disableEntities = libxml_disable_entity_loader(false);
$valid = @$dom->schemaValidateSource($source); $valid = @$dom->schemaValidateSource($source);
libxml_disable_entity_loader($disableEntities);
foreach ($tmpfiles as $tmpfile) { foreach ($tmpfiles as $tmpfile) {
@unlink($tmpfile); @unlink($tmpfile);

View File

@ -84,6 +84,19 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceOf('DOMDocument', $xml, '->parseFileToDOM() returns an SimpleXMLElement object'); $this->assertInstanceOf('DOMDocument', $xml, '->parseFileToDOM() returns an SimpleXMLElement object');
} }
public function testLoadWithExternalEntitiesDisabled()
{
$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);
$this->assertTrue(count($containerBuilder->getParameterBag()->all()) > 0, 'Parameters can be read from the config file.');
}
public function testLoadParameters() public function testLoadParameters()
{ {
$container = new ContainerBuilder(); $container = new ContainerBuilder();

View File

@ -142,10 +142,16 @@ class XliffFileLoader implements LoaderInterface
$source = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-1.2-strict.xsd'); $source = file_get_contents(__DIR__.'/schema/dic/xliff-core/xliff-core-1.2-strict.xsd');
$source = str_replace('http://www.w3.org/2001/xml.xsd', $location, $source); $source = str_replace('http://www.w3.org/2001/xml.xsd', $location, $source);
$disableEntities = libxml_disable_entity_loader(false);
if (!@$dom->schemaValidateSource($source)) { if (!@$dom->schemaValidateSource($source)) {
libxml_disable_entity_loader($disableEntities);
throw new InvalidResourceException(sprintf('Invalid resource provided: "%s"; Errors: %s', $file, implode("\n", $this->getXmlErrors($internalErrors)))); throw new InvalidResourceException(sprintf('Invalid resource provided: "%s"; Errors: %s', $file, implode("\n", $this->getXmlErrors($internalErrors))));
} }
libxml_disable_entity_loader($disableEntities);
$dom->normalizeDocument(); $dom->normalizeDocument();
libxml_clear_errors(); libxml_clear_errors();

View File

@ -46,6 +46,20 @@ class XliffFileLoaderTest extends \PHPUnit_Framework_TestCase
libxml_use_internal_errors($internalErrors); libxml_use_internal_errors($internalErrors);
} }
public function testLoadWithExternalEntitiesDisabled()
{
$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);
$this->assertEquals('en', $catalogue->getLocale());
$this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
}
public function testLoadWithResname() public function testLoadWithResname()
{ {
$loader = new XliffFileLoader(); $loader = new XliffFileLoader();