diff --git a/UPGRADING b/UPGRADING
index 9dbcd594a29a..9f5db24d4317 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -32,6 +32,12 @@ PHP 8.7 UPGRADE NOTES
curl_setopt() return false, instead of being silently interpreted as a
combination of redirect types.
+- DOM:
+ . Dom\import_simplexml() now throws a TypeError when the node already has a
+ legacy DOMNode object attached and its document is not yet locked to a
+ model. Previously it returned that legacy object, which violated the
+ Attr|Element return type.
+
- PDO:
. PDOStatement::bindColumn() now throws a ValueError when the column name is
not present in the result set. It previously reported the condition
diff --git a/ext/dom/php_dom.c b/ext/dom/php_dom.c
index 0833d9a3c9be..b0750dfe6f12 100644
--- a/ext/dom/php_dom.c
+++ b/ext/dom/php_dom.c
@@ -589,6 +589,12 @@ static void dom_import_simplexml_common(INTERNAL_FUNCTION_PARAMETERS, php_libxml
RETURN_THROWS();
}
+ if (old_class_type == PHP_LIBXML_CLASS_UNSET && new_class == PHP_LIBXML_CLASS_MODERN
+ && php_dom_object_get_data(nodep) != NULL) {
+ zend_argument_type_error(1, "must not be already imported as a DOMNode");
+ RETURN_THROWS();
+ }
+
/* Lock the node class type to prevent creating multiple representations of the same node. */
nodeobj->document->class_type = new_class;
diff --git a/ext/dom/tests/modern/xml/import_simplexml_legacy_wrapper.phpt b/ext/dom/tests/modern/xml/import_simplexml_legacy_wrapper.phpt
new file mode 100644
index 000000000000..e06bc88a99ec
--- /dev/null
+++ b/ext/dom/tests/modern/xml/import_simplexml_legacy_wrapper.phpt
@@ -0,0 +1,42 @@
+--TEST--
+Dom\import_simplexml() refuses a node that already carries a legacy object
+--EXTENSIONS--
+dom
+simplexml
+--FILE--
+loadXML('');
+$element = $doc->documentElement;
+$sxe = simplexml_import_dom($element);
+try {
+ Dom\import_simplexml($sxe);
+} catch (TypeError $e) {
+ echo $e->getMessage(), "\n";
+}
+
+// Same for an attribute node, which SimpleXML refuses to import, so it is
+// passed directly.
+$attr = $doc->documentElement->getAttributeNode('z');
+try {
+ Dom\import_simplexml($attr);
+} catch (TypeError $e) {
+ echo $e->getMessage(), "\n";
+}
+
+// The document keeps its model: it must not have been relabelled on the way out.
+var_dump(get_class($doc->createElement('z')));
+
+// A node with no legacy object attached is still imported normally.
+$clean = simplexml_load_string('');
+var_dump(get_class(Dom\import_simplexml($clean)));
+
+?>
+--EXPECT--
+Dom\import_simplexml(): Argument #1 ($node) must not be already imported as a DOMNode
+Dom\import_simplexml(): Argument #1 ($node) must not be already imported as a DOMNode
+string(10) "DOMElement"
+string(11) "Dom\Element"