-
Notifications
You must be signed in to change notification settings - Fork 2
Add mysqlclient
support
#17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
021c4f6
Import firewall inside main function
d367cd6
Create new mysqlclient file for sink
cf0a54a
Ad mysqlclient sink import in main file
95094eb
rm unbuffered parameter, not used in mysqlclient
3c8cdf1
The sql statement needs to be decoded
3cd63e6
Linting
f4b1842
Move aikido_firewall import to the top
96bb8c2
Make info log debug log
96d09bd
Merge remote-tracking branch 'origin/main' into AIK-3202
6a73e9c
Merge remote-tracking branch 'origin/AIK-3167' into AIK-3202
e5fb92b
Merge branch 'AIK-3199' into AIK-3202
1790cc8
Remove wrong check_context_for_sql_injection
02e2a38
Remove test op and set to right operation
c28e996
Merge branch 'AIK-3199' into AIK-3202
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 `mysqlclient` | ||
""" | ||
|
||
import copy | ||
import json | ||
from importlib.metadata import version | ||
import importhook | ||
from aikido_firewall.context import get_current_context | ||
from aikido_firewall.vulnerabilities.sql_injection.check_context_for_sql_injection import ( | ||
check_context_for_sql_injection, | ||
) | ||
from aikido_firewall.vulnerabilities.sql_injection.dialects import MySQL | ||
from aikido_firewall.helpers.logging import logger | ||
|
||
|
||
@importhook.on_import("MySQLdb.connections") | ||
def on_mysqlclient_import(mysql): | ||
""" | ||
Hook 'n wrap on `MySQLdb.connections` | ||
Our goal is to wrap the query() function of the Connection class : | ||
https://github.com/PyMySQL/mysqlclient/blob/9fd238b9e3105dcbed2b009a916828a38d1f0904/src/MySQLdb/connections.py#L257 | ||
Returns : Modified MySQLdb.connections object | ||
""" | ||
modified_mysql = importhook.copy_module(mysql) | ||
|
||
prev_query_function = copy.deepcopy(mysql.Connection.query) | ||
|
||
def aikido_new_query(_self, sql): | ||
logger.debug("Wrapper - `mysqlclient` version : %s", version("mysqlclient")) | ||
|
||
context = get_current_context() | ||
result = check_context_for_sql_injection(sql, "Test_op", context, MySQL()) | ||
result = check_context_for_sql_injection( | ||
sql.decode("utf-8"), "Test_op", context, MySQL() | ||
bitterpanda63 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
logger.debug("sql_injection results : %s", json.dumps(result)) | ||
if result: | ||
raise Exception("SQL Injection [aikido_firewall]") | ||
return prev_query_function(_self, sql) | ||
|
||
# pylint: disable=no-member | ||
setattr(mysql.Connection, "query", aikido_new_query) | ||
logger.debug("Wrapped `mysqlclient` module") | ||
return modified_mysql | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.