Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(report): add required fields to the SARIF template #2341

Merged
merged 13 commits into from
Jun 20, 2022
20 changes: 16 additions & 4 deletions integration/testdata/alpine-310.sarif.golden
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@
"uriBaseId": "ROOTPATH"
},
"region": {
"startLine": 1
"startLine": 1,
"startColumn": 1,
"endLine": 1,
"endColumn": 1
}
}
}
Expand All @@ -104,7 +107,10 @@
"uriBaseId": "ROOTPATH"
},
"region": {
"startLine": 1
"startLine": 1,
"startColumn": 1,
"endLine": 1,
"endColumn": 1
}
}
}
Expand All @@ -125,7 +131,10 @@
"uriBaseId": "ROOTPATH"
},
"region": {
"startLine": 1
"startLine": 1,
"startColumn": 1,
"endLine": 1,
"endColumn": 1
}
}
}
Expand All @@ -146,7 +155,10 @@
"uriBaseId": "ROOTPATH"
},
"region": {
"startLine": 1
"startLine": 1,
"startColumn": 1,
"endLine": 1,
"endColumn": 1
}
}
}
Expand Down
23 changes: 16 additions & 7 deletions pkg/report/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"
"strings"

containerName "github.com/google/go-containerregistry/pkg/name"
"github.com/owenrumney/go-sarif/v2/sarif"
"golang.org/x/xerrors"

