Skip to content

Commit 34b3353

Browse files
committed
feat: add score for partial matching (CWE, location, SAST)
1 parent 2548842 commit 34b3353

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

codesectools/sasts/all/parser.py

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,15 @@ def stats_by_scores(self) -> dict:
132132
for defect_file, defects in defect_files.items():
133133
defects_cwes = {d.cwe for d in defects if d.cwe.id != -1}
134134

135-
defects_same_cwe = 0
135+
same_cwe = 0
136136
for cwe in defects_cwes:
137137
cwes_sasts = {d.sast_name for d in defects if d.cwe == cwe}
138138
if set(self.sast_names) == cwes_sasts:
139-
defects_same_cwe += 1
139+
same_cwe += 1
140140
else:
141-
defects_same_cwe += (
142-
len(set(self.sast_names) & cwes_sasts) - 1
143-
) / len(self.sast_names)
141+
same_cwe += (len(set(self.sast_names) & cwes_sasts) - 1) / len(
142+
self.sast_names
143+
)
144144

145145
defects_severity = []
146146
defect_locations = {}
@@ -156,67 +156,68 @@ def stats_by_scores(self) -> dict:
156156
defect_locations[line] = []
157157
defect_locations[line].append(defect)
158158

159-
defects_same_location = 0
160-
defects_same_location_same_cwe = 0
159+
same_location = 0
160+
same_location_same_cwe = 0
161161
for _, defects_ in defect_locations.items():
162+
same_location_coeff = 0
162163
if set(defect.sast_name for defect in defects_) == set(self.sast_names):
163-
defects_same_location += 1
164-
defects_by_cwe = {}
165-
for defect in defects_:
166-
if not defects_by_cwe.get(defect.cwe):
167-
defects_by_cwe[defect.cwe] = []
168-
defects_by_cwe[defect.cwe].append(defect)
169-
170-
for _, defects_ in defects_by_cwe.items():
171-
if set(defect.sast_name for defect in defects_) == set(
172-
self.sast_names
173-
):
174-
defects_same_location_same_cwe += 1
175-
else:
176-
defects_same_location_same_cwe += (
164+
same_location_coeff = 1
165+
else:
166+
same_location_coeff = (
167+
len(
168+
set(defect.sast_name for defect in defects_)
169+
& set(self.sast_names)
170+
)
171+
- 1
172+
) / len(set(self.sast_names))
173+
same_location += same_location_coeff
174+
175+
defects_by_cwe = {}
176+
for defect in defects_:
177+
if not defects_by_cwe.get(defect.cwe):
178+
defects_by_cwe[defect.cwe] = []
179+
defects_by_cwe[defect.cwe].append(defect)
180+
181+
for _, defects_ in defects_by_cwe.items():
182+
if set(defect.sast_name for defect in defects_) == set(
183+
self.sast_names
184+
):
185+
same_location_same_cwe += same_location_coeff * 1
186+
else:
187+
same_location_same_cwe += (
188+
same_location_coeff
189+
* (
177190
len(
178191
set(defect.sast_name for defect in defects_)
179192
& set(self.sast_names)
180193
)
181194
- 1
182-
) / len(self.sast_names)
195+
)
196+
/ len(self.sast_names)
197+
)
183198

184199
stats[defect_file] = {
185200
"score": {
186201
"severity": sum(defects_severity) / len(defects_severity),
187-
"defect_number": len(defects),
188-
"defects_same_cwe": defects_same_cwe * 2,
189-
"defects_same_location": defects_same_location * 4,
190-
"defects_same_location_same_cwe": defects_same_location_same_cwe
191-
* 8,
192-
},
193-
"count": {
194-
"defect_number": len(defects),
195-
"defects_same_cwe": defects_same_cwe,
196-
"defects_same_location": defects_same_location,
197-
"defects_same_location_same_cwe": defects_same_location_same_cwe,
202+
"same_cwe": same_cwe * 2,
203+
"same_location": same_location * 4,
204+
"same_location_same_cwe": same_location_same_cwe * 8,
198205
},
199206
}
200-
201207
return stats
202208

203209
def prepare_report_data(self) -> dict:
204210
"""Prepare data needed to generate a report."""
205-
report = {"score": {}, "files": {}}
211+
report = {}
206212
scores = self.stats_by_scores()
207213

208-
report["score"] = {k: 0 for k, _ in list(scores.values())[0]["score"].items()}
209-
210214
defect_files = {}
211215
for defect in self.defects:
212216
if defect.filepath_str not in defect_files:
213217
defect_files[defect.filepath_str] = []
214218
defect_files[defect.filepath_str].append(defect)
215219

216220
for defect_file, defects in defect_files.items():
217-
for k, v in scores[defect_file]["score"].items():
218-
report["score"][k] += v
219-
220221
locations = []
221222
for defect in defects:
222223
for group in group_successive(defect.lines):
@@ -225,19 +226,18 @@ def prepare_report_data(self) -> dict:
225226
(defect.sast_name, defect.cwe, defect.message, (start, end))
226227
)
227228

228-
report["files"][defect_file] = {
229-
"score": scores[defect_file]["score"],
230-
"count": scores[defect_file]["count"],
229+
report[defect_file] = {
230+
"score": sum(v for v in scores[defect_file]["score"].values()),
231231
"source_path": str(self.source_path / defect.filepath),
232232
"locations": locations,
233233
"defects": defects,
234234
}
235235

236-
report["files"] = {
236+
report = {
237237
k: v
238238
for k, v in sorted(
239-
report["files"].items(),
240-
key=lambda item: sum(v for v in item[1]["score"].values()),
239+
report.items(),
240+
key=lambda item: item[1]["score"],
241241
reverse=True,
242242
)
243243
}

0 commit comments

Comments
 (0)