From a2ed0c8d032c9540ea12394557fc76aa0061143a Mon Sep 17 00:00:00 2001 From: tonghuaroot Date: Sat, 21 Nov 2026 17:18:51 +0800 Subject: [PATCH] gh-162371: Validate namespaces in minidom renameNode Document.renameNode() skipped the qualified-name checks used by the other namespace-aware methods. Reuse the existing validator before mutating the node. --- Doc/library/xml.dom.rst | 2 ++ Lib/test/test_minidom.py | 13 +++++++++++++ Lib/xml/dom/minidom.py | 2 ++ .../2026-11-21-17-17-51.gh-issue-162371.M4nP8Q.rst | 3 +++ 4 files changed, 20 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-21-17-17-51.gh-issue-162371.M4nP8Q.rst diff --git a/Doc/library/xml.dom.rst b/Doc/library/xml.dom.rst index 05166214c7fc31f..14551a5325a012a 100644 --- a/Doc/library/xml.dom.rst +++ b/Doc/library/xml.dom.rst @@ -787,6 +787,8 @@ inherits properties from :class:`Node`. Raise :exc:`WrongDocumentErr` if *n* was created by another document, and :exc:`NotSupportedErr` if it is neither an element nor an attribute. + Raise :exc:`InvalidCharacterErr` if *name* is not a valid XML name, + and :exc:`NamespaceErr` if it is not valid for *namespaceURI*. .. _dom-element-objects: diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py index f50deaa2075d652..60eb6262c75a23c 100644 --- a/Lib/test/test_minidom.py +++ b/Lib/test/test_minidom.py @@ -2417,6 +2417,11 @@ def testNamespaceErr(self): ("http://xml.python.org/ns", "p:1e"), ("http://xml.python.org/ns", "xml:e"), # the xml prefix ]: + for node in (doc.createElement("e"), doc.createAttribute("a")): + with self.subTest(namespaceURI=namespaceURI, qname=qname, + nodeType=node.nodeType): + self.assertRaises(xml.dom.NamespaceErr, + doc.renameNode, node, namespaceURI, qname) with self.subTest(namespaceURI=namespaceURI, qname=qname): self.assertRaises(xml.dom.NamespaceErr, doc.createElementNS, namespaceURI, qname) @@ -2433,6 +2438,10 @@ def testNamespaceErr(self): (XMLNS_NS, "p:a"), # and it allows nothing else (XMLNS_NS, "a"), ]: + with self.subTest(namespaceURI=namespaceURI, qname=qname, + method="renameNode"): + self.assertRaises(xml.dom.NamespaceErr, doc.renameNode, + doc.createAttribute("a"), namespaceURI, qname) with self.subTest(namespaceURI=namespaceURI, qname=qname): self.assertRaises(xml.dom.NamespaceErr, doc.createAttributeNS, namespaceURI, qname) @@ -2471,6 +2480,10 @@ def testInvalidCharacterErr(self): doc = parseString("") impl = getDOMImplementation() for name in ("", "bad name", "1st", "-x", ".x", "a