Expand Down Expand Up @@ -87,10 +88,11 @@ func (sw *SarifWriter) addSarifRule(data *sarifData) {
func (sw *SarifWriter) addSarifResult(data *sarifData) {
sw.addSarifRule(data)

region := sarif.NewRegion().WithStartLine(1)
region := sarif.NewRegion().WithStartLine(1).WithEndLine(1)
if data.startLine > 0 {
region = sarif.NewSimpleRegion(data.startLine, data.endLine)
}
region.WithStartColumn(1).WithEndColumn(1)

location := sarif.NewPhysicalLocation().
WithArtifactLocation(sarif.NewSimpleArtifactLocation(data.artifactLocation).WithUriBaseId("ROOTPATH")).
Expand Down Expand Up @@ -124,14 +126,16 @@ func (sw SarifWriter) Write(report types.Report) error {

ruleIndexes := map[string]int{}
for _, res := range report.Results {
target := ToPathUri(res.Target)

for _, vuln := range res.Vulnerabilities {
fullDescription := vuln.Description
if fullDescription == "" {
fullDescription = vuln.Title
}
path := vuln.PkgPath
if path == "" {
path = res.Target
path := target
if vuln.PkgPath != "" {
path = ToPathUri(vuln.PkgPath)
}
sw.addSarifResult(&sarifData{
title: "vulnerability",
Expand All @@ -140,7 +144,7 @@ func (sw SarifWriter) Write(report types.Report) error {
cvssScore: getCVSSScore(vuln),
url: vuln.PrimaryURL,
resourceClass: string(res.Class),
artifactLocation: toPathUri(path),
artifactLocation: path,
resultIndex: getRuleIndex(vuln.VulnerabilityID, ruleIndexes),
fullDescription: html.EscapeString(fullDescription),
helpText: fmt.Sprintf("Vulnerability %v\nSeverity: %v\nPackage: %v\nFixed Version: %v\nLink: [%v](%v)\n%v",
Expand All @@ -159,7 +163,7 @@ func (sw SarifWriter) Write(report types.Report) error {
cvssScore: severityToScore(misconf.Severity),
url: misconf.PrimaryURL,
resourceClass: string(res.Class),
artifactLocation: toPathUri(res.Target),
artifactLocation: target,
startLine: misconf.CauseMetadata.StartLine,
endLine: misconf.CauseMetadata.EndLine,
resultIndex: getRuleIndex(misconf.ID, ruleIndexes),
Expand Down Expand Up @@ -207,11 +211,16 @@ func toSarifErrorLevel(severity string) string {
}
}

func toPathUri(input string) string {
func ToPathUri(input string) string {
var matches = pathRegex.FindStringSubmatch(input)
if matches != nil {
input = matches[pathRegex.SubexpIndex("path")]
}
ref, err := containerName.ParseReference(input)
if err == nil {
input = ref.Context().RepositoryStr()
}

return strings.ReplaceAll(input, "\\", "/")
}

Expand Down
62 changes: 52 additions & 10 deletions pkg/report/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestReportWriter_Sarif(t *testing.T) {
name: "report with vulnerabilities",
input: types.Results{
{
Target: "test",
Target: "library/test",
Class: types.ClassOSPkg,
Vulnerabilities: []types.DetectedVulnerability{
{
Expand Down Expand Up @@ -97,10 +97,15 @@ func TestReportWriter_Sarif(t *testing.T) {
{
PhysicalLocation: &sarif.PhysicalLocation{
ArtifactLocation: &sarif.ArtifactLocation{
URI: toPtr("test"),
URI: toPtr("library/test"),
URIBaseId: toPtr("ROOTPATH"),
},
Region: &sarif.Region{StartLine: toPtr(1)},
Region: &sarif.Region{
StartLine: toPtr(1),
EndLine: toPtr(1),
StartColumn: toPtr(1),
EndColumn: toPtr(1),
},
},
},
},
Expand All @@ -111,7 +116,7 @@ func TestReportWriter_Sarif(t *testing.T) {
name: "report with misconfigurations",
input: types.Results{
{
Target: "test",
Target: "library/test",
Class: types.ClassConfig,
Misconfigurations: []types.DetectedMisconfiguration{
{
Expand Down Expand Up @@ -140,15 +145,20 @@ func TestReportWriter_Sarif(t *testing.T) {
RuleID: toPtr("KSV001"),
RuleIndex: toPtr[uint](0),
Level: toPtr("error"),
Message: sarif.Message{Text: toPtr("Artifact: test\nType: \nVulnerability KSV001\nSeverity: HIGH\nMessage: Message\nLink: [KSV001](https://avd.aquasec.com/appshield/ksv001)")},
Message: sarif.Message{Text: toPtr("Artifact: library/test\nType: \nVulnerability KSV001\nSeverity: HIGH\nMessage: Message\nLink: [KSV001](https://avd.aquasec.com/appshield/ksv001)")},
Locations: []*sarif.Location{
{
PhysicalLocation: &sarif.PhysicalLocation{
ArtifactLocation: &sarif.ArtifactLocation{
URI: toPtr("test"),
URI: toPtr("library/test"),
URIBaseId: toPtr("ROOTPATH"),
},
Region: &sarif.Region{StartLine: toPtr(1)},
Region: &sarif.Region{
StartLine: toPtr(1),
EndLine: toPtr(1),
StartColumn: toPtr(1),
EndColumn: toPtr(1),
},
},
},
},
Expand All @@ -157,15 +167,20 @@ func TestReportWriter_Sarif(t *testing.T) {
RuleID: toPtr("KSV002"),
RuleIndex: toPtr[uint](1),
Level: toPtr("error"),
Message: sarif.Message{Text: toPtr("Artifact: test\nType: \nVulnerability KSV002\nSeverity: CRITICAL\nMessage: Message\nLink: [KSV002](https://avd.aquasec.com/appshield/ksv002)")},
Message: sarif.Message{Text: toPtr("Artifact: library/test\nType: \nVulnerability KSV002\nSeverity: CRITICAL\nMessage: Message\nLink: [KSV002](https://avd.aquasec.com/appshield/ksv002)")},
Locations: []*sarif.Location{
{
PhysicalLocation: &sarif.PhysicalLocation{
ArtifactLocation: &sarif.ArtifactLocation{
URI: toPtr("test"),
URI: toPtr("library/test"),
URIBaseId: toPtr("ROOTPATH"),
},
Region: &sarif.Region{StartLine: toPtr(1)},
Region: &sarif.Region{
StartLine: toPtr(1),
EndLine: toPtr(1),
StartColumn: toPtr(1),
EndColumn: toPtr(1),
},
},
},
},
Expand Down Expand Up @@ -244,3 +259,30 @@ func TestReportWriter_Sarif(t *testing.T) {
})
}
}

func TestToPathUri(t *testing.T) {
tests := []struct {
input string
output string
}{
{
input: "almalinux@sha256:08042694fffd61e6a0b3a22dadba207c8937977915ff6b1879ad744fd6638837",
output: "library/almalinux",
},
{
input: "alpine:latest (alpine 3.13.4)",
output: "library/alpine",
},
{
input: "docker.io/my-organization/my-app:2c6912aee7bde44b84d810aed106ca84f40e2e29",
output: "my-organization/my-app",
},
}

for _, test := range tests {
got := report.ToPathUri(test.input)
if got != test.output {
t.Errorf("toPathUri(%q) got %q, wanted %q", test.input, got, test.output)
}
}
}