Skip to content

Commit

Permalink
django_rawsql_used: support keyword arguments used in RawSQL
Browse files Browse the repository at this point in the history
Fix rule B611: django_rawsql_used breaking when a user passes in
keyword arguments to Django's `RawSQL`.

Resolves: #764
  • Loading branch information
kevinmarsh committed Jul 24, 2023
1 parent 6d6ec6d commit f2f46cc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
7 changes: 6 additions & 1 deletion bandit/plugins/django_sql_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ def django_rawsql_used(context):
description = "Use of RawSQL potential SQL attack vector."
if context.is_module_imported_like("django.db.models"):
if context.call_function_name == "RawSQL":
sql = context.node.args[0]
if context.node.args:
sql = context.node.args[0]
else:
kwargs = keywords2dict(context.node.keywords)
sql = kwargs["sql"]

if not isinstance(sql, ast.Str):
return bandit.Issue(
severity=bandit.MEDIUM,
Expand Down
2 changes: 2 additions & 0 deletions examples/django_sql_injection_raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
raw = '"username") AS "val" FROM "auth_user"' \
' WHERE "username"="admin" OR 1=%s --'
User.objects.annotate(val=RawSQL(raw, [0]))
User.objects.annotate(val=RawSQL(sql='{}secure'.format('no'), params=[]))
User.objects.annotate(val=RawSQL(params=[], sql='{}secure'.format('no')))
4 changes: 2 additions & 2 deletions tests/functional/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,8 @@ def test_django_sql_injection_raw(self):
"""Test insecure raw functions on Django."""

expect = {
"SEVERITY": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 4, "HIGH": 0},
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 4, "HIGH": 0},
"SEVERITY": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 6, "HIGH": 0},
"CONFIDENCE": {"UNDEFINED": 0, "LOW": 0, "MEDIUM": 6, "HIGH": 0},
}
self.check_example("django_sql_injection_raw.py", expect)

Expand Down

0 comments on commit f2f46cc

Please sign in to comment.