Skip to content

Commit 5ed85a4

Browse files
committed
Move test for dangerous XML before where the document is loaded
1 parent f6dfe8e commit 5ed85a4

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

src/DOMDocumentFactory.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
namespace SimpleSAML\XML;
66

77
use DOMDocument;
8-
use RuntimeException;
98
use SimpleSAML\Assert\Assert;
109
use SimpleSAML\XML\Exception\IOException;
10+
use SimpleSAML\XML\Exception\RuntimeException;
1111
use SimpleSAML\XML\Exception\UnparseableXMLException;
1212

1313
use function defined;
@@ -33,6 +33,12 @@ public static function fromString(string $xml): DOMDocument
3333
{
3434
libxml_set_external_entity_loader(null);
3535
Assert::notWhitespaceOnly($xml);
36+
Assert::notRegex(
37+
$xml,
38+
'/<(\s*)!(\s*)DOCTYPE/',
39+
'Dangerous XML detected, DOCTYPE nodes are not allowed in the XML body',
40+
RuntimeException::class,
41+
);
3642

3743
$internalErrors = libxml_use_internal_errors(true);
3844
libxml_clear_errors();
@@ -57,14 +63,6 @@ public static function fromString(string $xml): DOMDocument
5763

5864
libxml_clear_errors();
5965

60-
foreach ($domDocument->childNodes as $child) {
61-
if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
62-
throw new RuntimeException(
63-
'Dangerous XML detected, DOCTYPE nodes are not allowed in the XML body',
64-
);
65-
}
66-
}
67-
6866
return $domDocument;
6967
}
7068

0 commit comments

Comments
 (0)