Skip to content

Commit f8f9413

Browse files
authored
Combine multiple runs with same Tool name in SARIF format (#384)
1 parent 9abb3ba commit f8f9413

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

commands/audit/sca/pnpm/pnpm_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestBuildDependencyTreeLimitedDepth(t *testing.T) {
4444
name: "With transitive dependencies",
4545
treeDepth: "1",
4646
expectedUniqueDeps: []string{
47-
"npm://axios:1.8.2",
47+
"npm://axios:1.8.3",
4848
"npm://balaganjs:1.0.0",
4949
"npm://yargs:13.3.0",
5050
"npm://zen-website:1.0.0",
@@ -54,7 +54,7 @@ func TestBuildDependencyTreeLimitedDepth(t *testing.T) {
5454
Nodes: []*xrayUtils.GraphNode{
5555
{
5656
Id: "npm://balaganjs:1.0.0",
57-
Nodes: []*xrayUtils.GraphNode{{Id: "npm://axios:1.8.2"}, {Id: "npm://yargs:13.3.0"}},
57+
Nodes: []*xrayUtils.GraphNode{{Id: "npm://axios:1.8.3"}, {Id: "npm://yargs:13.3.0"}},
5858
},
5959
},
6060
},

utils/formats/sarifutils/sarifutils.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,34 @@ func CombineReports(reports ...*sarif.Report) (combined *sarif.Report, err error
8484
}
8585
for _, report := range reports {
8686
for _, run := range report.Runs {
87-
combined.AddRun(run)
87+
appendRunInfoToReport(combined, run)
88+
}
89+
}
90+
return
91+
}
92+
93+
func CombineMultipleRunsWithSameTool(report *sarif.Report) (combined *sarif.Report, err error) {
94+
if combined, err = NewReport(); err != nil {
95+
return
96+
}
97+
for _, run := range report.Runs {
98+
appendRunInfoToReport(combined, run)
99+
}
100+
return
101+
}
102+
103+
func appendRunInfoToReport(combined *sarif.Report, run *sarif.Run) {
104+
if existingRun := getRunByToolName(GetRunToolName(run), combined); existingRun != nil {
105+
AggregateMultipleRunsIntoSingle([]*sarif.Run{run}, existingRun)
106+
} else {
107+
combined.AddRun(run)
108+
}
109+
}
110+
111+
func getRunByToolName(toolName string, report *sarif.Report) (run *sarif.Run) {
112+
for _, r := range report.Runs {
113+
if GetRunToolName(r) == toolName {
114+
return r
88115
}
89116
}
90117
return

utils/formats/sarifutils/test_sarifutils.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ func CreateDummyRule(impactPaths [][]formats.ComponentRow, ruleId, ruleDescripti
8484
var ruleProperties = sarif.NewPropertyBag()
8585
ruleProperties.Add(severityutils.SarifSeverityRuleProperty, maxCveScore)
8686
ruleProperties.Add(SarifImpactPathsRulePropertyKey, impactPaths)
87-
return sarif.NewRule(ruleId).WithProperties(ruleProperties.Properties).WithDescription(ruleDescription).WithHelp(sarif.NewMultiformatMessageString(summary).WithMarkdown(markdownDescription))
87+
description := sarif.NewMultiformatMessageString(summary).WithMarkdown(markdownDescription)
88+
return sarif.NewRule(ruleId).WithName(ruleId).WithProperties(ruleProperties.Properties).WithDescription(ruleDescription).WithHelp(description).WithFullDescription(description)
8889

8990
}
9091

utils/results/common.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,11 @@ func GetScaIssueId(depName, version, issueId string) string {
570570
return fmt.Sprintf("%s_%s_%s", issueId, depName, version)
571571
}
572572

573+
// replaces underscore with dash
574+
func IdToName(input string) string {
575+
return strings.Join(strings.Split(input, "_"), "-")
576+
}
577+
573578
// GetUniqueKey returns a unique string key of format "vulnerableDependency:vulnerableVersion:xrayID:fixVersionExist"
574579
func GetUniqueKey(vulnerableDependency, vulnerableVersion, xrayID string, fixVersionExist bool) string {
575580
return strings.Join([]string{vulnerableDependency, vulnerableVersion, xrayID, strconv.FormatBool(fixVersionExist)}, ":")

utils/results/conversion/sarifparser/sarifparser.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (sc *CmdResultsSarifConverter) Get() (*sarif.Report, error) {
106106
if err := sc.ParseNewTargetResults(results.ScanTarget{}, nil); err != nil {
107107
return sarifutils.NewReport()
108108
}
109-
return sc.current, nil
109+
return sarifutils.CombineMultipleRunsWithSameTool(sc.current)
110110
}
111111

112112
func (sc *CmdResultsSarifConverter) Reset(cmdType utils.CommandType, _, xrayVersion string, entitledForJas, _ bool, _ error) (err error) {
@@ -490,7 +490,9 @@ func getScaIssueSarifRule(impactPaths [][]formats.ComponentRow, ruleId, ruleDesc
490490
cveRuleProperties.Add(sarifutils.SarifImpactPathsRulePropertyKey, impactPaths)
491491
}
492492
return sarif.NewRule(ruleId).
493+
WithName(results.IdToName(ruleId)).
493494
WithDescription(ruleDescription).
495+
WithFullDescription(sarif.NewMultiformatMessageString(summary).WithMarkdown(markdownDescription)).
494496
WithHelp(sarif.NewMultiformatMessageString(summary).WithMarkdown(markdownDescription)).
495497
WithProperties(cveRuleProperties.Properties)
496498
}

0 commit comments

Comments
 (0)