Skip to content

Commit c0680bb

Browse files
committed
Process the code snippet before adding it to the SARIF report
Preprocess the code snippet from the issue in order to extract only the line(s) of code where the issue is located. In addition remove the line numbers and whitespaces before writing the code snippet into the SARIF report.
1 parent db8d98b commit c0680bb

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

report/sarif/formatter.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,24 @@ func parseSarifRegion(issue *gosec.Issue) (*Region, error) {
188188
if err != nil {
189189
return nil, err
190190
}
191-
snippet := NewArtifactContent(issue.Code)
191+
var code string
192+
line := startLine
193+
codeLines := strings.Split(issue.Code, "\n")
194+
for _, codeLine := range codeLines {
195+
lineStart := fmt.Sprintf("%d:", line)
196+
if strings.HasPrefix(codeLine, lineStart) {
197+
code += strings.TrimSpace(
198+
strings.TrimPrefix(codeLine, lineStart))
199+
if endLine > startLine {
200+
code += "\n"
201+
}
202+
line++
203+
if line > endLine {
204+
break
205+
}
206+
}
207+
}
208+
snippet := NewArtifactContent(code)
192209
return NewRegion(startLine, endLine, col, col, "go").WithSnippet(snippet), nil
193210
}
194211

report/sarif/sarif_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,59 @@ var _ = Describe("Sarif Formatter", func() {
5656
hasSuppressions, _ := regexp.MatchString(`"suppressions": \[(\s*){`, result)
5757
Expect(hasSuppressions).To(BeTrue())
5858
})
59+
It("sarif formatted report should contain the formatted one line code snippet", func() {
60+
ruleID := "G101"
61+
cwe := gosec.GetCweByRule(ruleID)
62+
code := "68: \t\t}\n69: \t\tvar data = template.HTML(v.TmplFile)\n70: \t\tisTmpl := true\n"
63+
expectedCode := "var data = template.HTML(v.TmplFile)"
64+
issue := gosec.Issue{
65+
File: "/home/src/project/test.go",
66+
Line: "69",
67+
Col: "14",
68+
RuleID: ruleID,
69+
What: "test",
70+
Confidence: gosec.High,
71+
Severity: gosec.High,
72+
Code: code,
73+
Cwe: cwe,
74+
Suppressions: []gosec.SuppressionInfo{
75+
{
76+
Kind: "kind",
77+
Justification: "justification",
78+
},
79+
},
80+
}
81+
reportInfo := gosec.NewReportInfo([]*gosec.Issue{&issue}, &gosec.Metrics{}, map[string][]gosec.Error{}).WithVersion("v2.7.0")
82+
sarifReport, err := sarif.GenerateReport([]string{}, reportInfo)
83+
Expect(err).ShouldNot(HaveOccurred())
84+
Expect(sarifReport.Runs[0].Results[0].Locations[0].PhysicalLocation.Region.Snippet.Text).Should(Equal(expectedCode))
85+
})
86+
It("sarif formatted report should contain the formatted multiple line code snippet", func() {
87+
ruleID := "G101"
88+
cwe := gosec.GetCweByRule(ruleID)
89+
code := "68: }\n69: var data = template.HTML(v.TmplFile)\n70: isTmpl := true\n"
90+
expectedCode := "var data = template.HTML(v.TmplFile)\nisTmpl := true\n"
91+
issue := gosec.Issue{
92+
File: "/home/src/project/test.go",
93+
Line: "69-70",
94+
Col: "14",
95+
RuleID: ruleID,
96+
What: "test",
97+
Confidence: gosec.High,
98+
Severity: gosec.High,
99+
Code: code,
100+
Cwe: cwe,
101+
Suppressions: []gosec.SuppressionInfo{
102+
{
103+
Kind: "kind",
104+
Justification: "justification",
105+
},
106+
},
107+
}
108+
reportInfo := gosec.NewReportInfo([]*gosec.Issue{&issue}, &gosec.Metrics{}, map[string][]gosec.Error{}).WithVersion("v2.7.0")
109+
sarifReport, err := sarif.GenerateReport([]string{}, reportInfo)
110+
Expect(err).ShouldNot(HaveOccurred())
111+
Expect(sarifReport.Runs[0].Results[0].Locations[0].PhysicalLocation.Region.Snippet.Text).Should(Equal(expectedCode))
112+
})
59113
})
60114
})

0 commit comments

Comments
 (0)