|
| 1 | +package reporter |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + _ "embed" |
| 6 | + "strings" |
| 7 | + "text/template" |
| 8 | + |
| 9 | + "github.com/go-errors/errors" |
| 10 | + vf "github.com/smithy-security/smithy/sdk/component/vulnerability-finding" |
| 11 | + ocsffindinginfo "github.com/smithy-security/smithy/sdk/gen/ocsf_ext/finding_info/v1" |
| 12 | + ocsf "github.com/smithy-security/smithy/sdk/gen/ocsf_schema/v1" |
| 13 | + "google.golang.org/protobuf/encoding/protojson" |
| 14 | + |
| 15 | + "github.com/smithy-security/smithy/components/reporters/slack/internal/reporter/util" |
| 16 | +) |
| 17 | + |
| 18 | +//go:embed issue.tpl |
| 19 | +var issueTpl string |
| 20 | + |
| 21 | +type IssueData struct { |
| 22 | + Description string |
| 23 | + Title string |
| 24 | + FindingID uint64 |
| 25 | + FindingLink string |
| 26 | + TargetName string |
| 27 | + TargetLink string |
| 28 | + IsRepository bool |
| 29 | + IsPurl bool |
| 30 | + Confidence string |
| 31 | + CWE string |
| 32 | + CWELink string |
| 33 | + CVE string |
| 34 | + Tool string |
| 35 | + RunName string |
| 36 | + RunLink string |
| 37 | + FindingPath string |
| 38 | + FindingStartLine uint32 |
| 39 | + FindingEndLine uint32 |
| 40 | + Reference string |
| 41 | +} |
| 42 | + |
| 43 | +func (r slackReporter) getMsgs(findings []*vf.VulnerabilityFinding) ([]string, error) { |
| 44 | + tpl, err := template.New("issue").Parse(issueTpl) |
| 45 | + if err != nil { |
| 46 | + return nil, errors.Errorf("could not parse thread template: %w", err) |
| 47 | + } |
| 48 | + |
| 49 | + const ( |
| 50 | + unknownValue = "unknown" |
| 51 | + unsetValue = "-" |
| 52 | + ) |
| 53 | + |
| 54 | + var msgs []string |
| 55 | + for _, finding := range findings { |
| 56 | + var ( |
| 57 | + findingPath, targetName, targetLink, reference, confidence = unknownValue, unknownValue, unknownValue, unknownValue, unknownValue |
| 58 | + findingStartLine, findingEndLine uint32 |
| 59 | + isRepository, isPurl bool |
| 60 | + ) |
| 61 | + |
| 62 | + if finding.Finding.GetConfidence() != "" { |
| 63 | + confidence = r.getConfidence(finding.Finding.GetConfidenceId()) |
| 64 | + } |
| 65 | + |
| 66 | + if len(finding.Finding.GetFindingInfo().DataSources) > 0 { |
| 67 | + var dataSource ocsffindinginfo.DataSource |
| 68 | + if err := protojson.Unmarshal([]byte(finding.Finding.GetFindingInfo().DataSources[0]), &dataSource); err != nil { |
| 69 | + return nil, errors.Errorf("could not unmarshal finding data source from finding info: %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + if dataSource.GetTargetType() == ocsffindinginfo.DataSource_TARGET_TYPE_REPOSITORY { |
| 73 | + reference = dataSource.GetSourceCodeMetadata().GetReference() |
| 74 | + |
| 75 | + switch dataSource.GetUri().GetUriSchema() { |
| 76 | + case ocsffindinginfo.DataSource_URI_SCHEMA_FILE: |
| 77 | + isRepository = true |
| 78 | + findingStartLine = dataSource.GetFileFindingLocationData().GetStartLine() |
| 79 | + findingEndLine = dataSource.GetFileFindingLocationData().GetEndLine() |
| 80 | + findingPath = dataSource.GetUri().GetPath() |
| 81 | + targetName = dataSource.GetSourceCodeMetadata().GetRepositoryUrl() |
| 82 | + var err error |
| 83 | + targetLink, err = util.MakeRepositoryLink(&dataSource) |
| 84 | + if err != nil { |
| 85 | + return nil, errors.Errorf("could not get repo target link: %w", err) |
| 86 | + } |
| 87 | + case ocsffindinginfo.DataSource_URI_SCHEMA_PURL: |
| 88 | + isPurl = true |
| 89 | + targetName = dataSource.GetPurlFindingLocationData().String() |
| 90 | + targetLink = dataSource.GetPurlFindingLocationData().String() |
| 91 | + findingPath = dataSource.GetPurlFindingLocationData().String() |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + targetName = strings.TrimPrefix(targetLink, "https://") |
| 97 | + |
| 98 | + for _, f := range finding.Finding.GetVulnerabilities() { |
| 99 | + var ( |
| 100 | + findingLink = util.MakeFindingLink( |
| 101 | + r.conf.SmithyDashURL.Host, |
| 102 | + finding.ID, |
| 103 | + ) |
| 104 | + runLink = util.MakeRunLink( |
| 105 | + r.conf.SmithyDashURL.Host, |
| 106 | + r.conf.SmithyInstanceID, |
| 107 | + ) |
| 108 | + tool, cwe, cweLink, cve = unknownValue, unsetValue, unsetValue, unsetValue |
| 109 | + ) |
| 110 | + |
| 111 | + if f.GetVendorName() != "" { |
| 112 | + tool = f.GetVendorName() |
| 113 | + } |
| 114 | + |
| 115 | + if f.GetCwe().GetCaption() != "" { |
| 116 | + cwe = f.GetCwe().GetCaption() |
| 117 | + } |
| 118 | + |
| 119 | + if f.GetCwe().GetSrcUrl() != "" { |
| 120 | + cweLink = f.GetCwe().GetSrcUrl() |
| 121 | + } |
| 122 | + |
| 123 | + if f.GetCve().GetUid() != "" { |
| 124 | + cve = f.GetCve().GetUid() |
| 125 | + } |
| 126 | + |
| 127 | + var buf bytes.Buffer |
| 128 | + if err := tpl.Execute( |
| 129 | + &buf, |
| 130 | + IssueData{ |
| 131 | + Title: f.GetTitle(), |
| 132 | + Description: f.GetDesc(), |
| 133 | + FindingID: finding.ID, |
| 134 | + FindingLink: findingLink, |
| 135 | + Tool: tool, |
| 136 | + TargetLink: targetLink, |
| 137 | + TargetName: targetName, |
| 138 | + IsRepository: isRepository, |
| 139 | + IsPurl: isPurl, |
| 140 | + Confidence: confidence, |
| 141 | + CWE: cwe, |
| 142 | + CWELink: cweLink, |
| 143 | + CVE: cve, |
| 144 | + RunName: r.conf.SmithyInstanceName, |
| 145 | + RunLink: runLink, |
| 146 | + FindingPath: findingPath, |
| 147 | + FindingStartLine: findingStartLine, |
| 148 | + FindingEndLine: findingEndLine, |
| 149 | + Reference: reference, |
| 150 | + }, |
| 151 | + ); err != nil { |
| 152 | + return nil, errors.Errorf("could not execute issue description template: %w", err) |
| 153 | + } |
| 154 | + |
| 155 | + msgs = append(msgs, buf.String()) |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return msgs, nil |
| 160 | +} |
| 161 | + |
| 162 | +func (r slackReporter) getPriority(severity string) string { |
| 163 | + switch severity { |
| 164 | + case ocsf.VulnerabilityFinding_SEVERITY_ID_LOW.String(), |
| 165 | + ocsf.VulnerabilityFinding_SEVERITY_ID_INFORMATIONAL.String(), |
| 166 | + ocsf.VulnerabilityFinding_SEVERITY_ID_OTHER.String(): |
| 167 | + return "Low" |
| 168 | + case ocsf.VulnerabilityFinding_SEVERITY_ID_MEDIUM.String(): |
| 169 | + return "Medium" |
| 170 | + case ocsf.VulnerabilityFinding_SEVERITY_ID_HIGH.String(): |
| 171 | + return "High" |
| 172 | + case ocsf.VulnerabilityFinding_SEVERITY_ID_FATAL.String(), |
| 173 | + ocsf.VulnerabilityFinding_SEVERITY_ID_CRITICAL.String(): |
| 174 | + return "Highest" |
| 175 | + } |
| 176 | + return severity |
| 177 | +} |
| 178 | + |
| 179 | +func (r slackReporter) getConfidence(confidence ocsf.VulnerabilityFinding_ConfidenceId) string { |
| 180 | + switch confidence { |
| 181 | + case ocsf.VulnerabilityFinding_CONFIDENCE_ID_LOW, |
| 182 | + ocsf.VulnerabilityFinding_CONFIDENCE_ID_OTHER: |
| 183 | + return "Low" |
| 184 | + case ocsf.VulnerabilityFinding_CONFIDENCE_ID_MEDIUM: |
| 185 | + return "Medium" |
| 186 | + case ocsf.VulnerabilityFinding_CONFIDENCE_ID_HIGH: |
| 187 | + return "High" |
| 188 | + } |
| 189 | + return "Unknown" |
| 190 | +} |
0 commit comments