Skip to content

Commit 1675457

Browse files
committed
feat(xml): dont resolve entities when parsing xml
1 parent 1e343da commit 1675457

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

src/pygef/broxml/parse_bore.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from pygef.bore import BoreData
1111
from pygef.broxml import resolvers
12-
from pygef.broxml.xml_parser import read_xml
12+
from pygef.broxml.xml_parser import BaseParser, read_xml
1313

1414
# maps keyword argument to:
1515
# xpath: query passed to elementree.find
@@ -136,9 +136,9 @@
136136

137137
def read_bore(file: io.BytesIO | Path | str) -> list[BoreData]:
138138
if isinstance(file, str) and not os.path.exists(file):
139-
root = etree.fromstring(file).getroot()
139+
root = etree.fromstring(file, parser=BaseParser).getroot()
140140
else:
141-
root = etree.parse(file).getroot()
141+
root = etree.parse(file, parser=BaseParser).getroot()
142142
match = re.compile(r"xsd/.*/(\d\.\d)")
143143
matched = match.search(root.nsmap["bhrgtcom"])
144144

src/pygef/broxml/parse_cpt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from lxml import etree
88

99
from pygef.broxml import resolvers
10-
from pygef.broxml.xml_parser import read_xml
10+
from pygef.broxml.xml_parser import BaseParser, read_xml
1111
from pygef.cpt import CPTData
1212

1313
# maps keyword argument to:
@@ -196,7 +196,7 @@
196196

197197
def read_cpt(file: io.BytesIO | Path | str) -> list[CPTData]:
198198
if isinstance(file, str) and not os.path.exists(file):
199-
root = etree.fromstring(file).getroot()
199+
root = etree.fromstring(file, parser=BaseParser).getroot()
200200
else:
201-
root = etree.parse(file).getroot()
201+
root = etree.parse(file, parser=BaseParser).getroot()
202202
return read_xml(root, CPTData, CPT_ATTRIBS, "dispatchDocument")

src/pygef/broxml/xml_parser.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
T = TypeVar("T", CPTData, BoreData)
1111

12+
BaseParser = etree.XMLParser(resolve_entities=False, dtd_validation=False)
13+
1214

1315
def read_xml(
1416
root: etree.Element,

0 commit comments

Comments
 (0)