-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from AikidoSec/AIK-3455
Add support for lxml
- Loading branch information
Showing
5 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
Sink module for `xml`, python's built-in function | ||
""" | ||
|
||
import copy | ||
import importhook | ||
from aikido_firewall.helpers.extract_data_from_xml_body import ( | ||
extract_data_from_xml_body, | ||
) | ||
from aikido_firewall.background_process.packages import add_wrapped_package | ||
|
||
|
||
@importhook.on_import("lxml.etree") | ||
def on_lxml_import(eltree): | ||
""" | ||
Hook 'n wrap on `lxml.etree`. | ||
- Wrap on fromstring() function | ||
- Wrap on | ||
Returns : Modified `lxml.etree` object | ||
""" | ||
modified_eltree = importhook.copy_module(eltree) | ||
|
||
former_fromstring = copy.deepcopy(eltree.fromstring) | ||
|
||
def aikido_fromstring(text, *args, **kwargs): | ||
res = former_fromstring(text, *args, **kwargs) | ||
extract_data_from_xml_body(user_input=text, root_element=res) | ||
return res | ||
|
||
former_fromstringlist = copy.deepcopy(eltree.fromstringlist) | ||
|
||
def aikido_fromstringlist(strings, *args, **kwargs): | ||
res = former_fromstringlist(strings, *args, **kwargs) | ||
for string in strings: | ||
extract_data_from_xml_body(user_input=string, root_element=res) | ||
return res | ||
|
||
# pylint: disable=no-member | ||
setattr(eltree, "fromstring", aikido_fromstring) | ||
setattr(modified_eltree, "fromstring", aikido_fromstring) | ||
|
||
# pylint: disable=no-member | ||
setattr(eltree, "fromstringlist", aikido_fromstringlist) | ||
setattr(modified_eltree, "fromstringlist", aikido_fromstringlist) | ||
add_wrapped_package("lxml") | ||
return modified_eltree |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import pytest | ||
import requests | ||
# e2e tests for flask_postgres sample app | ||
post_url_fw = "http://localhost:8092/xml_post_lxml" | ||
post_url_nofw = "http://localhost:8093/xml_post_lxml" | ||
|
||
def test_safe_response_with_firewall(): | ||
xml_data = '<dogs><dog dog_name="Bobby" /></dogs>' | ||
res = requests.post(post_url_fw, data=xml_data) | ||
assert res.status_code == 200 | ||
|
||
|
||
def test_safe_response_without_firewall(): | ||
xml_data = '<dogs><dog dog_name="Bobby" /></dogs>' | ||
res = requests.post(post_url_nofw, data=xml_data) | ||
assert res.status_code == 200 | ||
|
||
|
||
def test_dangerous_response_with_firewall(): | ||
xml_data = '<dogs><dog dog_name="Malicious dog\', TRUE); -- " /></dogs>' | ||
res = requests.post(post_url_fw, data=xml_data) | ||
assert res.status_code == 500 | ||
|
||
def test_dangerous_response_without_firewall(): | ||
xml_data = '<dogs><dog dog_name="Malicious dog\', TRUE); -- " /></dogs>' | ||
res = requests.post(post_url_nofw, data=xml_data) | ||
assert res.status_code == 200 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
flask==2.3.3 | ||
psycopg2-binary | ||
cryptography | ||
lxml |