Skip to content

Commit ee80733

Browse files
authored
Add a flag to filter issues by confidence (#316)
Refactor also how the issues are filtered by severity. Signed-off-by: Cosmin Cojocar <[email protected]>
1 parent 29cec13 commit ee80733

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

cmd/gosec/main.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ var (
9393
flagScanVendor = flag.Bool("vendor", false, "Scan the vendor folder")
9494

9595
// fail by severity
96-
flagSeverity = flag.String("severity", "low", "Fail the scanning for issues with the given or higher severity. Valid options are: low, medium, high")
96+
flagSeverity = flag.String("severity", "low", "Filter out the issues with a lower severity than the given value. Valid options are: low, medium, high")
97+
98+
// fail by confidence
99+
flagConfidence = flag.String("confidence", "low", "Filter out the issues with a lower confidence than the given value. Valid options are: low, medium, high")
97100

98101
// do not fail
99102
flagNoFail = flag.Bool("no-fail", false, "Do not fail the scanning, even if issues were found")
@@ -199,6 +202,16 @@ func convertToScore(severity string) (gosec.Score, error) {
199202
}
200203
}
201204

205+
func filterIssues(issues []*gosec.Issue, severity gosec.Score, confidence gosec.Score) []*gosec.Issue {
206+
result := []*gosec.Issue{}
207+
for _, issue := range issues {
208+
if issue.Severity >= severity && issue.Confidence >= confidence {
209+
result = append(result, issue)
210+
}
211+
}
212+
return result
213+
}
214+
202215
func main() {
203216
// Setup usage description
204217
flag.Usage = usage
@@ -232,10 +245,15 @@ func main() {
232245

233246
failSeverity, err := convertToScore(*flagSeverity)
234247
if err != nil {
235-
logger.Fatal(err)
248+
logger.Fatalf("Invalid severity value: %v", err)
236249
}
237250

238-
// Load config
251+
failConfidence, err := convertToScore(*flagConfidence)
252+
if err != nil {
253+
logger.Fatalf("Invalid confidence value: %v", err)
254+
}
255+
256+
// Load the analyzer configuration
239257
config, err := loadConfig(*flagConfig)
240258
if err != nil {
241259
logger.Fatal(err)
@@ -284,16 +302,11 @@ func main() {
284302
sortIssues(issues)
285303
}
286304

287-
issuesFound := false
288-
for _, issue := range issues {
289-
if issue.Severity >= failSeverity {
290-
issuesFound = true
291-
break
292-
}
293-
}
305+
// Filter the issues by severity and confidence
306+
issues = filterIssues(issues, failSeverity, failConfidence)
294307

295308
// Exit quietly if nothing was found
296-
if !issuesFound && *flagQuiet {
309+
if len(issues) == 0 && *flagQuiet {
297310
os.Exit(0)
298311
}
299312

@@ -307,7 +320,7 @@ func main() {
307320
logWriter.Close() // #nosec
308321

309322
// Do we have an issue? If so exit 1 unless NoFail is set
310-
if (issuesFound || len(errors) != 0) && !*flagNoFail {
323+
if (len(issues) > 0 || len(errors) > 0) && !*flagNoFail {
311324
os.Exit(1)
312325
}
313326
}

0 commit comments

Comments
 (0)