Skip to content

Commit 32c336b

Browse files
committed
Various fixes for handling of target paths. (#933)
1 parent 2ab2a7e commit 32c336b

File tree

4 files changed

+123
-145
lines changed

4 files changed

+123
-145
lines changed

packages/cli/src/commands/scan/cmd-scan-create.mts

Lines changed: 33 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,44 @@
1-
import { existsSync, promises as fs } from 'node:fs'
21
import path from 'node:path'
32

4-
import { joinAnd } from '@socketsecurity/lib/arrays'
5-
import { getDefaultLogger } from '@socketsecurity/lib/logger'
3+
import { joinAnd } from '@socketsecurity/registry/lib/arrays'
4+
import { logger } from '@socketsecurity/registry/lib/logger'
65

76
import { handleCreateNewScan } from './handle-create-new-scan.mts'
87
import { outputCreateNewScan } from './output-create-new-scan.mts'
98
import { reachabilityFlags } from './reachability-flags.mts'
109
import { suggestOrgSlug } from './suggest-org-slug.mts'
1110
import { suggestTarget } from './suggest_target.mts'
12-
import { DRY_RUN_BAILING_NOW } from '../../constants/cli.mts'
13-
import { REQUIREMENTS_TXT, SOCKET_JSON } from '../../constants/paths.mts'
14-
import { REPORT_LEVEL_ERROR } from '../../constants/reporting.mjs'
11+
import { validateReachabilityTarget } from './validate-reachability-target.mts'
12+
import constants, { REQUIREMENTS_TXT, SOCKET_JSON } from '../../constants.mts'
1513
import { commonFlags, outputFlags } from '../../flags.mts'
16-
import { meowOrExit } from '../../utils/cli/with-subcommands.mjs'
17-
import { getEcosystemChoicesForMeow } from '../../utils/ecosystem/types.mjs'
14+
import { meowOrExit } from '../../utils/cli/with-subcommands.mts'
15+
import { getEcosystemChoicesForMeow } from '../../utils/ecosystem/types.mts'
1816
import {
1917
detectDefaultBranch,
2018
getRepoName,
2119
gitBranch,
22-
} from '../../utils/git/operations.mjs'
20+
} from '../../utils/git/operations.mts'
2321
import {
2422
getFlagApiRequirementsOutput,
2523
getFlagListOutput,
2624
} from '../../utils/output/formatting.mts'
27-
import { getOutputKind } from '../../utils/output/mode.mjs'
25+
import { getOutputKind } from '../../utils/output/mode.mts'
2826
import { cmdFlagValueToArray } from '../../utils/process/cmd.mts'
2927
import { readOrDefaultSocketJsonUp } from '../../utils/socket/json.mts'
30-
import { determineOrgSlug } from '../../utils/socket/org-slug.mjs'
31-
import { hasDefaultApiToken } from '../../utils/socket/sdk.mjs'
28+
import { determineOrgSlug } from '../../utils/socket/org-slug.mts'
29+
import { hasDefaultApiToken } from '../../utils/socket/sdk.mts'
3230
import { socketDashboardLink } from '../../utils/terminal/link.mts'
3331
import { checkCommandInput } from '../../utils/validation/check-input.mts'
3432
import { detectManifestActions } from '../manifest/detect-manifest-actions.mts'
3533

34+
3635
import type { REPORT_LEVEL } from './types.mts'
3736
import type { MeowFlags } from '../../flags.mts'
3837
import type {
3938
CliCommandConfig,
4039
CliCommandContext,
41-
} from '../../utils/cli/with-subcommands.mjs'
42-
import type { PURL_Type } from '../../utils/ecosystem/types.mjs'
43-
const logger = getDefaultLogger()
40+
} from '../../utils/cli/with-subcommands.mts'
41+
import type { PURL_Type } from '../../utils/ecosystem/types.mts'
4442

4543
export const CMD_NAME = 'create'
4644

