Skip to content

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 14 commits into from
Jul 30, 2024
Merged
1 change: 1 addition & 0 deletions aikido_firewall/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@ def protect(module="any", server=True):

# Import sinks
import aikido_firewall.sinks.pymysql
import aikido_firewall.sinks.mysqlclient

logger.info("Aikido python firewall started")
45 changes: 45 additions & 0 deletions aikido_firewall/sinks/mysqlclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
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)

Check warning on line 25 in aikido_firewall/sinks/mysqlclient.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L25

Added line #L25 was not covered by tests

prev_query_function = copy.deepcopy(mysql.Connection.query)

Check warning on line 27 in aikido_firewall/sinks/mysqlclient.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L27

Added line #L27 was not covered by tests

def aikido_new_query(_self, sql):
logger.debug("Wrapper - `mysqlclient` version : %s", version("mysqlclient"))

Check warning on line 30 in aikido_firewall/sinks/mysqlclient.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L29-L30

Added lines #L29 - L30 were not covered by tests

context = get_current_context()
result = check_context_for_sql_injection(

Check warning on line 33 in aikido_firewall/sinks/mysqlclient.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L32-L33

Added lines #L32 - L33 were not covered by tests
sql.decode("utf-8"), "MySQLdb.connections.query", context, MySQL()
)

logger.debug("sql_injection results : %s", json.dumps(result))
if result:
raise Exception("SQL Injection [aikido_firewall]")
return prev_query_function(_self, sql)

Check warning on line 40 in aikido_firewall/sinks/mysqlclient.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L37-L40

Added lines #L37 - L40 were not covered by tests

# pylint: disable=no-member
setattr(mysql.Connection, "query", aikido_new_query)
logger.debug("Wrapped `mysqlclient` module")
return modified_mysql

Check warning on line 45 in aikido_firewall/sinks/mysqlclient.py

View check run for this annotation

Codecov / codecov/patch

aikido_firewall/sinks/mysqlclient.py#L43-L45

Added lines #L43 - L45 were not covered by tests
5 changes: 2 additions & 3 deletions sample-apps/django-mysql/manage.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import aikido_firewall # Aikido module
aikido_firewall.protect("django")

import os
import sys
import aikido_firewall # Aikido module


def main():
"""Run administrative tasks."""
aikido_firewall.protect("django")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sample-django-mysql-app.settings')
try:
from django.core.management import execute_from_command_line
Expand Down
Loading