Skip to content

Commit 19fa856

Browse files
committed
fix: make sure that nil Cwe pointer is handled when getting the CWE ID
1 parent 62fa4b4 commit 19fa856

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

cwe/types.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ func (w *Weakness) SprintURL() string {
1919

2020
// SprintID format the CWE ID
2121
func (w *Weakness) SprintID() string {
22-
return fmt.Sprintf("%s-%s", Acronym, w.ID)
22+
id := "0000"
23+
if w != nil {
24+
id = w.ID
25+
}
26+
return fmt.Sprintf("%s-%s", Acronym, id)
2327
}
2428

2529
// MarshalJSON print only id and URL

report/golint/writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func WriteReport(w io.Writer, data *gosec.ReportInfo) error {
1515

1616
for _, issue := range data.Issues {
1717
what := issue.What
18-
if issue.Cwe.ID != "" {
18+
if issue.Cwe != nil && issue.Cwe.ID != "" {
1919
what = fmt.Sprintf("[%s] %s", issue.Cwe.SprintID(), issue.What)
2020
}
2121

report/junit/formatter.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ import (
88
)
99

1010
func generatePlaintext(issue *gosec.Issue) string {
11+
cweID := "CWE"
12+
if issue.Cwe != nil {
13+
cweID = issue.Cwe.ID
14+
}
1115
return "Results:\n" +
1216
"[" + issue.File + ":" + issue.Line + "] - " +
1317
issue.What + " (Confidence: " + strconv.Itoa(int(issue.Confidence)) +
1418
", Severity: " + strconv.Itoa(int(issue.Severity)) +
15-
", CWE: " + issue.Cwe.ID + ")\n" + "> " + html.EscapeString(issue.Code)
19+
", CWE: " + cweID + ")\n" + "> " + html.EscapeString(issue.Code)
1620
}
1721

1822
// GenerateReport Convert a gosec report to a JUnit Report

report/sarif/formatter.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ func GenerateReport(rootPaths []string, data *gosec.ReportInfo) (*Report, error)
2727
weaknesses := make(map[string]*cwe.Weakness)
2828

2929
for _, issue := range data.Issues {
30-
_, ok := weaknesses[issue.Cwe.ID]
31-
if !ok {
32-
weakness := cwe.Get(issue.Cwe.ID)
33-
weaknesses[issue.Cwe.ID] = weakness
34-
cweTaxon := parseSarifTaxon(weakness)
35-
cweTaxa = append(cweTaxa, cweTaxon)
30+
if issue.Cwe != nil {
31+
_, ok := weaknesses[issue.Cwe.ID]
32+
if !ok {
33+
weakness := cwe.Get(issue.Cwe.ID)
34+
weaknesses[issue.Cwe.ID] = weakness
35+
cweTaxon := parseSarifTaxon(weakness)
36+
cweTaxa = append(cweTaxa, cweTaxon)
37+
}
3638
}
3739

3840
r, ok := rulesIndices[issue.RuleID]
@@ -97,6 +99,9 @@ func parseSarifRule(issue *gosec.Issue) *ReportingDescriptor {
9799
}
98100

99101
func buildSarifReportingDescriptorRelationship(weakness *cwe.Weakness) *ReportingDescriptorRelationship {
102+
if weakness == nil {
103+
return nil
104+
}
100105
return &ReportingDescriptorRelationship{
101106
Target: &ReportingDescriptorReference{
102107
ID: weakness.ID,

0 commit comments

Comments
 (0)