-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcreate-scan-from-github.mts
More file actions
804 lines (724 loc) · 20.3 KB
/
create-scan-from-github.mts
File metadata and controls
804 lines (724 loc) · 20.3 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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
import {
createWriteStream,
existsSync,
promises as fs,
mkdirSync,
mkdtempSync,
} from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { pipeline } from 'node:stream/promises'
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
import { logger } from '@socketsecurity/registry/lib/logger'
import { confirm, select } from '@socketsecurity/registry/lib/prompts'
import { fetchSupportedScanFileNames } from './fetch-supported-scan-file-names.mts'
import { handleCreateNewScan } from './handle-create-new-scan.mts'
import constants from '../../constants.mts'
import { debugApiRequest, debugApiResponse } from '../../utils/debug.mts'
import { formatErrorWithDetail } from '../../utils/errors.mts'
import { isReportSupportedFile } from '../../utils/glob.mts'
import { fetchListAllRepos } from '../repository/fetch-list-all-repos.mts'
import type { CResult, OutputKind } from '../../types.mts'
export async function createScanFromGithub({
all,
githubApiUrl,
githubToken,
interactive,
orgGithub,
orgSlug,
outputKind,
repos,
}: {
all: boolean
githubApiUrl: string
githubToken: string
interactive: boolean
orgSlug: string
orgGithub: string
outputKind: OutputKind
repos: string
}): Promise<CResult<undefined>> {
let targetRepos: string[] = repos
.trim()
.split(',')
.map(r => r.trim())
.filter(Boolean)
if (all || !targetRepos.length) {
// Fetch from Socket API
const result = await fetchListAllRepos(orgSlug, {
direction: 'asc',
sort: 'name',
})
if (!result.ok) {
return result
}
targetRepos = result.data.results.map(obj => obj.slug || '')
}
targetRepos = targetRepos.map(s => s.trim()).filter(Boolean)
logger.info(`Have ${targetRepos.length} repo names to Scan!`)
logger.log('')
if (!targetRepos.filter(Boolean).length) {
return {
ok: false,
message: 'No repo found',
cause:
'You did not set the --repos value and/or the server responded with zero repos when asked for some. Unable to proceed.',
}
}
// Non-interactive or explicitly requested; just do it.
if (interactive && targetRepos.length > 1 && !all && !repos) {
const which = await selectFocus(targetRepos)
if (!which.ok) {
return which
}
targetRepos = which.data
}
// 10 is an arbitrary number. Maybe confirm whenever count>1 ?
// Do not ask to confirm when the list was given explicit.
if (interactive && (all || !repos) && targetRepos.length > 10) {
const sure = await makeSure(targetRepos.length)
if (!sure.ok) {
return sure
}
}
let scansCreated = 0
for (const repoSlug of targetRepos) {
// eslint-disable-next-line no-await-in-loop
const scanCResult = await scanRepo(repoSlug, {
githubApiUrl,
githubToken,
orgSlug,
orgGithub,
outputKind,
repos,
})
if (scanCResult.ok) {
const { scanCreated } = scanCResult.data
if (scanCreated) {
scansCreated += 1
}
}
}
logger.success(targetRepos.length, 'GitHub repos detected')
logger.success(scansCreated, 'with supported Manifest files')
return {
ok: true,
data: undefined,
}
}
async function scanRepo(
repoSlug: string,
{
githubApiUrl,
githubToken,
orgGithub,
orgSlug,
outputKind,
repos,
}: {
githubApiUrl: string
githubToken: string
orgSlug: string
orgGithub: string
outputKind: OutputKind
repos: string
},
): Promise<CResult<{ scanCreated: boolean }>> {
logger.info(
`Requesting repo details from GitHub API for: \`${orgGithub}/${repoSlug}\`...`,
)
logger.group()
const result = await scanOneRepo(repoSlug, {
githubApiUrl,
githubToken,
orgSlug,
orgGithub,
outputKind,
repos,
})
logger.groupEnd()
logger.log('')
return result
}
async function scanOneRepo(
repoSlug: string,
{
githubApiUrl,
githubToken,
orgGithub,
orgSlug,
outputKind,
}: {
githubApiUrl: string
githubToken: string
orgSlug: string
orgGithub: string
outputKind: OutputKind
repos: string
},
): Promise<CResult<{ scanCreated: boolean }>> {
const repoResult = await getRepoDetails({
orgGithub,
repoSlug,
githubApiUrl,
githubToken,
})
if (!repoResult.ok) {
return repoResult
}
const { defaultBranch, repoApiUrl } = repoResult.data
logger.info(`Default branch: \`${defaultBranch}\``)
const treeResult = await getRepoBranchTree({
defaultBranch,
githubToken,
orgGithub,
repoSlug,
repoApiUrl,
})
if (!treeResult.ok) {
return treeResult
}
const files = treeResult.data
if (!files.length) {
logger.warn(
'No files were reported for the default branch. Moving on to next repo.',
)
return { ok: true, data: { scanCreated: false } }
}
const tmpDir = mkdtempSync(path.join(os.tmpdir(), repoSlug))
debugFn('notice', 'init: temp dir for scan root', tmpDir)
const downloadResult = await testAndDownloadManifestFiles({
files,
tmpDir,
repoSlug,
defaultBranch,
orgGithub,
repoApiUrl,
githubToken,
})
if (!downloadResult.ok) {
return downloadResult
}
const commitResult = await getLastCommitDetails({
orgGithub,
repoSlug,
defaultBranch,
repoApiUrl,
githubToken,
})
if (!commitResult.ok) {
return commitResult
}
const { lastCommitMessage, lastCommitSha, lastCommitter } = commitResult.data
// Make request for full scan
// I think we can just kick off the socket scan create command now...
await handleCreateNewScan({
autoManifest: false,
branchName: defaultBranch,
commitHash: lastCommitSha,
commitMessage: lastCommitMessage || '',
committers: lastCommitter || '',
cwd: tmpDir,
defaultBranch: true,
interactive: false,
orgSlug,
outputKind,
pendingHead: true,
pullRequest: 0,
reach: {
reachAnalysisMemoryLimit: 0,
reachAnalysisTimeout: 0,
reachConcurrency: 1,
reachDebug: false,
reachDisableAnalysisSplitting: false,
reachDisableAnalytics: false,
reachEcosystems: [],
reachExcludePaths: [],
reachLazyMode: false,
reachSkipCache: false,
reachUseOnlyPregeneratedSboms: false,
reachVersion: undefined,
runReachabilityAnalysis: false,
},
readOnly: false,
repoName: repoSlug,
report: false,
reportLevel: constants.REPORT_LEVEL_ERROR,
targets: ['.'],
tmp: false,
})
return { ok: true, data: { scanCreated: true } }
}
async function testAndDownloadManifestFiles({
defaultBranch,
files,
githubToken,
orgGithub,
repoApiUrl,
repoSlug,
tmpDir,
}: {
files: string[]
tmpDir: string
repoSlug: string
defaultBranch: string
orgGithub: string
repoApiUrl: string
githubToken: string
}): Promise<CResult<unknown>> {
logger.info(
`File tree for ${defaultBranch} contains`,
files.length,
`entries. Searching for supported manifest files...`,
)
logger.group()
let fileCount = 0
let firstFailureResult
for (const file of files) {
// eslint-disable-next-line no-await-in-loop
const result = await testAndDownloadManifestFile({
file,
tmpDir,
defaultBranch,
repoApiUrl,
githubToken,
})
if (result.ok) {
if (result.data.isManifest) {
fileCount += 1
}
} else if (!firstFailureResult) {
firstFailureResult = result
}
}
logger.groupEnd()
logger.info('Found and downloaded', fileCount, 'manifest files')
if (!fileCount) {
if (firstFailureResult) {
logger.fail(
'While no supported manifest files were downloaded, at least one error encountered trying to do so. Showing the first error.',
)
return firstFailureResult
}
return {
ok: false,
message: 'No manifest files found',
cause: `No supported manifest files were found in the latest commit on the branch ${defaultBranch} for repo ${orgGithub}/${repoSlug}. Skipping full scan.`,
}
}
return { ok: true, data: undefined }
}
async function testAndDownloadManifestFile({
defaultBranch,
file,
githubToken,
repoApiUrl,
tmpDir,
}: {
file: string
tmpDir: string
defaultBranch: string
repoApiUrl: string
githubToken: string
}): Promise<CResult<{ isManifest: boolean }>> {
debugFn('notice', 'testing: file', file)
const supportedFilesCResult = await fetchSupportedScanFileNames()
const supportedFiles = supportedFilesCResult.ok
? supportedFilesCResult.data
: undefined
if (!supportedFiles || !isReportSupportedFile(file, supportedFiles)) {
debugFn('notice', 'skip: not a known pattern')
// Not an error.
return { ok: true, data: { isManifest: false } }
}
debugFn(
'notice',
'found: manifest file, going to attempt to download it;',
file,
)
const result = await downloadManifestFile({
file,
tmpDir,
defaultBranch,
repoApiUrl,
githubToken,
})
return result.ok ? { ok: true, data: { isManifest: true } } : result
}
async function downloadManifestFile({
defaultBranch,
file,
githubToken,
repoApiUrl,
tmpDir,
}: {
file: string
tmpDir: string
defaultBranch: string
repoApiUrl: string
githubToken: string
}): Promise<CResult<undefined>> {
debugFn('notice', 'request: download url from GitHub')
const fileUrl = `${repoApiUrl}/contents/${file}?ref=${defaultBranch}`
debugDir('inspect', { fileUrl })
debugApiRequest('GET', fileUrl)
let downloadUrlResponse: Response
try {
downloadUrlResponse = await fetch(fileUrl, {
method: 'GET',
headers: {
Authorization: `Bearer ${githubToken}`,
},
})
debugApiResponse('GET', fileUrl, downloadUrlResponse.status)
} catch (e) {
debugApiResponse('GET', fileUrl, undefined, e)
throw e
}
debugFn('notice', 'complete: request')
const downloadUrlText = await downloadUrlResponse.text()
debugFn('inspect', 'response: raw download url', downloadUrlText)
let downloadUrl
try {
downloadUrl = JSON.parse(downloadUrlText).download_url
} catch {
logger.fail(
`GitHub response contained invalid JSON for download url for: ${file}`,
)
return {
ok: false,
message: 'Invalid JSON response',
cause: `Server responded with invalid JSON for download url ${downloadUrl}`,
}
}
const localPath = path.join(tmpDir, file)
debugFn(
'notice',
'download: manifest file started',
downloadUrl,
'->',
localPath,
)
// Now stream the file to that file...
const result = await streamDownloadWithFetch(localPath, downloadUrl)
if (!result.ok) {
// Do we proceed? Bail? Hrm...
logger.fail(
`Failed to download manifest file, skipping to next file. File: ${file}`,
)
return result
}
debugFn('notice', 'download: manifest file completed')
return { ok: true, data: undefined }
}
// Courtesy of gemini:
async function streamDownloadWithFetch(
localPath: string,
downloadUrl: string,
): Promise<CResult<string>> {
let response // Declare response here to access it in catch if needed
try {
debugApiRequest('GET', downloadUrl)
response = await fetch(downloadUrl)
debugApiResponse('GET', downloadUrl, response.status)
if (!response.ok) {
const errorMsg = `Download failed due to bad server response: ${response.status} ${response.statusText} for ${downloadUrl}`
logger.fail(errorMsg)
return { ok: false, message: 'Download Failed', cause: errorMsg }
}
if (!response.body) {
logger.fail(
`Download failed because the server response was empty, for ${downloadUrl}`,
)
return {
ok: false,
message: 'Download Failed',
cause: 'Response body is null or undefined.',
}
}
// Make sure the dir exists. It may be nested and we need to construct that
// before starting the download.
const dir = path.dirname(localPath)
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true })
}
const fileStream = createWriteStream(localPath)
// Using stream.pipeline for better error handling and cleanup
await pipeline(response.body, fileStream)
// 'pipeline' will automatically handle closing streams and propagating errors.
// It resolves when the piping is fully complete and fileStream is closed.
return { ok: true, data: localPath }
} catch (e) {
if (!response) {
debugApiResponse('GET', downloadUrl, undefined, e)
}
logger.fail(
'An error was thrown while trying to download a manifest file... url:',
downloadUrl,
)
debugDir('error', e)
// If an error occurs and fileStream was created, attempt to clean up.
if (existsSync(localPath)) {
// Check if fileStream was even opened before trying to delete
// This check might be too simplistic depending on when error occurs
try {
await fs.unlink(localPath)
} catch (e) {
logger.fail(
formatErrorWithDetail(`Error deleting partial file ${localPath}`, e),
)
}
}
// Construct a more informative error message
let detailedError = `Error during download of ${downloadUrl}: ${(e as { message: string }).message}`
if ((e as { cause: string }).cause) {
// Include cause if available (e.g., from network errors)
detailedError += `\nCause: ${(e as { cause: string }).cause}`
}
if (response && !response.ok) {
// If error was due to bad HTTP status
detailedError += ` (HTTP Status: ${response.status} ${response.statusText})`
}
debugFn('error', detailedError)
return { ok: false, message: 'Download Failed', cause: detailedError }
}
}
async function getLastCommitDetails({
defaultBranch,
githubToken,
orgGithub,
repoApiUrl,
repoSlug,
}: {
orgGithub: string
repoSlug: string
defaultBranch: string
repoApiUrl: string
githubToken: string
}): Promise<
CResult<{
lastCommitSha: string
lastCommitter: string | undefined
lastCommitMessage: string
}>
> {
logger.info(
`Requesting last commit for default branch ${defaultBranch} for ${orgGithub}/${repoSlug}...`,
)
const commitApiUrl = `${repoApiUrl}/commits?sha=${defaultBranch}&per_page=1`
debugFn('inspect', 'url: commit', commitApiUrl)
debugApiRequest('GET', commitApiUrl)
let commitResponse: Response
try {
commitResponse = await fetch(commitApiUrl, {
headers: {
Authorization: `Bearer ${githubToken}`,
},
})
debugApiResponse('GET', commitApiUrl, commitResponse.status)
} catch (e) {
debugApiResponse('GET', commitApiUrl, undefined, e)
throw e
}
const commitText = await commitResponse.text()
debugFn('inspect', 'response: commit', commitText)
let lastCommit
try {
lastCommit = JSON.parse(commitText)?.[0]
} catch {
logger.fail(`GitHub response contained invalid JSON for last commit`)
logger.error(commitText)
return {
ok: false,
message: 'Invalid JSON response',
cause: `Server responded with invalid JSON for last commit of repo ${repoSlug}`,
}
}
const lastCommitSha = lastCommit.sha
const lastCommitter = Array.from(
new Set([lastCommit.commit.author.name, lastCommit.commit.committer.name]),
)[0]
const lastCommitMessage = lastCommit.message
if (!lastCommitSha) {
return {
ok: false,
message: 'Missing commit SHA',
cause: 'Unable to get last commit for repo',
}
}
if (!lastCommitter) {
return {
ok: false,
message: 'Missing committer',
cause: 'Last commit does not have information about who made the commit',
}
}
return { ok: true, data: { lastCommitSha, lastCommitter, lastCommitMessage } }
}
async function selectFocus(repos: string[]): Promise<CResult<string[]>> {
const proceed = await select<string>({
message: 'Please select the repo to process:',
choices: repos
.map(slug => ({
name: slug,
value: slug,
description: `Create scan for the ${slug} repo through GitHub`,
}))
.concat({
name: '(Exit)',
value: '',
description: 'Cancel this action and exit',
}),
})
if (!proceed) {
return {
ok: false,
message: 'Canceled by user',
cause: 'User chose to cancel the action',
}
}
return { ok: true, data: [proceed] }
}
async function makeSure(count: number): Promise<CResult<undefined>> {
if (
!(await confirm({
message: `Are you sure you want to run this for ${count} repos?`,
default: false,
}))
) {
return {
ok: false,
message: 'User canceled',
cause: 'Action canceled by user',
}
}
return { ok: true, data: undefined }
}
async function getRepoDetails({
githubApiUrl,
githubToken,
orgGithub,
repoSlug,
}: {
orgGithub: string
repoSlug: string
githubApiUrl: string
githubToken: string
}): Promise<
CResult<{ defaultBranch: string; repoDetails: unknown; repoApiUrl: string }>
> {
const repoApiUrl = `${githubApiUrl}/repos/${orgGithub}/${repoSlug}`
debugDir('inspect', { repoApiUrl })
let repoDetailsResponse: Response
try {
debugApiRequest('GET', repoApiUrl)
repoDetailsResponse = await fetch(repoApiUrl, {
method: 'GET',
headers: {
Authorization: `Bearer ${githubToken}`,
},
})
debugApiResponse('GET', repoApiUrl, repoDetailsResponse.status)
} catch (e) {
debugApiResponse('GET', repoApiUrl, undefined, e)
throw e
}
logger.success(`Request completed.`)
const repoDetailsText = await repoDetailsResponse.text()
debugFn('inspect', 'response: repo', repoDetailsText)
let repoDetails
try {
repoDetails = JSON.parse(repoDetailsText)
} catch {
logger.fail(`GitHub response contained invalid JSON for repo ${repoSlug}`)
logger.error(repoDetailsText)
return {
ok: false,
message: 'Invalid JSON response',
cause: `Server responded with invalid JSON for repo ${repoSlug}`,
}
}
const defaultBranch = repoDetails.default_branch
if (!defaultBranch) {
return {
ok: false,
message: 'Default Branch Not Found',
cause: `Repo ${repoSlug} does not have a default branch set or it was not reported`,
}
}
return { ok: true, data: { defaultBranch, repoDetails, repoApiUrl } }
}
async function getRepoBranchTree({
defaultBranch,
githubToken,
orgGithub,
repoApiUrl,
repoSlug,
}: {
defaultBranch: string
githubToken: string
orgGithub: string
repoApiUrl: string
repoSlug: string
}): Promise<CResult<string[]>> {
logger.info(
`Requesting default branch file tree; branch \`${defaultBranch}\`, repo \`${orgGithub}/${repoSlug}\`...`,
)
const treeApiUrl = `${repoApiUrl}/git/trees/${defaultBranch}?recursive=1`
debugFn('inspect', 'url: tree', treeApiUrl)
let treeResponse: Response
try {
debugApiRequest('GET', treeApiUrl)
treeResponse = await fetch(treeApiUrl, {
method: 'GET',
headers: {
Authorization: `Bearer ${githubToken}`,
},
})
debugApiResponse('GET', treeApiUrl, treeResponse.status)
} catch (e) {
debugApiResponse('GET', treeApiUrl, undefined, e)
throw e
}
const treeText = await treeResponse.text()
debugFn('inspect', 'response: tree', treeText)
let treeDetails
try {
treeDetails = JSON.parse(treeText)
} catch {
logger.fail(
`GitHub response contained invalid JSON for default branch of repo ${repoSlug}`,
)
logger.error(treeText)
return {
ok: false,
message: 'Invalid JSON response',
cause: `Server responded with invalid JSON for repo ${repoSlug}`,
}
}
if (treeDetails.message) {
if (treeDetails.message === 'Git Repository is empty.') {
logger.warn(
`GitHub reports the default branch of repo ${repoSlug} to be empty. Moving on to next repo.`,
)
return { ok: true, data: [] }
}
logger.fail('Negative response from GitHub:', treeDetails.message)
return {
ok: false,
message: 'Unexpected error response',
cause: `GitHub responded with an unexpected error while asking for details on the default branch: ${treeDetails.message}`,
}
}
if (!treeDetails.tree || !Array.isArray(treeDetails.tree)) {
debugDir('inspect', { treeDetails: { tree: treeDetails.tree } })
return {
ok: false,
message: `Tree response for default branch ${defaultBranch} for ${orgGithub}/${repoSlug} was not a list`,
}
}
const files = (treeDetails.tree as Array<{ type: string; path: string }>)
.filter(obj => obj.type === 'blob')
.map(obj => obj.path)
return { ok: true, data: files }
}