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 #8399

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions pkg/report/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ func clearURI(s string) string {
s = strings.ReplaceAll(s, "[email protected]:", "github.com/")
s = strings.ReplaceAll(s, ".git", "")
s = strings.ReplaceAll(s, "?ref=", "/tree/")
case strings.HasPrefix(s, "[email protected]:"):
// build bitbucket url format
// e.g. `[email protected]:terraform-aws-modules/terraform-aws-s3-bucket.git?ref=v4.2.0/main.tf` -> `bitbucket.org/terraform-aws-modules/terraform-aws-s3-bucket/src/v4.2.0/main.tf`
// cf. https://github.com/aquasecurity/trivy/issues/8154
s = strings.ReplaceAll(s, "[email protected]:", "bitbucket.org/")
s = strings.ReplaceAll(s, ".git", "")
s = strings.ReplaceAll(s, "?ref=", "/src/")
Comment on lines +360 to +366
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can refactor this logic out into a function as it's also repeated with GitHub?

case strings.HasPrefix(s, "git::https:/") && !strings.HasPrefix(s, "git::https://"):
s = strings.TrimPrefix(s, "git::https:/")
s = strings.ReplaceAll(s, ".git", "")
Expand Down
5 changes: 5 additions & 0 deletions pkg/report/sarif_private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ func Test_clearURI(t *testing.T) {
uri: "[email protected]:terraform-aws-modules/terraform-aws-s3-bucket.git?ref=v4.2.0/main.tf",
want: "github.com/terraform-aws-modules/terraform-aws-s3-bucket/tree/v4.2.0/main.tf",
},
{
name: "bitbucket",
uri: "[email protected]:terraform-aws-modules/terraform-aws-s3-bucket.git?ref=v4.2.0/main.tf",
want: "bitbucket.org/terraform-aws-modules/terraform-aws-s3-bucket/src/v4.2.0/main.tf",
},
{
name: "git",
uri: "git::https://example.com/storage.git?ref=51d462976d84fdea54b47d80dcabbf680badcdb8",
Expand Down
184 changes: 184 additions & 0 deletions pkg/report/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,190 @@ func TestReportWriter_Sarif(t *testing.T) {
},
},
},
{
name: "ref to bitbucket",
input: types.Report{
Results: types.Results{
{
Target: "git::https:/bitbucket.org/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:/bitbucket.org/terraform-google-modules/terraform-google-kubernetes-engine?ref=c4809044b52b91505bfba5ef9f25526aa0361788/modules/workload-identity/main.tf",
Location: ftypes.Location{
StartLine: 87,
EndLine: 93,
},
},
},
},
},
},
},
{
Target: "[email protected]:terraform-aws-modules/terraform-aws-s3-bucket.git?ref=v4.2.0/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: "[email protected]:terraform-aws-modules/terraform-aws-s3-bucket.git?ref=v4.2.0/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/main/sarif-2.1/schema/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": []any{
"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: bitbucket.org/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("bitbucket.org/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("bitbucket.org/terraform-google-modules/terraform-google-kubernetes-engine?ref=c4809044b52b91505bfba5ef9f25526aa0361788/modules/workload-identity/main.tf"),
},
},
},
{
RuleID: lo.ToPtr("AVD-GCP-0007"),
RuleIndex: lo.ToPtr(uint(0)),
Level: lo.ToPtr("error"),
Message: *sarif.NewTextMessage("Artifact: bitbucket.org/terraform-aws-modules/terraform-aws-s3-bucket/src/v4.2.0/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("bitbucket.org/terraform-aws-modules/terraform-aws-s3-bucket/src/v4.2.0/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("bitbucket.org/terraform-aws-modules/terraform-aws-s3-bucket/src/v4.2.0/main.tf"),
},
},
},
},
ColumnKind: "utf16CodeUnits",
OriginalUriBaseIDs: map[string]*sarif.ArtifactLocation{
"ROOTPATH": {
URI: lo.ToPtr("file:///"),
},
},
},
},
},
},
}

for _, tt := range tests {
Expand Down