Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions integration/testdata/alpine-310.sarif.golden
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
"markdown": "**Vulnerability CVE-2019-1549**\n| Severity | Package | Fixed Version | Link |\n| --- | --- | --- | --- |\n|MEDIUM|libssl1.1|1.1.1d-r0|[CVE-2019-1549](https://avd.aquasec.com/nvd/cve-2019-1549)|\n\nOpenSSL 1.1.1 introduced a rewritten random number generator (RNG). This was intended to include protection in the event of a fork() system call in order to ensure that the parent and child processes did not share the same RNG state. However this protection was not being used in the default case. A partial mitigation for this issue is that the output from a high precision timer is mixed into the RNG state so the likelihood of a parent and child process sharing state is significantly reduced. If an application already calls OPENSSL_init_crypto() explicitly using OPENSSL_INIT_ATFORK then this problem does not occur at all. Fixed in OpenSSL 1.1.1d (Affected 1.1.1-1.1.1c)."
},
"properties": {
"cvss-vector": {
"nvd": {
"V2Vector": "AV:N/AC:L/Au:N/C:P/I:N/A:N",
"V3Vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N",
"V2Score": 5,
"V3Score": 5.3
},
"redhat": {
"V3Vector": "CVSS:3.0/AV:N/AC:H/PR:N/UI:N/S:U/C:L/I:L/A:N",
"V3Score": 4.8
}
},
"precision": "very-high",
"security-severity": "5.3",
"tags": [
Expand Down Expand Up @@ -54,6 +66,18 @@
"markdown": "**Vulnerability CVE-2019-1551**\n| Severity | Package | Fixed Version | Link |\n| --- | --- | --- | --- |\n|MEDIUM|libssl1.1|1.1.1d-r2|[CVE-2019-1551](https://avd.aquasec.com/nvd/cve-2019-1551)|\n\nThere is an overflow bug in the x64_64 Montgomery squaring procedure used in exponentiation with 512-bit moduli. No EC algorithms are affected. Analysis suggests that attacks against 2-prime RSA1024, 3-prime RSA1536, and DSA1024 as a result of this defect would be very difficult to perform and are not believed likely. Attacks against DH512 are considered just feasible. However, for an attack the target would have to re-use the DH512 private key, which is not recommended anyway. Also applications directly using the low level API BN_mod_exp may be affected if they use BN_FLG_CONSTTIME. Fixed in OpenSSL 1.1.1e (Affected 1.1.1-1.1.1d). Fixed in OpenSSL 1.0.2u (Affected 1.0.2-1.0.2t)."
},
"properties": {
"cvss-vector": {
"nvd": {
"V2Vector": "AV:N/AC:L/Au:N/C:P/I:N/A:N",
"V3Vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N",
"V2Score": 5,
"V3Score": 5.3
},
"redhat": {
"V3Vector": "CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:L/I:L/A:N",
"V3Score": 4.8
}
},
"precision": "very-high",
"security-severity": "5.3",
"tags": [
Expand Down
4 changes: 4 additions & 0 deletions pkg/report/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/owenrumney/go-sarif/v2/sarif"
"golang.org/x/xerrors"

dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/types"
Expand Down Expand Up @@ -67,6 +68,7 @@ type sarifData struct {
locationMessage string
message string
cvssScore string
cvssVector dbTypes.VendorCVSS
locations []location
}

Expand Down Expand Up @@ -96,6 +98,7 @@ func (sw *SarifWriter) addSarifRule(data *sarifData) {
},
"precision": "very-high",
"security-severity": data.cvssScore,
"cvss-vector": data.cvssVector,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have documentation about this field or official examples?
We need to make sure we are using the correct names/structure for this property.

Copy link

@orchestr7 orchestr7 Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is called cvss in json output, @axidex you can follow that naming, I guess

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed field from cvss-vector to cvss
JSON ref

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is our (Trivy) field name.
But I told about sarif schema.
Does sarif use similar fields?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is our (Trivy) field name. But I told about sarif schema. Does sarif use similar fields?

I didn't see anything like that in semgrep, codeql or any other tools

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a structure for this:

type CVSSData struct {
	CVSSV2Vector string  `json:"cvssv2_vector,omitempty"`
	CVSSV2Score  float64 `json:"cvssv2_score,omitempty"`

	CVSSV3Vector string  `json:"cvssv3_vector,omitempty"`
	CVSSV3Score  float64 `json:"cvssv3_score,omitempty"`

	CVSSV40Vector string  `json:"cvssv40_vector,omitempty"`
	CVSSV40Score  float64 `json:"cvssv40_score,omitempty"`
}

Copy link
Contributor

@DmitriyLewen DmitriyLewen Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to create struct for this.
cvssv2_score is property. (same for other fields)
So should be (example):

              "properties": {
                "cvssv2_vector": "AV:N/AC:L/Au:N/C:P/I:N/A:N",
                "cvssv2_score": 5,
                "cvssv3_vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N",
                  "cvssv3_score": 5.3
                "precision": "very-high",

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to create struct for this. cvssv2_score is property. (same for other fields) So should be (example):

              "properties": {
                "cvssv2_vector": "AV:N/AC:L/Au:N/C:P/I:N/A:N",
                "cvssv2_score": 5,
                "cvssv3_vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:N/A:N",
                  "cvssv3_score": 5.3
                "precision": "very-high",

Changed this in the new version

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.
I created small refactoring, can you take a look:

diff --git a/pkg/report/sarif.go b/pkg/report/sarif.go
index 2e2268450..ee7f2b87b 100644
--- a/pkg/report/sarif.go
+++ b/pkg/report/sarif.go
@@ -43,17 +43,6 @@ var (
 	pathRegex = regexp.MustCompile(`(?P<path>.+?)(?:\s*\((?:.*?)\).*?)?$`)
 )
 
-type CVSSData struct {
-	CVSSV2Vector string
-	CVSSV2Score  float64
-
-	CVSSV3Vector string
-	CVSSV3Score  float64
-
-	CVSSV40Vector string
-	CVSSV40Score  float64
-}
-
 // SarifWriter implements result Writer
 type SarifWriter struct {
 	Output        io.Writer
@@ -78,7 +67,7 @@ type sarifData struct {
 	locationMessage  string
 	message          string
 	cvssScore        string
-	cvssData         CVSSData
+	cvssData         map[string]any
 	locations        []location
 }
 
@@ -100,7 +89,7 @@ func (sw *SarifWriter) addSarifRule(data *sarifData) {
 		WithDefaultConfiguration(&sarif.ReportingConfiguration{
 			Level: toSarifErrorLevel(data.severity),
 		}).
-		WithProperties(MakeProperties(data.title, data.severity, data.cvssScore, data.cvssData))
+		WithProperties(toProperties(data.title, data.severity, data.cvssScore, data.cvssData))
 	if data.url != nil && data.url.String() != "" {
 		r.WithHelpURI(data.url.String())
 	}
@@ -162,12 +151,13 @@ func (sw *SarifWriter) Write(_ context.Context, report types.Report) error {
 			if vuln.PkgPath != "" {
 				path = ToPathUri(vuln.PkgPath, res.Class)
 			}
+			cvssData, cvssScore := toCVSSData(vuln)
 			sw.addSarifResult(&sarifData{
 				title:            "vulnerability",
 				vulnerabilityId:  vuln.VulnerabilityID,
 				severity:         vuln.Severity,
-				cvssScore:        getCVSSScore(vuln),
-				cvssData:         getCVSSData(vuln),
+				cvssScore:        cvssScore,
+				cvssData:         cvssData,
 				url:              toUri(vuln.PrimaryURL),
 				resourceClass:    res.Class,
 				artifactLocation: toUri(path),
@@ -419,29 +409,25 @@ func (sw *SarifWriter) getLocations(name, version, path string, pkgs []ftypes.Pa
 	return locs
 }
 
-func getCVSSScore(vuln types.DetectedVulnerability) string {
-	// Take the vendor score
-	if cvss, ok := vuln.CVSS[vuln.SeveritySource]; ok {
-		return fmt.Sprintf("%.1f", cvss.V3Score)
-	}
-
-	// Converts severity to score
-	return severityToScore(vuln.Severity)
-}
-
-func getCVSSData(vuln types.DetectedVulnerability) CVSSData {
+// toCVSSData extracts CVSS data from the vulnerability and returns it along with the score.
+// If CVSS V3 Score is not available, it returns an empty CVSSData struct and a score based on severity.
+func toCVSSData(vuln types.DetectedVulnerability) (map[string]any, string) {
+	score := severityToScore(vuln.Severity)
+	var data = make(map[string]any)
 	if cvss, ok := vuln.CVSS[vuln.SeveritySource]; ok {
-		return CVSSData{
-			CVSSV2Score:   cvss.V2Score,
-			CVSSV2Vector:  cvss.V2Vector,
-			CVSSV3Score:   cvss.V3Score,
-			CVSSV3Vector:  cvss.V3Vector,
-			CVSSV40Score:  cvss.V40Score,
-			CVSSV40Vector: cvss.V40Vector,
+		data["cvssv2_vector"] = cvss.V2Vector
+		data["cvssv2_score"] = cvss.V2Score
+		data["cvssv3_vector"] = cvss.V3Vector
+		data["cvssv3_score"] = cvss.V3Score
+		data["cvssv40_vector"] = cvss.V40Vector
+		data["cvssv40_score"] = cvss.V40Score
+
+		if cvss.V3Score != 0 {
+			score = fmt.Sprintf("%.1f", cvss.V3Score)
 		}
 	}
 
-	return CVSSData{}
+	return data, score
 }
 
 func severityToScore(severity string) string {
@@ -459,7 +445,7 @@ func severityToScore(severity string) string {
 	}
 }
 
-func MakeProperties(title, severity, cvssScore string, cvssData CVSSData) sarif.Properties {
+func toProperties(title, severity, cvssScore string, cvssData map[string]any) sarif.Properties {
 	properties := sarif.Properties{
 		"tags": []string{
 			title,
@@ -470,28 +456,18 @@ func MakeProperties(title, severity, cvssScore string, cvssData CVSSData) sarif.
 		"security-severity": cvssScore,
 	}
 
-	// Add CVSS v2
-	if cvssData.CVSSV2Vector != "" {
-		properties["cvssv2_vector"] = cvssData.CVSSV2Vector
-	}
-	if cvssData.CVSSV2Score != 0 {
-		properties["cvssv2_score"] = cvssData.CVSSV2Score
-	}
-
-	// Add CVSS v3
-	if cvssData.CVSSV3Vector != "" {
-		properties["cvssv3_vector"] = cvssData.CVSSV3Vector
-	}
-	if cvssData.CVSSV3Score != 0 {
-		properties["cvssv3_score"] = cvssData.CVSSV3Score
-	}
-
-	// Add CVSS v4
-	if cvssData.CVSSV40Vector != "" {
-		properties["cvssv40_vector"] = cvssData.CVSSV40Vector
-	}
-	if cvssData.CVSSV40Score != 0 {
-		properties["cvssv40_score"] = cvssData.CVSSV40Score
+	for key, value := range cvssData {
+		switch v := value.(type) {
+		case string:
+			if v == "" {
+				continue
+			}
+		case float64:
+			if v == 0 {
+				continue
+			}
+		}
+		properties[key] = value
 	}
 
 	return properties

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the refactoring! I have made changes based on your suggestions.

})
if data.url != nil && data.url.String() != "" {
r.WithHelpURI(data.url.String())
Expand Down Expand Up @@ -163,6 +166,7 @@ func (sw *SarifWriter) Write(_ context.Context, report types.Report) error {
vulnerabilityId: vuln.VulnerabilityID,
severity: vuln.Severity,
cvssScore: getCVSSScore(vuln),
cvssVector: vuln.CVSS,
url: toUri(vuln.PrimaryURL),
resourceClass: res.Class,
artifactLocation: toUri(path),
Expand Down
15 changes: 15 additions & 0 deletions pkg/report/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ func TestReportWriter_Sarif(t *testing.T) {
},
"precision": "very-high",
"security-severity": "7.5",
"cvss-vector": map[string]any{
"nvd": map[string]any{
"V3Vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H",
"V3Score": 9.8,
},
"redhat": map[string]any{
"V3Vector": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:H",
"V3Score": 7.5,
},
},
},
Help: &sarif.MultiformatMessageString{
Text: lo.ToPtr("Vulnerability CVE-2020-0001\nSeverity: HIGH\nPackage: foo\nFixed Version: 3.4.5\nLink: [CVE-2020-0001](https://avd.aquasec.com/nvd/cve-2020-0001)\nbaz"),
Expand Down Expand Up @@ -244,6 +254,7 @@ func TestReportWriter_Sarif(t *testing.T) {
},
"precision": "very-high",
"security-severity": "8.0",
"cvss-vector": nil,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: It makes no sense to add the value nil here.
Same for the other lines below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests aren't passing without nil failing here

--- Expected
+++ Actual
@@ -73,3 +73,4 @@
        MessageStrings: (*sarif.MessageStrings)(<nil>),
-       Properties: (sarif.Properties) (len=3) {
+       Properties: (sarif.Properties) (len=4) {
+        (string) (len=11) "cvss-vector": (interface {}) <nil>,

Copy link
Contributor Author

@axidex axidex Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

},
Help: &sarif.MultiformatMessageString{
Text: lo.ToPtr("Misconfiguration KSV001\nType: Kubernetes Security Check\nSeverity: HIGH\nCheck: Image tag ':latest' used\nMessage: Message\nLink: [KSV001](https://avd.aquasec.com/appshield/ksv001)\n"),
Expand All @@ -267,6 +278,7 @@ func TestReportWriter_Sarif(t *testing.T) {
},
"precision": "very-high",
"security-severity": "9.5",
"cvss-vector": nil,
},
Help: &sarif.MultiformatMessageString{
Text: lo.ToPtr("Misconfiguration KSV002\nType: Kubernetes Security Check\nSeverity: CRITICAL\nCheck: SYS_ADMIN capability added\nMessage: Message\nLink: [KSV002](https://avd.aquasec.com/appshield/ksv002)\n"),
Expand Down Expand Up @@ -384,6 +396,7 @@ func TestReportWriter_Sarif(t *testing.T) {
},
"precision": "very-high",
"security-severity": "9.5",
"cvss-vector": nil,
},
Help: &sarif.MultiformatMessageString{
Text: lo.ToPtr("Secret AWS Secret Access Key\nSeverity: CRITICAL\nMatch: 'AWS_secret_KEY'=\"****************************************\""),
Expand Down Expand Up @@ -477,6 +490,7 @@ func TestReportWriter_Sarif(t *testing.T) {
},
"precision": "very-high",
"security-severity": "8.0",
"cvss-vector": nil,
},
},
},
Expand Down Expand Up @@ -659,6 +673,7 @@ func TestReportWriter_Sarif(t *testing.T) {
},
"precision": "very-high",
"security-severity": "8.0",
"cvss-vector": nil,
},
},
},
Expand Down