@@ -24,6 +24,23 @@ const node = (overrides = {}) => {
2424 }
2525}
2626
27+ // A registry node with no `resolved` URL in the lockfile. Its trusted name
28+ // comes from a dependency edge, but its version isn't trustable, so
29+ // versionedKeyFor returns null (npm/cli#9558).
30+ const noResolvedNode = ( overrides = { } ) => {
31+ const name = overrides . name ?? 'pkg'
32+ const version = overrides . version ?? '1.0.0'
33+ return {
34+ name,
35+ packageName : overrides . packageName ?? name ,
36+ version,
37+ resolved : undefined ,
38+ location : `node_modules/${ name } ` ,
39+ isRegistryDependency : true ,
40+ edgesIn : new Set ( [ { name, spec : overrides . spec ?? `^${ version } ` } ] ) ,
41+ }
42+ }
43+
2744t . test ( 'nameKeyFor / versionedKeyFor — registry' , async t => {
2845 const n = node ( { name : 'canvas' , version : '2.11.0' } )
2946 t . equal ( nameKeyFor ( n ) , 'canvas' )
@@ -214,6 +231,73 @@ t.test('applyApprovalForPackage — keeps existing pin matching one installed, a
214231 t . strictSame ( allowScripts , { '[email protected] ' :
true , '[email protected] ' :
true } ) 215232} )
216233
234+ t . test ( 'versionedKeyFor — registry dep without a resolved URL has no trustable version' , async t => {
235+ const n = noResolvedNode ( { name : 'esbuild' , version : '0.25.0' } )
236+ t . equal ( nameKeyFor ( n ) , 'esbuild' , 'name still recoverable from the dependency edge' )
237+ t . equal ( versionedKeyFor ( n ) , null , 'version cannot be trusted without a resolved URL' )
238+ } )
239+
240+ t . test ( 'applyApprovalForPackage — pin mode falls back to name-only when version is not trustable' , async t => {
241+ const { allowScripts, changes, warning } = applyApprovalForPackage (
242+ { } ,
243+ [ noResolvedNode ( { name : 'esbuild' , version : '0.25.0' } ) ] ,
244+ { pin : true }
245+ )
246+ t . strictSame ( allowScripts , { esbuild : true } , 'approved by name instead of silently skipped' )
247+ t . strictSame ( changes , [ { key : 'esbuild' , change : 'added' } ] )
248+ t . match ( warning , / n o " r e s o l v e d " U R L / , 'explains why a pin was not written' )
249+ } )
250+
251+ t . test ( 'applyApprovalForPackage — mixed resolved/no-resolved collapses to one name-only entry' , async t => {
252+ const { allowScripts, warning } = applyApprovalForPackage (
253+ { } ,
254+ [
255+ node ( { name : 'esbuild' , version : '0.21.5' } ) ,
256+ noResolvedNode ( { name : 'esbuild' , version : '0.25.0' } ) ,
257+ ] ,
258+ { pin : true }
259+ )
260+ t . strictSame ( allowScripts , { esbuild : true } , 'no redundant pin alongside the name-only entry' )
261+ t . match ( warning , / n o " r e s o l v e d " U R L / )
262+ } )
263+
264+ t . test ( 'applyApprovalForPackage — existing pin collapses into name-only when a sibling cannot be pinned' , async t => {
265+ const { allowScripts, changes } = applyApprovalForPackage (
266+ 267+ [
268+ node ( { name : 'esbuild' , version : '0.21.5' } ) ,
269+ noResolvedNode ( { name : 'esbuild' , version : '0.25.0' } ) ,
270+ ] ,
271+ { pin : true }
272+ )
273+ t . strictSame ( allowScripts , { esbuild : true } )
274+ t . match ( changes , [
275+ { key :
'[email protected] ' , change :
'removed-pinned-allow' } , 276+ { key : 'esbuild' , change : 'added' } ,
277+ ] )
278+ } )
279+
280+ t . test ( 'applyApprovalForPackage — no-resolved dep already approved by name is a no-op' , async t => {
281+ const { allowScripts, changes } = applyApprovalForPackage (
282+ { esbuild : true } ,
283+ [ noResolvedNode ( { name : 'esbuild' , version : '0.25.0' } ) ] ,
284+ { pin : true }
285+ )
286+ t . strictSame ( allowScripts , { esbuild : true } )
287+ t . strictSame ( changes , [ ] )
288+ } )
289+
290+ t . test ( 'applyApprovalForPackage — no-resolved dep with name-only deny still loses to the deny' , async t => {
291+ const { allowScripts, changes, warning } = applyApprovalForPackage (
292+ { esbuild : false } ,
293+ [ noResolvedNode ( { name : 'esbuild' , version : '0.25.0' } ) ] ,
294+ { pin : true }
295+ )
296+ t . strictSame ( allowScripts , { esbuild : false } , 'deny wins; nothing approved' )
297+ t . strictSame ( changes , [ ] )
298+ t . match ( warning , / e s b u i l d i s d e n i e d / )
299+ } )
300+
217301t . test ( 'applyDenyForPackage — empty allowScripts adds name-only false' , async t => {
218302 const { allowScripts, changes } = applyDenyForPackage (
219303 { } ,
0 commit comments