-
-
Notifications
You must be signed in to change notification settings - Fork 832
Expand file tree
/
Copy pathdjango_sql_injection.py
More file actions
107 lines (89 loc) · 3.12 KB
/
Copy pathdjango_sql_injection.py
File metadata and controls
107 lines (89 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# -*- coding:utf-8 -*-
#
# Copyright (C) 2018 [Victor Torre](https://github.com/ehooo)
#
# SPDX-License-Identifier: Apache-2.0
import ast
import bandit
from bandit.core import test_properties as test
def keywords2dict(keywords):
kwargs = {}
for node in keywords:
if isinstance(node, ast.keyword):
kwargs[node.arg] = node.value
return kwargs
@test.checks('Call')
@test.test_id('B610')
def django_extra_used(context):
"""**B610: Potential SQL injection on extra function**
.. seealso::
- https://docs.djangoproject.com/en/dev/topics/security/\
#sql-injection-protection
.. versionadded:: 1.5.0
"""
description = "Use of extra potential SQL attack vector."
if context.call_function_name == 'extra':
kwargs = keywords2dict(context.node.keywords)
args = context.node.args
if args:
if len(args) >= 1:
kwargs['select'] = args[0]
if len(args) >= 2:
kwargs['where'] = args[1]
if len(args) >= 3:
kwargs['params'] = args[2]
if len(args) >= 4:
kwargs['tables'] = args[3]
if len(args) >= 5:
kwargs['order_by'] = args[4]
if len(args) >= 6:
kwargs['select_params'] = args[5]
insecure = False
for key in ['where', 'tables']:
if key in kwargs:
if isinstance(kwargs[key], ast.List):
for val in kwargs[key].elts:
if not isinstance(val, ast.Str):
insecure = True
break
else:
insecure = True
break
if not insecure and 'select' in kwargs:
if isinstance(kwargs['select'], ast.Dict):
for k in kwargs['select'].keys:
if not isinstance(k, ast.Str):
insecure = True
break
if not insecure:
for v in kwargs['select'].values:
if not isinstance(v, ast.Str):
insecure = True
break
else:
insecure = True
if insecure:
return bandit.Issue(
severity=bandit.MEDIUM,
confidence=bandit.MEDIUM,
text=description
)
@test.checks('Call')
@test.test_id('B611')
def django_rawsql_used(context):
"""**B611: Potential SQL injection on RawSQL function**
.. seealso::
- https://docs.djangoproject.com/en/dev/topics/security/\
#sql-injection-protection
.. versionadded:: 1.5.0
"""
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 not isinstance(sql, ast.Str):
return bandit.Issue(
severity=bandit.MEDIUM,
confidence=bandit.MEDIUM,
text=description
)