-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcmd-scan-reach.mts
More file actions
290 lines (257 loc) · 8.11 KB
/
cmd-scan-reach.mts
File metadata and controls
290 lines (257 loc) · 8.11 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
import { existsSync, promises as fs } from 'node:fs'
import path from 'node:path'
import { joinAnd } from '@socketsecurity/lib/arrays'
import { getDefaultLogger } from '@socketsecurity/lib/logger'
import { handleScanReach } from './handle-scan-reach.mts'
import { reachabilityFlags } from './reachability-flags.mts'
import { suggestTarget } from './suggest_target.mts'
import { DRY_RUN_BAILING_NOW } from '../../constants/cli.mts'
import { commonFlags, outputFlags } from '../../flags.mts'
import { meowOrExit } from '../../utils/cli/with-subcommands.mjs'
import { getEcosystemChoicesForMeow } from '../../utils/ecosystem/types.mjs'
import {
getFlagApiRequirementsOutput,
getFlagListOutput,
} from '../../utils/output/formatting.mts'
import { getOutputKind } from '../../utils/output/mode.mjs'
import { cmdFlagValueToArray } from '../../utils/process/cmd.mts'
import { determineOrgSlug } from '../../utils/socket/org-slug.mjs'
import { hasDefaultApiToken } from '../../utils/socket/sdk.mjs'
import { checkCommandInput } from '../../utils/validation/check-input.mts'
import type { MeowFlags } from '../../flags.mts'
import type {
CliCommandConfig,
CliCommandContext,
} from '../../utils/cli/with-subcommands.mjs'
import type { PURL_Type } from '../../utils/ecosystem/types.mjs'
export const CMD_NAME = 'reach'
const description = 'Compute tier 1 reachability'
const hidden = true
const generalFlags: MeowFlags = {
...commonFlags,
...outputFlags,
cwd: {
type: 'string',
default: '',
description: 'working directory, defaults to process.cwd()',
},
org: {
type: 'string',
default: '',
description:
'Force override the organization slug, overrides the default org from config',
},
output: {
type: 'string',
default: '',
description:
'Path to write the reachability report to (must end with .json). Defaults to .socket.facts.json in the current working directory.',
shortFlag: 'o',
},
}
export const cmdScanReach = {
description,
hidden,
run,
}
async function run(
argv: string[] | readonly string[],
importMeta: ImportMeta,
{ parentName }: CliCommandContext,
): Promise<void> {
const config: CliCommandConfig = {
commandName: CMD_NAME,
description,
hidden,
flags: {
...generalFlags,
...reachabilityFlags,
},
help: command =>
`
Usage
$ ${command} [options] [CWD=.]
API Token Requirements
${getFlagApiRequirementsOutput(`${parentName}:${CMD_NAME}`)}
Options
${getFlagListOutput(generalFlags)}
Reachability Options
${getFlagListOutput(reachabilityFlags)}
Runs the Socket reachability analysis without creating a scan in Socket.
The output is written to .socket.facts.json in the current working directory
unless the --output flag is specified.
Note: Manifest files are uploaded to Socket's backend services because the
reachability analysis requires creating a Software Bill of Materials (SBOM)
from these files before the analysis can run.
Examples
$ ${command}
$ ${command} ./proj
$ ${command} ./proj --reach-ecosystems npm,pypi
$ ${command} --output custom-report.json
$ ${command} ./proj --output ./reports/analysis.json
`,
}
const cli = meowOrExit({
argv,
config,
importMeta,
parentName,
})
const {
cwd: cwdOverride,
interactive = true,
json,
markdown,
org: orgFlag,
output: outputPath,
reachAnalysisMemoryLimit,
reachAnalysisTimeout,
reachDisableAnalytics,
reachMinSeverity,
reachSkipCache,
reachUseUnreachableFromPrecomputation,
} = cli.flags as unknown as {
cwd: string
interactive: boolean
json: boolean
markdown: boolean
org: string
output: string
reachAnalysisTimeout: number
reachAnalysisMemoryLimit: number
reachDisableAnalytics: boolean
reachMinSeverity: string
reachSkipCache: boolean
reachUseUnreachableFromPrecomputation: boolean
}
const dryRun = !!cli.flags['dryRun']
// Process comma-separated values for isMultiple flags.
const reachEcosystemsRaw = cmdFlagValueToArray(cli.flags['reachEcosystems'])
const reachExcludePaths = cmdFlagValueToArray(cli.flags['reachExcludePaths'])
// Validate ecosystem values.
const reachEcosystems: PURL_Type[] = []
const validEcosystems = getEcosystemChoicesForMeow()
for (const ecosystem of reachEcosystemsRaw) {
if (!validEcosystems.includes(ecosystem)) {
throw new Error(
`Invalid ecosystem: "${ecosystem}". Valid values are: ${joinAnd(validEcosystems)}`,
)
}
reachEcosystems.push(ecosystem as PURL_Type)
}
// Validate severity value if provided.
const validSeverities = ['info', 'low', 'moderate', 'high', 'critical']
if (
reachMinSeverity &&
!validSeverities.includes(reachMinSeverity.toLowerCase())
) {
throw new Error(
`Invalid severity: "${reachMinSeverity}". Valid values are: ${joinAnd(validSeverities)}`,
)
}
const processCwd = process.cwd()
const cwd =
cwdOverride && cwdOverride !== '.' && cwdOverride !== processCwd
? path.resolve(processCwd, cwdOverride)
: processCwd
// Accept zero or more paths. Default to cwd() if none given.
let targets: string[] = cli.input.length ? [...cli.input] : [cwd]
// Use suggestTarget if no targets specified and in interactive mode
if (!targets.length && !dryRun && interactive) {
targets = await suggestTarget()
}
const { 0: orgSlug } = await determineOrgSlug(orgFlag, interactive, dryRun)
const hasApiToken = hasDefaultApiToken()
const outputKind = getOutputKind(json, markdown)
// Resolve target path to absolute for validation.
const targetPath = path.isAbsolute(targets[0]!)
? targets[0]!
: path.resolve(cwd, targets[0]!)
// Check if target is inside cwd.
const relativePath = path.relative(cwd, targetPath)
const isInsideCwd =
!relativePath.startsWith('..') && !path.isAbsolute(relativePath)
let isDirectory = false
if (existsSync(targetPath)) {
const targetStat = await fs.stat(targetPath)
isDirectory = targetStat.isDirectory()
}
const wasValidInput = checkCommandInput(
outputKind,
{
nook: true,
test: !!orgSlug,
message: 'Org name by default setting, --org, or auto-discovered',
fail: 'missing',
},
{
nook: true,
test: hasApiToken,
message: 'This command requires an API token for access',
fail: 'try `socket login`',
},
{
nook: true,
test: !json || !markdown,
message: 'The json and markdown flags cannot be both set, pick one',
fail: 'omit one',
},
{
nook: true,
test: !outputPath || outputPath.endsWith('.json'),
message: 'The --output path must end with .json',
fail: 'use a path ending with .json',
},
{
nook: true,
test: targets.length === 1,
message: 'Reachability analysis requires exactly one target directory',
fail: 'provide exactly one directory path',
},
{
nook: true,
test: isDirectory,
message: 'Reachability analysis target must be a directory',
fail: 'provide a directory path, not a file',
},
{
nook: true,
test: existsSync(targetPath),
message: 'Target directory must exist',
fail: 'provide an existing directory path',
},
{
nook: true,
test: isInsideCwd,
message: 'Target directory must be inside the current working directory',
fail: 'provide a path inside the working directory',
},
)
if (!wasValidInput) {
return
}
if (dryRun) {
getDefaultLogger().log(DRY_RUN_BAILING_NOW)
return
}
await handleScanReach({
cwd,
interactive,
orgSlug,
outputKind,
outputPath,
reachabilityOptions: {
reachAnalysisTimeout: Number(reachAnalysisTimeout),
reachAnalysisMemoryLimit: Number(reachAnalysisMemoryLimit),
reachDisableAnalytics: Boolean(reachDisableAnalytics),
reachEcosystems,
reachExcludePaths,
reachMinSeverity,
reachSkipCache: Boolean(reachSkipCache),
reachUseUnreachableFromPrecomputation: Boolean(
reachUseUnreachableFromPrecomputation,
),
},
targets,
})
}