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): handle [email protected] schema for misconfigs in sarif report #7898

Merged
merged 7 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
12 changes: 11 additions & 1 deletion pkg/report/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,18 @@ func ToPathUri(input string, resultClass types.ResultClass) string {
return clearURI(input)
}

// clearURI clears URI for misconfigs
func clearURI(s string) string {
return strings.ReplaceAll(strings.ReplaceAll(s, "\\", "/"), "git::https:/", "")
s = strings.ReplaceAll(s, "\\", "/")
if strings.HasPrefix(s, "git::https:/") {
s = strings.ReplaceAll(s, "git::https:/", "")
nikpivkin marked this conversation as resolved.
Show resolved Hide resolved
} else if strings.HasPrefix(s, "[email protected]:") {
// cf. https://developer.hashicorp.com/terraform/language/modules/sources#github
s = strings.ReplaceAll(s, "[email protected]:", "github.com/")
s = strings.ReplaceAll(s, ".git", "")
}

return s
}

func toUri(str string) *url.URL {
Expand Down
64 changes: 64 additions & 0 deletions pkg/report/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,44 @@ func TestReportWriter_Sarif(t *testing.T) {
},
},
},
{
Target: "[email protected]:rbs-path/terraform-modules.git/modules/compute/aws-lambda?ref=v3.8.0/.terraform/modules/cleanup/modules/compute/aws-lambda/cloudwatch.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: "[email protected]:rbs-path/terraform-modules.git/modules/compute/aws-lambda?ref=v3.8.0/.terraform/modules/cleanup/modules/compute/aws-lambda/cloudwatch.tf",
Location: ftypes.Location{
StartLine: 87,
EndLine: 93,
},
},
},
},
},
},
},
},
},
want: &sarif.Report{
Expand Down Expand Up @@ -655,6 +693,32 @@ func TestReportWriter_Sarif(t *testing.T) {
},
},
},
{
RuleID: lo.ToPtr("AVD-GCP-0007"),
RuleIndex: lo.ToPtr(uint(0)),
Level: lo.ToPtr("error"),
Message: *sarif.NewTextMessage("Artifact: github.com/rbs-path/terraform-modules/modules/compute/aws-lambda?ref=v3.8.0/.terraform/modules/cleanup/modules/compute/aws-lambda/cloudwatch.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/rbs-path/terraform-modules/modules/compute/aws-lambda?ref=v3.8.0/.terraform/modules/cleanup/modules/compute/aws-lambda/cloudwatch.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/rbs-path/terraform-modules/modules/compute/aws-lambda?ref=v3.8.0/.terraform/modules/cleanup/modules/compute/aws-lambda/cloudwatch.tf"),
},
},
},
},
ColumnKind: "utf16CodeUnits",
OriginalUriBaseIDs: map[string]*sarif.ArtifactLocation{
Expand Down