Skip to content

Commit

Permalink
prevents injection of malicious doc types
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Aug 28, 2012
1 parent 47fe725 commit 4e0c992
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 0 deletions.
Expand Up @@ -223,6 +223,12 @@ private function parseFile($file)

libxml_use_internal_errors($internalErrors);

foreach ($dom->childNodes as $child) {
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
throw new \InvalidArgumentException('Document types are not allowed.');
}
}

$this->validate($dom, $file);

return simplexml_import_dom($dom, 'Symfony\\Component\\DependencyInjection\\SimpleXMLElement');
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Routing/Loader/XmlFileLoader.php
Expand Up @@ -162,6 +162,12 @@ protected function loadFile($file)

libxml_use_internal_errors($internalErrors);

foreach ($dom->childNodes as $child) {
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
throw new \InvalidArgumentException('Document types are not allowed.');
}
}

$this->validate($dom);

return $dom;
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Translation/Loader/XliffFileLoader.php
Expand Up @@ -64,6 +64,14 @@ private function parseFile($file)
throw new \RuntimeException(implode("\n", $this->getXmlErrors($internalErrors)));
}

foreach ($dom->childNodes as $child) {
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
libxml_use_internal_errors($internalErrors);

throw new \RuntimeException('Document types are not allowed.');
}
}

$location = str_replace('\\', '/', __DIR__).'/schema/dic/xliff-core/xml.xsd';
$parts = explode('/', $location);
if (0 === stripos($location, 'phar://')) {
Expand Down
Expand Up @@ -195,6 +195,12 @@ protected function parseFile($file)

libxml_use_internal_errors($internalErrors);

foreach ($dom->childNodes as $child) {
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
throw new MappingException('Document types are not allowed.');
}
}

return simplexml_import_dom($dom);
}

Expand Down
@@ -0,0 +1,3 @@
<?xml version="1.0"?>
<!DOCTYPE foo>
<foo></foo>
Expand Up @@ -310,4 +310,16 @@ public function testNoNamingConflictsForAnonymousServices()
$inner2 = $services[(string) $args2[0]];
$this->assertEquals('BarClass2', $inner2->getClass(), '->load() uses the same configuration as for the anonymous ones');
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Document types are not allowed.
*/
public function testDocTypeIsNotAllowed()
{
$container = new ContainerBuilder();

$loader1 = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader1->load('withdoctype.xml');
}
}
@@ -0,0 +1,3 @@
<?xml version="1.0"?>
<!DOCTYPE foo>
<foo></foo>
10 changes: 10 additions & 0 deletions tests/Symfony/Tests/Component/Routing/Loader/XmlFileLoaderTest.php
Expand Up @@ -75,6 +75,16 @@ public function getPathsToInvalidFiles()
{
return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'));
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Document types are not allowed.
*/
public function testDocTypeIsNotAllowed()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$loader->load('withdoctype.xml');
}
}

/**
Expand Down
Expand Up @@ -54,4 +54,14 @@ public function testLoadThrowsAnExceptionIfFileNotLocal()
$resource = 'http://example.com/resources.xliff';
$loader->load($resource, 'en', 'domain1');
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessage Document types are not allowed.
*/
public function testDocTypeIsNotAllowed()
{
$loader = new XliffFileLoader();
$loader->load(__DIR__.'/../fixtures/withdoctype.xliff', 'en', 'domain1');
}
}
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<!DOCTYPE foo>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="file.ext">
<body>
<trans-unit id="1">
<source>foo</source>
<target>bar</target>
</trans-unit>
</body>
</file>
</xliff>
Expand Up @@ -71,4 +71,16 @@ public function testLoadClassMetadata()

$this->assertEquals($expected, $metadata);
}

/**
* @expectedException Symfony\Component\Validator\Exception\MappingException
* @expectedExceptionMessage Document types are not allowed.
*/
public function testDocTypeIsNotAllowed()
{
$loader = new XmlFileLoader(__DIR__.'/withdoctype.xml');
$metadata = new ClassMetadata('Symfony\Tests\Component\Validator\Fixtures\Entity');

$loader->loadClassMetadata($metadata);
}
}
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE foo>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
<class name="Symfony\Tests\Component\Validator\Fixtures\Entity" />
</constraint-mapping>

0 comments on commit 4e0c992

Please sign in to comment.