@@ -567,3 +567,72 @@ t.test('audit with filterSet limiting to only mkdirp and minimist', async t => {
567567 t . equal ( report . get ( 'nyc' ) , undefined , 'no nyc vuln reported' )
568568 t . equal ( report . get ( 'mkdirp' ) . simpleRange , '0.4.1 - 0.5.1' , 'mkdirp vuln reported' )
569569} )
570+
571+ t . test ( 'determinism: multiple metavulns with identical range but different dependencies' , async t => {
572+ const registry = createRegistry ( t )
573+
574+ // Create a tree where A depends on both B and C. Both B and C are vulnerable.
575+ const path = t . testdir ( )
576+ const tree = new Node ( {
577+ path,
578+ pkg : {
579+ name : 'root' ,
580+ dependencies : {
581+ A : '1.0.0' ,
582+ } ,
583+ } ,
584+ children : [
585+ {
586+ pkg : { name : 'A' , version : '1.0.0' , dependencies : { B : '1.0.0' , C : '1.0.0' } } ,
587+ } ,
588+ {
589+ pkg : { name : 'B' , version : '1.0.0' } ,
590+ } ,
591+ {
592+ pkg : { name : 'C' , version : '1.0.0' } ,
593+ } ,
594+ ] ,
595+ } )
596+
597+ registry . audit ( {
598+ times : 5 ,
599+ results : {
600+ B : [ { id : 1 , url : 'https://B' , title : 'B vuln' , severity : 'high' , vulnerable_versions : '*' } ] ,
601+ C : [ { id : 2 , url : 'https://C' , title : 'C vuln' , severity : 'high' , vulnerable_versions : '*' } ] ,
602+ } ,
603+ } )
604+
605+ // We intentionally do not mock the packuments for A, B, and C.
606+ // By using the fixtures directory, unmatched GET requests will receive a 404.
607+ // This triggers the metavuln calculator fallback which defaults the effective range to `*`.
608+ // As a result, both B and C trigger metavulns on A with identical ranges (`*`),
609+ // producing the identical collision key `A@*` required to reproduce the determinism bug.
610+ registry . mocks ( { dir : join ( __dirname , 'fixtures' ) } )
611+
612+ // We loop 5 times just to show it is deterministic.
613+ const results = [ ]
614+ let lastReport
615+ for ( let i = 0 ; i < 5 ; i ++ ) {
616+ lastReport = await AuditReport . load ( tree , { registry : 'https://registry.npmjs.org' } )
617+ results . push ( JSON . stringify ( lastReport . toJSON ( ) ) )
618+ }
619+
620+ const uniqueResults = new Set ( results )
621+ t . equal ( uniqueResults . size , 1 , 'output is identical across runs' )
622+
623+ // The key assertion is that BOTH B and C are correctly included in A's via list,
624+ // which formally proves the determinism bug dropping a via path is fixed.
625+ const A = lastReport . get ( 'A' )
626+ const B = lastReport . get ( 'B' )
627+ const C = lastReport . get ( 'C' )
628+
629+ t . ok ( A , 'A is vulnerable' )
630+ const viaNames = [ ...A . via ] . map ( v => v . name )
631+ const BEffects = [ ...B . effects ] . map ( v => v . name )
632+ const CEffects = [ ...C . effects ] . map ( v => v . name )
633+
634+ t . ok ( viaNames . includes ( 'B' ) , 'A via list includes B' )
635+ t . ok ( viaNames . includes ( 'C' ) , 'A via list includes C' )
636+ t . ok ( BEffects . includes ( 'A' ) , 'B effects includes A' )
637+ t . ok ( CEffects . includes ( 'A' ) , 'C effects includes A' )
638+ } )
0 commit comments