|
1 | | -import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug' |
| 1 | +import { debugDir } from '@socketsecurity/registry/lib/debug' |
2 | 2 | import { logger } from '@socketsecurity/registry/lib/logger' |
3 | | -import { pluralize } from '@socketsecurity/registry/lib/words' |
4 | 3 |
|
5 | 4 | import { npmFix } from './npm-fix.mts' |
6 | 5 | import { outputFixResult } from './output-fix-result.mts' |
7 | 6 | import { pnpmFix } from './pnpm-fix.mts' |
8 | 7 | import { CMD_NAME } from './shared.mts' |
9 | 8 | import constants from '../../constants.mts' |
| 9 | +import { handleApiCall } from '../../utils/api.mts' |
10 | 10 | import { cmdFlagValueToArray } from '../../utils/cmd.mts' |
11 | 11 | import { spawnCoana } from '../../utils/coana.mts' |
12 | 12 | import { detectAndValidatePackageEnvironment } from '../../utils/package-environment.mts' |
| 13 | +import { getPackageFilesForScan } from '../../utils/path-resolve.mts' |
| 14 | +import { setupSdk } from '../../utils/sdk.mts' |
| 15 | +import { fetchSupportedScanFileNames } from '../scan/fetch-supported-scan-file-names.mts' |
13 | 16 |
|
14 | 17 | import type { FixConfig } from './agent-fix.mts' |
15 | | -import type { OutputKind } from '../../types.mts' |
| 18 | +import type { CResult, OutputKind } from '../../types.mts' |
16 | 19 | import type { Remap } from '@socketsecurity/registry/lib/objects' |
17 | 20 |
|
18 | 21 | export type HandleFixConfig = Remap< |
@@ -40,66 +43,105 @@ export async function handleFix({ |
40 | 43 | testScript, |
41 | 44 | unknownFlags, |
42 | 45 | }: HandleFixConfig) { |
43 | | - let { length: ghsasCount } = ghsas |
44 | | - if (ghsasCount) { |
45 | | - spinner?.start('Fetching GHSA IDs...') |
46 | | - |
47 | | - if (ghsasCount === 1 && ghsas[0] === 'auto') { |
48 | | - const ghsasCResult = await spawnCoana( |
49 | | - ['compute-fixes-and-upgrade-purls', cwd], |
50 | | - { cwd, spinner }, |
51 | | - ) |
52 | | - |
53 | | - spinner?.stop() |
54 | | - |
55 | | - if (ghsasCResult.ok) { |
56 | | - const ghsasOutput = ghsasCResult.data as string |
57 | | - ghsas = cmdFlagValueToArray( |
58 | | - /(?<=Vulnerabilities found: )[^\n]+/.exec(ghsasOutput)?.[0], |
59 | | - ) |
60 | | - ghsasCount = ghsas.length |
61 | | - } else { |
62 | | - debugFn('error', 'fail: Coana CLI') |
63 | | - debugDir('inspect', { |
64 | | - message: ghsasCResult.message, |
65 | | - cause: ghsasCResult.cause, |
66 | | - }) |
67 | | - ghsas = [] |
68 | | - ghsasCount = 0 |
69 | | - } |
| 46 | + if (ghsas.length === 1 && ghsas[0] === 'auto') { |
| 47 | + let lastCResult: CResult<any> |
| 48 | + const sockSdkCResult = await setupSdk() |
| 49 | + |
| 50 | + lastCResult = sockSdkCResult |
| 51 | + const sockSdk = sockSdkCResult.ok ? sockSdkCResult.data : undefined |
70 | 52 |
|
71 | | - spinner?.start() |
| 53 | + const supportedFilesCResult = sockSdk |
| 54 | + ? await fetchSupportedScanFileNames() |
| 55 | + : undefined |
| 56 | + |
| 57 | + if (supportedFilesCResult) { |
| 58 | + lastCResult = supportedFilesCResult |
72 | 59 | } |
73 | 60 |
|
74 | | - if (ghsasCount) { |
75 | | - spinner?.info(`Found ${ghsasCount} GHSA ${pluralize('ID', ghsasCount)}.`) |
| 61 | + const supportedFiles = supportedFilesCResult?.ok |
| 62 | + ? supportedFilesCResult.data |
| 63 | + : undefined |
76 | 64 |
|
77 | | - const ghsaFixesCResult = await spawnCoana( |
78 | | - [ |
79 | | - 'compute-fixes-and-upgrade-purls', |
| 65 | + const packagePaths = supportedFiles |
| 66 | + ? await getPackageFilesForScan(['.'], supportedFiles!, { |
80 | 67 | cwd, |
81 | | - '--apply-fixes-to', |
82 | | - ...ghsas, |
83 | | - ...unknownFlags, |
84 | | - ], |
85 | | - { cwd, spinner }, |
86 | | - ) |
87 | | - |
88 | | - spinner?.stop() |
89 | | - |
90 | | - if (!ghsaFixesCResult.ok) { |
91 | | - debugFn('error', 'fail: Coana CLI') |
92 | | - debugDir('inspect', { |
93 | | - message: ghsaFixesCResult.message, |
94 | | - cause: ghsaFixesCResult.cause, |
95 | 68 | }) |
96 | | - } |
| 69 | + : [] |
| 70 | + |
| 71 | + const uploadCResult = sockSdk |
| 72 | + ? await handleApiCall( |
| 73 | + sockSdk?.uploadManifestFiles(orgSlug, packagePaths), |
| 74 | + { |
| 75 | + desc: 'upload manifests', |
| 76 | + }, |
| 77 | + ) |
| 78 | + : undefined |
97 | 79 |
|
98 | | - await outputFixResult(ghsaFixesCResult, outputKind) |
99 | | - return |
| 80 | + if (uploadCResult) { |
| 81 | + lastCResult = uploadCResult |
| 82 | + } |
| 83 | + |
| 84 | + const tarHash = uploadCResult?.ok ? (uploadCResult as any).data.tarHash : '' |
| 85 | + |
| 86 | + const idsOutputCResult = tarHash |
| 87 | + ? await spawnCoana( |
| 88 | + [ |
| 89 | + 'compute-fixes-and-upgrade-purls', |
| 90 | + cwd, |
| 91 | + '--manifests-tar-hash', |
| 92 | + tarHash, |
| 93 | + ], |
| 94 | + { cwd, spinner, env: { SOCKET_ORG_SLUG: orgSlug } }, |
| 95 | + ) |
| 96 | + : undefined |
| 97 | + |
| 98 | + if (idsOutputCResult) { |
| 99 | + lastCResult = idsOutputCResult |
100 | 100 | } |
101 | 101 |
|
102 | | - spinner?.infoAndStop('No GHSA IDs found.') |
| 102 | + const idsOutput = idsOutputCResult?.ok |
| 103 | + ? (idsOutputCResult.data as string) |
| 104 | + : '' |
| 105 | + |
| 106 | + const ids = cmdFlagValueToArray( |
| 107 | + /(?<=Vulnerabilities found: )[^\n]+/.exec(idsOutput)?.[0], |
| 108 | + ) |
| 109 | + |
| 110 | + const fixCResult = ids.length |
| 111 | + ? await spawnCoana( |
| 112 | + [ |
| 113 | + 'compute-fixes-and-upgrade-purls', |
| 114 | + cwd, |
| 115 | + '--manifests-tar-hash', |
| 116 | + tarHash, |
| 117 | + '--apply-fixes-to', |
| 118 | + ...ids, |
| 119 | + ...unknownFlags, |
| 120 | + ], |
| 121 | + { cwd, spinner, env: { SOCKET_ORG_SLUG: orgSlug } }, |
| 122 | + ) |
| 123 | + : undefined |
| 124 | + |
| 125 | + if (fixCResult) { |
| 126 | + lastCResult = fixCResult |
| 127 | + } |
| 128 | + // const fixCResult = await spawnCoana( |
| 129 | + // [ |
| 130 | + // cwd, |
| 131 | + // '--socket-mode', |
| 132 | + // DOT_SOCKET_DOT_FACTS_JSON, |
| 133 | + // '--manifests-tar-hash', |
| 134 | + // tarHash, |
| 135 | + // ...unknownFlags, |
| 136 | + // ], |
| 137 | + // { cwd, spinner, env: { SOCKET_ORG_SLUG: orgSlug } }, |
| 138 | + // ) |
| 139 | + debugDir('inspect', { lastCResult }) |
| 140 | + |
| 141 | + if (!lastCResult.ok) { |
| 142 | + await outputFixResult(lastCResult, outputKind) |
| 143 | + return |
| 144 | + } |
103 | 145 |
|
104 | 146 | await outputFixResult( |
105 | 147 | { |
|
0 commit comments