Skip to content

Commit 0f934cf

Browse files
authored
some fixes for et (#2589)
1 parent b2d2089 commit 0f934cf

File tree

6 files changed

+18
-10
lines changed

6 files changed

+18
-10
lines changed

mslib/msui/flighttrack.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@
4040

4141
import fs
4242
import xml.dom.minidom
43-
import xml.parsers.expat
43+
import defusedxml.minidom
44+
from defusedxml import DefusedXmlException
4445

4546
from PyQt5 import QtGui, QtCore, QtWidgets
4647

@@ -55,7 +56,7 @@
5556
from mslib.msui.performance_settings import DEFAULT_PERFORMANCE
5657

5758
from mslib.utils import writexml
58-
xml.dom.minidom.Element.writexml = writexml
59+
xml.dom.minidom.Element.writexml = writexml # nosec, we take care of writing correct XML
5960
# Constants for identifying the table columns when the WaypointsTableModel is
6061
# used with a QTableWidget.
6162
LOCATION, LAT, LON, FLIGHTLEVEL, PRESSURE = list(range(5))
@@ -97,8 +98,8 @@ def seconds_to_string(seconds):
9798

9899
def load_from_xml_data(xml_content, name="Flight track"):
99100
try:
100-
doc = xml.dom.minidom.parseString(xml_content)
101-
except xml.parsers.expat.ExpatError as ex:
101+
doc = defusedxml.minidom.parseString(xml_content)
102+
except DefusedXmlException as ex:
102103
raise SyntaxError(str(ex))
103104

104105
ft_el = doc.getElementsByTagName("FlightTrack")[0]
@@ -615,7 +616,7 @@ def save_to_ftml(self, filename=None):
615616
file_dir.close()
616617

617618
def get_xml_doc(self):
618-
doc = xml.dom.minidom.Document()
619+
doc = xml.dom.minidom.Document() # nosec, we take care of writing correct XML
619620
ft_el = doc.createElement("FlightTrack")
620621
ft_el.setAttribute("version", __version__)
621622
doc.appendChild(ft_el)

mslib/msui/kmloverlay_dockwidget.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,13 @@ def merge_file(self):
601601
for index in checked_files: # index is the indices of checked files
602602
_dirname, _name = os.path.split(self.listWidget.item(index).text())
603603
_fs = fs.open_fs(_dirname)
604+
# Create a secure XML Parser
605+
secure_parser = et.XMLParser(resolve_entities=False, no_network=True)
606+
# resolve_entities False, prevents entity expansion
607+
# no_network, prevents automatically loading remote documents
608+
# https://gist.github.com/jack-om/f2c762f399e6ee652f05320921ece4c9
604609
with _fs.open(_name, 'r') as kmlf:
605-
tree = et.parse(kmlf) # parse kml file
610+
tree = et.parse(kmlf, parser=secure_parser) # nosec, parse using the secured parser
606611
root = tree.getroot() # get the root of the file
607612
self.remove_ns(root) # removes <kml> and </kml>
608613
element.append(copy.deepcopy(root[0]))

mslib/mswms/mpl_lsec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def plot_lsection(self, data, lats, lons, valid_time, init_time):
9292
# Derive additional data fields and make the plot.
9393
self._prepare_datafields()
9494

95-
impl = getDOMImplementation()
95+
impl = getDOMImplementation() # nosec, this is used to create and write a new XML document
9696
xmldoc = impl.createDocument(None, "MSS_LinearSection_Data", None)
9797

9898
# Title of this section.

mslib/mswms/mpl_vsec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ def plot_vsection(self, data, lats, lons, valid_time, init_time,
285285
# =========================================================================
286286
elif mime_type == "text/xml":
287287

288-
impl = getDOMImplementation()
288+
impl = getDOMImplementation() # nosec, this is used to create and write a new XML document
289289
xmldoc = impl.createDocument(None, "MSS_VerticalSection_Data", None)
290290

291291
# Title of this section.

mslib/mswms/wms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
import werkzeug
5353
import urllib.parse
5454

55-
from xml.etree import ElementTree
55+
from defusedxml import ElementTree
5656
from chameleon import PageTemplateLoader
5757
from owslib.crs import axisorder_yx
5858
from PIL import Image

mslib/utils/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def setup_logging(args):
8585
logger.addHandler(fh)
8686

8787

88+
# ToDo likely this can be removed in python 3 because that uses unicode
8889
# modified Version from minidom, https://github.com/python/cpython/blob/2.7/Lib/xml/dom/minidom.py
8990
# MSS needed to change all writings as unicode not str
9091
from xml.dom.minidom import _write_data, Node
@@ -102,11 +103,12 @@ def writexml(self, writer, indent="", addindent="", newl=""):
102103

103104
for a_name in sorted(attrs.keys()):
104105
writer.write(" %s=\"" % a_name)
105-
_write_data(writer, attrs[a_name].value)
106+
_write_data(writer, attrs[a_name].value) # nosec, we take care of writing correct XML
106107
writer.write("\"")
107108
if self.childNodes:
108109
writer.write(">")
109110
if (len(self.childNodes) == 1 and self.childNodes[0].nodeType == Node.TEXT_NODE):
111+
# nosec, we take care of writing correct XML
110112
self.childNodes[0].writexml(writer, '', '', '')
111113
else:
112114
writer.write(newl)

0 commit comments

Comments
 (0)