Fixed the error handling when decoding invalid XML to avoid a Warning

This commit is contained in:
Christophe Coevoet 2013-10-28 13:20:44 +01:00
parent 6fcb06034a
commit b2550b90ae
2 changed files with 14 additions and 0 deletions

View File

@ -76,6 +76,10 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
libxml_use_internal_errors($internalErrors);
libxml_disable_entity_loader($disableEntities);
if ($error = libxml_get_last_error()) {
throw new UnexpectedValueException($error->message);
}
foreach ($dom->childNodes as $child) {
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
throw new UnexpectedValueException('Document types are not allowed.');

View File

@ -20,6 +20,8 @@ use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
class XmlEncoderTest extends \PHPUnit_Framework_TestCase
{
private $encoder;
protected function setUp()
{
$this->encoder = new XmlEncoder;
@ -301,6 +303,14 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $this->encoder->decode($xml, 'xml'));
}
/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
public function testDecodeInvalidXml()
{
$this->encoder->decode('<?xml version="1.0"?><invalid><xml>', 'xml');
}
public function testPreventsComplexExternalEntities()
{
$oldCwd = getcwd();