forked from npm/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunreviewed-scripts.js
More file actions
188 lines (170 loc) · 6.42 KB
/
Copy pathunreviewed-scripts.js
File metadata and controls
188 lines (170 loc) · 6.42 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
const t = require('tap')
const {
collectUnreviewedScripts,
strictAllowScriptsError,
} = require('../lib/unreviewed-scripts.js')
// Loads a fresh copy of the shared module with `install-scripts.js` mocked,
// so script detection (including the synthetic node-gyp `binding.gyp` path)
// can be controlled without touching the filesystem.
const mockCollect = (t, getInstallScripts) =>
t.mock('../lib/unreviewed-scripts.js', {
'../lib/install-scripts.js': getInstallScripts,
}).collectUnreviewedScripts
// Minimal tree fixture for the walk.
const tree = (nodes) => ({
inventory: new Map(nodes.map((n, i) => [`node_modules/${n.name || `n${i}`}`, n])),
})
// Registry-shaped node so the real script-allowed/install-scripts helpers
// behave deterministically (registry tarballs skip `prepare`, and the
// identity matcher keys off the resolved URL).
const node = ({
name = 'pkg',
version = '1.0.0',
scripts = {},
isProjectRoot = false,
isWorkspace = false,
isLink = false,
inBundle = false,
resolved,
} = {}) => ({
name,
packageName: name,
version,
resolved: resolved ?? `https://registry.npmjs.org/${name}/-/${name}-${version}.tgz`,
location: `node_modules/${name}`,
isProjectRoot,
isWorkspace,
isLink,
inBundle,
isRegistryDependency: true,
package: { name, version, scripts },
})
t.test('collectUnreviewedScripts', async t => {
t.test('returns [] when ignoreScripts is set', async t => {
const result = await collectUnreviewedScripts({
tree: tree([node({ scripts: { install: 'x' } })]),
policy: null,
ignoreScripts: true,
})
t.strictSame(result, [])
})
t.test('returns [] when dangerouslyAllowAllScripts is set', async t => {
const result = await collectUnreviewedScripts({
tree: tree([node({ scripts: { install: 'x' } })]),
policy: null,
dangerouslyAllowAllScripts: true,
})
t.strictSame(result, [])
})
t.test('returns [] when tree has no inventory', async t => {
t.strictSame(await collectUnreviewedScripts({ tree: undefined }), [])
t.strictSame(await collectUnreviewedScripts({ tree: {} }), [])
t.strictSame(await collectUnreviewedScripts({}), [])
t.strictSame(await collectUnreviewedScripts(), [])
})
t.test('skips project root, workspace, linked, and bundled nodes', async t => {
const result = await collectUnreviewedScripts({
tree: tree([
node({ name: 'root', scripts: { install: 'x' }, isProjectRoot: true }),
node({ name: 'ws', scripts: { install: 'x' }, isWorkspace: true }),
node({ name: 'linked', scripts: { install: 'x' }, isLink: true }),
node({ name: 'bundled', scripts: { install: 'x' }, inBundle: true }),
]),
policy: null,
})
t.strictSame(result, [])
})
t.test('skips nodes with no install-relevant scripts', async t => {
const result = await collectUnreviewedScripts({
tree: tree([node({ scripts: { test: 'jest' } })]),
policy: null,
})
t.strictSame(result, [])
})
t.test('collects unreviewed install scripts', async t => {
const result = await collectUnreviewedScripts({
tree: tree([
node({ name: 'a', scripts: { preinstall: 'pre' } }),
node({ name: 'b', scripts: { install: 'inst' } }),
node({ name: 'c', scripts: { postinstall: 'post' } }),
]),
policy: null,
})
t.equal(result.length, 3)
t.strictSame(result[0].scripts, { preinstall: 'pre' })
t.strictSame(result[1].scripts, { install: 'inst' })
t.strictSame(result[2].scripts, { postinstall: 'post' })
})
t.test('skips nodes the policy allows or denies', async t => {
const result = await collectUnreviewedScripts({
tree: tree([
node({ name: 'allowed', version: '1.0.0', scripts: { install: 'x' } }),
node({ name: 'denied', version: '1.0.0', scripts: { install: 'x' } }),
node({ name: 'pending', version: '1.0.0', scripts: { install: 'x' } }),
]),
policy: { allowed: true, denied: false },
})
t.equal(result.length, 1)
t.equal(result[0].node.name, 'pending')
})
t.test('skips reviewed local directory link targets', async t => {
const target = node({ name: 'local', scripts: { install: 'x' } })
target.resolved = null
target.isRegistryDependency = false
target.path = require('node:path').resolve('local')
target.realpath = target.path
target.linksIn = new Set([{ resolved: 'file:../local' }])
t.strictSame(await collectUnreviewedScripts({
tree: tree([target]),
policy: { 'file:../local': false },
}), [])
t.strictSame(await collectUnreviewedScripts({
tree: tree([target]),
policy: { 'file:local': true },
}), [])
})
t.test('detects synthetic node-gyp via binding.gyp runtime check', async t => {
const collect = mockCollect(t, async (n) => {
if (n.path === '/has-bindings') {
return { install: 'node-gyp rebuild' }
}
return {}
})
const result = await collect({
tree: tree([
{ ...node({ name: 'native' }), path: '/has-bindings' },
{ ...node({ name: 'pure-js' }), path: '/no-bindings' },
]),
policy: null,
})
t.equal(result.length, 1)
t.equal(result[0].node.name, 'native')
t.strictSame(result[0].scripts, { install: 'node-gyp rebuild' })
})
})
t.test('strictAllowScriptsError', async t => {
const unreviewed = [
{ node: { package: { name: 'a', version: '1.0.0' } }, scripts: { install: 'do-a' } },
{ node: { package: { name: 'b', version: '2.0.0' } }, scripts: { preinstall: 'pre', postinstall: 'post' } },
]
const err = strictAllowScriptsError(unreviewed, { remediation: 'FIX IT.' })
t.equal(err.code, 'ESTRICTALLOWSCRIPTS')
t.match(err.message, /2 package\(s\) have install scripts not covered by allowScripts:/)
t.match(err.message, /a@1\.0\.0 \(install: do-a\)/)
t.match(err.message, /b@2\.0\.0 \(preinstall: pre; postinstall: post\)/)
t.match(err.message, /FIX IT\.$/)
})
t.test('strictAllowScriptsError falls back to node.name when no package', async t => {
const err = strictAllowScriptsError(
[{ node: { name: 'c' }, scripts: { install: 'x' } }],
{ remediation: 'go.' }
)
t.match(err.message, /\n {2}c \(install: x\)\n/)
})
t.test('strictAllowScriptsError defaults options when called without them', async t => {
const err = strictAllowScriptsError(
[{ node: { name: 'c' }, scripts: { install: 'x' } }]
)
t.equal(err.code, 'ESTRICTALLOWSCRIPTS')
t.match(err.message, /\n {2}c \(install: x\)\n/)
})