forked from npm/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript-allowed.js
More file actions
671 lines (614 loc) · 25.2 KB
/
Copy pathscript-allowed.js
File metadata and controls
671 lines (614 loc) · 25.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
const t = require('tap')
const isScriptAllowed = require('../lib/script-allowed.js')
const { trustedDisplay } = isScriptAllowed
// Test nodes default to a consistent registry-tarball shape: the resolved
// URL's name+version match the supplied name+version. Tests that need to
// simulate manifest confusion (mismatched URL) can override `resolved`
// independently.
const node = (overrides = {}) => {
const name = overrides.name ?? overrides.packageName ?? 'pkg'
const packageName = overrides.packageName ?? name
const version = overrides.version ?? '1.0.0'
// For real aliased installs, the URL is the TARGET package's URL, so
// build the default URL from packageName, not name.
const urlPkg = packageName
return {
name,
packageName,
version,
resolved: overrides.resolved
?? `https://registry.npmjs.org/${urlPkg}/-/${urlPkg}-${version}.tgz`,
location: overrides.location ?? `node_modules/${name}`,
...overrides,
}
}
t.test('returns null when no policy is set', t => {
t.equal(isScriptAllowed(node(), null), null)
t.equal(isScriptAllowed(node(), undefined), null)
t.equal(isScriptAllowed(node(), {}), null)
t.end()
})
t.test('registry — name-only allow', t => {
t.equal(isScriptAllowed(node({ name: 'canvas', version: '2.11.0' }), { canvas: true }), true)
t.equal(isScriptAllowed(node({ name: 'other', version: '1.0.0' }), { canvas: true }), null)
t.end()
})
t.test('registry — exact version match', t => {
const policy = { 'canvas@2.11.0': true }
t.equal(isScriptAllowed(node({ name: 'canvas', version: '2.11.0' }), policy), true)
t.equal(isScriptAllowed(node({ name: 'canvas', version: '2.11.1' }), policy), null)
t.end()
})
t.test('registry — semver range', t => {
const policy = { 'sharp@0.33.2 || 0.34.0': true }
t.equal(isScriptAllowed(node({ name: 'sharp', version: '0.33.2' }), policy), true)
t.equal(isScriptAllowed(node({ name: 'sharp', version: '0.34.0' }), policy), true)
t.equal(isScriptAllowed(node({ name: 'sharp', version: '0.33.3' }), policy), null)
t.end()
})
t.test('registry — version mismatch returns null', t => {
const policy = { 'canvas@2.11.0': true }
t.equal(isScriptAllowed(node({ name: 'canvas', version: '3.0.0' }), policy), null)
t.end()
})
t.test('alias must not match the alias name', t => {
// install: `trusted@npm:naughty@1.0.0`
// node.name === 'trusted', node.packageName === 'naughty'
const aliased = node({ name: 'trusted', packageName: 'naughty', version: '1.0.0' })
// Key matching the alias name must NOT match.
t.equal(isScriptAllowed(aliased, { trusted: true }), null)
// Key matching the real package name DOES match.
t.equal(isScriptAllowed(aliased, { naughty: true }), true)
t.equal(isScriptAllowed(aliased, { 'naughty@1.0.0': true }), true)
t.end()
})
t.test('registry-style key does not match a tarball or git node', t => {
const gitNode = node({
name: 'pkg',
version: '1.0.0',
resolved: 'git+ssh://git@github.com/foo/bar.git#deadbeefcafebabe1234567890abcdef12345678',
})
t.equal(isScriptAllowed(gitNode, { pkg: true }), null)
t.end()
})
t.test('git — repo-only match by canonical ssh URL', t => {
const gitNode = node({
name: 'bar',
packageName: 'bar',
version: '1.0.0',
resolved: 'git+ssh://git@github.com/foo/bar.git#deadbeefcafebabe1234567890abcdef12345678',
})
// Key by repo only — name-only allow for the git source.
t.equal(isScriptAllowed(gitNode, { 'github:foo/bar': true }), true)
t.equal(isScriptAllowed(gitNode, { 'github:foo/other': true }), null)
t.end()
})
t.test('git — short SHA prefix matches full SHA', t => {
const gitNode = node({
name: 'bar',
packageName: 'bar',
version: '1.0.0',
resolved: 'git+ssh://git@github.com/foo/bar.git#deadbeefcafebabe1234567890abcdef12345678',
})
t.equal(isScriptAllowed(gitNode, { 'github:foo/bar#deadbeef': true }), true)
t.equal(isScriptAllowed(gitNode, { 'github:foo/bar#deadbee': true }), true)
t.equal(isScriptAllowed(gitNode, { 'github:foo/bar#abcdef0': true }), null)
t.end()
})
t.test('file path — exact resolved match', t => {
const fileNode = node({
name: 'local-pkg',
packageName: 'local-pkg',
version: '1.0.0',
resolved: 'file:../local-pkg',
})
t.equal(isScriptAllowed(fileNode, { 'file:../local-pkg': true }), true)
t.equal(isScriptAllowed(fileNode, { 'file:../other': true }), null)
t.end()
})
t.test('directory key — npa parses absolute paths as type=directory', t => {
// npa treats absolute paths as { type: 'directory' }, which the
// matcher shares with the 'file' case. path.resolve produces a
// platform-correct absolute path so this works on Windows.
const absDir = require('node:path').resolve('local-pkg')
const dirNode = node({
name: 'local-pkg',
packageName: 'local-pkg',
version: '1.0.0',
resolved: absDir,
})
t.equal(isScriptAllowed(dirNode, { [absDir]: true }), true)
t.end()
})
t.test('local tarball key — npa parses *.tgz paths as type=file', t => {
// npa treats `*.tgz` paths as { type: 'file' }, separate from
// 'directory'. Both share the matchFileOrDir body.
const tgzNode = node({
name: 'local-pkg',
packageName: 'local-pkg',
version: '1.0.0',
resolved: 'file:../local-pkg.tgz',
})
t.equal(isScriptAllowed(tgzNode, { 'file:../local-pkg.tgz': true }), true)
t.end()
})
t.test('remote tarball — exact resolved match', t => {
const remoteNode = node({
name: 'pkg',
packageName: 'pkg',
version: '1.0.0',
resolved: 'https://example.com/pkg.tgz',
})
t.equal(isScriptAllowed(remoteNode, { 'https://example.com/pkg.tgz': true }), true)
t.equal(isScriptAllowed(remoteNode, { 'https://example.com/other.tgz': true }), null)
t.end()
})
t.test('omitLockfileRegistryResolved: name-only match via edges; version-pinned does not', t => {
// Without a resolved URL, the trusted name comes from an incoming
// dependency edge (consumer-written), not from node.location (which
// for aliases would expose the alias name and let a malicious publisher
// bypass the policy). Version isn't trustable in this case, so
// version-pinned policy entries cannot match.
const omitted = {
name: 'canvas',
packageName: 'canvas',
version: '2.11.0',
resolved: undefined,
location: 'node_modules/canvas',
edgesIn: new Set([{ name: 'canvas', spec: '^2.0.0' }]),
}
t.equal(isScriptAllowed(omitted, { canvas: true }), true)
t.equal(isScriptAllowed(omitted, { 'canvas@2.11.0': true }), null,
'version-pinned match requires the trusted URL-derived version')
t.equal(isScriptAllowed(omitted, { 'canvas@3': true }), null)
t.end()
})
t.test('omitLockfileRegistryResolved + alias: location is ignored; underlying name wins', t => {
// Consumer's package.json has `"trusted": "npm:naughty@1.0.0"`. With
// omitLockfileRegistryResolved, the resolved URL is absent. The install
// location is `node_modules/trusted` (alias path). The matcher MUST
// derive the underlying name from the edge's alias subSpec, not from
// the location.
const aliasOmitted = {
name: 'trusted',
packageName: 'naughty',
version: '1.0.0',
resolved: undefined,
location: 'node_modules/trusted',
edgesIn: new Set([{ name: 'trusted', spec: 'npm:naughty@1.0.0' }]),
}
// Alias name MUST NOT match.
t.equal(isScriptAllowed(aliasOmitted, { trusted: true }), null,
'alias name does not authorize the underlying package')
// Underlying name DOES match.
t.equal(isScriptAllowed(aliasOmitted, { naughty: true }), true,
'underlying package name authorizes scripts')
t.end()
})
t.test('omit-lockfile with no edges returns null (no trusted identity)', t => {
const orphan = {
name: 'canvas',
packageName: 'canvas',
version: '1.0.0',
resolved: undefined,
location: 'node_modules/canvas',
// No edgesIn at all.
}
t.equal(isScriptAllowed(orphan, { canvas: true }), null,
'cannot match without a trusted name source')
t.end()
})
t.test('deny wins on conflict', t => {
const n = node({ name: 'pkg', version: '2.0.0' })
t.equal(isScriptAllowed(n, { 'pkg@1.0.0 || 2.0.0': true, 'pkg@2.0.0 || 3.0.0': false }), false)
t.equal(isScriptAllowed(n, { pkg: true, 'pkg@2.0.0': false }), false)
t.end()
})
t.test('name-only deny without overlap returns false', t => {
t.equal(isScriptAllowed(node({ name: 'core-js', version: '3.0.0' }), { 'core-js': false }), false)
t.end()
})
t.test('skips unparseable policy keys', t => {
t.equal(
isScriptAllowed(node({ name: 'pkg', version: '1.0.0' }), { '@@@invalid': true, pkg: true }),
true
)
t.end()
})
t.test('registry — forbidden semver ranges are rejected', async t => {
const n = node({ name: 'sharp', version: '0.33.5' })
// Caret range that DOES match version, but RFC forbids — must return null
t.equal(isScriptAllowed(n, { 'sharp@^0.33.0': true }), null)
t.equal(isScriptAllowed(n, { 'sharp@~0.33.0': true }), null)
t.equal(isScriptAllowed(n, { 'sharp@>=0.33.0': true }), null)
t.equal(isScriptAllowed(n, { 'sharp@<1.0.0': true }), null)
// Partial versions like `0.33` are ranges in semver — also rejected
t.equal(isScriptAllowed(n, { 'sharp@0.33': true }), null)
t.equal(isScriptAllowed(n, { 'sharp@0': true }), null)
})
t.test('registry — wildcard versions are allowed (treated as name-only)', async t => {
const n = node({ name: 'sharp', version: '0.33.5' })
t.equal(isScriptAllowed(n, { 'sharp@*': true }), true)
})
t.test('registry — exact disjunction with full semver is allowed', async t => {
const n = node({ name: 'sharp', version: '0.33.2' })
t.equal(isScriptAllowed(n, { 'sharp@0.33.2 || 0.34.0': true }), true)
})
t.test('registry — exact disjunction where version is not listed returns null', async t => {
const n = node({ name: 'sharp', version: '0.33.5' })
t.equal(isScriptAllowed(n, { 'sharp@0.33.2 || 0.34.0': true }), null)
})
t.test('forbidden range deny does NOT win (would silently allow new versions otherwise)', async t => {
// A user wrote `sharp@^0.33.0: false` thinking they're denying all 0.33.x.
// The matcher rejects the range, so the entry is effectively absent —
// deny does not match the node, returns null (unreviewed).
const n = node({ name: 'sharp', version: '0.33.5' })
t.equal(isScriptAllowed(n, { 'sharp@^0.33.0': false }), null)
})
t.test('isRegistryNode — spoofed tarball URL is NOT treated as registry', async t => {
// An attacker-controlled URL that happens to contain /-/<name>-<version>
// must not match a registry-style allow entry.
const spoofed = node({
name: 'trusted',
version: '1.0.0',
resolved: 'https://evil.com/-/trusted-1.0.0.tgz',
})
// Without a path segment before /-/, the URL is not a registry tarball
// pattern. The 'trusted@1.0.0' allow must not match.
t.equal(isScriptAllowed(spoofed, { 'trusted@1.0.0': true }), null)
t.equal(isScriptAllowed(spoofed, { trusted: true }), null)
// The user can still allow this specific URL via an exact resolved match.
t.equal(
isScriptAllowed(spoofed, { 'https://evil.com/-/trusted-1.0.0.tgz': true }),
true
)
})
t.test('isRegistryNode — arborist isRegistryDependency overrides URL guessing', async t => {
// A real arborist Node has isRegistryDependency. When false, the URL
// pattern is ignored entirely.
const arboristNode = {
name: 'trusted',
packageName: 'trusted',
version: '1.0.0',
resolved: 'https://registry.npmjs.org/trusted/-/trusted-1.0.0.tgz',
isRegistryDependency: false, // edges say it came from a non-registry source
}
t.equal(isScriptAllowed(arboristNode, { 'trusted@1.0.0': true }), null)
})
t.test('isRegistryNode — arborist isRegistryDependency true accepts even unusual URLs', async t => {
const arboristNode = {
name: 'trusted',
packageName: 'trusted',
version: '1.0.0',
resolved: 'https://internal.corp/private-registry/trusted/-/trusted-1.0.0.tgz',
isRegistryDependency: true, // edges say it came from a configured registry
}
t.equal(isScriptAllowed(arboristNode, { 'trusted@1.0.0': true }), true)
})
t.test('bundled deps cannot be allowlisted (never run)', async t => {
// Bundled dependencies have inBundle=true and no independent resolved
// URL. They can never be allowlisted because matching by name@version
// from the bundled tarball would reintroduce manifest confusion. They
// always return null, and their install scripts never run.
const bundled = {
name: 'bundled-pkg',
packageName: 'bundled-pkg',
version: '1.0.0',
resolved: undefined,
inBundle: true,
}
// Name-only allow: must NOT match a bundled dep.
t.equal(isScriptAllowed(bundled, { 'bundled-pkg': true }), null)
// Versioned allow: must NOT match a bundled dep.
t.equal(isScriptAllowed(bundled, { 'bundled-pkg@1.0.0': true }), null)
// Disjunction allow: must NOT match a bundled dep.
t.equal(isScriptAllowed(bundled, { 'bundled-pkg@1.0.0 || 2.0.0': true }), null)
// No policy: still null (no policy = nothing to evaluate against).
t.equal(isScriptAllowed(bundled, {}), null)
t.equal(isScriptAllowed(bundled, null), null)
})
t.test('bundled deps: deny entry does not match either (returns null, not false)', async t => {
// A deny entry doesn't apply to bundled deps because they're outside
// the policy scope entirely. They're blocked because they never run,
// not via a policy entry.
const bundled = {
name: 'bundled-pkg',
packageName: 'bundled-pkg',
version: '1.0.0',
resolved: undefined,
inBundle: true,
}
t.equal(isScriptAllowed(bundled, { 'bundled-pkg': false }), null)
})
t.test('bundled dep with resolved field is still rejected', async t => {
// Defensive: even if a bundled dep somehow has a resolved URL, the
// inBundle flag wins over identity matching.
const bundledWithResolved = {
name: 'pkg',
packageName: 'pkg',
version: '1.0.0',
resolved: 'https://registry.npmjs.org/pkg/-/pkg-1.0.0.tgz',
inBundle: true,
}
t.equal(isScriptAllowed(bundledWithResolved, { 'pkg@1.0.0': true }), null)
})
t.test('inBundle: false does not affect normal matching', async t => {
// Sanity check: explicit inBundle: false behaves identically to absent.
const normal = {
name: 'pkg',
packageName: 'pkg',
version: '1.0.0',
resolved: 'https://registry.npmjs.org/pkg/-/pkg-1.0.0.tgz',
inBundle: false,
}
t.equal(isScriptAllowed(normal, { 'pkg@1.0.0': true }), true)
})
t.test('isolated mode (linked): bundled IsolatedNode is blocked', async t => {
// Regression guard: in isolated/linked mode the gate runs against
// IsolatedNode instances, not real Nodes. A bundled IsolatedNode must
// report inBundle so the gate blocks it even when its resolved URL
// looks like a registry identity that a name entry would otherwise
// match. Without inBundle on IsolatedNode the guard is silently
// skipped and the bundled install script runs.
const { IsolatedNode } = require('../lib/isolated-classes.js')
const bundled = new IsolatedNode({
inBundle: true,
location: 'node_modules/bundled-pkg',
name: 'bundled-pkg',
package: { name: 'bundled-pkg', version: '1.0.0' },
path: '/project/node_modules/bundled-pkg',
resolved: 'https://registry.npmjs.org/bundled-pkg/-/bundled-pkg-1.0.0.tgz',
})
t.equal(bundled.inBundle, true, 'bundled IsolatedNode reports inBundle')
t.equal(isScriptAllowed(bundled, { 'bundled-pkg': true }), null)
t.equal(isScriptAllowed(bundled, { 'bundled-pkg@1.0.0': true }), null)
const store = new IsolatedNode({
isInStore: true,
isRegistryDependency: true, // carried from the source node by #externalProxy
location: 'node_modules/.store/store-pkg@1.0.0/node_modules/store-pkg',
name: 'store-pkg',
package: { name: 'store-pkg', version: '1.0.0' },
path: '/project/node_modules/.store/store-pkg@1.0.0/node_modules/store-pkg',
resolved: 'https://registry.npmjs.org/store-pkg/-/store-pkg-1.0.0.tgz',
})
t.equal(store.inBundle, false, 'external store IsolatedNode is not bundled')
t.equal(isScriptAllowed(store, { 'store-pkg@1.0.0': true }), true)
})
t.test('manifest confusion: malicious tarball self-name cannot bypass allow entry', async t => {
// A package author publishes 'naughty' to the registry but inside the
// tarball claims `package.json#name = "trusted"` and the matching
// version. The lockfile records the registry URL for 'naughty'.
// node.packageName / node.version return the tarball's claims; the
// matcher MUST ignore both and consult only the URL.
const malicious = {
name: 'naughty', // dependency edge name (consumer's deps)
packageName: 'trusted', // tarball's self-claimed name (LIE)
version: '1.0.0', // tarball's self-claimed version
resolved: 'https://registry.npmjs.org/naughty/-/naughty-1.0.0.tgz',
location: 'node_modules/naughty',
isRegistryDependency: true,
}
// The 'trusted' allowlist entry must NOT match this node.
t.equal(isScriptAllowed(malicious, { trusted: true }), null)
t.equal(isScriptAllowed(malicious, { 'trusted@1.0.0': true }), null)
// A 'naughty' entry (the URL-derived name) DOES match.
t.equal(isScriptAllowed(malicious, { naughty: true }), true)
t.equal(isScriptAllowed(malicious, { 'naughty@1.0.0': true }), true)
})
t.test('manifest confusion: malicious version claim cannot satisfy version pin', async t => {
// The tarball claims version 1.0.0 but the URL records 2.0.0. The
// matcher must trust the URL.
const malicious = {
name: 'pkg',
packageName: 'pkg',
version: '1.0.0', // tarball lie
resolved: 'https://registry.npmjs.org/pkg/-/pkg-2.0.0.tgz',
location: 'node_modules/pkg',
isRegistryDependency: true,
}
// Pin for the URL version matches.
t.equal(isScriptAllowed(malicious, { 'pkg@2.0.0': true }), true)
// Pin for the tarball's lie does NOT match.
t.equal(isScriptAllowed(malicious, { 'pkg@1.0.0': true }), null)
})
t.test('manifest confusion: scoped registry tarball', async t => {
const node = {
name: 'pkg',
packageName: 'totally-different',
version: '9.9.9',
resolved: 'https://registry.npmjs.org/@scope/real/-/real-1.0.0.tgz',
location: 'node_modules/@scope/real',
isRegistryDependency: true,
}
t.equal(isScriptAllowed(node, { '@scope/real': true }), true)
t.equal(isScriptAllowed(node, { '@scope/real@1.0.0': true }), true)
t.equal(isScriptAllowed(node, { 'totally-different': true }), null)
})
t.test('git committish: matching is one-directional (key is prefix of resolved SHA)', async t => {
// The lockfile records a 40-char SHA; the policy key has a (typically
// shorter) SHA. The trusted check is `nodeFullSha.startsWith(keyShortSha)`.
// The reverse direction must NOT match — a malformed lockfile that
// happens to record only a short committish must not let a longer key
// authorize it.
const shortNode = node({
name: 'bar',
packageName: 'bar',
version: '1.0.0',
// Node committish is 8 chars; key committish below is longer.
resolved: 'git+ssh://git@github.com/foo/bar.git#deadbeef',
})
// Key SHA strictly longer than the node committish: must not match.
t.equal(
isScriptAllowed(shortNode, { 'github:foo/bar#deadbeefcafebabe1234567890abcdef12345678': true }),
null
)
// Node fully starts with key — matches normally.
const fullNode = node({
name: 'bar',
packageName: 'bar',
version: '1.0.0',
resolved: 'git+ssh://git@github.com/foo/bar.git#deadbeefcafebabe1234567890abcdef12345678',
})
t.equal(isScriptAllowed(fullNode, { 'github:foo/bar#deadbeef': true }), true)
})
t.test('git policy key against a non-git node does not match', t => {
// matchGit fast-exits when the node has no resolved or its resolved
// does not begin with `git`.
const registryNode = node({
name: 'bar',
packageName: 'bar',
version: '1.0.0',
resolved: 'https://registry.npmjs.org/bar/-/bar-1.0.0.tgz',
})
t.equal(isScriptAllowed(registryNode, { 'github:foo/bar': true }), null)
const noResolved = {
name: 'bar',
packageName: 'bar',
version: '1.0.0',
resolved: undefined,
edgesIn: new Set([{ name: 'bar', spec: 'github:foo/bar' }]),
}
t.equal(isScriptAllowed(noResolved, { 'github:foo/bar': true }), null)
t.end()
})
t.test('git — non-hosted git URL matches via fetchSpec', t => {
// Self-hosted git server: not on GitHub/GitLab/etc, so `hosted` is
// undefined and the matcher falls back to comparing fetchSpec.
const gitNode = node({
name: 'bar',
packageName: 'bar',
version: '1.0.0',
resolved: 'git+ssh://git.example.com/foo/bar.git#deadbeefcafebabe1234567890abcdef12345678',
})
t.equal(
isScriptAllowed(gitNode, { 'git+ssh://git.example.com/foo/bar.git': true }),
true
)
t.equal(
isScriptAllowed(gitNode, { 'git+ssh://git.example.com/foo/other.git': true }),
null
)
t.end()
})
t.test('git — hosted key against non-hosted node falls through', t => {
// A GitHub-style key cannot match a self-hosted git node: one side
// produces a hosted.ssh URL, the other does not, so neither the
// hosted branch nor the fetchSpec branch applies.
const gitNode = node({
name: 'bar',
packageName: 'bar',
version: '1.0.0',
resolved: 'git+ssh://git.example.com/foo/bar.git#deadbeefcafebabe1234567890abcdef12345678',
})
t.equal(isScriptAllowed(gitNode, { 'github:foo/bar': true }), null)
t.end()
})
t.test('git — resolved URL without a committish does not match keys with one', t => {
// The lockfile somehow recorded the git URL without a #SHA suffix.
// Any policy key that pins a specific committish must NOT match.
const gitNode = node({
name: 'bar',
packageName: 'bar',
version: '1.0.0',
resolved: 'git+ssh://git@github.com/foo/bar.git',
})
t.equal(isScriptAllowed(gitNode, { 'github:foo/bar#deadbeef': true }), null)
// Repo-only key still matches.
t.equal(isScriptAllowed(gitNode, { 'github:foo/bar': true }), true)
t.end()
})
t.test('file/directory key against a node with no resolved does not match', t => {
const noResolved = {
name: 'local-pkg',
packageName: 'local-pkg',
version: '1.0.0',
resolved: undefined,
edgesIn: new Set([{ name: 'local-pkg', spec: 'file:../local-pkg' }]),
}
t.equal(isScriptAllowed(noResolved, { 'file:../local-pkg': true }), null)
t.end()
})
t.test('remote key against a node with no resolved does not match', t => {
const noResolved = {
name: 'pkg',
packageName: 'pkg',
version: '1.0.0',
resolved: undefined,
edgesIn: new Set([{ name: 'pkg', spec: 'https://example.com/pkg.tgz' }]),
}
t.equal(isScriptAllowed(noResolved, { 'https://example.com/pkg.tgz': true }), null)
t.end()
})
t.test('alias-typed policy key never matches anything', t => {
// Policy key parsed as type 'alias' (e.g. `foo@npm:bar@1.0.0`) is
// explicitly disallowed. The user must address the real package name.
const aliased = node({ name: 'foo', packageName: 'bar', version: '1.0.0' })
t.equal(isScriptAllowed(aliased, { 'foo@npm:bar@1.0.0': true }), null)
t.end()
})
t.test('registry — `pkg@latest` tag spec does not match (rejected up front)', t => {
// npa parses `canvas@latest` as { type: 'tag' }. Validated out by
// resolve-allow-scripts.js#validatePolicy; defense-in-depth here.
const n = node({ name: 'canvas', version: '2.11.0' })
t.equal(isScriptAllowed(n, { 'canvas@latest': true }), null)
t.equal(isScriptAllowed(n, { 'other@latest': true }), null)
t.end()
})
t.test('omit-lockfile: unparseable edge specs are skipped', t => {
// An edge whose spec npa.resolve cannot parse is ignored; the matcher
// continues to the next edge to find a trusted name.
const omitted = {
name: 'canvas',
packageName: 'canvas',
version: '2.11.0',
resolved: undefined,
location: 'node_modules/canvas',
edgesIn: new Set([
{ name: 'canvas', spec: '\u0000not a real spec' },
{ name: 'canvas', spec: '^2.0.0' },
]),
}
t.equal(isScriptAllowed(omitted, { canvas: true }), true)
t.end()
})
t.test('omit-lockfile: edges with only non-registry specs yield no trusted name', t => {
// Every incoming edge is a git/file/remote spec, so nameFromEdges
// walks them all and returns null at the end.
const omitted = {
name: 'canvas',
packageName: 'canvas',
version: '2.11.0',
resolved: undefined,
location: 'node_modules/canvas',
edgesIn: new Set([
{ name: 'canvas', spec: 'github:foo/canvas' },
{ name: 'canvas', spec: 'file:../canvas' },
]),
}
t.equal(isScriptAllowed(omitted, { canvas: true }), null)
t.end()
})
t.test('trustedDisplay returns URL-derived identity when available', t => {
// Registry tarball URL carries name and version — those are trusted and
// must override the tarball-self-claimed package fields.
const n = node({
name: 'pkg',
packageName: 'lying-name',
version: '9.9.9',
resolved: 'https://registry.npmjs.org/canvas/-/canvas-2.11.0.tgz',
})
t.strictSame(trustedDisplay(n), { name: 'canvas', version: '2.11.0' })
t.end()
})
t.test('trustedDisplay falls back to node.name/version when URL has no identity', t => {
// Git source: matchGit handles it for matching, but trustedDisplay
// accepts the node's self-reported name/version for human-facing output.
const n = node({
name: 'bar',
packageName: 'bar',
version: '1.2.3',
resolved: 'git+ssh://git@github.com/foo/bar.git#deadbeefcafebabe1234567890abcdef12345678',
})
t.strictSame(trustedDisplay(n), { name: 'bar', version: '1.2.3' })
t.end()
})