8000 refined previous commit · s7ntech/symfony@c896d71 · GitHub
[go: up one dir, main page]

Skip to content

Commit c896d71

Browse files
committed
refined previous commit
1 parent a2a6cdc commit c896d71

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,24 @@ public function encode($data, $format)
5454
*/
5555
public function decode($data, $format)
5656
{
57-
$this->assertNoCustomDocType($data);
5857
$internalErrors = libxml_use_internal_errors(true);
5958
$disableEntities = libxml_disable_entity_loader(true);
6059
libxml_clear_errors();
6160

62-
$xml = simplexml_load_string($data);
61+
$dom = new \DOMDocument();
62+
$dom->loadXML($data);
63+
6364
libxml_use_internal_errors($internalErrors);
6465
libxml_disable_entity_loader($disableEntities);
6566

67+
foreach ($dom->childNodes as $child) {
68+
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
69+
throw new UnexpectedValueException('Document types are not allowed.');
70+
}
71+
}
72+
73+
$xml = simplexml_import_dom($dom);
74+
6675
if ($error = libxml_get_last_error()) {
6776
throw new UnexpectedValueException($error->message);
6877
}
@@ -291,17 +300,6 @@ private function buildXml($parentNode, $data)
291300
throw new UnexpectedValueException('An unexpected value could not be serialized: '.var_export($data, true));
292301
}
293302

294-
private function assertNoCustomDocType($data)
295-
{
296-
$dom = new \DOMDocument;
297-
$dom->loadXML($data);
298-
foreach ($dom->childNodes as $child) {
299-
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
300-
throw new \InvalidArgumentException('Document types are not allowed.');
301-
}
302-
}
303-
}
304-
305303
/**
306304
* Selects the type of node to create and appends it to the parent.
307305
*

tests/Symfony/Tests/Component/Serializer/Encoder/XmlEncoderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function testSetRootNodeName()
5454
}
5555

5656
/**
57-
* @expectedException \InvalidArgumentException
57+
* @expectedException UnexpectedValueException
5858
* @expectedExceptionMessage Document types are not allowed.
5959
*/
6060
public function testDocTypeIsNotAllowed()
@@ -255,8 +255,8 @@ public function testPreventsComplexExternalEntities()
255255
} catch (\Exception $e) {
256256
chdir($oldCwd);
257257

258-
if (!$e instanceof \InvalidArgumentException) {
259-
$this->fail('Expected InvalidArgumentException');
258+
if (!$e instanceof UnexpectedValueException) {
259+
$this->fail('Expected UnexpectedValueException');
260260
}
261261
}
262262
}

0 commit comments

Comments
 (0)
0