|
| 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