-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
283 lines (228 loc) · 9.24 KB
/
__init__.py
File metadata and controls
283 lines (228 loc) · 9.24 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
"""Defines the core abstract classes for parsing SAST tool results.
This module provides the `Defect` and `AnalysisResult` classes, which serve as
standardized data structures for holding information about vulnerabilities and
the overall analysis process. Each SAST integration must implement a concrete
subclass of `AnalysisResult` to parse its specific output format.
"""
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Literal, Self
from codesectools.shared.cwe import CWE
class Defect:
"""Represent a single defect or finding reported by a SAST tool.
Attributes:
filepath (Path): The path to the file where the defect was found.
filepath_str (str): The string representation of the file path.
filename (str): The name of the file.
sast_name (str): The name of the SAST tool that reported the defect.
checker (str): The name of the checker or rule that reported the defect.
level (Literal["none", "note", "warning", "error"]): The severity level of the defect.
level (str): The level of the checker (e.g., security, performance).
cwe (CWE): The CWE associated with the defect.
message (str): The description of the defect.
lines (list[int] | None): A list of line numbers where the defect is located.
"""
def __init__(
self,
sast_name: str,
filepath: Path,
checker: str,
level: Literal["none", "note", "warning", "error"],
cwe: CWE,
message: str,
lines: list[int] | None,
) -> None:
"""Initialize a Defect instance.
Args:
sast_name: The name of the SAST tool.
filepath: The file path of the defect.
checker: The name of the rule/checker.
level: The severity level of the defect.
cwe: The CWE associated with the defect.
message: The description of the defect.
lines: A list of line numbers where the defect is located.
"""
if not filepath.is_file():
raise FileNotFoundError(filepath.resolve())
self.filepath = filepath
self.filepath_str = str(filepath.resolve())
self.filename = filepath.name
self.sast_name = sast_name
self.checker = checker
self.level = level
self.cwe = cwe
self.message = message
self.lines = lines
def __repr__(self) -> str:
"""Return a developer-friendly string representation of the Defect.
Returns:
A string showing the class name and key attributes of the defect.
"""
return f"""{self.__class__.__name__}(
sast: \t{self.sast_name}
filepath: \t{self.filepath}
checker: \t{self.checker}
level: \t{self.level}
cwe: \t{self.cwe}
)"""
class AnalysisResult(ABC):
"""Abstract base class for a SAST tool's analysis result.
This class provides a standardized structure for holding analysis data,
including defects, execution time, and language information. It also offers
methods for calculating statistics based on the results.
Attributes:
sast_name (str): The name of the SAST tool that produced the result.
name (str): The name of the project or dataset analyzed.
source_path (Path): The path to the source code that was analyzed.
lang (str): The programming language of the source code.
files (list[str]): A list of file paths that were part of the analysis.
defects (list[Defect]): A list of `Defect` objects found during analysis.
time (float): The total time taken for the analysis in seconds.
lines_of_codes (int): The number of lines of code analyzed.
"""
sast_name: str
level_color_map = {
"error": "red",
"warning": "orange",
"note": "yellow",
"none": "gray",
}
def __init__(
self,
name: str,
source_path: Path,
lang: str,
defects: list[Defect],
time: float,
lines_of_codes: int,
) -> None:
"""Initialize an AnalysisResult instance.
Args:
name: The name of the project or dataset.
source_path: The path to the analyzed source code.
lang: The programming language of the source code.
defects: A list of found defects.
time: The analysis duration in seconds.
lines_of_codes: The number of lines of code analyzed.
"""
self.name = name
self.source_path = source_path
self.lang = lang
self.defects = defects
self.time = time
self.lines_of_codes = lines_of_codes
@property
def files(self) -> list[str]:
"""Get the list of unique file paths containing defects."""
return list(set(d.filepath_str for d in self.defects))
def __repr__(self) -> str:
"""Return a developer-friendly string representation of the AnalysisResult.
Returns:
A string showing key metrics of the analysis.
"""
return f"""{self.__class__.__name__}(
name: \t{self.name}
lang: \t{self.lang}
files: \t{self.files}
file_count: \t{len(self.files)}
defect_count: \t{len(self.defects)}
time: \t{self.time}
)"""
@classmethod
@abstractmethod
def load_from_output_dir(cls, output_dir: Path) -> Self:
"""Load and parse analysis results from a specified directory.
This method must be implemented by subclasses to handle the specific
output files of a SAST tool.
Args:
output_dir: The directory containing the raw analysis output files.
Returns:
An instance of the `AnalysisResult` subclass.
"""
pass
@classmethod
def load_from_output_dirs(cls, output_dirs: list[Path]) -> list[Self]:
"""Load and parse analysis results from multiple directories.
Args:
output_dirs: An iterable of directory paths containing results.
Returns:
A list of `AnalysisResult` subclass instances.
"""
analysis_results = []
for output_dir in output_dirs:
analysis_results.append(cls.load_from_output_dir(output_dir))
return analysis_results
def checker_to_level(self, checker: str) -> str:
"""Map a checker name to its severity level.
Args:
checker: The name of the checker.
Returns:
The level string for the checker, or "none" if not found.
"""
for defect in self.defects:
if checker == defect.checker:
return defect.level
return "none"
def stats_by_checkers(self) -> dict:
"""Calculate statistics on defects, grouped by checker.
Returns:
A dictionary where keys are checker names and values are dicts
containing defect counts and affected files.
"""
stats = {}
for defect in self.defects:
if defect.checker not in stats.keys():
stats[defect.checker] = {"count": 1, "files": [defect.filepath_str]}
else:
stats[defect.checker]["files"].append(defect.filepath_str)
stats[defect.checker]["count"] += 1
return stats
def stats_by_levels(self) -> dict:
"""Calculate statistics on defects, grouped by level.
Returns:
A dictionary where keys are level names and values are dicts
containing counts and checker lists.
"""
stats = {}
for defect in self.defects:
if defect.level not in stats.keys():
stats[defect.level] = {
"count": 1,
"checkers": [defect.checker],
"unique": 1,
}
else:
stats[defect.level]["checkers"].append(defect.checker)
stats[defect.level]["count"] = len(stats[defect.level]["checkers"])
stats[defect.level]["unique"] = len(
set(stats[defect.level]["checkers"])
)
return stats
def stats_by_files(self) -> dict:
"""Calculate statistics on defects, grouped by file.
Returns:
A dictionary where keys are filenames and values are dicts
containing defect counts and the checkers that fired.
"""
stats = {}
for defect in self.defects:
if defect.filepath_str not in stats.keys():
stats[defect.filepath_str] = {"count": 1, "checkers": [defect.checker]}
else:
stats[defect.filepath_str]["checkers"].append(defect.checker)
stats[defect.filepath_str]["count"] += 1
return stats
def stats_by_cwes(self) -> dict:
"""Calculate statistics on defects, grouped by CWE ID.
Returns:
A dictionary where keys are CWE IDs and values are dicts
containing defect counts and affected files.
"""
stats = {}
for defect in self.defects:
if defect.cwe not in stats.keys():
stats[defect.cwe] = {"count": 1, "files": [defect.filepath_str]}
else:
stats[defect.cwe]["files"].append(defect.filepath_str)
stats[defect.cwe]["count"] += 1
return stats