Skip to content

Commit 31b8663

Browse files
authored
Add --min-satisfying option to fix command (#678)
1 parent 892a57a commit 31b8663

File tree

5 files changed

+49
-8
lines changed

5 files changed

+49
-8
lines changed

src/commands/fix/agent-fix.mts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export type FixConfig = {
6464
autoMerge: boolean
6565
cwd: string
6666
limit: number
67+
minSatisfying: boolean
6768
purls: string[]
6869
rangeStyle: RangeStyle
6970
spinner: Spinner | undefined
@@ -113,8 +114,16 @@ export async function agentFix(
113114
fixConfig: FixConfig,
114115
): Promise<CResult<{ fixed: boolean }>> {
115116
const { pkgPath: rootPath } = pkgEnvDetails
116-
const { autoMerge, cwd, limit, rangeStyle, spinner, test, testScript } =
117-
fixConfig
117+
const {
118+
autoMerge,
119+
cwd,
120+
limit,
121+
minSatisfying,
122+
rangeStyle,
123+
spinner,
124+
test,
125+
testScript,
126+
} = fixConfig
118127

119128
let count = 0
120129

@@ -292,11 +301,10 @@ export async function agentFix(
292301
firstPatchedVersionIdentifier,
293302
vulnerableVersionRange,
294303
} of infos) {
295-
const newVersion = findBestPatchVersion(
296-
node,
297-
availableVersions,
304+
const newVersion = findBestPatchVersion(node, availableVersions, {
305+
minSatisfying,
298306
vulnerableVersionRange,
299-
)
307+
})
300308
const newVersionPackument = newVersion
301309
? packument.versions[newVersion]
302310
: undefined

src/commands/fix/cmd-fix.mts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ const config: CliCommandConfig = {
5353
default: Infinity,
5454
description: 'The number of fixes to attempt at a time',
5555
},
56+
maxSatisfying: {
57+
type: 'boolean',
58+
default: true,
59+
description: 'Use the maximum satisfying version for dependency updates',
60+
hidden: true,
61+
},
62+
minSatisfying: {
63+
type: 'boolean',
64+
default: false,
65+
description:
66+
'Constrain dependency updates to the minimum satisfying version',
67+
},
5668
purl: {
5769
type: 'string',
5870
default: [],
@@ -170,6 +182,8 @@ async function run(
170182
(cli.flags['limit']
171183
? parseInt(String(cli.flags['limit'] || ''), 10)
172184
: Infinity) || Infinity
185+
const maxSatisfying = Boolean(cli.flags['maxSatisfying'])
186+
const minSatisfying = Boolean(cli.flags['minSatisfying']) || !maxSatisfying
173187
const purls = cmdFlagValueToArray(cli.flags['purl'])
174188
const testScript = String(cli.flags['testScript'] || 'test')
175189

@@ -178,6 +192,7 @@ async function run(
178192
cwd,
179193
ghsas,
180194
limit,
195+
minSatisfying,
181196
outputKind,
182197
purls,
183198
rangeStyle,

src/commands/fix/cmd-fix.test.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ describe('socket fix', async () => {
2626
--ghsa Provide a list of GHSA IDs (\\u200bhttps://docs.github.com/en/code-security/security-advisories/working-with-global-security-advisories-from-the-github-advisory-database/about-the-github-advisory-database#about-ghsa-ids\\u200b) to compute fixes for, as either a comma separated value or as multiple flags.
2727
Use '--ghsa auto' to automatically lookup GHSA IDs and compute fixes for them.
2828
--limit The number of fixes to attempt at a time
29+
--minSatisfying Constrain dependency updates to the minimum satisfying version
2930
--purl Provide a list of PURLs (\\u200bhttps://github.com/package-url/purl-spec?tab=readme-ov-file#purl\\u200b) to compute fixes for, as either a comma separated value or as multiple flags,
3031
instead of querying the Socket API
3132
--rangeStyle Define how updated dependency versions should be written in package.json.

src/commands/fix/handle-fix.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export async function handleFix({
3030
cwd,
3131
ghsas,
3232
limit,
33+
minSatisfying,
3334
outputKind,
3435
purls,
3536
rangeStyle,
@@ -151,6 +152,7 @@ export async function handleFix({
151152
autoMerge,
152153
cwd,
153154
limit,
155+
minSatisfying,
154156
purls,
155157
rangeStyle,
156158
spinner,

src/shadow/npm/arborist-helpers.mts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,20 @@ function getUrlOrigin(input: string): string {
4141
return ''
4242
}
4343

44+
export type BestPatchVersionOptions = {
45+
minSatisfying?: boolean | undefined
46+
vulnerableVersionRange?: string | undefined
47+
}
48+
4449
export function findBestPatchVersion(
4550
node: NodeClass,
4651
availableVersions: string[],
47-
vulnerableVersionRange?: string,
52+
options?: BestPatchVersionOptions | undefined,
4853
): string | null {
54+
const { minSatisfying = false, vulnerableVersionRange } = {
55+
__proto__: null,
56+
...options,
57+
} as BestPatchVersionOptions
4958
const manifestData = getManifestData(NPM, node.name)
5059
let eligibleVersions
5160
if (manifestData && manifestData.name === manifestData.package) {
@@ -68,7 +77,13 @@ export function findBestPatchVersion(
6877
!semver.satisfies(v, vulnerableVersionRange)),
6978
)
7079
}
71-
return eligibleVersions ? semver.maxSatisfying(eligibleVersions, '*') : null
80+
if (eligibleVersions) {
81+
const satisfying = minSatisfying
82+
? semver.minSatisfying
83+
: semver.maxSatisfying
84+
return satisfying(eligibleVersions, '*')
85+
}
86+
return null
7287
}
7388

7489
export function findPackageNode(

0 commit comments

Comments
 (0)