Skip to content

Commit 5fb4c4d

Browse files
committed
Python: Port xml.etree tests
1 parent a7134ca commit 5fb4c4d

File tree

3 files changed

+36
-64
lines changed

3 files changed

+36
-64
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from io import StringIO
2+
import xml.etree.ElementTree
3+
4+
x = "some xml"
5+
6+
# Parsing in different ways
7+
xml.etree.ElementTree.fromstring(x) # $ input=x vuln='Billion Laughs' vuln='Quadratic Blowup'
8+
xml.etree.ElementTree.fromstringlist(x) # $ input=x vuln='Billion Laughs' vuln='Quadratic Blowup'
9+
xml.etree.ElementTree.XML(x) # $ input=x vuln='Billion Laughs' vuln='Quadratic Blowup'
10+
xml.etree.ElementTree.parse(StringIO(x)) # $ input=StringIO(..) vuln='Billion Laughs' vuln='Quadratic Blowup'
11+
12+
# With parsers (no options available to disable/enable security features)
13+
parser = xml.etree.ElementTree.XMLParser()
14+
xml.etree.ElementTree.fromstring(x, parser=parser) # $ input=x vuln='Billion Laughs' vuln='Quadratic Blowup'
15+
16+
# note: it's technically possible to use the thing wrapper func `fromstring` with an
17+
# `lxml` parser, and thereby change what vulnerabilities you are exposed to.. but it
18+
# seems very unlikely that anyone would do this, so we have intentionally not added any
19+
# tests for this.

python/ql/test/experimental/query-tests/Security/CWE-611/dont_extract/PoC.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,23 @@ def test_ok_xml():
250250
assert root.tag == "test"
251251
assert root.text == "hello world"
252252

253+
@staticmethod
254+
def test_ok_xml_sax_parser():
255+
# you _can_ pass a SAX parser to xml.etree... but it doesn't give you the output :|
256+
parser = xml.sax.make_parser()
257+
root = xml.etree.ElementTree.fromstring(ok_xml, parser=parser)
258+
assert root == None
259+
260+
@staticmethod
261+
def test_ok_xml_lxml_parser():
262+
# this is technically possible, since parsers follow the same API, and the
263+
# `fromstring` function is just a thin wrapper... seems very unlikely that
264+
# anyone would do this though :|
265+
parser = lxml.etree.XMLParser()
266+
root = xml.etree.ElementTree.fromstring(ok_xml, parser=parser)
267+
assert root.tag == "test"
268+
assert root.text == "hello world"
269+
253270
@staticmethod
254271
def test_xxe_not_possible():
255272
parser = xml.etree.ElementTree.XMLParser()

python/ql/test/experimental/query-tests/Security/CWE-611/xml_etree.py

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)