forked from PyCQA/bandit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
issue.py
237 lines (197 loc) · 6.5 KB
/
issue.py
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#
# Copyright 2015 Hewlett-Packard Development Company, L.P.
#
# SPDX-License-Identifier: Apache-2.0
import linecache
import sys
from bandit.core import constants
class Cwe:
NOTSET = 0
IMPROPER_INPUT_VALIDATION = 20
PATH_TRAVERSAL = 22
OS_COMMAND_INJECTION = 78
XSS = 79
BASIC_XSS = 80
SQL_INJECTION = 89
CODE_INJECTION = 94
IMPROPER_WILDCARD_NEUTRALIZATION = 155
HARD_CODED_PASSWORD = 259
IMPROPER_ACCESS_CONTROL = 284
IMPROPER_CERT_VALIDATION = 295
CLEARTEXT_TRANSMISSION = 319
INADEQUATE_ENCRYPTION_STRENGTH = 326
BROKEN_CRYPTO = 327
INSUFFICIENT_RANDOM_VALUES = 330
INSECURE_TEMP_FILE = 377
DESERIALIZATION_OF_UNTRUSTED_DATA = 502
MULTIPLE_BINDS = 605
IMPROPER_CHECK_OF_EXCEPT_COND = 703
INCORRECT_PERMISSION_ASSIGNMENT = 732
MITRE_URL_PATTERN = "https://cwe.mitre.org/data/definitions/%s.html"
def __init__(self, id=NOTSET):
self.id = id
def link(self):
if self.id == Cwe.NOTSET:
return ""
return Cwe.MITRE_URL_PATTERN % str(self.id)
def __str__(self):
if self.id == Cwe.NOTSET:
return ""
return "CWE-%i (%s)" % (self.id, self.link())
def as_dict(self):
return (
{"id": self.id, "link": self.link()}
if self.id != Cwe.NOTSET
else {}
)
def as_jsons(self):
return str(self.as_dict())
def from_dict(self, data):
if "id" in data:
self.id = int(data["id"])
else:
self.id = Cwe.NOTSET
def __eq__(self, other):
return self.id == other.id
def __ne__(self, other):
return self.id != other.id
def __hash__(self):
return id(self)
class Issue:
def __init__(
self,
severity,
cwe=0,
confidence=constants.CONFIDENCE_DEFAULT,
text="",
ident=None,
lineno=None,
test_id="",
col_offset=0,
):
self.severity = severity
self.cwe = Cwe(cwe)
self.confidence = confidence
if isinstance(text, bytes):
text = text.decode("utf-8")
self.text = text
self.ident = ident
self.fname = ""
self.test = ""
self.test_id = test_id
self.lineno = lineno
self.col_offset = col_offset
self.linerange = []
def __str__(self):
return (
"Issue: '%s' from %s:%s: CWE: %s, Severity: %s Confidence: "
"%s at %s:%i"
) % (
self.text,
self.test_id,
(self.ident or self.test),
str(self.cwe),
self.severity,
self.confidence,
self.fname,
self.lineno,
)
def __eq__(self, other):
# if the issue text, severity, confidence, and filename match, it's
# the same issue from our perspective
match_types = [
"text",
"severity",
"cwe",
"confidence",
"fname",
"test",
"test_id",
]
return all(
getattr(self, field) == getattr(other, field)
for field in match_types
)
def __ne__(self, other):
return not self.__eq__(other)
def __hash__(self):
return id(self)
def filter(self, severity, confidence):
"""Utility to filter on confidence and severity
This function determines whether an issue should be included by
comparing the severity and confidence rating of the issue to minimum
thresholds specified in 'severity' and 'confidence' respectively.
Formatters should call manager.filter_results() directly.
This will return false if either the confidence or severity of the
issue are lower than the given threshold values.
:param severity: Severity threshold
:param confidence: Confidence threshold
:return: True/False depending on whether issue meets threshold
"""
rank = constants.RANKING
return rank.index(self.severity) >= rank.index(
severity
) and rank.index(self.confidence) >= rank.index(confidence)
def get_code(self, max_lines=3, tabbed=False):
"""Gets lines of code from a file the generated this issue.
:param max_lines: Max lines of context to return
:param tabbed: Use tabbing in the output
:return: strings of code
"""
lines = []
max_lines = max(max_lines, 1)
lmin = max(1, self.lineno - max_lines // 2)
lmax = lmin + len(self.linerange) + max_lines - 1
if self.fname == "<stdin>":
sys.stdin.seek(0)
for line_num in range(1, lmin):
sys.stdin.readline()
tmplt = "%i\t%s" if tabbed else "%i %s"
for line in range(lmin, lmax):
if self.fname == "<stdin>":
text = sys.stdin.readline()
else:
text = linecache.getline(self.fname, line)
if isinstance(text, bytes):
text = text.decode("utf-8")
if not len(text):
break
lines.append(tmplt % (line, text))
return "".join(lines)
def as_dict(self, with_code=True):
"""Convert the issue to a dict of values for outputting."""
out = {
"filename": self.fname,
"test_name": self.test,
"test_id": self.test_id,
"issue_severity": self.severity,
"issue_cwe": self.cwe.as_dict(),
"issue_confidence": self.confidence,
"issue_text": self.text.encode("utf-8").decode("utf-8"),
"line_number": self.lineno,
"line_range": self.linerange,
"col_offset": self.col_offset,
}
if with_code:
out["code"] = self.get_code()
return out
def from_dict(self, data, with_code=True):
self.code = data["code"]
self.fname = data["filename"]
self.severity = data["issue_severity"]
self.cwe = cwe_from_dict(data["issue_cwe"])
self.confidence = data["issue_confidence"]
self.text = data["issue_text"]
self.test = data["test_name"]
self.test_id = data["test_id"]
self.lineno = data["line_number"]
self.linerange = data["line_range"]
self.col_offset = data.get("col_offset", 0)
def cwe_from_dict(data):
cwe = Cwe()
cwe.from_dict(data)
return cwe
def issue_from_dict(data):
i = Issue(severity=data["issue_severity"])
i.from_dict(data)
return i