Debian Codegraph @

workspaces/arborist/test/script-allowed.js

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