|
| 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 | +const ( |
| 22 | + unknownValue = "unknown" |
| 23 | + unsetValue = "-" |
| 24 | +) |
| 25 | + |
| 26 | +// IssueData holds all the information needed to create a Slack issue message from a VulnerabilityFinding and Vulnerability instance. |
| 27 | +// It is designed to be used with text/template to generate formatted messages. |
| 28 | +type IssueData struct { |
| 29 | + Description string |
| 30 | + Title string |
| 31 | + FindingID uint64 |
| 32 | + FindingLink string |
| 33 | + TargetName string |
| 34 | + TargetLink string |
| 35 | + IsRepository bool |
| 36 | + IsPurl bool |
| 37 | + Confidence string |
| 38 | + Priority string |
| 39 | + CWE string |
| 40 | + CWELink string |
| 41 | + CVE string |
| 42 | + Tool string |
| 43 | + RunName string |
| 44 | + RunLink string |
| 45 | + FindingPath string |
| 46 | + FindingStartLine uint32 |
| 47 | + FindingEndLine uint32 |
| 48 | + Reference string |
| 49 | +} |
| 50 | + |
| 51 | +// NewIssueData creates a new IssueData struct from a VulnerabilityFinding and configuration. |
| 52 | +// It sets default values for most of the IssueData fields, and extracts relevant information for elements that are common across vulns from a single scan (e.g. time started or runID) |
| 53 | +func NewIssueData(finding *vf.VulnerabilityFinding, conf *Conf) (*IssueData, error) { |
| 54 | + if finding == nil || finding.Finding == nil { |
| 55 | + return nil, errors.New("finding or finding.Finding is nil") |
| 56 | + } |
| 57 | + |
| 58 | + if conf == nil { |
| 59 | + return nil, errors.New("configuration is nil") |
| 60 | + } |
| 61 | + |
| 62 | + var ( |
| 63 | + findingPath, targetLink, reference, confidence, severity, title, description = unknownValue, unknownValue, unknownValue, unknownValue, unknownValue, unsetValue, unsetValue |
| 64 | + findingStartLine, findingEndLine uint32 |
| 65 | + isRepository, isPurl bool |
| 66 | + findingLink = util.MakeFindingLink(conf.SmithyDashURL.Host, finding.ID) |
| 67 | + runLink = util.MakeRunLink(conf.SmithyDashURL.Host, conf.SmithyInstanceID) |
| 68 | + ) |
| 69 | + |
| 70 | + if len(finding.Finding.GetFindingInfo().DataSources) > 0 { |
| 71 | + var dataSource ocsffindinginfo.DataSource |
| 72 | + if err := protojson.Unmarshal([]byte(finding.Finding.GetFindingInfo().DataSources[0]), &dataSource); err != nil { |
| 73 | + return nil, errors.Errorf("could not unmarshal finding data source from finding info: %w", err) |
| 74 | + } |
| 75 | + |
| 76 | + if dataSource.GetTargetType() == ocsffindinginfo.DataSource_TARGET_TYPE_REPOSITORY { |
| 77 | + reference = dataSource.GetSourceCodeMetadata().GetReference() |
| 78 | + |
| 79 | + switch dataSource.GetUri().GetUriSchema() { |
| 80 | + case ocsffindinginfo.DataSource_URI_SCHEMA_FILE: |
| 81 | + isRepository = true |
| 82 | + findingStartLine = dataSource.GetFileFindingLocationData().GetStartLine() |
| 83 | + findingEndLine = dataSource.GetFileFindingLocationData().GetEndLine() |
| 84 | + findingPath = dataSource.GetUri().GetPath() |
| 85 | + var err error |
| 86 | + targetLink, err = util.MakeRepositoryLink(&dataSource) |
| 87 | + if err != nil { |
| 88 | + return nil, errors.Errorf("could not get repo target link: %w", err) |
| 89 | + } |
| 90 | + case ocsffindinginfo.DataSource_URI_SCHEMA_PURL: |
| 91 | + isPurl = true |
| 92 | + targetLink = dataSource.GetPurlFindingLocationData().String() |
| 93 | + findingPath = dataSource.GetPurlFindingLocationData().String() |
| 94 | + default: |
| 95 | + return nil, errors.Errorf("unsupported data source uri schema: %s", dataSource.GetUri().GetUriSchema().String()) |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + issueData := &IssueData{ |
| 102 | + FindingID: finding.ID, |
| 103 | + FindingLink: findingLink, |
| 104 | + TargetLink: targetLink, |
| 105 | + TargetName: strings.TrimPrefix(targetLink, "https://"), |
| 106 | + IsRepository: isRepository, |
| 107 | + IsPurl: isPurl, |
| 108 | + Confidence: confidence, |
| 109 | + RunName: conf.SmithyInstanceName, |
| 110 | + RunLink: runLink, |
| 111 | + FindingPath: findingPath, |
| 112 | + FindingStartLine: findingStartLine, |
| 113 | + FindingEndLine: findingEndLine, |
| 114 | + Reference: reference, |
| 115 | + Priority: severity, |
| 116 | + Title: title, |
| 117 | + Description: description, |
| 118 | + } |
| 119 | + |
| 120 | + if finding.Finding.GetConfidence() != "" { |
| 121 | + issueData.Confidence = issueData.getConfidence(finding.Finding.GetConfidenceId()) |
| 122 | + } |
| 123 | + |
| 124 | + // Set default, overridable values for Priority and Description |
| 125 | + if finding.Finding.GetSeverity() != "" { |
| 126 | + issueData.Priority = issueData.getPriority(finding.Finding.GetSeverity()) |
| 127 | + } |
| 128 | + |
| 129 | + if finding.Finding.GetMessage() != "" { |
| 130 | + issueData.Description = finding.Finding.GetMessage() |
| 131 | + } |
| 132 | + |
| 133 | + return issueData, nil |
| 134 | +} |
| 135 | + |
| 136 | +// NewVulnerability enriches the current IssueData with a new Vulnerability instance from the IssueData. |
| 137 | +// It sets default values for Tool, CWE, CWELink, and CVE, and extracts relevant information from the Vulnerability instance to populate the IssueData fields. |
| 138 | +// It overrides Title, Description, and Priority fields if the Vulnerability instance provides values for them. |
| 139 | +func (i IssueData) EnrichWithNewVulnerability(vulnerability *ocsf.Vulnerability) (*IssueData, error) { |
| 140 | + |
| 141 | + i.Tool, i.CWE, i.CWELink, i.CVE = unknownValue, unsetValue, unsetValue, unsetValue |
| 142 | + |
| 143 | + if vulnerability.GetVendorName() != "" { |
| 144 | + i.Tool = vulnerability.GetVendorName() |
| 145 | + } |
| 146 | + |
| 147 | + if vulnerability.GetCwe().GetCaption() != "" { |
| 148 | + i.CWE = vulnerability.GetCwe().GetCaption() |
| 149 | + } |
| 150 | + |
| 151 | + if vulnerability.GetCwe().GetSrcUrl() != "" { |
| 152 | + i.CWELink = vulnerability.GetCwe().GetSrcUrl() |
| 153 | + } |
| 154 | + |
| 155 | + if vulnerability.GetCve().GetUid() != "" { |
| 156 | + i.CVE = vulnerability.GetCve().GetUid() |
| 157 | + } |
| 158 | + |
| 159 | + // Override Title, Description, and Priority if the vulnerability provides them |
| 160 | + if vulnerability.GetSeverity() != "" { |
| 161 | + i.Priority = i.getPriority(vulnerability.GetSeverity()) |
| 162 | + } |
| 163 | + |
| 164 | + if vulnerability.GetDesc() != "" { |
| 165 | + i.Description = vulnerability.GetDesc() |
| 166 | + } |
| 167 | + |
| 168 | + if vulnerability.GetTitle() != "" { |
| 169 | + i.Title = vulnerability.GetTitle() |
| 170 | + } |
| 171 | + |
| 172 | + return &i, nil |
| 173 | +} |
| 174 | + |
| 175 | +// String executes the provided template with the IssueData and returns the resulting string. |
| 176 | +func (i IssueData) String(tpl *template.Template) (string, error) { |
| 177 | + var buf bytes.Buffer |
| 178 | + if err := tpl.Execute(&buf, i); err != nil { |
| 179 | + return "", errors.Errorf("could not execute issue description template: %w", err) |
| 180 | + } |
| 181 | + return buf.String(), nil |
| 182 | +} |
| 183 | + |
| 184 | +func (i IssueData) getPriority(severity string) string { |
| 185 | + switch severity { |
| 186 | + case ocsf.VulnerabilityFinding_SEVERITY_ID_LOW.String(), |
| 187 | + ocsf.VulnerabilityFinding_SEVERITY_ID_INFORMATIONAL.String(), |
| 188 | + ocsf.VulnerabilityFinding_SEVERITY_ID_OTHER.String(): |
| 189 | + return "Low" |
| 190 | + case ocsf.VulnerabilityFinding_SEVERITY_ID_MEDIUM.String(): |
| 191 | + return "Medium" |
| 192 | + case ocsf.VulnerabilityFinding_SEVERITY_ID_HIGH.String(): |
| 193 | + return "High" |
| 194 | + case ocsf.VulnerabilityFinding_SEVERITY_ID_FATAL.String(), |
| 195 | + ocsf.VulnerabilityFinding_SEVERITY_ID_CRITICAL.String(): |
| 196 | + return "Highest" |
| 197 | + } |
| 198 | + return severity |
| 199 | +} |
| 200 | + |
| 201 | +func (i IssueData) getConfidence(confidence ocsf.VulnerabilityFinding_ConfidenceId) string { |
| 202 | + switch confidence { |
| 203 | + case ocsf.VulnerabilityFinding_CONFIDENCE_ID_LOW, |
| 204 | + ocsf.VulnerabilityFinding_CONFIDENCE_ID_OTHER: |
| 205 | + return "Low" |
| 206 | + case ocsf.VulnerabilityFinding_CONFIDENCE_ID_MEDIUM: |
| 207 | + return "Medium" |
| 208 | + case ocsf.VulnerabilityFinding_CONFIDENCE_ID_HIGH: |
| 209 | + return "High" |
| 210 | + } |
| 211 | + return "Unknown" |
| 212 | +} |
0 commit comments