Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Xml/Dom/Configurator/canonicalize.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ function canonicalize(): Closure
return $document;
}

// Round-trip through saveXml first to normalize namespace declarations.
// C14N on DOM-manipulated documents can produce duplicate xmlns attributes
// on certain libxml versions (e.g. 2.9.14), causing createFromString to hang.
// @see https://github.com/php/php-src/issues/21548
$normalized = XMLDocument::createFromString(
non_empty_string()->assert($document->saveXml()),
);

return Document::fromLoader(
xml_string_loader(
non_empty_string()->assert($document->C14N()),
non_empty_string()->assert($normalized->C14N()),
LIBXML_NSCLEAN + LIBXML_NOCDATA
),
pretty_print(),
Expand Down
6 changes: 3 additions & 3 deletions src/Xml/Dom/Manipulator/Xmlns/rename_element_namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ function rename_element_namespace(Element $element, string $namespaceURI, string
if (is_xmlns_attribute($attr) && $attr->value === $namespaceURI) {
try {
$attr->rename($attr->namespaceURI, 'xmlns:' . $newPrefix);

} catch (DOMException $e) {
if ($e->getCode() === INVALID_MODIFICATION_ERR) {
// Remove the attribute that would become a duplicate
// Remove the attribute that would become a duplicate.
// The target prefix already exists on the element, so the old
// xmlns declaration can simply be dropped.
$element->removeAttributeNode($attr);
} else {
// @codeCoverageIgnoreStart
throw $e;
// @codeCoverageIgnoreEnd
}
}
$attr->rename($attr->namespaceURI, 'xmlns:' . $newPrefix);
}
});

Expand Down
21 changes: 21 additions & 0 deletions tests/Xml/Dom/Configurator/CanonicalizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\TestCase;
use VeeWee\Xml\Dom\Document;
use function VeeWee\Xml\Dom\Configurator\canonicalize;
use function VeeWee\Xml\Dom\Configurator\optimize_namespaces;
use function VeeWee\Xml\Dom\Locator\document_element;
use function VeeWee\Xml\Dom\Mapper\xml_string;

Expand All @@ -22,6 +23,26 @@ public function test_it_can_canonicalize(string $input, string $expected): void
static::assertSame($expected, $actual);
}

/**
* Regression test: canonicalize() on a DOM-manipulated document (e.g. after
* optimize_namespaces) would hang on libxml 2.9.14 because C14N produced
* duplicate xmlns declarations.
* @see https://github.com/php/php-src/issues/21548
*/
public function test_it_can_canonicalize_after_dom_manipulation(): void
{
$doc = Document::fromXmlString(
'<root xmlns="urn:a" attr="val"><child/></root>',
optimize_namespaces(),
);

$canonicalized = Document::fromUnsafeDocument($doc->toUnsafeDocument(), canonicalize());
$actual = xml_string()($canonicalized->map(document_element()));

static::assertStringContainsString('root', $actual);
static::assertStringNotContainsString('xmlns:ns1="urn:a" attr="val" xmlns:ns1="urn:a"', $actual);
}

public function test_it_can_canonicalize_empty_xml(): void
{
$configurator = canonicalize();
Expand Down
17 changes: 17 additions & 0 deletions tests/Xml/Dom/Configurator/ComparableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ public static function provideXmls()
</foo>
EOXML,
];
// Regression test: default xmlns + regular attributes caused C14N to produce
// duplicate xmlns declarations on libxml 2.9.14, making comparable() hang.
// @see https://github.com/php/php-src/issues/21548
yield 'default-xmlns-with-attributes' => [
<<<EOXML
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Test">
<types><xsd:schema/></types>
</definitions>
EOXML,
<<<EOXML
<ns1:definitions name="Test" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/" xmlns:ns2="http://www.w3.org/2001/XMLSchema">
<ns1:types>
<ns2:schema/>
</ns1:types>
</ns1:definitions>
EOXML,
];
yield 'sorted-attributes' => [
<<<EOXML
<foo xmlns:a="http://a" xmlns:z="http://z" version="1.9" target="universe">
Expand Down