Skip to content

Commit

Permalink
fix(misconf): clear location URI for SARIF (aquasecurity#6405)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikpivkin authored Mar 29, 2024
1 parent 625f22b commit 712dcd3
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 5 deletions.
13 changes: 9 additions & 4 deletions pkg/report/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,16 @@ func (sw *SarifWriter) Write(ctx context.Context, report types.Report) error {
})
}
for _, misconf := range res.Misconfigurations {
locationURI := clearURI(res.Target)
sw.addSarifResult(&sarifData{
title: "misconfiguration",
vulnerabilityId: misconf.ID,
severity: misconf.Severity,
cvssScore: severityToScore(misconf.Severity),
url: misconf.PrimaryURL,
resourceClass: res.Class,
artifactLocation: target,
locationMessage: target,
artifactLocation: locationURI,
locationMessage: locationURI,
locations: []location{
{
startLine: misconf.CauseMetadata.StartLine,
Expand All @@ -201,7 +202,7 @@ func (sw *SarifWriter) Write(ctx context.Context, report types.Report) error {
helpMarkdown: fmt.Sprintf("**Misconfiguration %v**\n| Type | Severity | Check | Message | Link |\n| --- | --- | --- | --- | --- |\n|%v|%v|%v|%s|[%v](%v)|\n\n%v",
misconf.ID, misconf.Type, misconf.Severity, misconf.Title, misconf.Message, misconf.ID, misconf.PrimaryURL, misconf.Description),
message: fmt.Sprintf("Artifact: %v\nType: %v\nVulnerability %v\nSeverity: %v\nMessage: %v\nLink: [%v](%v)",
res.Target, res.Type, misconf.ID, misconf.Severity, misconf.Message, misconf.ID, misconf.PrimaryURL),
locationURI, res.Type, misconf.ID, misconf.Severity, misconf.Message, misconf.ID, misconf.PrimaryURL),
})
}
for _, secret := range res.Secrets {
Expand Down Expand Up @@ -338,7 +339,11 @@ func ToPathUri(input string, resultClass types.ResultClass) string {
input = ref.Context().RepositoryStr()
}

return strings.ReplaceAll(strings.ReplaceAll(input, "\\", "/"), "git::https:/", "")
return clearURI(input)
}

func clearURI(s string) string {
return strings.ReplaceAll(strings.ReplaceAll(s, "\\", "/"), "git::https:/", "")
}

func (sw *SarifWriter) getLocations(name, version, path string, pkgs []ftypes.Package) []location {
Expand Down
123 changes: 122 additions & 1 deletion pkg/report/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package report_test

import (
"bytes"
"context"
"encoding/json"
"testing"

Expand Down Expand Up @@ -541,6 +542,126 @@ func TestReportWriter_Sarif(t *testing.T) {
},
},
},
{
name: "ref to github",
input: types.Report{
Results: types.Results{
{
Target: "git::https:/github.com/terraform-google-modules/terraform-google-kubernetes-engine?ref=c4809044b52b91505bfba5ef9f25526aa0361788/modules/workload-identity/main.tf",
Class: types.ClassConfig,
Type: ftypes.Terraform,
Misconfigurations: []types.DetectedMisconfiguration{
{
Type: "Terraform Security Check",
ID: "AVD-GCP-0007",
AVDID: "AVD-GCP-0007",
Title: "Service accounts should not have roles assigned with excessive privileges",
Description: "Service accounts should have a minimal set of permissions assigned in order to do their job. They should never have excessive access as if compromised, an attacker can escalate privileges and take over the entire account.",
Message: "Service account is granted a privileged role.",
Query: "data..",
Resolution: "Limit service account access to minimal required set",
Severity: "HIGH",
PrimaryURL: "https://avd.aquasec.com/misconfig/avd-gcp-0007",
References: []string{
"https://cloud.google.com/iam/docs/understanding-roles",
"https://avd.aquasec.com/misconfig/avd-gcp-0007",
},
Status: "Fail",
CauseMetadata: ftypes.CauseMetadata{
StartLine: 91,
EndLine: 91,
Occurrences: []ftypes.Occurrence{
{
Resource: "google_project_iam_member.workload_identity_sa_bindings[\"roles/storage.admin\"]",
Filename: "git::https:/github.com/terraform-google-modules/terraform-google-kubernetes-engine?ref=c4809044b52b91505bfba5ef9f25526aa0361788/modules/workload-identity/main.tf",
Location: ftypes.Location{
StartLine: 87,
EndLine: 93,
},
},
},
},
},
},
},
},
},
want: &sarif.Report{
Version: "2.1.0",
Schema: "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
Runs: []*sarif.Run{
{
Tool: *sarif.NewTool(
&sarif.ToolComponent{
FullName: lo.ToPtr("Trivy Vulnerability Scanner"),
Name: "Trivy",
Version: lo.ToPtr(""),
InformationURI: lo.ToPtr("https://github.com/aquasecurity/trivy"),
Rules: []*sarif.ReportingDescriptor{
{
ID: "AVD-GCP-0007",
Name: lo.ToPtr("Misconfiguration"),
ShortDescription: sarif.NewMultiformatMessageString("Service accounts should not have roles assigned with excessive privileges"),
FullDescription: sarif.NewMultiformatMessageString("Service accounts should have a minimal set of permissions assigned in order to do their job. They should never have excessive access as if compromised, an attacker can escalate privileges and take over the entire account."),
DefaultConfiguration: &sarif.ReportingConfiguration{
Level: "error",
},
HelpURI: lo.ToPtr("https://avd.aquasec.com/misconfig/avd-gcp-0007"),
Help: &sarif.MultiformatMessageString{
Text: lo.ToPtr("Misconfiguration AVD-GCP-0007\nType: Terraform Security Check\nSeverity: HIGH\nCheck: Service accounts should not have roles assigned with excessive privileges\nMessage: Service account is granted a privileged role.\nLink: [AVD-GCP-0007](https://avd.aquasec.com/misconfig/avd-gcp-0007)\nService accounts should have a minimal set of permissions assigned in order to do their job. They should never have excessive access as if compromised, an attacker can escalate privileges and take over the entire account."),
Markdown: lo.ToPtr("**Misconfiguration AVD-GCP-0007**\n| Type | Severity | Check | Message | Link |\n| --- | --- | --- | --- | --- |\n|Terraform Security Check|HIGH|Service accounts should not have roles assigned with excessive privileges|Service account is granted a privileged role.|[AVD-GCP-0007](https://avd.aquasec.com/misconfig/avd-gcp-0007)|\n\nService accounts should have a minimal set of permissions assigned in order to do their job. They should never have excessive access as if compromised, an attacker can escalate privileges and take over the entire account."),
},
Properties: sarif.Properties{
"tags": []interface{}{
"misconfiguration",
"security",
"HIGH",
},
"precision": "very-high",
"security-severity": "8.0",
},
},
},
},
),
Results: []*sarif.Result{
{
RuleID: lo.ToPtr("AVD-GCP-0007"),
RuleIndex: lo.ToPtr(uint(0)),
Level: lo.ToPtr("error"),
Message: *sarif.NewTextMessage("Artifact: github.com/terraform-google-modules/terraform-google-kubernetes-engine?ref=c4809044b52b91505bfba5ef9f25526aa0361788/modules/workload-identity/main.tf\nType: terraform\nVulnerability AVD-GCP-0007\nSeverity: HIGH\nMessage: Service account is granted a privileged role.\nLink: [AVD-GCP-0007](https://avd.aquasec.com/misconfig/avd-gcp-0007)"),
Locations: []*sarif.Location{
{
PhysicalLocation: sarif.NewPhysicalLocation().
WithArtifactLocation(
&sarif.ArtifactLocation{
URI: lo.ToPtr("github.com/terraform-google-modules/terraform-google-kubernetes-engine?ref=c4809044b52b91505bfba5ef9f25526aa0361788/modules/workload-identity/main.tf"),
URIBaseId: lo.ToPtr("ROOTPATH"),
},
).
WithRegion(
&sarif.Region{
StartLine: lo.ToPtr(91),
StartColumn: lo.ToPtr(1),
EndLine: lo.ToPtr(91),
EndColumn: lo.ToPtr(1),
},
),
Message: sarif.NewTextMessage("github.com/terraform-google-modules/terraform-google-kubernetes-engine?ref=c4809044b52b91505bfba5ef9f25526aa0361788/modules/workload-identity/main.tf"),
},
},
},
},
ColumnKind: "utf16CodeUnits",
OriginalUriBaseIDs: map[string]*sarif.ArtifactLocation{
"ROOTPATH": {
URI: lo.ToPtr("file:///"),
},
},
},
},
},
},
}

for _, tt := range tests {
Expand All @@ -549,7 +670,7 @@ func TestReportWriter_Sarif(t *testing.T) {
w := report.SarifWriter{
Output: sarifWritten,
}
err := w.Write(nil, tt.input)
err := w.Write(context.TODO(), tt.input)
assert.NoError(t, err)

result := &sarif.Report{}
Expand Down

0 comments on commit 712dcd3

Please sign in to comment.