Skip to content

Commit 0cc03a9

Browse files
committed
feat(fix): add --all flag to process all vulnerabilities
Ported from v1.x commit da83fa1 (#967) - Add --all flag to process all discovered vulnerabilities in local mode - Make --all incompatible with --id (mutually exclusive) - Add deprecation warning when neither --all nor --id provided in local mode - Update shouldDiscoverGhsaIds logic to check all || !ghsas.length - Pass all flag through cmd-fix -> handle-fix -> coana-fix - Update test fixtures to include all parameter Based on PR #967
1 parent d389f09 commit 0cc03a9

File tree

5 files changed

+40
-14
lines changed

5 files changed

+40
-14
lines changed

packages/cli/src/commands/fix/cmd-fix.mts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ const generalFlags: MeowFlags = {
7272
default: true,
7373
description:
7474
'Allow major version updates. Use --no-major-updates to disable.',
75-
// Hidden to allow custom documenting of the negated `--no-major-updates` variant.
75+
// Hidden to allow custom documenting the negated `--no-major-updates` variant.
7676
hidden: true,
7777
},
78+
all: {
79+
type: 'boolean',
80+
default: false,
81+
description:
82+
'Process all discovered vulnerabilities in local mode. Cannot be used with --id.',
83+
},
7884
id: {
7985
type: 'string',
8086
default: [],
@@ -91,7 +97,7 @@ const generalFlags: MeowFlags = {
9197
'PURLs',
9298
'https://github.com/package-url/purl-spec',
9399
)} (e.g., pkg:npm/package@1.0.0) - automatically converted to GHSA
94-
Can be provided as comma separated values or as multiple flags`,
100+
Can be provided as comma separated values or as multiple flags. Cannot be used with --all.`,
95101
isMultiple: true,
96102
},
97103
limit: {
@@ -258,6 +264,7 @@ async function run(
258264
)
259265

260266
const {
267+
all,
261268
applyFixes,
262269
autopilot,
263270
exclude,
@@ -276,6 +283,7 @@ async function run(
276283
// socket-cli/patches/meow#13.2.0.patch.
277284
unknownFlags = [],
278285
} = cli.flags as unknown as {
286+
all: boolean
279287
applyFixes: boolean
280288
autopilot: boolean
281289
exclude: string[]
@@ -303,6 +311,12 @@ async function run(
303311

304312
const outputKind = getOutputKind(json, markdown)
305313

314+
const ghsas = arrayUnique([
315+
...cmdFlagValueToArray(cli.flags['id']),
316+
...cmdFlagValueToArray(cli.flags['ghsa']),
317+
...cmdFlagValueToArray(cli.flags['purl']),
318+
])
319+
306320
const wasValidInput = checkCommandInput(
307321
outputKind,
308322
{
@@ -316,6 +330,12 @@ async function run(
316330
message: 'The json and markdown flags cannot be both set, pick one',
317331
fail: 'omit one',
318332
},
333+
{
334+
nook: true,
335+
test: !all || !ghsas.length,
336+
message: 'The --all and --id flags cannot be used together',
337+
fail: 'omit one',
338+
},
319339
)
320340
if (!wasValidInput) {
321341
return
@@ -344,16 +364,11 @@ async function run(
344364

345365
const spinner = undefined
346366

347-
const ghsas = arrayUnique([
348-
...cmdFlagValueToArray(cli.flags['id']),
349-
...cmdFlagValueToArray(cli.flags['ghsa']),
350-
...cmdFlagValueToArray(cli.flags['purl']),
351-
])
352-
353367
const includePatterns = cmdFlagValueToArray(include)
354368
const excludePatterns = cmdFlagValueToArray(exclude)
355369

356370
await handleFix({
371+
all,
357372
applyFixes,
358373
autopilot,
359374
cwd,

packages/cli/src/commands/fix/coana-fix.mts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export async function coanaFix(
7171
fixConfig: FixConfig,
7272
): Promise<CResult<{ data?: unknown; fixed: boolean }>> {
7373
const {
74+
all,
7475
applyFixes,
7576
autopilot,
7677
cwd,
@@ -145,13 +146,18 @@ export async function coanaFix(
145146
}
146147
}
147148

148-
const isAll =
149-
!ghsas.length ||
150-
(ghsas.length === 1 && (ghsas[0] === 'all' || ghsas[0] === 'auto'))
149+
const shouldDiscoverGhsaIds = all || !ghsas.length
151150

152151
const shouldOpenPrs = fixEnv.isCi && fixEnv.repoInfo
153152

154153
if (!shouldOpenPrs) {
154+
// In local mode, if neither --all nor --id is provided, show deprecation warning.
155+
if (shouldDiscoverGhsaIds && !all) {
156+
logger.warn(
157+
'Implicit --all is deprecated in local mode and will be removed in a future release. Please use --all explicitly.',
158+
)
159+
}
160+
155161
// Inform user about local mode when fixes will be applied.
156162
if (applyFixes && ghsas.length) {
157163
const envCheck = checkCiEnvVars()
@@ -172,7 +178,7 @@ export async function coanaFix(
172178
}
173179
}
174180

175-
const ids = isAll ? ['all'] : ghsas.slice(0, limit)
181+
const ids = shouldDiscoverGhsaIds ? ['all'] : ghsas.slice(0, limit)
176182
if (!ids.length) {
177183
spinner?.stop()
178184
return { ok: true, data: { fixed: false } }
@@ -262,9 +268,9 @@ export async function coanaFix(
262268

263269
let ids: string[] | undefined
264270

265-
// When isAll is true, discover vulnerabilities by running coana with --output-file.
271+
// When shouldDiscoverGhsaIds is true, discover vulnerabilities by running coana with --output-file.
266272
// This gives us the GHSA IDs needed to create individual PRs in CI mode.
267-
if (shouldSpawnCoana && isAll) {
273+
if (shouldSpawnCoana && shouldDiscoverGhsaIds) {
268274
const discoverTmpFile = path.join(
269275
os.tmpdir(),
270276
`socket-discover-${Date.now()}.json`,

packages/cli/src/commands/fix/handle-fix.mts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export async function convertIdsToGhsas(ids: string[]): Promise<string[]> {
103103
}
104104

105105
export async function handleFix({
106+
all,
106107
applyFixes,
107108
autopilot,
108109
cwd,
@@ -124,6 +125,7 @@ export async function handleFix({
124125
}: HandleFixConfig) {
125126
debug(`Starting fix command for ${orgSlug}`)
126127
debugDir({
128+
all,
127129
applyFixes,
128130
autopilot,
129131
cwd,
@@ -144,6 +146,7 @@ export async function handleFix({
144146

145147
await outputFixResult(
146148
await coanaFix({
149+
all,
147150
applyFixes,
148151
autopilot,
149152
cwd,

packages/cli/src/commands/fix/types.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { RangeStyle } from '../../utils/semver.mts'
33
import type { Spinner } from '@socketsecurity/lib/spinner'
44

55
export type FixConfig = {
6+
all: boolean
67
applyFixes: boolean
78
autopilot: boolean
89
cwd: string

packages/cli/test/unit/commands/fix/handle-fix-limit.test.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ vi.mock('node:fs', () => ({
123123

124124
describe('socket fix --limit behavior verification', () => {
125125
const baseConfig: FixConfig = {
126+
all: false,
126127
applyFixes: true,
127128
autopilot: false,
128129
cwd: '/test/cwd',

0 commit comments

Comments
 (0)