1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 17:36:05 +00:00

fix(xml): read XML with its entities not expanded and nothing fetched

util.get_xml_path and the XML editor and "define from XML" pages parsed
the XML a superuser entered with lxml's default parser. lxml 6, which
the requirements ask for, does not load external entities by default,
but that was the library's default, not the panel's choice: with an
older lxml the same XML read a file of the panel's host into the VM's
name. util.xml_tree parses with resolve_entities=False and no_network,
as parse_xml already did, and the three places use it.
This commit is contained in:
catborise 2026-11-23 15:16:23 +03:00
parent b7884d1ce8
commit e88f568f6a
3 changed files with 18 additions and 5 deletions

View file

@ -2158,7 +2158,7 @@ def change_xml(request, pk):
diff = difflib.unified_diff(current.splitlines(), new_xml.splitlines(), "current", "new", lineterm="")
return JsonResponse({"diff": list(diff)})
shut_off = instance.proxy.get_status() == 5
new_tree = etree.fromstring(new_xml)
new_tree = util.xml_tree(new_xml)
try:
with audit_vm(request, instance, "change XML"):
# host devices given or changed are checked as the Host devices tab checks them
@ -2860,7 +2860,7 @@ def create_instance_select_type(request, compute_id):
with libvirt_compute_lock(compute):
with audit(request, compute, f"VM {name}", "define from XML", instance=name) as entry:
compute.refuse_new_vms() # put in maintenance since the page was opened
check_hostdevs(conn, name, [etree.fromstring(xml)])
check_hostdevs(conn, name, [util.xml_tree(xml)])
conn._defineXML(xml)
refresh_instance_database(compute)
instance = compute.instance_set.get(name=name)

View file

@ -1,3 +1,4 @@
import tempfile
import unittest
@ -52,6 +53,14 @@ class TestVrtManagerUtil(unittest.TestCase):
with self.assertRaises(ValueError):
get_xml_path(xml)
def test_get_xml_path_does_not_read_an_external_entity(self):
with tempfile.NamedTemporaryFile("w", suffix=".txt") as secret:
secret.write("the panel's secret")
secret.flush()
xml = (f'<!DOCTYPE domain [<!ENTITY leak SYSTEM "file://{secret.name}">]>'
"<domain><name>&leak;</name></domain>")
self.assertNotIn("secret", get_xml_path(xml, "/domain/name") or "")
def test_vol_dev_type(self):
self.assertEqual(vol_dev_type("ide"), "hd")
self.assertEqual(vol_dev_type("fdc"), "fd")

View file

@ -88,12 +88,16 @@ def xml_escape(xml_str):
return xml_str
def xml_tree(xml):
"""XML parsed with its entities not expanded and nothing fetched."""
return etree.fromstring(xml, etree.XMLParser(resolve_entities=False, no_network=True))
def parse_xml(xml):
"""XML a user entered, read for the panel's checks: its entities not
expanded, nothing fetched and no DTD; libvirt gets the text itself."""
parser = etree.XMLParser(resolve_entities=False, no_network=True)
try:
tree = etree.fromstring(xml.encode(), parser) # bytes: the XML may declare its encoding
tree = xml_tree(xml.encode()) # bytes: the XML may declare its encoding
except etree.XMLSyntaxError as err:
raise OperationError(gettext_noop("The XML is not valid: %(err)s"), err=err) from None
if tree.getroottree().docinfo.internalDTD is not None:
@ -109,7 +113,7 @@ def get_xml_path(xml, path=None, func=None):
doc = None
result = None
doc = etree.fromstring(xml)
doc = xml_tree(xml)
if path:
result = get_xpath(doc, path)
elif func: