Skip to content
This repository was archived by the owner on Jan 22, 2026. It is now read-only.

Commit eb47d49

Browse files
committed
Add xml_escape util function, apply where needed.
1 parent bba30d0 commit eb47d49

4 files changed

Lines changed: 17 additions & 8 deletions

File tree

virtinst/DistroManager.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import re
2828
import struct
2929
import tempfile
30+
import util
3031
import Guest
3132
from virtinst import _virtinst as _
3233

@@ -219,9 +220,9 @@ def _get_osblob(self, install, hvm, arch = None, loader = None):
219220
osblob += " <type>%s</type>\n" % type
220221

221222
if install and self.install["kernel"]:
222-
osblob += " <kernel>%s</kernel>\n" % self.install["kernel"]
223-
osblob += " <initrd>%s</initrd>\n" % self.install["initrd"]
224-
osblob += " <cmdline>%s</cmdline>\n" % self.install["extraargs"]
223+
osblob += " <kernel>%s</kernel>\n" % util.xml_escape(self.install["kernel"])
224+
osblob += " <initrd>%s</initrd>\n" % util.xml_escape(self.install["initrd"])
225+
osblob += " <cmdline>%s</cmdline>\n" % util.xml_escape(self.install["extraargs"])
225226
else:
226227
if loader:
227228
osblob += " <loader>%s</loader>\n" % loader

virtinst/Guest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ def get_xml_config(self, disknode):
180180
else:
181181
ret += " <driver name='%(name)s' type='%(type)s'/>\n" % { "name": self.driver_name, "type": self.driver_type }
182182
if self.path is not None:
183-
path = self.path.replace("&", "&amp;")
184-
path = path.replace("'", "&apos;")
183+
path = util.xml_escape(self.path)
185184
ret += " <source %(typeattr)s='%(disk)s'/>\n" % { "typeattr": typeattr, "disk": path }
186185
if self.target is not None:
187186
disknode = self.target

virtinst/ImageManager.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ def _get_osblob(self, install, hvm, arch = None, loader = None):
108108
osblob += " <type>%s</type>\n" % type
109109

110110
if self.boot.kernel:
111-
osblob += " <kernel>%s</kernel>\n" % self._abspath(self.boot.kernel)
112-
osblob += " <initrd>%s</initrd>\n" % self._abspath(self.boot.initrd)
113-
osblob += " <cmdline>%s</cmdline>\n" % self.boot.cmdline
111+
osblob += " <kernel>%s</kernel>\n" % util.xml_escape(self._abspath(self.boot.kernel))
112+
osblob += " <initrd>%s</initrd>\n" % util.xml_escape(self._abspath(self.boot.initrd))
113+
osblob += " <cmdline>%s</cmdline>\n" % util.xml_escape(self.boot.cmdline)
114114
osblob += " </os>"
115115
elif hvm:
116116
if loader:

virtinst/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,12 @@ def get_phy_cpus(conn):
207207
hostinfo = conn.getInfo()
208208
pcpus = hostinfo[4] * hostinfo[5] * hostinfo[6] * hostinfo[7]
209209
return pcpus
210+
211+
def xml_escape(str):
212+
"""Replaces chars ' " < > & with xml safe counterparts"""
213+
str.replace("&", "&amp;")
214+
str.replace("'", "&apos;")
215+
str.replace("\"", "&quot;")
216+
str.replace("<", "&lt;")
217+
str.replace(">", "&gt;")
218+
return str

0 commit comments

Comments
 (0)