1414// --allow-major also roll root-pinned packages across a major version
1515// --report=FILE also write the markdown report to FILE
1616// --severity=LEVEL lowest severity to act on: low, moderate, high, critical (default moderate)
17+ // --urgent-severity=LEVEL advisories of this severity or above do not wait for --max-age-days:
18+ // when no version that is old enough fixes them, the oldest fixed version is picked
19+ // --all also act on packages that are not in a runtime bundle; needs no build
1720
1821import { execFileSync } from 'node:child_process' ;
1922import fs from 'node:fs' ;
@@ -33,6 +36,8 @@ const baseRef = args.base ?? 'HEAD';
3336const allowMajor = ! ! args [ 'allow-major' ] ;
3437const severities = [ 'low' , 'moderate' , 'high' , 'critical' ] ;
3538const minSeverity = severities . indexOf ( args . severity ?? 'moderate' ) ;
39+ const urgentSeverity = args [ 'urgent-severity' ] ? severities . indexOf ( args [ 'urgent-severity' ] ) : severities . length ;
40+ const all = ! ! args . all ;
3641const cutoff = new Date ( Date . now ( ) - maxAgeDays * 24 * 3600 * 1000 ) ;
3742
3843const lockPath = path . join ( root , 'package-lock.json' ) ;
@@ -155,18 +160,27 @@ function constraints(lockJson) {
155160// version under it, and that nested copy never reaches a bundle. A runtime
156161// dependent that rejects the new version would get such a nested copy too,
157162// and that one would be bundled, so it blocks.
158- function chooseVersion ( { name, current, nodePath, badRanges, cons, isRuntime } ) {
163+ function chooseVersion ( { name, current, nodePath, badRanges, urgentRanges = [ ] , cons, isRuntime } ) {
159164 const time = view ( name ) . time ;
160165 const rootPinned = cons . some ( c => c . from === '' ) ;
161166 const others = cons . filter ( c => c . from !== '' && ( c . from . startsWith ( 'packages/' ) || isRuntime ( c . from ) ) ) ;
162167 const allowPrerelease = ! ! semver . prerelease ( current ) ;
163- const candidates = Object . keys ( time )
168+ const newer = Object . keys ( time )
164169 . filter ( v => semver . valid ( v ) && ( allowPrerelease || ! semver . prerelease ( v ) ) )
165- . filter ( v => semver . gt ( v , current ) )
166- . filter ( v => ! badRanges . some ( r => semver . satisfies ( v , r , { includePrerelease : true } ) ) ) ;
167- const oldEnough = candidates . filter ( v => new Date ( time [ v ] ) <= cutoff ) ;
168- const accepted = oldEnough . filter ( v => others . every ( c => semver . satisfies ( v , c . range , { includePrerelease : true } ) ) ) ;
169- const pick = accepted . sort ( semver . rcompare ) [ 0 ] ;
170+ . filter ( v => semver . gt ( v , current ) ) ;
171+ const outside = ranges => v => ! ranges . some ( r => semver . satisfies ( v , r , { includePrerelease : true } ) ) ;
172+ const isOldEnough = v => new Date ( time [ v ] ) <= cutoff ;
173+ const isAccepted = v => others . every ( c => semver . satisfies ( v , c . range , { includePrerelease : true } ) ) ;
174+ const candidates = newer . filter ( outside ( badRanges ) ) ;
175+ const oldEnough = candidates . filter ( isOldEnough ) ;
176+ const accepted = oldEnough . filter ( isAccepted ) ;
177+ let pick = accepted . sort ( semver . rcompare ) [ 0 ] ;
178+ if ( ! pick && urgentRanges . length ) {
179+ // An urgent advisory does not wait. A version that is old enough is still
180+ // preferred, even when it leaves the less severe advisories open.
181+ const fixed = newer . filter ( outside ( urgentRanges ) ) . filter ( isAccepted ) ;
182+ pick = fixed . filter ( isOldEnough ) . sort ( semver . rcompare ) [ 0 ] ?? fixed . sort ( semver . compare ) [ 0 ] ;
183+ }
170184 if ( ! pick ) {
171185 if ( ! candidates . length )
172186 return { reason : 'no fixed version published' } ;
@@ -180,7 +194,7 @@ function chooseVersion({ name, current, nodePath, badRanges, cons, isRuntime })
180194 const major = semver . major ( pick ) !== semver . major ( current ) ;
181195 if ( rootPinned && major && ! allowMajor )
182196 return { reason : `needs a major bump to ${ pick } in package.json (rerun with --allow-major)` , version : pick } ;
183- return { version : pick , rootPinned, major, nodePath } ;
197+ return { version : pick , rootPinned, major, nodePath, urgent : ! isOldEnough ( pick ) } ;
184198}
185199
186200// ---------- lock file edits ----------
@@ -226,12 +240,12 @@ function checkInstalled(names) {
226240 throw new Error ( `npm ls reports problems after the roll:\n${ ls . problems . join ( '\n' ) } ` ) ;
227241}
228242
229- function pinDownNewcomers ( baseJson ) {
243+ function pinDownNewcomers ( baseJson , planned ) {
230244 const json = lock ( ) ;
231245 const cons = constraints ( json ) ;
232246 let changed = false ;
233247 for ( const { nodePath, name, before, after } of lockDiff ( baseJson , json ) ) {
234- if ( ! after || ! semver . valid ( after ) || publishTime ( name , after ) <= cutoff )
248+ if ( ! after || ! semver . valid ( after ) || publishTime ( name , after ) <= cutoff || planned . has ( ` ${ name } @ ${ after } ` ) )
235249 continue ;
236250 const choice = chooseVersion ( { name, current : before && semver . valid ( before ) ? before : '0.0.0' , nodePath, badRanges : [ ] , cons : ( cons . get ( nodePath ) ?? [ ] ) . filter ( c => c . from !== '' ) , isRuntime : ( ) => true } ) ;
237251 if ( ! choice . version || choice . version === after )
@@ -247,9 +261,12 @@ function pinDownNewcomers(baseJson) {
247261
248262async function main ( ) {
249263 const audit = JSON . parse ( npm ( [ 'audit' , '--json' ] ) ) ;
250- const inlined = bundleInlinedPaths ( ) ;
251- const workspaceDeps = workspaceRuntimeDeps ( ) ;
252- const isRuntime = nodePath => inlined . some ( l => l . startsWith ( nodePath + '/' ) ) || ( nodePath . split ( 'node_modules/' ) . length === 2 && workspaceDeps . has ( nameOf ( nodePath ) ) ) ;
264+ let isRuntime = ( ) => true ;
265+ if ( ! all ) {
266+ const inlined = bundleInlinedPaths ( ) ;
267+ const workspaceDeps = workspaceRuntimeDeps ( ) ;
268+ isRuntime = nodePath => inlined . some ( l => l . startsWith ( nodePath + '/' ) ) || ( nodePath . split ( 'node_modules/' ) . length === 2 && workspaceDeps . has ( nameOf ( nodePath ) ) ) ;
269+ }
253270
254271 const lockJson = lock ( ) ;
255272 const cons = constraints ( lockJson ) ;
@@ -269,14 +286,17 @@ async function main() {
269286 continue ;
270287 }
271288 const current = lockJson . packages [ nodePath ] . version ;
272- const choice = chooseVersion ( { name, current, nodePath, badRanges : advisories . map ( a => a . range ) , cons : cons . get ( nodePath ) ?? [ ] , isRuntime } ) ;
289+ const urgentRanges = advisories . filter ( a => severities . indexOf ( a . severity ) >= urgentSeverity ) . map ( a => a . range ) ;
290+ const choice = chooseVersion ( { name, current, nodePath, badRanges : advisories . map ( a => a . range ) , urgentRanges, cons : cons . get ( nodePath ) ?? [ ] , isRuntime } ) ;
273291 plan . push ( { name, nodePath, current, severity : vuln . severity , advisories : advisories . map ( a => a . url . replace ( '/advisories/' , '' ) ) , ...choice } ) ;
274292 }
275293 }
276294
277- console . log ( `\n## Runtime advisories (${ args . severity ?? 'moderate' } and above), versions published on or before ${ fmtDate ( cutoff ) } \n` ) ;
278- for ( const p of plan )
279- console . log ( `- ${ p . nodePath } ${ p . current } [${ p . severity } , ${ p . advisories . join ( ' ' ) } ]: ${ p . version && ! p . reason ? `roll to ${ p . version } ${ p . major ? ' (major)' : '' } ` : `not rolled: ${ p . reason } ` } ` ) ;
295+ console . log ( `\n## ${ all ? 'Advisories' : 'Runtime advisories' } (${ args . severity ?? 'moderate' } and above), versions published on or before ${ fmtDate ( cutoff ) } \n` ) ;
296+ for ( const p of plan ) {
297+ const action = p . version && ! p . reason ? `roll to ${ p . version } ${ p . major ? ' (major)' : '' } ${ p . urgent ? ' (urgent, newer than the age limit)' : '' } ` : `not rolled: ${ p . reason } ` ;
298+ console . log ( `- ${ p . nodePath } ${ p . current } [${ p . severity } , ${ p . advisories . join ( ' ' ) } ]: ${ action } ` ) ;
299+ }
280300 if ( ! plan . length )
281301 console . log ( '- none' ) ;
282302 if ( skipped . length )
@@ -298,9 +318,10 @@ async function main() {
298318 else
299319 setLockVersion ( p . nodePath , p . name , p . version ) ;
300320 }
321+ const planned = new Set ( actionable . map ( p => `${ p . name } @${ p . version } ` ) ) ;
301322 for ( let round = 0 ; round < 5 ; round ++ ) {
302323 npm ( [ 'install' , '--no-audit' , '--no-fund' ] ) ;
303- if ( ! pinDownNewcomers ( baseJson ) )
324+ if ( ! pinDownNewcomers ( baseJson , planned ) )
304325 break ;
305326 }
306327 checkInstalled ( actionable . map ( p => p . name ) ) ;
0 commit comments