@@ -132,8 +130,8 @@ const generalFlags: MeowFlags = {
132130
},
133131
reportLevel: {
134132
type: 'string',
135-
default: REPORT_LEVEL_ERROR,
136-
description: `Which policy level alerts should be reported (default '${REPORT_LEVEL_ERROR}')`,
133+
default: constants.REPORT_LEVEL_ERROR,
134+
description: `Which policy level alerts should be reported (default '${constants.REPORT_LEVEL_ERROR}')`,
137135
},
138136
setAsAlertsPage: {
139137
type: 'boolean',
@@ -170,7 +168,7 @@ async function run(
170168
...generalFlags,
171169
...reachabilityFlags,
172170
},
173-
// Note: Could document socket.yml's "projectIgnorePaths" setting in help text.
171+
// TODO: Your project's "socket.yml" file's "projectIgnorePaths".
174172
help: command => `
175173
Usage
176174
$ ${command} [options] [TARGET...]
@@ -270,8 +268,8 @@ async function run(
270268
tmp: boolean
271269
// Reachability flags.
272270
reach: boolean
273-
reachAnalysisMemoryLimit: number
274271
reachAnalysisTimeout: number
272+
reachAnalysisMemoryLimit: number
275273
reachConcurrency: number
276274
reachDebug: boolean
277275
reachDisableAnalytics: boolean
@@ -294,17 +292,6 @@ async function run(
294292
reachEcosystems.push(ecosystem as PURL_Type)
295293
}
296294

297-
// Validate severity value if provided.
298-
const validSeverities = ['info', 'low', 'moderate', 'high', 'critical']
299-
if (
300-
reachMinSeverity &&
301-
!validSeverities.includes(reachMinSeverity.toLowerCase())
302-
) {
303-
throw new Error(
304-
`Invalid severity: "${reachMinSeverity}". Valid values are: ${joinAnd(validSeverities)}`,
305-
)
306-
}
307-
308295
const dryRun = !!cli.flags['dryRun']
309296

310297
let {
@@ -375,7 +362,7 @@ async function run(
375362
let updatedInput = false
376363

377364
// Accept zero or more paths. Default to cwd() if none given.
378-
let targets: string[] = cli.input ? [...cli.input] : [cwd]
365+
let targets = cli.input.length ? [...cli.input] : [cwd]
379366

380367
if (!targets.length && !dryRun && interactive) {
381368
targets = await suggestTarget()
@@ -431,7 +418,7 @@ async function run(
431418
)
432419
logger.error('```')
433420
logger.error(
434-
` socket scan create [other flags] ${orgSlug} ${targets.join(' ')}`,
421+
` socket scan create [other flags...] ${orgSlug} ${targets.join(' ')}`,
435422
)
436423
logger.error('```')
437424
logger.error('')
@@ -473,30 +460,14 @@ async function run(
473460
reachDisableAnalysisSplitting
474461

475462
// Validate target constraints when --reach is enabled.
476-
let reachTargetValid = true
477-
let reachTargetIsDirectory = false
478-
let reachTargetExists = false
479-
let reachTargetInsideCwd = false
480-
481-
if (reach) {
482-
// Resolve target path to absolute for validation.
483-
const targetPath = path.isAbsolute(targets[0]!)
484-
? targets[0]!
485-
: path.resolve(cwd, targets[0]!)
486-
487-
// Check if target is inside cwd.
488-
const relativePath = path.relative(cwd, targetPath)
489-
reachTargetInsideCwd =
490-
!relativePath.startsWith('..') && !path.isAbsolute(relativePath)
491-
492-
reachTargetExists = existsSync(targetPath)
493-
if (reachTargetExists) {
494-
const targetStat = await fs.stat(targetPath)
495-
reachTargetIsDirectory = targetStat.isDirectory()
496-
}
497-
498-
reachTargetValid = targets.length === 1
499-
}
463+
const reachTargetValidation = reach
464+
? await validateReachabilityTarget(targets, cwd)
465+
: {
466+
isDirectory: false,
467+
isInsideCwd: false,
468+
isValid: true,
469+
targetExists: false,
470+
}
500471

501472
const wasValidInput = checkCommandInput(
502473
outputKind,
@@ -543,27 +514,27 @@ async function run(
543514
},
544515
{
545516
nook: true,
546-
test: !reach || reachTargetValid,
517+
test: !reach || reachTargetValidation.isValid,
547518
message:
548519
'Reachability analysis requires exactly one target directory when --reach is enabled',
549520
fail: 'provide exactly one directory path',
550521
},
551522
{
552523
nook: true,
553-
test: !reach || reachTargetIsDirectory,
524+
test: !reach || reachTargetValidation.isDirectory,
554525
message:
555526
'Reachability analysis target must be a directory when --reach is enabled',
556527
fail: 'provide a directory path, not a file',
557528
},
558529
{
559530
nook: true,
560-
test: !reach || reachTargetExists,
531+
test: !reach || reachTargetValidation.targetExists,
561532
message: 'Target directory must exist when --reach is enabled',
562533
fail: 'provide an existing directory path',
563534
},
564535
{
565536
nook: true,
566-
test: !reach || reachTargetInsideCwd,
537+
test: !reach || reachTargetValidation.isInsideCwd,
567538
message:
568539
'Target directory must be inside the current working directory when --reach is enabled',
569540
fail: 'provide a path inside the working directory',
@@ -574,7 +545,7 @@ async function run(
574545
}
575546

576547
if (dryRun) {
577-
logger.log(DRY_RUN_BAILING_NOW)
548+
logger.log(constants.DRY_RUN_BAILING_NOW)
578549
return
579550
}
580551

@@ -601,7 +572,7 @@ async function run(
601572
reachDisableAnalysisSplitting: Boolean(reachDisableAnalysisSplitting),
602573
reachEcosystems,
603574
reachExcludePaths,
604-
reachMinSeverity,
575+
reachMinSeverity: String(reachMinSeverity),
605576
reachSkipCache: Boolean(reachSkipCache),
606577
reachUseUnreachableFromPrecomputation: Boolean(
607578
reachUseUnreachableFromPrecomputation,

0 commit comments

Comments
 (0)