Skip to content

Commit c0fa847

Browse files
author
Wout Feys
committed
Create lxml source and add to protect() function
1 parent 3c908c9 commit c0fa847

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

aikido_firewall/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def protect(module="any", server=True):
3232
import aikido_firewall.sources.django
3333
import aikido_firewall.sources.flask
3434
import aikido_firewall.sources.xml
35+
import aikido_firewall.sources.lxml
3536

3637
import aikido_firewall.sources.gunicorn
3738
import aikido_firewall.sources.uwsgi

aikido_firewall/sources/lxml.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Sink module for `xml`, python's built-in function
3+
"""
4+
5+
import copy
6+
import importhook
7+
from aikido_firewall.helpers.process_xml import process_xml
8+
from aikido_firewall.background_process.packages import add_wrapped_package
9+
10+
11+
@importhook.on_import("lxml.etree")
12+
def on_lxml_import(eltree):
13+
"""
14+
Hook 'n wrap on `lxml.etree`.
15+
- Wrap on fromstring() function
16+
- Wrap on
17+
Returns : Modified `lxml.etree` object
18+
"""
19+
modified_eltree = importhook.copy_module(eltree)
20+
21+
former_fromstring = copy.deepcopy(eltree.fromstring)
22+
23+
def aikido_fromstring(text, *args, **kwargs):
24+
res = former_fromstring(text, *args, **kwargs)
25+
process_xml(user_input=text, root_element=res)
26+
return res
27+
28+
former_fromstringlist = copy.deepcopy(eltree.fromstringlist)
29+
30+
def aikido_fromstringlist(strings, *args, **kwargs):
31+
res = former_fromstringlist(strings, *args, **kwargs)
32+
for string in strings:
33+
process_xml(user_input=string, root_element=res)
34+
return res
35+
36+
# pylint: disable=no-member
37+
setattr(eltree, "fromstring", aikido_fromstring)
38+
setattr(modified_eltree, "fromstring", aikido_fromstring)
39+
40+
# pylint: disable=no-member
41+
setattr(eltree, "fromstringlist", aikido_fromstringlist)
42+
setattr(modified_eltree, "fromstringlist", aikido_fromstringlist)
43+
add_wrapped_package("lxml")
44+
return modified_eltree

0 commit comments

Comments
 (0)