Skip to content

Commit a2ed0c8

Browse files
committed
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.
1 parent 3d248a2 commit a2ed0c8

4 files changed

Lines changed: 20 additions & 0 deletions

File tree

‎Doc/library/xml.dom.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,8 @@ inherits properties from :class:`Node`.
787787

788788
Raise :exc:`WrongDocumentErr` if *n* was created by another document,
789789
and :exc:`NotSupportedErr` if it is neither an element nor an attribute.
790+
Raise :exc:`InvalidCharacterErr` if *name* is not a valid XML name,
791+
and :exc:`NamespaceErr` if it is not valid for *namespaceURI*.
790792

791793

792794
.. _dom-element-objects:

‎Lib/test/test_minidom.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,6 +2417,11 @@ def testNamespaceErr(self):
24172417
("http://xml.python.org/ns", "p:1e"),
24182418
("http://xml.python.org/ns", "xml:e"), # the xml prefix
24192419
]:
2420+
for node in (doc.createElement("e"), doc.createAttribute("a")):
2421+
with self.subTest(namespaceURI=namespaceURI, qname=qname,
2422+
nodeType=node.nodeType):
2423+
self.assertRaises(xml.dom.NamespaceErr,
2424+
doc.renameNode, node, namespaceURI, qname)
24202425
with self.subTest(namespaceURI=namespaceURI, qname=qname):
24212426
self.assertRaises(xml.dom.NamespaceErr,
24222427
doc.createElementNS, namespaceURI, qname)
@@ -2433,6 +2438,10 @@ def testNamespaceErr(self):
24332438
(XMLNS_NS, "p:a"), # and it allows nothing else
24342439
(XMLNS_NS, "a"),
24352440
]:
2441+
with self.subTest(namespaceURI=namespaceURI, qname=qname,
2442+
method="renameNode"):
2443+
self.assertRaises(xml.dom.NamespaceErr, doc.renameNode,
2444+
doc.createAttribute("a"), namespaceURI, qname)
24362445
with self.subTest(namespaceURI=namespaceURI, qname=qname):
24372446
self.assertRaises(xml.dom.NamespaceErr,
24382447
doc.createAttributeNS, namespaceURI, qname)
@@ -2471,6 +2480,10 @@ def testInvalidCharacterErr(self):
24712480
doc = parseString("<doc/>")
24722481
impl = getDOMImplementation()
24732482
for name in ("", "bad name", "1st", "-x", ".x", "a<b", "a&b", "a\tb"):
2483+
for node in (doc.createElement("e"), doc.createAttribute("a")):
2484+
with self.subTest(name=name, nodeType=node.nodeType):
2485+
self.assertRaises(xml.dom.InvalidCharacterErr,
2486+
doc.renameNode, node, None, name)
24742487
with self.subTest(name=name):
24752488
self.assertRaises(xml.dom.InvalidCharacterErr,
24762489
doc.createElement, name)

‎Lib/xml/dom/minidom.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,6 +2111,8 @@ def renameNode(self, n, namespaceURI, name):
21112111
if n.nodeType not in (Node.ELEMENT_NODE, Node.ATTRIBUTE_NODE):
21122112
raise xml.dom.NotSupportedErr(
21132113
"renameNode() only applies to element and attribute nodes")
2114+
_check_qualified_name(
2115+
namespaceURI, name, n.nodeType == Node.ATTRIBUTE_NODE)
21142116
if namespaceURI != EMPTY_NAMESPACE:
21152117
if ':' in name:
21162118
prefix, localName = name.split(':', 1)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Make :meth:`~xml.dom.Document.renameNode` validate qualified names against
2+
their namespace URI, raising :exc:`~xml.dom.InvalidCharacterErr` or
3+
:exc:`~xml.dom.NamespaceErr` for invalid input. Patch by tonghuaroot.

0 commit comments

Comments
 (0)