forked from jfrog/frogbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanpullrequest.go
More file actions
364 lines (326 loc) · 15.1 KB
/
scanpullrequest.go
File metadata and controls
364 lines (326 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package scanpullrequest
import (
"context"
"errors"
"fmt"
"os"
"github.com/jfrog/frogbot/v2/utils"
"github.com/jfrog/froggit-go/vcsclient"
"github.com/jfrog/froggit-go/vcsutils"
"github.com/jfrog/gofrog/datastructures"
"github.com/jfrog/jfrog-cli-security/utils/formats"
"github.com/jfrog/jfrog-cli-security/utils/jasutils"
"github.com/jfrog/jfrog-cli-security/utils/results"
"github.com/jfrog/jfrog-cli-security/utils/results/conversion"
"github.com/jfrog/jfrog-cli-security/utils/xsc"
"github.com/jfrog/jfrog-client-go/utils/log"
"github.com/jfrog/jfrog-client-go/xray/services"
)
const (
SecurityIssueFoundErr = "issues were detected by Frogbot\n You can avoid marking the Frogbot scan as failed by setting failOnSecurityIssues to false in the " + utils.FrogbotConfigFile + " file"
noGitHubEnvErr = "frogbot did not scan this PR, because a GitHub Environment named 'frogbot' does not exist. Please refer to the Frogbot documentation for instructions on how to create the Environment"
noGitHubEnvReviewersErr = "frogbot did not scan this PR, because the existing GitHub Environment named 'frogbot' doesn't have reviewers selected. Please refer to the Frogbot documentation for instructions on how to create the Environment"
analyticsScanPrScanType = "PR"
)
type ScanPullRequestCmd struct{}
// Run ScanPullRequest method only works for a single repository scan.
// Therefore, the first repository config represents the repository on which Frogbot runs, and it is the only one that matters.
func (cmd *ScanPullRequestCmd) Run(configAggregator utils.RepoAggregator, client vcsclient.VcsClient, frogbotRepoConnection *utils.UrlAccessChecker) (err error) {
if err = utils.ValidateSingleRepoConfiguration(&configAggregator); err != nil {
return
}
repoConfig := &(configAggregator)[0]
if repoConfig.GitProvider == vcsutils.GitHub {
if err = verifyGitHubFrogbotEnvironment(client, repoConfig); err != nil {
return
}
}
repoConfig.OutputWriter.SetHasInternetConnection(frogbotRepoConnection.IsConnected())
if repoConfig.PullRequestDetails, err = client.GetPullRequestByID(context.Background(), repoConfig.RepoOwner, repoConfig.RepoName, int(repoConfig.PullRequestDetails.ID)); err != nil {
return
}
return scanPullRequest(repoConfig, client)
}
// Verify that the 'frogbot' GitHub environment was properly configured on the repository
func verifyGitHubFrogbotEnvironment(client vcsclient.VcsClient, repoConfig *utils.Repository) error {
if repoConfig.APIEndpoint != "" && repoConfig.APIEndpoint != "https://api.github.com" {
// Don't verify 'frogbot' environment on GitHub on-prem
return nil
}
if _, exist := os.LookupEnv(utils.GitHubActionsEnv); !exist {
// Don't verify 'frogbot' environment on non GitHub Actions CI
return nil
}
// If the repository is not public, using 'frogbot' environment is not mandatory
repoInfo, err := client.GetRepositoryInfo(context.Background(), repoConfig.RepoOwner, repoConfig.RepoName)
if err != nil {
return err
}
if repoInfo.RepositoryVisibility != vcsclient.Public {
return nil
}
// Get the 'frogbot' environment info and make sure it exists and includes reviewers
repoEnvInfo, err := client.GetRepositoryEnvironmentInfo(context.Background(), repoConfig.RepoOwner, repoConfig.RepoName, "frogbot")
if err != nil {
return errors.New(err.Error() + "\n" + noGitHubEnvErr)
}
if len(repoEnvInfo.Reviewers) == 0 {
return errors.New(noGitHubEnvReviewersErr)
}
return nil
}
// By default, includeAllVulnerabilities is set to false and the scan goes as follows:
// a. Audit the dependencies of the source and the target branches.
// b. Compare the vulnerabilities found in source and target branches, and show only the new vulnerabilities added by the pull request.
// Otherwise, only the source branch is scanned and all found vulnerabilities are being displayed.
func scanPullRequest(repo *utils.Repository, client vcsclient.VcsClient) (err error) {
pullRequestDetails := repo.PullRequestDetails
log.Info(fmt.Sprintf("Scanning Pull Request #%d (from source branch: <%s/%s/%s> to target branch: <%s/%s/%s>)",
pullRequestDetails.ID,
pullRequestDetails.Source.Owner, pullRequestDetails.Source.Repository, pullRequestDetails.Source.Name,
pullRequestDetails.Target.Owner, pullRequestDetails.Target.Repository, pullRequestDetails.Target.Name))
log.Info("-----------------------------------------------------------")
analyticsService := utils.AddAnalyticsGeneralEvent(nil, &repo.Server, analyticsScanPrScanType)
defer func() {
analyticsService.UpdateAndSendXscAnalyticsGeneralEventFinalize(err)
}()
// Audit PR code
issues, err := auditPullRequest(repo, client, analyticsService)
if err != nil {
return
}
// Output results
shouldSendExposedSecretsEmail := issues.SecretsExists() && repo.SmtpServer != ""
if shouldSendExposedSecretsEmail {
secretsEmailDetails := utils.NewSecretsEmailDetails(client, repo, issues.Secrets)
if err = utils.AlertSecretsExposed(secretsEmailDetails); err != nil {
return
}
}
// Handle PR comments for scan output
if err = utils.HandlePullRequestCommentsAfterScan(issues, repo, client, int(pullRequestDetails.ID)); err != nil {
return
}
// Fail the Frogbot task if a security issue is found and Frogbot isn't configured to avoid the failure.
if toFailTaskStatus(repo, issues) {
err = errors.New(SecurityIssueFoundErr)
return
}
return
}
func toFailTaskStatus(repo *utils.Repository, issues *utils.IssuesCollection) bool {
failFlagSet := repo.FailOnSecurityIssues != nil && *repo.FailOnSecurityIssues
return failFlagSet && issues.IssuesExists()
}
// Downloads Pull Requests branches code and audits them
func auditPullRequest(repoConfig *utils.Repository, client vcsclient.VcsClient, analyticsService *xsc.AnalyticsMetricsService) (issuesCollection *utils.IssuesCollection, err error) {
scanDetails := utils.NewScanDetails(client, &repoConfig.Server, &repoConfig.Git).
SetXrayGraphScanParams(repoConfig.Watches, repoConfig.JFrogProjectKey, len(repoConfig.AllowedLicenses) > 0).
SetFixableOnly(repoConfig.FixableOnly).
SetFailOnInstallationErrors(*repoConfig.FailOnSecurityIssues).
SetConfigProfile(repoConfig.ConfigProfile).
SetSkipAutoInstall(repoConfig.SkipAutoInstall).
SetDisableJas(repoConfig.DisableJas)
if scanDetails, err = scanDetails.SetMinSeverity(repoConfig.MinSeverity); err != nil {
return
}
// If MSI exists we always need to report events
if analyticsService.GetMsi() != "" {
// MSI is passed to XrayGraphScanParams, so it can be later used by other analytics events in the scan phase
scanDetails.XrayGraphScanParams.MultiScanId = analyticsService.GetMsi()
}
issuesCollection = &utils.IssuesCollection{}
for i := range repoConfig.Projects {
scanDetails.SetProject(&repoConfig.Projects[i])
var projectIssues *utils.IssuesCollection
if projectIssues, err = auditPullRequestInProject(repoConfig, scanDetails); err != nil {
return
}
issuesCollection.Append(projectIssues)
}
if analyticsService.ShouldReportEvents() {
analyticsService.AddScanFindingsToXscAnalyticsGeneralEventFinalize(issuesCollection.CountIssuesCollectionFindings())
}
return
}
func auditPullRequestInProject(repoConfig *utils.Repository, scanDetails *utils.ScanDetails) (auditIssues *utils.IssuesCollection, err error) {
// Download source branch
sourcePullRequestInfo := scanDetails.PullRequestDetails.Source
sourceBranchWd, cleanupSource, err := utils.DownloadRepoToTempDir(scanDetails.Client(), sourcePullRequestInfo.Owner, sourcePullRequestInfo.Repository, sourcePullRequestInfo.Name)
if err != nil {
return
}
defer func() {
err = errors.Join(err, cleanupSource())
}()
// Audit source branch
var sourceResults *results.SecurityCommandResults
workingDirs := utils.GetFullPathWorkingDirs(scanDetails.Project.WorkingDirs, sourceBranchWd)
log.Info("Scanning source branch...")
sourceResults, err = scanDetails.RunInstallAndAudit(workingDirs...)
if err != nil {
return
}
// Set JAS output flags
repoConfig.OutputWriter.SetJasOutputFlags(sourceResults.EntitledForJas, len(sourceResults.GetJasScansResults(jasutils.Applicability)) > 0)
// Get all issues that exist in the source branch
if repoConfig.IncludeAllVulnerabilities {
if auditIssues, err = getAllIssues(sourceResults, repoConfig.AllowedLicenses, scanDetails.HasViolationContext()); err != nil {
return
}
utils.ConvertSarifPathsToRelative(auditIssues, sourceBranchWd)
return
}
var targetBranchWd string
if auditIssues, targetBranchWd, err = auditTargetBranch(repoConfig, scanDetails, sourceResults); err != nil {
return
}
utils.ConvertSarifPathsToRelative(auditIssues, sourceBranchWd, targetBranchWd)
return
}
func auditTargetBranch(repoConfig *utils.Repository, scanDetails *utils.ScanDetails, sourceScanResults *results.SecurityCommandResults) (newIssues *utils.IssuesCollection, targetBranchWd string, err error) {
// Download target branch (if needed)
cleanupTarget := func() error { return nil }
if !repoConfig.IncludeAllVulnerabilities {
targetBranchInfo := repoConfig.PullRequestDetails.Target
if targetBranchWd, cleanupTarget, err = utils.DownloadRepoToTempDir(scanDetails.Client(), targetBranchInfo.Owner, targetBranchInfo.Repository, targetBranchInfo.Name); err != nil {
return
}
}
defer func() {
err = errors.Join(err, cleanupTarget())
}()
// Set target branch scan details
var targetResults *results.SecurityCommandResults
workingDirs := utils.GetFullPathWorkingDirs(scanDetails.Project.WorkingDirs, targetBranchWd)
log.Info("Scanning target branch...")
targetResults, err = scanDetails.RunInstallAndAudit(workingDirs...)
if err != nil {
return
}
// Get newly added issues
newIssues, err = getNewlyAddedIssues(targetResults, sourceScanResults, repoConfig.AllowedLicenses, scanDetails.HasViolationContext())
return
}
func getAllIssues(cmdResults *results.SecurityCommandResults, allowedLicenses []string, hasViolationContext bool) (*utils.IssuesCollection, error) {
log.Info("Frogbot is configured to show all vulnerabilities")
simpleJsonResults, err := conversion.NewCommandResultsConvertor(conversion.ResultConvertParams{
IncludeVulnerabilities: true,
HasViolationContext: hasViolationContext,
AllowedLicenses: allowedLicenses,
IncludeLicenses: true,
SimplifiedOutput: true,
}).ConvertToSimpleJson(cmdResults)
if err != nil {
return nil, err
}
return &utils.IssuesCollection{
Vulnerabilities: append(simpleJsonResults.Vulnerabilities, simpleJsonResults.SecurityViolations...),
Iacs: simpleJsonResults.Iacs,
Secrets: simpleJsonResults.Secrets,
Sast: simpleJsonResults.Sast,
Licenses: simpleJsonResults.LicensesViolations,
}, nil
}
// Returns all the issues found in the source branch that didn't exist in the target branch.
func getNewlyAddedIssues(targetResults, sourceResults *results.SecurityCommandResults, allowedLicenses []string, hasViolationContext bool) (*utils.IssuesCollection, error) {
var err error
convertor := conversion.NewCommandResultsConvertor(conversion.ResultConvertParams{IncludeVulnerabilities: true, HasViolationContext: hasViolationContext, IncludeLicenses: len(allowedLicenses) > 0, AllowedLicenses: allowedLicenses, SimplifiedOutput: true})
simpleJsonSource, err := convertor.ConvertToSimpleJson(sourceResults)
if err != nil {
return nil, err
}
simpleJsonTarget, err := convertor.ConvertToSimpleJson(targetResults)
if err != nil {
return nil, err
}
var newVulnerabilitiesOrViolations []formats.VulnerabilityOrViolationRow
if len(simpleJsonSource.Vulnerabilities) > 0 || len(simpleJsonSource.SecurityViolations) > 0 {
newVulnerabilitiesOrViolations = append(
getUniqueVulnerabilityOrViolationRows(simpleJsonTarget.Vulnerabilities, simpleJsonSource.Vulnerabilities),
getUniqueVulnerabilityOrViolationRows(simpleJsonTarget.SecurityViolations, simpleJsonSource.SecurityViolations)...,
)
}
var newLicenses []formats.LicenseRow
if len(simpleJsonSource.LicensesViolations) > 0 {
newLicenses = getUniqueLicenseRows(simpleJsonTarget.LicensesViolations, simpleJsonSource.LicensesViolations)
}
var newIacs []formats.SourceCodeRow
if len(simpleJsonSource.Iacs) > 0 {
newIacs = createNewSourceCodeRows(simpleJsonTarget.Iacs, simpleJsonSource.Iacs)
}
var newSecrets []formats.SourceCodeRow
if len(simpleJsonSource.Secrets) > 0 {
newSecrets = createNewSourceCodeRows(simpleJsonTarget.Secrets, simpleJsonSource.Secrets)
}
var newSast []formats.SourceCodeRow
if len(simpleJsonSource.Sast) > 0 {
newSast = createNewSourceCodeRows(simpleJsonTarget.Sast, simpleJsonSource.Sast)
}
return &utils.IssuesCollection{
Vulnerabilities: newVulnerabilitiesOrViolations,
Iacs: newIacs,
Secrets: newSecrets,
Sast: newSast,
Licenses: newLicenses,
}, nil
}
func createNewSourceCodeRows(targetResults, sourceResults []formats.SourceCodeRow) []formats.SourceCodeRow {
targetSourceCodeVulnerabilitiesKeys := datastructures.MakeSet[string]()
for _, row := range targetResults {
if row.Fingerprint != "" {
targetSourceCodeVulnerabilitiesKeys.Add(row.Fingerprint)
} else {
targetSourceCodeVulnerabilitiesKeys.Add(row.File + row.Snippet)
}
}
var addedSourceCodeVulnerabilities []formats.SourceCodeRow
for _, row := range sourceResults {
if !targetSourceCodeVulnerabilitiesKeys.Exists(row.File+row.Snippet) && !targetSourceCodeVulnerabilitiesKeys.Exists(row.Fingerprint) {
addedSourceCodeVulnerabilities = append(addedSourceCodeVulnerabilities, row)
}
}
return addedSourceCodeVulnerabilities
}
func getUniqueVulnerabilityOrViolationRows(targetRows, sourceRows []formats.VulnerabilityOrViolationRow) []formats.VulnerabilityOrViolationRow {
existingRows := make(map[string]formats.VulnerabilityOrViolationRow)
var newRows []formats.VulnerabilityOrViolationRow
for _, row := range targetRows {
existingRows[utils.GetVulnerabiltiesUniqueID(row)] = row
}
for _, row := range sourceRows {
if _, exists := existingRows[utils.GetVulnerabiltiesUniqueID(row)]; !exists {
newRows = append(newRows, row)
}
}
return newRows
}
func getUniqueLicenseRows(targetRows, sourceRows []formats.LicenseRow) []formats.LicenseRow {
existingLicenses := make(map[string]formats.LicenseRow)
var newLicenses []formats.LicenseRow
for _, row := range targetRows {
existingLicenses[getUniqueLicenseKey(row)] = row
}
for _, row := range sourceRows {
if _, exists := existingLicenses[getUniqueLicenseKey(row)]; !exists {
newLicenses = append(newLicenses, row)
}
}
return newLicenses
}
func getUniqueLicenseKey(license formats.LicenseRow) string {
return license.LicenseKey + license.ImpactedDependencyName + license.ImpactedDependencyType
}
func aggregateScanResults(scanResults []services.ScanResponse) services.ScanResponse {
aggregateResults := services.ScanResponse{
Violations: []services.Violation{},
Vulnerabilities: []services.Vulnerability{},
Licenses: []services.License{},
}
for _, scanResult := range scanResults {
aggregateResults.Violations = append(aggregateResults.Violations, scanResult.Violations...)
aggregateResults.Vulnerabilities = append(aggregateResults.Vulnerabilities, scanResult.Vulnerabilities...)
aggregateResults.Licenses = append(aggregateResults.Licenses, scanResult.Licenses...)
}
return aggregateResults
}