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

Merge pull request #696 from catborise/upstream-xml-safety

fix(xml): no entity expansion in user XML; test regex in any case
This commit is contained in:
catborise 2026-11-23 16:03:05 +03:00 • committed by GitHub
commit a47df0f7fc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 19 additions and 6 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:

View file

@ -10,7 +10,7 @@ from django.test import SimpleTestCase, TestCase
from django.urls import reverse
# a <script> without src that is not json_script data
INLINE_SCRIPT = re.compile(r"<script(?![^>]*\bsrc=)(?![^>]*application/json)[^>]*>")
INLINE_SCRIPT = re.compile(r"<script(?![^>]*\bsrc=)(?![^>]*application/json)[^>]*>", re.I)
INLINE_HANDLER = re.compile(r"\son[a-z]+\s*=", re.I)