Skip to content

Commit acbbc8a

Browse files
author
Diego Velásquez
committed
chore: remove warnings from defusedxml package
1 parent 42e84e3 commit acbbc8a

File tree

3 files changed

+13
-21
lines changed

3 files changed

+13
-21
lines changed

openedx/core/lib/safe_lxml/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ def defuse_xml_libs():
77
"""
88
Monkey patch and defuse all stdlib xml packages and lxml.
99
"""
10-
from defusedxml import defuse_stdlib
11-
defuse_stdlib()
1210

1311
import lxml
1412
import lxml.etree

openedx/core/lib/safe_lxml/etree.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616

1717
from lxml.etree import XMLParser as _XMLParser
1818
from lxml.etree import * # lint-amnesty, pylint: disable=redefined-builtin
19-
from lxml.etree import _Element, _ElementTree
20-
21-
# This should be imported after lxml.etree so that it overrides the following attributes.
22-
from defusedxml.lxml import XML, fromstring, parse
19+
# These private elements are used in some libraries to also defuse xml exploits for their own purposes.
20+
# We need to re-expose them so that the libraries still work.
21+
from lxml.etree import _Comment, _Element, _ElementTree, _Entity, _ProcessingInstruction
2322

2423

2524
class XMLParser(_XMLParser): # pylint: disable=function-redefined

openedx/core/lib/safe_lxml/tests.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
11
"""
22
Test that we have defused XML.
3-
4-
For these tests, the defusing will happen in one or more of the `conftest.py`
5-
files that runs at pytest startup calls `defuse_xml_libs()`.
6-
7-
In production, the defusing happens when the LMS or Studio `wsgi.py` files
8-
call `defuse_xml_libs()`.
93
"""
104

115

12-
import defusedxml
136
from lxml import etree
147

158
import pytest
169

1710

18-
@pytest.mark.parametrize("attr", ["XML", "fromstring", "parse"])
19-
def test_etree_is_defused(attr):
20-
func = getattr(etree, attr)
21-
assert "defused" in func.__code__.co_filename
11+
def test_entities_resolved():
12+
xml = '<?xml version="1.0"?><!DOCTYPE mydoc [<!ENTITY hi "Hello">]> <root>&hi;</root>'
13+
parser = etree.XMLParser(resolve_entities=True)
14+
tree = etree.fromstring(xml, parser=parser)
15+
pr = etree.tostring(tree)
16+
assert pr == b'<root>Hello</root>'
2217

2318

2419
def test_entities_arent_resolved():
25-
# Make sure we have disabled entity resolution.
2620
xml = '<?xml version="1.0"?><!DOCTYPE mydoc [<!ENTITY hi "Hello">]> <root>&hi;</root>'
27-
parser = etree.XMLParser()
28-
with pytest.raises(defusedxml.EntitiesForbidden):
29-
_ = etree.XML(xml, parser=parser)
21+
parser = etree.XMLParser(resolve_entities=False)
22+
tree = etree.fromstring(xml, parser=parser)
23+
pr = etree.tostring(tree)
24+
assert pr == b'<root>&hi;</root>'

0 commit comments

Comments
 (0)