@nodesecure/scanner 5.0.0 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/i18n/english.js +6 -0
- package/i18n/french.js +7 -0
- package/package.json +2 -1
- package/src/class/dependency.class.js +5 -1
- package/src/depWalker.js +367 -366
package/i18n/english.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
const scanner = {
|
|
2
|
+
disable_scarf: "This dependency could collect data against your consent so think to disable it with the env var: SCARF_ANALYTICS",
|
|
3
|
+
keylogging: "This dependency can retrieve your keyboard and mouse inputs. It can be used for 'keylogging' attacks/malwares."
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export default { scanner };
|
package/i18n/french.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
const scanner = {
|
|
2
|
+
disable_scarf: "Cette dépendance peut récolter des données contre votre volonté, pensez donc à la désactiver en fournissant la variable d'environnement SCARF_ANALYTICS",
|
|
3
|
+
keylogging: "Cette dépendance peut obtenir vos entrées clavier ou de souris. Cette dépendance peut être utilisée en tant que 'keylogging' attacks/malwares."
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export default { scanner };
|
|
7
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nodesecure/scanner",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.1",
|
|
4
4
|
"description": "A package API to run a static analysis of your module's dependencies.",
|
|
5
5
|
"exports": "./index.js",
|
|
6
6
|
"engines": {
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"files": [
|
|
18
18
|
"src",
|
|
19
|
+
"i18n",
|
|
19
20
|
"types",
|
|
20
21
|
"index.js",
|
|
21
22
|
"index.d.ts"
|
|
@@ -13,11 +13,15 @@ export default class Dependency {
|
|
|
13
13
|
this.alias = {};
|
|
14
14
|
|
|
15
15
|
if (parent !== null) {
|
|
16
|
-
parent.
|
|
16
|
+
parent.addChildren();
|
|
17
17
|
}
|
|
18
18
|
this.#parent = parent;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
addChildren() {
|
|
22
|
+
this.dependencyCount += 1;
|
|
23
|
+
}
|
|
24
|
+
|
|
21
25
|
get fullName() {
|
|
22
26
|
return `${this.name} ${this.version}`;
|
|
23
27
|
}
|
package/src/depWalker.js
CHANGED
|
@@ -1,366 +1,367 @@
|
|
|
1
|
-
// Import Node.js Dependencies
|
|
2
|
-
import path from "path";
|
|
3
|
-
import { readFileSync, promises as fs } from "fs";
|
|
4
|
-
import timers from "timers/promises";
|
|
5
|
-
import os from "os";
|
|
6
|
-
|
|
7
|
-
// Import Third-party Dependencies
|
|
8
|
-
import combineAsyncIterators from "combine-async-iterators";
|
|
9
|
-
import * as iter from "itertools";
|
|
10
|
-
import pacote from "pacote";
|
|
11
|
-
import Arborist from "@npmcli/arborist";
|
|
12
|
-
import Lock from "@slimio/lock";
|
|
13
|
-
import * as vuln from "@nodesecure/vuln";
|
|
14
|
-
import { ScannerLoggerEvents } from "./constants.js";
|
|
15
|
-
|
|
16
|
-
// Import Internal Dependencies
|
|
17
|
-
import {
|
|
18
|
-
mergeDependencies, getCleanDependencyName, getDependenciesWarnings, addMissingVersionFlags, isGitDependency,
|
|
19
|
-
NPM_TOKEN
|
|
20
|
-
} from "./utils/index.js";
|
|
21
|
-
import { scanDirOrArchive } from "./tarball.js";
|
|
22
|
-
import { packageMetadata } from "./npmRegistry.js";
|
|
23
|
-
import Dependency from "./class/dependency.class.js";
|
|
24
|
-
import Logger from "./class/logger.class.js";
|
|
25
|
-
|
|
26
|
-
const { version: packageVersion } = JSON.parse(
|
|
27
|
-
readFileSync(
|
|
28
|
-
new URL(path.join("..", "package.json"), import.meta.url)
|
|
29
|
-
)
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
export async function* searchDeepDependencies(packageName, gitURL, options) {
|
|
33
|
-
const { exclude, currDepth = 0, parent, maxDepth, registry } = options;
|
|
34
|
-
|
|
35
|
-
const { name, version, deprecated, ...pkg } = await pacote.manifest(gitURL ?? packageName, {
|
|
36
|
-
...NPM_TOKEN,
|
|
37
|
-
registry,
|
|
38
|
-
cache: `${os.homedir()}/.npm`
|
|
39
|
-
});
|
|
40
|
-
const { dependencies, customResolvers, alias } = mergeDependencies(pkg);
|
|
41
|
-
|
|
42
|
-
const current = new Dependency(name, version, parent);
|
|
43
|
-
current.alias = Object.fromEntries(alias);
|
|
44
|
-
|
|
45
|
-
if (gitURL !== null) {
|
|
46
|
-
current.isGit(gitURL);
|
|
47
|
-
try {
|
|
48
|
-
await pacote.manifest(`${name}@${version}`, {
|
|
49
|
-
...NPM_TOKEN,
|
|
50
|
-
registry,
|
|
51
|
-
cache: `${os.homedir()}/.npm`
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
catch {
|
|
55
|
-
current.existOnRemoteRegistry = false;
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
current.addFlag("isDeprecated", deprecated === true);
|
|
59
|
-
current.addFlag("hasCustomResolver", customResolvers.size > 0);
|
|
60
|
-
current.addFlag("hasDependencies", dependencies.size > 0);
|
|
61
|
-
|
|
62
|
-
if (currDepth !== maxDepth) {
|
|
63
|
-
const config = {
|
|
64
|
-
exclude, currDepth: currDepth + 1, parent: current, maxDepth, registry
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const gitDependencies = iter.filter(customResolvers.entries(), ([, valueStr]) => isGitDependency(valueStr));
|
|
68
|
-
for (const [depName, valueStr] of gitDependencies) {
|
|
69
|
-
yield* searchDeepDependencies(depName, valueStr, config);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const depsNames = await Promise.all(iter.map(dependencies.entries(), getCleanDependencyName));
|
|
73
|
-
for (const [fullName, cleanName, isLatest] of depsNames) {
|
|
74
|
-
if (!isLatest) {
|
|
75
|
-
current.addFlag("hasOutdatedDependency");
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (exclude.has(cleanName)) {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const
|
|
97
|
-
current
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
current.
|
|
109
|
-
current.addFlag("
|
|
110
|
-
current.addFlag("
|
|
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
|
-
...iter.
|
|
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
|
-
* @param {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
.start(ScannerLoggerEvents.analysis.
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const
|
|
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
|
-
payload.
|
|
356
|
-
payload.
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
await
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
1
|
+
// Import Node.js Dependencies
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { readFileSync, promises as fs } from "fs";
|
|
4
|
+
import timers from "timers/promises";
|
|
5
|
+
import os from "os";
|
|
6
|
+
|
|
7
|
+
// Import Third-party Dependencies
|
|
8
|
+
import combineAsyncIterators from "combine-async-iterators";
|
|
9
|
+
import * as iter from "itertools";
|
|
10
|
+
import pacote from "pacote";
|
|
11
|
+
import Arborist from "@npmcli/arborist";
|
|
12
|
+
import Lock from "@slimio/lock";
|
|
13
|
+
import * as vuln from "@nodesecure/vuln";
|
|
14
|
+
import { ScannerLoggerEvents } from "./constants.js";
|
|
15
|
+
|
|
16
|
+
// Import Internal Dependencies
|
|
17
|
+
import {
|
|
18
|
+
mergeDependencies, getCleanDependencyName, getDependenciesWarnings, addMissingVersionFlags, isGitDependency,
|
|
19
|
+
NPM_TOKEN
|
|
20
|
+
} from "./utils/index.js";
|
|
21
|
+
import { scanDirOrArchive } from "./tarball.js";
|
|
22
|
+
import { packageMetadata } from "./npmRegistry.js";
|
|
23
|
+
import Dependency from "./class/dependency.class.js";
|
|
24
|
+
import Logger from "./class/logger.class.js";
|
|
25
|
+
|
|
26
|
+
const { version: packageVersion } = JSON.parse(
|
|
27
|
+
readFileSync(
|
|
28
|
+
new URL(path.join("..", "package.json"), import.meta.url)
|
|
29
|
+
)
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
export async function* searchDeepDependencies(packageName, gitURL, options) {
|
|
33
|
+
const { exclude, currDepth = 0, parent, maxDepth, registry } = options;
|
|
34
|
+
|
|
35
|
+
const { name, version, deprecated, ...pkg } = await pacote.manifest(gitURL ?? packageName, {
|
|
36
|
+
...NPM_TOKEN,
|
|
37
|
+
registry,
|
|
38
|
+
cache: `${os.homedir()}/.npm`
|
|
39
|
+
});
|
|
40
|
+
const { dependencies, customResolvers, alias } = mergeDependencies(pkg);
|
|
41
|
+
|
|
42
|
+
const current = new Dependency(name, version, parent);
|
|
43
|
+
current.alias = Object.fromEntries(alias);
|
|
44
|
+
|
|
45
|
+
if (gitURL !== null) {
|
|
46
|
+
current.isGit(gitURL);
|
|
47
|
+
try {
|
|
48
|
+
await pacote.manifest(`${name}@${version}`, {
|
|
49
|
+
...NPM_TOKEN,
|
|
50
|
+
registry,
|
|
51
|
+
cache: `${os.homedir()}/.npm`
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
catch {
|
|
55
|
+
current.existOnRemoteRegistry = false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
current.addFlag("isDeprecated", deprecated === true);
|
|
59
|
+
current.addFlag("hasCustomResolver", customResolvers.size > 0);
|
|
60
|
+
current.addFlag("hasDependencies", dependencies.size > 0);
|
|
61
|
+
|
|
62
|
+
if (currDepth !== maxDepth) {
|
|
63
|
+
const config = {
|
|
64
|
+
exclude, currDepth: currDepth + 1, parent: current, maxDepth, registry
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const gitDependencies = iter.filter(customResolvers.entries(), ([, valueStr]) => isGitDependency(valueStr));
|
|
68
|
+
for (const [depName, valueStr] of gitDependencies) {
|
|
69
|
+
yield* searchDeepDependencies(depName, valueStr, config);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const depsNames = await Promise.all(iter.map(dependencies.entries(), getCleanDependencyName));
|
|
73
|
+
for (const [fullName, cleanName, isLatest] of depsNames) {
|
|
74
|
+
if (!isLatest) {
|
|
75
|
+
current.addFlag("hasOutdatedDependency");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (exclude.has(cleanName)) {
|
|
79
|
+
current.addChildren();
|
|
80
|
+
exclude.get(cleanName).add(current.fullName);
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
exclude.set(cleanName, new Set([current.fullName]));
|
|
84
|
+
yield* searchDeepDependencies(fullName, null, config);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
yield current;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function* deepReadEdges(currentPackageName, options) {
|
|
93
|
+
const { to, parent, exclude, fullLockMode, includeDevDeps, registry } = options;
|
|
94
|
+
const { version, integrity = to.integrity } = to.package;
|
|
95
|
+
|
|
96
|
+
const updatedVersion = version === "*" || typeof version === "undefined" ? "latest" : version;
|
|
97
|
+
const current = new Dependency(currentPackageName, updatedVersion, parent);
|
|
98
|
+
current.dev = to.dev;
|
|
99
|
+
|
|
100
|
+
if (fullLockMode && !includeDevDeps) {
|
|
101
|
+
const { deprecated, _integrity, ...pkg } = await pacote.manifest(`${currentPackageName}@${updatedVersion}`, {
|
|
102
|
+
...NPM_TOKEN,
|
|
103
|
+
registry,
|
|
104
|
+
cache: `${os.homedir()}/.npm`
|
|
105
|
+
});
|
|
106
|
+
const { customResolvers, alias } = mergeDependencies(pkg);
|
|
107
|
+
|
|
108
|
+
current.alias = Object.fromEntries(alias);
|
|
109
|
+
current.addFlag("hasValidIntegrity", _integrity === integrity);
|
|
110
|
+
current.addFlag("isDeprecated");
|
|
111
|
+
current.addFlag("hasCustomResolver", customResolvers.size > 0);
|
|
112
|
+
|
|
113
|
+
if (isGitDependency(to.resolved)) {
|
|
114
|
+
current.isGit(to.resolved);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
current.addFlag("hasDependencies", to.edgesOut.size > 0);
|
|
118
|
+
|
|
119
|
+
for (const [packageName, { to: toNode }] of to.edgesOut) {
|
|
120
|
+
if (toNode === null || (!includeDevDeps && toNode.dev)) {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const cleanName = `${packageName}@${toNode.package.version}`;
|
|
124
|
+
|
|
125
|
+
if (exclude.has(cleanName)) {
|
|
126
|
+
current.addChildren();
|
|
127
|
+
exclude.get(cleanName).add(current.fullName);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
exclude.set(cleanName, new Set([current.fullName]));
|
|
131
|
+
yield* deepReadEdges(packageName, { parent: current, to: toNode, exclude, registry });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
yield current;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export async function* getRootDependencies(manifest, options) {
|
|
138
|
+
const {
|
|
139
|
+
maxDepth = 4, exclude,
|
|
140
|
+
usePackageLock, fullLockMode, includeDevDeps,
|
|
141
|
+
location,
|
|
142
|
+
registry
|
|
143
|
+
} = options;
|
|
144
|
+
|
|
145
|
+
const { dependencies, customResolvers, alias } = mergeDependencies(manifest, void 0);
|
|
146
|
+
const parent = new Dependency(manifest.name, manifest.version);
|
|
147
|
+
parent.alias = Object.fromEntries(alias);
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
await pacote.manifest(`${manifest.name}@${manifest.version}`, {
|
|
151
|
+
...NPM_TOKEN,
|
|
152
|
+
registry,
|
|
153
|
+
cache: `${os.homedir()}/.npm`
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
parent.existOnRemoteRegistry = false;
|
|
158
|
+
}
|
|
159
|
+
parent.addFlag("hasCustomResolver", customResolvers.size > 0);
|
|
160
|
+
parent.addFlag("hasDependencies", dependencies.size > 0);
|
|
161
|
+
|
|
162
|
+
let iterators;
|
|
163
|
+
if (usePackageLock) {
|
|
164
|
+
const arb = new Arborist({
|
|
165
|
+
...NPM_TOKEN,
|
|
166
|
+
path: location,
|
|
167
|
+
registry
|
|
168
|
+
});
|
|
169
|
+
let tree;
|
|
170
|
+
try {
|
|
171
|
+
await fs.access(path.join(location, "node_modules"));
|
|
172
|
+
tree = await arb.loadActual();
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
tree = await arb.loadVirtual();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
iterators = [
|
|
179
|
+
...iter
|
|
180
|
+
.filter(tree.edgesOut.entries(), ([, { to }]) => to !== null && (includeDevDeps ? true : (!to.dev || to.isWorkspace)))
|
|
181
|
+
.map(([packageName, { to }]) => [packageName, to.isWorkspace ? to.target : to])
|
|
182
|
+
.map(([packageName, to]) => deepReadEdges(packageName, {
|
|
183
|
+
to,
|
|
184
|
+
parent,
|
|
185
|
+
fullLockMode,
|
|
186
|
+
includeDevDeps,
|
|
187
|
+
exclude,
|
|
188
|
+
registry
|
|
189
|
+
}))
|
|
190
|
+
];
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
const configRef = { exclude, maxDepth, parent, registry };
|
|
194
|
+
iterators = [
|
|
195
|
+
...iter.filter(customResolvers.entries(), ([, valueStr]) => isGitDependency(valueStr))
|
|
196
|
+
.map(([depName, valueStr]) => searchDeepDependencies(depName, valueStr, configRef)),
|
|
197
|
+
...iter.map(dependencies.entries(), ([name, ver]) => searchDeepDependencies(`${name}@${ver}`, null, configRef))
|
|
198
|
+
];
|
|
199
|
+
}
|
|
200
|
+
for await (const dep of combineAsyncIterators({}, ...iterators)) {
|
|
201
|
+
yield dep;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Add root dependencies to the exclude Map (because the parent is not handled by searchDeepDependencies)
|
|
205
|
+
// if we skip this the code will fail to re-link properly dependencies in the following steps
|
|
206
|
+
const depsName = await Promise.all(iter.map(dependencies.entries(), getCleanDependencyName));
|
|
207
|
+
for (const [, fullRange, isLatest] of depsName) {
|
|
208
|
+
if (!isLatest) {
|
|
209
|
+
parent.addFlag("hasOutdatedDependency");
|
|
210
|
+
}
|
|
211
|
+
if (exclude.has(fullRange)) {
|
|
212
|
+
exclude.get(fullRange).add(parent.fullName);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
yield parent;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* @param {*} manifest
|
|
221
|
+
* @param {*} options
|
|
222
|
+
* @param {Logger} logger
|
|
223
|
+
*/
|
|
224
|
+
export async function depWalker(manifest, options = {}, logger = new Logger()) {
|
|
225
|
+
const {
|
|
226
|
+
forceRootAnalysis = false,
|
|
227
|
+
usePackageLock = false,
|
|
228
|
+
includeDevDeps = false,
|
|
229
|
+
fullLockMode = false,
|
|
230
|
+
maxDepth,
|
|
231
|
+
location,
|
|
232
|
+
vulnerabilityStrategy = vuln.strategies.NONE,
|
|
233
|
+
registry
|
|
234
|
+
} = options;
|
|
235
|
+
|
|
236
|
+
// Create TMP directory
|
|
237
|
+
const tmpLocation = await fs.mkdtemp(path.join(os.tmpdir(), "/"));
|
|
238
|
+
|
|
239
|
+
const payload = {
|
|
240
|
+
id: tmpLocation.slice(-6),
|
|
241
|
+
rootDependencyName: manifest.name,
|
|
242
|
+
scannerVersion: packageVersion,
|
|
243
|
+
vulnerabilityStrategy,
|
|
244
|
+
warnings: []
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// We are dealing with an exclude Map to avoid checking a package more than one time in searchDeepDependencies
|
|
248
|
+
const exclude = new Map();
|
|
249
|
+
const dependencies = new Map();
|
|
250
|
+
|
|
251
|
+
{
|
|
252
|
+
logger
|
|
253
|
+
.start(ScannerLoggerEvents.analysis.tree)
|
|
254
|
+
.start(ScannerLoggerEvents.analysis.tarball)
|
|
255
|
+
.start(ScannerLoggerEvents.analysis.registry);
|
|
256
|
+
const fetchedMetadataPackages = new Set();
|
|
257
|
+
const promisesToWait = [];
|
|
258
|
+
|
|
259
|
+
const tarballLocker = new Lock({ maxConcurrent: 5 });
|
|
260
|
+
tarballLocker.on("freeOne", () => logger.tick(ScannerLoggerEvents.analysis.tarball));
|
|
261
|
+
|
|
262
|
+
const rootDepsOptions = { maxDepth, exclude, usePackageLock, fullLockMode, includeDevDeps, location, registry };
|
|
263
|
+
for await (const currentDep of getRootDependencies(manifest, rootDepsOptions)) {
|
|
264
|
+
const { name, version, dev } = currentDep;
|
|
265
|
+
const current = currentDep.exportAsPlainObject(name === manifest.name ? 0 : void 0);
|
|
266
|
+
let proceedDependencyAnalysis = true;
|
|
267
|
+
|
|
268
|
+
if (dependencies.has(name)) {
|
|
269
|
+
// TODO: how to handle different metadata ?
|
|
270
|
+
const dep = dependencies.get(name);
|
|
271
|
+
|
|
272
|
+
const currVersion = Object.keys(current.versions)[0];
|
|
273
|
+
if (currVersion in dep.versions) {
|
|
274
|
+
// The dependency has already entered the analysis
|
|
275
|
+
// This happens if the package is used by multiple packages in the tree
|
|
276
|
+
proceedDependencyAnalysis = false;
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
dep.versions[currVersion] = current.versions[currVersion];
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
dependencies.set(name, current);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// If the dependency is a DevDependencies we ignore it.
|
|
287
|
+
if (dev) {
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (proceedDependencyAnalysis) {
|
|
292
|
+
logger.tick(ScannerLoggerEvents.analysis.tree);
|
|
293
|
+
|
|
294
|
+
// There is no need to fetch 'N' times the npm metadata for the same package.
|
|
295
|
+
if (fetchedMetadataPackages.has(name)) {
|
|
296
|
+
logger.tick(ScannerLoggerEvents.analysis.registry);
|
|
297
|
+
}
|
|
298
|
+
else {
|
|
299
|
+
fetchedMetadataPackages.add(name);
|
|
300
|
+
promisesToWait.push(packageMetadata(name, version, {
|
|
301
|
+
ref: current,
|
|
302
|
+
logger
|
|
303
|
+
}));
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
promisesToWait.push(scanDirOrArchive(name, version, {
|
|
307
|
+
ref: current.versions[version],
|
|
308
|
+
location,
|
|
309
|
+
tmpLocation: forceRootAnalysis && name === manifest.name ? null : tmpLocation,
|
|
310
|
+
locker: tarballLocker,
|
|
311
|
+
logger,
|
|
312
|
+
registry
|
|
313
|
+
}));
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
logger.end(ScannerLoggerEvents.analysis.tree);
|
|
318
|
+
|
|
319
|
+
// Wait for all extraction to be done!
|
|
320
|
+
await Promise.allSettled(promisesToWait);
|
|
321
|
+
await timers.setImmediate();
|
|
322
|
+
|
|
323
|
+
logger.end(ScannerLoggerEvents.analysis.tarball).end(ScannerLoggerEvents.analysis.registry);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const { hydratePayloadDependencies, strategy } = await vuln.setStrategy(vulnerabilityStrategy);
|
|
327
|
+
await hydratePayloadDependencies(dependencies, {
|
|
328
|
+
useStandardFormat: true,
|
|
329
|
+
path: location
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
payload.vulnerabilityStrategy = strategy;
|
|
333
|
+
|
|
334
|
+
// We do this because it "seem" impossible to link all dependencies in the first walk.
|
|
335
|
+
// Because we are dealing with package only one time it may happen sometimes.
|
|
336
|
+
for (const [packageName, dependency] of dependencies) {
|
|
337
|
+
for (const [verStr, verDescriptor] of Object.entries(dependency.versions)) {
|
|
338
|
+
verDescriptor.flags.push(...addMissingVersionFlags(new Set(verDescriptor.flags), dependency));
|
|
339
|
+
|
|
340
|
+
const usedDeps = exclude.get(`${packageName}@${verStr}`) || new Set();
|
|
341
|
+
if (usedDeps.size === 0) {
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const usedBy = Object.create(null);
|
|
346
|
+
for (const [name, version] of [...usedDeps].map((name) => name.split(" "))) {
|
|
347
|
+
usedBy[name] = version;
|
|
348
|
+
}
|
|
349
|
+
Object.assign(verDescriptor.usedBy, usedBy);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
try {
|
|
354
|
+
const { warnings, flaggedAuthors } = await getDependenciesWarnings(dependencies);
|
|
355
|
+
payload.warnings = warnings;
|
|
356
|
+
payload.flaggedAuthors = flaggedAuthors;
|
|
357
|
+
payload.dependencies = Object.fromEntries(dependencies);
|
|
358
|
+
|
|
359
|
+
return payload;
|
|
360
|
+
}
|
|
361
|
+
finally {
|
|
362
|
+
await timers.setImmediate();
|
|
363
|
+
await fs.rm(tmpLocation, { recursive: true, force: true });
|
|
364
|
+
|
|
365
|
+
logger.emit(ScannerLoggerEvents.done);
|
|
366
|
+
}
|
|
367
|
+
}
|