@nodesecure/scanner 3.4.1 → 3.7.0
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/README.md +3 -1
- package/index.js +14 -3
- package/package.json +21 -21
- package/src/class/dependency.class.js +5 -1
- package/src/depWalker.js +23 -14
- package/src/manifest.js +63 -57
- package/src/npmRegistry.js +74 -68
- package/src/tarball.js +12 -8
- package/src/utils/analyzeDependencies.js +71 -40
- package/src/utils/warnings.js +36 -36
- package/types/scanner.d.ts +11 -0
package/README.md
CHANGED
|
@@ -59,6 +59,7 @@ function verify(packageName?: string | null): Promise<Scanner.VerifyPayload>;
|
|
|
59
59
|
```ts
|
|
60
60
|
interface Options {
|
|
61
61
|
readonly maxDepth?: number;
|
|
62
|
+
readonly registry?: string | URL;
|
|
62
63
|
readonly usePackageLock?: boolean;
|
|
63
64
|
readonly includeDevDeps?: boolean;
|
|
64
65
|
readonly vulnerabilityStrategy: Strategy.Kind;
|
|
@@ -70,7 +71,7 @@ interface Options {
|
|
|
70
71
|
## Contributors ✨
|
|
71
72
|
|
|
72
73
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
73
|
-
[](#contributors-)
|
|
74
75
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
75
76
|
|
|
76
77
|
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
|
|
@@ -85,6 +86,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
|
|
85
86
|
<td align="center"><a href="https://mickaelcroquet.fr"><img src="https://avatars.githubusercontent.com/u/23740372?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Haze</b></sub></a><br /><a href="https://github.com/NodeSecure/scanner/commits?author=CroquetMickael" title="Code">💻</a></td>
|
|
86
87
|
<td align="center"><a href="https://github.com/mbalabash"><img src="https://avatars.githubusercontent.com/u/16868922?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Maksim Balabash</b></sub></a><br /><a href="https://github.com/NodeSecure/scanner/commits?author=mbalabash" title="Code">💻</a></td>
|
|
87
88
|
<td align="center"><a href="https://dev.to/antoinecoulon"><img src="https://avatars.githubusercontent.com/u/43391199?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Antoine Coulon</b></sub></a><br /><a href="https://github.com/NodeSecure/scanner/commits?author=antoine-coulon" title="Code">💻</a> <a href="#security-antoine-coulon" title="Security">🛡️</a></td>
|
|
89
|
+
<td align="center"><a href="https://www.linkedin.com/in/nicolas-hallaert/"><img src="https://avatars.githubusercontent.com/u/39910164?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nicolas Hallaert</b></sub></a><br /><a href="https://github.com/NodeSecure/scanner/commits?author=Rossb0b" title="Code">💻</a></td>
|
|
88
90
|
</tr>
|
|
89
91
|
</table>
|
|
90
92
|
|
package/index.js
CHANGED
|
@@ -19,7 +19,16 @@ import * as tarball from "./src/tarball.js";
|
|
|
19
19
|
const kDefaultCwdOptions = { forceRootAnalysis: true, usePackageLock: true, includeDevDeps: false };
|
|
20
20
|
|
|
21
21
|
export async function cwd(location = process.cwd(), options = {}, logger = new Logger()) {
|
|
22
|
-
const
|
|
22
|
+
const registry = options.registry ? new URL(options.registry).toString() : getLocalRegistryURL();
|
|
23
|
+
|
|
24
|
+
const finalizedOptions = Object.assign(
|
|
25
|
+
{ location },
|
|
26
|
+
kDefaultCwdOptions,
|
|
27
|
+
{
|
|
28
|
+
...options,
|
|
29
|
+
registry
|
|
30
|
+
}
|
|
31
|
+
);
|
|
23
32
|
|
|
24
33
|
logger.start(ScannerLoggerEvents.manifest.read);
|
|
25
34
|
const packagePath = path.join(location, "package.json");
|
|
@@ -30,13 +39,15 @@ export async function cwd(location = process.cwd(), options = {}, logger = new L
|
|
|
30
39
|
}
|
|
31
40
|
|
|
32
41
|
export async function from(packageName, options, logger = new Logger()) {
|
|
42
|
+
const registry = options.registry ? new URL(options.registry).toString() : getLocalRegistryURL();
|
|
43
|
+
|
|
33
44
|
logger.start(ScannerLoggerEvents.manifest.fetch);
|
|
34
45
|
const manifest = await pacote.manifest(packageName, {
|
|
35
|
-
...NPM_TOKEN, registry
|
|
46
|
+
...NPM_TOKEN, registry, cache: `${os.homedir()}/.npm`
|
|
36
47
|
});
|
|
37
48
|
logger.end(ScannerLoggerEvents.manifest.fetch);
|
|
38
49
|
|
|
39
|
-
return depWalker(manifest, options, logger);
|
|
50
|
+
return depWalker(manifest, Object.assign(options, { registry }), logger);
|
|
40
51
|
}
|
|
41
52
|
|
|
42
53
|
export async function verify(packageName = null) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nodesecure/scanner",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.7.0",
|
|
4
4
|
"description": "A package API to run a static analysis of your module's dependencies.",
|
|
5
5
|
"exports": "./index.js",
|
|
6
6
|
"engines": {
|
|
@@ -48,39 +48,39 @@
|
|
|
48
48
|
},
|
|
49
49
|
"homepage": "https://github.com/NodeSecure/scanner#readme",
|
|
50
50
|
"devDependencies": {
|
|
51
|
-
"@nodesecure/eslint-config": "^1.
|
|
51
|
+
"@nodesecure/eslint-config": "^1.4.1",
|
|
52
52
|
"@slimio/is": "^1.5.1",
|
|
53
|
-
"@small-tech/esm-tape-runner": "^
|
|
54
|
-
"@small-tech/tap-monkey": "^1.
|
|
55
|
-
"@types/node": "^
|
|
56
|
-
"c8": "^7.11.
|
|
53
|
+
"@small-tech/esm-tape-runner": "^2.0.0",
|
|
54
|
+
"@small-tech/tap-monkey": "^1.4.0",
|
|
55
|
+
"@types/node": "^18.0.0",
|
|
56
|
+
"c8": "^7.11.3",
|
|
57
57
|
"cross-env": "^7.0.3",
|
|
58
|
-
"dotenv": "^16.0.
|
|
59
|
-
"eslint": "^8.
|
|
58
|
+
"dotenv": "^16.0.1",
|
|
59
|
+
"eslint": "^8.18.0",
|
|
60
60
|
"get-folder-size": "^3.1.0",
|
|
61
|
-
"pkg-ok": "^
|
|
62
|
-
"sinon": "^
|
|
61
|
+
"pkg-ok": "^3.0.0",
|
|
62
|
+
"sinon": "^14.0.0",
|
|
63
63
|
"snap-shot-core": "^10.2.4",
|
|
64
|
-
"tape": "^5.5.
|
|
64
|
+
"tape": "^5.5.3"
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
-
"@nodesecure/flags": "^2.
|
|
67
|
+
"@nodesecure/flags": "^2.4.0",
|
|
68
68
|
"@nodesecure/fs-walk": "^1.0.0",
|
|
69
|
-
"@nodesecure/i18n": "^
|
|
70
|
-
"@nodesecure/js-x-ray": "^
|
|
71
|
-
"@nodesecure/npm-registry-sdk": "^1.
|
|
69
|
+
"@nodesecure/i18n": "^2.0.0",
|
|
70
|
+
"@nodesecure/js-x-ray": "^5.0.1",
|
|
71
|
+
"@nodesecure/npm-registry-sdk": "^1.4.0",
|
|
72
72
|
"@nodesecure/ntlp": "^2.1.0",
|
|
73
73
|
"@nodesecure/utils": "^1.0.0",
|
|
74
|
-
"@nodesecure/vuln": "^1.
|
|
75
|
-
"@npm/types": "^1.0.
|
|
76
|
-
"@npmcli/arborist": "^5.
|
|
74
|
+
"@nodesecure/vuln": "^1.7.0",
|
|
75
|
+
"@npm/types": "^1.0.2",
|
|
76
|
+
"@npmcli/arborist": "^5.2.3",
|
|
77
77
|
"@slimio/lock": "^1.0.0",
|
|
78
|
-
"builtins": "^
|
|
78
|
+
"builtins": "^5.0.1",
|
|
79
79
|
"combine-async-iterators": "^2.0.1",
|
|
80
80
|
"itertools": "^1.7.1",
|
|
81
81
|
"lodash.difference": "^4.5.0",
|
|
82
|
-
"pacote": "^13.
|
|
83
|
-
"semver": "^7.3.
|
|
82
|
+
"pacote": "^13.6.1",
|
|
83
|
+
"semver": "^7.3.7"
|
|
84
84
|
},
|
|
85
85
|
"type": "module"
|
|
86
86
|
}
|
|
@@ -66,6 +66,9 @@ export default class Dependency {
|
|
|
66
66
|
description: "",
|
|
67
67
|
size: 0,
|
|
68
68
|
author: {},
|
|
69
|
+
engines: {},
|
|
70
|
+
repository: {},
|
|
71
|
+
scripts: {},
|
|
69
72
|
warnings: this.warnings,
|
|
70
73
|
composition: {
|
|
71
74
|
extensions: [],
|
|
@@ -75,7 +78,8 @@ export default class Dependency {
|
|
|
75
78
|
missing: [],
|
|
76
79
|
required_files: [],
|
|
77
80
|
required_nodejs: [],
|
|
78
|
-
required_thirdparty: []
|
|
81
|
+
required_thirdparty: [],
|
|
82
|
+
required_subpath: []
|
|
79
83
|
},
|
|
80
84
|
license: "unkown license",
|
|
81
85
|
gitUrl: this.gitUrl
|
package/src/depWalker.js
CHANGED
|
@@ -11,7 +11,6 @@ import pacote from "pacote";
|
|
|
11
11
|
import Arborist from "@npmcli/arborist";
|
|
12
12
|
import Lock from "@slimio/lock";
|
|
13
13
|
import * as vuln from "@nodesecure/vuln";
|
|
14
|
-
import { getLocalRegistryURL } from "@nodesecure/npm-registry-sdk";
|
|
15
14
|
import { ScannerLoggerEvents } from "./constants.js";
|
|
16
15
|
|
|
17
16
|
// Import Internal Dependencies
|
|
@@ -31,11 +30,11 @@ const { version: packageVersion } = JSON.parse(
|
|
|
31
30
|
);
|
|
32
31
|
|
|
33
32
|
export async function* searchDeepDependencies(packageName, gitURL, options) {
|
|
34
|
-
const { exclude, currDepth = 0, parent, maxDepth } = options;
|
|
33
|
+
const { exclude, currDepth = 0, parent, maxDepth, registry } = options;
|
|
35
34
|
|
|
36
35
|
const { name, version, deprecated, ...pkg } = await pacote.manifest(gitURL ?? packageName, {
|
|
37
36
|
...NPM_TOKEN,
|
|
38
|
-
registry
|
|
37
|
+
registry,
|
|
39
38
|
cache: `${os.homedir()}/.npm`
|
|
40
39
|
});
|
|
41
40
|
const { dependencies, customResolvers } = mergeDependencies(pkg);
|
|
@@ -48,7 +47,7 @@ export async function* searchDeepDependencies(packageName, gitURL, options) {
|
|
|
48
47
|
|
|
49
48
|
if (currDepth !== maxDepth) {
|
|
50
49
|
const config = {
|
|
51
|
-
exclude, currDepth: currDepth + 1, parent: current, maxDepth
|
|
50
|
+
exclude, currDepth: currDepth + 1, parent: current, maxDepth, registry
|
|
52
51
|
};
|
|
53
52
|
|
|
54
53
|
const gitDependencies = iter.filter(customResolvers.entries(), ([, valueStr]) => isGitDependency(valueStr));
|
|
@@ -76,7 +75,7 @@ export async function* searchDeepDependencies(packageName, gitURL, options) {
|
|
|
76
75
|
}
|
|
77
76
|
|
|
78
77
|
export async function* deepReadEdges(currentPackageName, options) {
|
|
79
|
-
const { to, parent, exclude, fullLockMode, includeDevDeps } = options;
|
|
78
|
+
const { to, parent, exclude, fullLockMode, includeDevDeps, registry } = options;
|
|
80
79
|
const { version, integrity = to.integrity } = to.package;
|
|
81
80
|
|
|
82
81
|
const updatedVersion = version === "*" || typeof version === "undefined" ? "latest" : version;
|
|
@@ -86,7 +85,7 @@ export async function* deepReadEdges(currentPackageName, options) {
|
|
|
86
85
|
if (fullLockMode && !includeDevDeps) {
|
|
87
86
|
const { deprecated, _integrity, ...pkg } = await pacote.manifest(`${currentPackageName}@${updatedVersion}`, {
|
|
88
87
|
...NPM_TOKEN,
|
|
89
|
-
registry
|
|
88
|
+
registry,
|
|
90
89
|
cache: `${os.homedir()}/.npm`
|
|
91
90
|
});
|
|
92
91
|
const { customResolvers } = mergeDependencies(pkg);
|
|
@@ -108,7 +107,7 @@ export async function* deepReadEdges(currentPackageName, options) {
|
|
|
108
107
|
}
|
|
109
108
|
else {
|
|
110
109
|
exclude.set(cleanName, new Set([current.fullName]));
|
|
111
|
-
yield* deepReadEdges(packageName, { parent: current, to: toNode, exclude });
|
|
110
|
+
yield* deepReadEdges(packageName, { parent: current, to: toNode, exclude, registry });
|
|
112
111
|
}
|
|
113
112
|
}
|
|
114
113
|
yield current;
|
|
@@ -118,7 +117,8 @@ export async function* getRootDependencies(manifest, options) {
|
|
|
118
117
|
const {
|
|
119
118
|
maxDepth = 4, exclude,
|
|
120
119
|
usePackageLock, fullLockMode, includeDevDeps,
|
|
121
|
-
location
|
|
120
|
+
location,
|
|
121
|
+
registry
|
|
122
122
|
} = options;
|
|
123
123
|
|
|
124
124
|
const { dependencies, customResolvers } = mergeDependencies(manifest, void 0);
|
|
@@ -131,7 +131,7 @@ export async function* getRootDependencies(manifest, options) {
|
|
|
131
131
|
const arb = new Arborist({
|
|
132
132
|
...NPM_TOKEN,
|
|
133
133
|
path: location,
|
|
134
|
-
registry
|
|
134
|
+
registry
|
|
135
135
|
});
|
|
136
136
|
let tree;
|
|
137
137
|
try {
|
|
@@ -146,11 +146,18 @@ export async function* getRootDependencies(manifest, options) {
|
|
|
146
146
|
...iter
|
|
147
147
|
.filter(tree.edgesOut.entries(), ([, { to }]) => to !== null && (includeDevDeps ? true : (!to.dev || to.isWorkspace)))
|
|
148
148
|
.map(([packageName, { to }]) => [packageName, to.isWorkspace ? to.target : to])
|
|
149
|
-
.map(([packageName, to]) => deepReadEdges(packageName, {
|
|
149
|
+
.map(([packageName, to]) => deepReadEdges(packageName, {
|
|
150
|
+
to,
|
|
151
|
+
parent,
|
|
152
|
+
fullLockMode,
|
|
153
|
+
includeDevDeps,
|
|
154
|
+
exclude,
|
|
155
|
+
registry
|
|
156
|
+
}))
|
|
150
157
|
];
|
|
151
158
|
}
|
|
152
159
|
else {
|
|
153
|
-
const configRef = { exclude, maxDepth, parent };
|
|
160
|
+
const configRef = { exclude, maxDepth, parent, registry };
|
|
154
161
|
iterators = [
|
|
155
162
|
...iter.filter(customResolvers.entries(), ([, valueStr]) => isGitDependency(valueStr))
|
|
156
163
|
.map(([depName, valueStr]) => searchDeepDependencies(depName, valueStr, configRef)),
|
|
@@ -189,7 +196,8 @@ export async function depWalker(manifest, options = {}, logger = new Logger()) {
|
|
|
189
196
|
fullLockMode = false,
|
|
190
197
|
maxDepth,
|
|
191
198
|
location,
|
|
192
|
-
vulnerabilityStrategy = vuln.strategies.NONE
|
|
199
|
+
vulnerabilityStrategy = vuln.strategies.NONE,
|
|
200
|
+
registry
|
|
193
201
|
} = options;
|
|
194
202
|
|
|
195
203
|
// Create TMP directory
|
|
@@ -218,7 +226,7 @@ export async function depWalker(manifest, options = {}, logger = new Logger()) {
|
|
|
218
226
|
const tarballLocker = new Lock({ maxConcurrent: 5 });
|
|
219
227
|
tarballLocker.on("freeOne", () => logger.tick(ScannerLoggerEvents.analysis.tarball));
|
|
220
228
|
|
|
221
|
-
const rootDepsOptions = { maxDepth, exclude, usePackageLock, fullLockMode, includeDevDeps, location };
|
|
229
|
+
const rootDepsOptions = { maxDepth, exclude, usePackageLock, fullLockMode, includeDevDeps, location, registry };
|
|
222
230
|
for await (const currentDep of getRootDependencies(manifest, rootDepsOptions)) {
|
|
223
231
|
const { name, version, dev } = currentDep;
|
|
224
232
|
const current = currentDep.exportAsPlainObject(name === manifest.name ? 0 : void 0);
|
|
@@ -267,7 +275,8 @@ export async function depWalker(manifest, options = {}, logger = new Logger()) {
|
|
|
267
275
|
location,
|
|
268
276
|
tmpLocation: forceRootAnalysis && name === manifest.name ? null : tmpLocation,
|
|
269
277
|
locker: tarballLocker,
|
|
270
|
-
logger
|
|
278
|
+
logger,
|
|
279
|
+
registry
|
|
271
280
|
}));
|
|
272
281
|
}
|
|
273
282
|
}
|
package/src/manifest.js
CHANGED
|
@@ -1,57 +1,63 @@
|
|
|
1
|
-
// Import Node.js Dependencies
|
|
2
|
-
import fs from "fs/promises";
|
|
3
|
-
import path from "path";
|
|
4
|
-
|
|
5
|
-
// Import Third-party Dependencies
|
|
6
|
-
import { parseManifestAuthor } from "@nodesecure/utils";
|
|
7
|
-
|
|
8
|
-
// CONSTANTS
|
|
9
|
-
// PR welcome to contribute to this list!
|
|
10
|
-
const kNativeNpmPackages = new Set([
|
|
11
|
-
"node-gyp", "node-pre-gyp", "node-gyp-build", "node-addon-api"
|
|
12
|
-
]);
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @see https://www.nerdycode.com/prevent-npm-executing-scripts-security/
|
|
16
|
-
*/
|
|
17
|
-
const kUnsafeNpmScripts = new Set([
|
|
18
|
-
"install",
|
|
19
|
-
"preinstall", "postinstall",
|
|
20
|
-
"preuninstall", "postuninstall"
|
|
21
|
-
]);
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @param {!string} location
|
|
25
|
-
* @returns {import("@npm/types").PackageJson}
|
|
26
|
-
*/
|
|
27
|
-
export async function read(location) {
|
|
28
|
-
const packageStr = await fs.readFile(
|
|
29
|
-
path.join(location, "package.json"),
|
|
30
|
-
"utf-8"
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
return JSON.parse(packageStr);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
1
|
+
// Import Node.js Dependencies
|
|
2
|
+
import fs from "fs/promises";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
// Import Third-party Dependencies
|
|
6
|
+
import { parseManifestAuthor } from "@nodesecure/utils";
|
|
7
|
+
|
|
8
|
+
// CONSTANTS
|
|
9
|
+
// PR welcome to contribute to this list!
|
|
10
|
+
const kNativeNpmPackages = new Set([
|
|
11
|
+
"node-gyp", "node-pre-gyp", "node-gyp-build", "node-addon-api"
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @see https://www.nerdycode.com/prevent-npm-executing-scripts-security/
|
|
16
|
+
*/
|
|
17
|
+
const kUnsafeNpmScripts = new Set([
|
|
18
|
+
"install",
|
|
19
|
+
"preinstall", "postinstall",
|
|
20
|
+
"preuninstall", "postuninstall"
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {!string} location
|
|
25
|
+
* @returns {import("@npm/types").PackageJson}
|
|
26
|
+
*/
|
|
27
|
+
export async function read(location) {
|
|
28
|
+
const packageStr = await fs.readFile(
|
|
29
|
+
path.join(location, "package.json"),
|
|
30
|
+
"utf-8"
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
return JSON.parse(packageStr);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function readAnalyze(location) {
|
|
37
|
+
const {
|
|
38
|
+
description = "", author = {}, scripts = {},
|
|
39
|
+
dependencies = {}, devDependencies = {}, gypfile = false,
|
|
40
|
+
engines = {},
|
|
41
|
+
repository = {},
|
|
42
|
+
imports = {}
|
|
43
|
+
} = await read(location);
|
|
44
|
+
|
|
45
|
+
const packageDeps = Object.keys(dependencies);
|
|
46
|
+
const packageDevDeps = Object.keys(devDependencies);
|
|
47
|
+
const hasNativePackage = [...packageDevDeps, ...packageDeps]
|
|
48
|
+
.some((pkg) => kNativeNpmPackages.has(pkg));
|
|
49
|
+
|
|
50
|
+
return {
|
|
51
|
+
author: typeof author === "string" ? parseManifestAuthor(author) : author,
|
|
52
|
+
description,
|
|
53
|
+
engines,
|
|
54
|
+
repository,
|
|
55
|
+
scripts,
|
|
56
|
+
hasScript: Object.keys(scripts)
|
|
57
|
+
.some((value) => kUnsafeNpmScripts.has(value.toLowerCase())),
|
|
58
|
+
packageDeps,
|
|
59
|
+
packageDevDeps,
|
|
60
|
+
nodejs: { imports },
|
|
61
|
+
hasNativeElements: hasNativePackage || gypfile
|
|
62
|
+
};
|
|
63
|
+
}
|
package/src/npmRegistry.js
CHANGED
|
@@ -1,68 +1,74 @@
|
|
|
1
|
-
// Import Third-party Dependencies
|
|
2
|
-
import semver from "semver";
|
|
3
|
-
import { parseManifestAuthor } from "@nodesecure/utils";
|
|
4
|
-
import { packument } from "@nodesecure/npm-registry-sdk";
|
|
5
|
-
|
|
6
|
-
export function parseAuthor(author) {
|
|
7
|
-
return typeof author === "string" ? parseManifestAuthor(author) : author;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export async function packageMetadata(name, version, options) {
|
|
11
|
-
const { ref, logger } = options;
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
const pkg = await packument(name);
|
|
15
|
-
|
|
16
|
-
const oneYearFromToday = new Date();
|
|
17
|
-
oneYearFromToday.setFullYear(oneYearFromToday.getFullYear() - 1);
|
|
18
|
-
|
|
19
|
-
const lastVersion = pkg["dist-tags"].latest;
|
|
20
|
-
const lastUpdateAt = new Date(pkg.time[lastVersion]);
|
|
21
|
-
const metadata = {
|
|
22
|
-
author: parseAuthor(pkg.author) ?? {},
|
|
23
|
-
homepage: pkg.homepage || null,
|
|
24
|
-
publishedCount: Object.values(pkg.versions).length,
|
|
25
|
-
lastVersion,
|
|
26
|
-
lastUpdateAt,
|
|
27
|
-
hasReceivedUpdateInOneYear: !(oneYearFromToday > lastUpdateAt),
|
|
28
|
-
maintainers: pkg.maintainers,
|
|
29
|
-
publishers: []
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
const isOutdated = semver.neq(version, lastVersion);
|
|
33
|
-
if (isOutdated) {
|
|
34
|
-
ref.versions[version].flags.push("isOutdated");
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const publishers = new Set();
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
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
|
-
|
|
1
|
+
// Import Third-party Dependencies
|
|
2
|
+
import semver from "semver";
|
|
3
|
+
import { parseManifestAuthor } from "@nodesecure/utils";
|
|
4
|
+
import { packument } from "@nodesecure/npm-registry-sdk";
|
|
5
|
+
|
|
6
|
+
export function parseAuthor(author) {
|
|
7
|
+
return typeof author === "string" ? parseManifestAuthor(author) : author;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export async function packageMetadata(name, version, options) {
|
|
11
|
+
const { ref, logger } = options;
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const pkg = await packument(name);
|
|
15
|
+
|
|
16
|
+
const oneYearFromToday = new Date();
|
|
17
|
+
oneYearFromToday.setFullYear(oneYearFromToday.getFullYear() - 1);
|
|
18
|
+
|
|
19
|
+
const lastVersion = pkg["dist-tags"].latest;
|
|
20
|
+
const lastUpdateAt = new Date(pkg.time[lastVersion]);
|
|
21
|
+
const metadata = {
|
|
22
|
+
author: parseAuthor(pkg.author) ?? {},
|
|
23
|
+
homepage: pkg.homepage || null,
|
|
24
|
+
publishedCount: Object.values(pkg.versions).length,
|
|
25
|
+
lastVersion,
|
|
26
|
+
lastUpdateAt,
|
|
27
|
+
hasReceivedUpdateInOneYear: !(oneYearFromToday > lastUpdateAt),
|
|
28
|
+
maintainers: pkg.maintainers ?? [],
|
|
29
|
+
publishers: []
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const isOutdated = semver.neq(version, lastVersion);
|
|
33
|
+
if (isOutdated) {
|
|
34
|
+
ref.versions[version].flags.push("isOutdated");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const publishers = new Set();
|
|
38
|
+
let searchForMaintainersInVersions = metadata.maintainers.length === 0;
|
|
39
|
+
for (const ver of Object.values(pkg.versions).reverse()) {
|
|
40
|
+
const { _npmUser: npmUser, version, maintainers = [] } = ver;
|
|
41
|
+
const isNullOrUndefined = typeof npmUser === "undefined" || npmUser === null;
|
|
42
|
+
if (isNullOrUndefined || !("name" in npmUser) || typeof npmUser.name !== "string") {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const authorName = metadata.author?.name ?? null;
|
|
47
|
+
if (authorName === null) {
|
|
48
|
+
metadata.author = npmUser;
|
|
49
|
+
}
|
|
50
|
+
else if (npmUser.name !== metadata.author.name) {
|
|
51
|
+
metadata.hasManyPublishers = true;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// TODO: add npmUser.email
|
|
55
|
+
if (!publishers.has(npmUser.name)) {
|
|
56
|
+
publishers.add(npmUser.name);
|
|
57
|
+
metadata.publishers.push({ ...npmUser, version, at: new Date(pkg.time[version]) });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (searchForMaintainersInVersions) {
|
|
61
|
+
metadata.maintainers.push(...maintainers);
|
|
62
|
+
searchForMaintainersInVersions = false;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
Object.assign(ref.metadata, metadata);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// ignore
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
logger.tick("registry");
|
|
73
|
+
}
|
|
74
|
+
}
|
package/src/tarball.js
CHANGED
|
@@ -14,7 +14,6 @@ import {
|
|
|
14
14
|
NPM_TOKEN
|
|
15
15
|
} from "./utils/index.js";
|
|
16
16
|
import * as manifest from "./manifest.js";
|
|
17
|
-
import { getLocalRegistryURL } from "@nodesecure/npm-registry-sdk";
|
|
18
17
|
|
|
19
18
|
// CONSTANTS
|
|
20
19
|
const kNativeCodeExtensions = new Set([".gyp", ".c", ".cpp", ".node", ".so", ".h"]);
|
|
@@ -47,7 +46,7 @@ export async function scanJavascriptFile(dest, file, packageName) {
|
|
|
47
46
|
}
|
|
48
47
|
|
|
49
48
|
export async function scanDirOrArchive(name, version, options) {
|
|
50
|
-
const { ref, location = process.cwd(), tmpLocation, locker } = options;
|
|
49
|
+
const { ref, location = process.cwd(), tmpLocation, locker, registry } = options;
|
|
51
50
|
|
|
52
51
|
const isNpmTarball = !(tmpLocation === null);
|
|
53
52
|
const dest = isNpmTarball ? path.join(tmpLocation, `${name}@${version}`) : location;
|
|
@@ -58,16 +57,20 @@ export async function scanDirOrArchive(name, version, options) {
|
|
|
58
57
|
if (isNpmTarball) {
|
|
59
58
|
await pacote.extract(ref.flags.includes("isGit") ? ref.gitUrl : `${name}@${version}`, dest, {
|
|
60
59
|
...NPM_TOKEN,
|
|
61
|
-
registry
|
|
60
|
+
registry,
|
|
62
61
|
cache: `${os.homedir()}/.npm`
|
|
63
62
|
});
|
|
64
63
|
await timers.setImmediate();
|
|
65
64
|
}
|
|
66
65
|
|
|
67
66
|
// Read the package.json at the root of the directory or archive.
|
|
68
|
-
const {
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
const {
|
|
68
|
+
packageDeps,
|
|
69
|
+
packageDevDeps,
|
|
70
|
+
author, description, hasScript, hasNativeElements, nodejs,
|
|
71
|
+
engines, repository, scripts
|
|
72
|
+
} = await manifest.readAnalyze(dest);
|
|
73
|
+
Object.assign(ref, { author, description, engines, repository, scripts });
|
|
71
74
|
|
|
72
75
|
// Get the composition of the (extracted) directory
|
|
73
76
|
const { ext, files, size } = await getTarballComposition(dest);
|
|
@@ -97,10 +100,11 @@ export async function scanDirOrArchive(name, version, options) {
|
|
|
97
100
|
const minifiedFiles = fileAnalysisResults.filter((row) => row.isMinified).flatMap((row) => row.file);
|
|
98
101
|
|
|
99
102
|
const {
|
|
100
|
-
nodeDependencies, thirdPartyDependencies, missingDependencies, unusedDependencies, flags
|
|
101
|
-
} = analyzeDependencies(dependencies, { packageDeps, packageDevDeps, tryDependencies });
|
|
103
|
+
nodeDependencies, thirdPartyDependencies, subpathImportsDependencies, missingDependencies, unusedDependencies, flags
|
|
104
|
+
} = analyzeDependencies(dependencies, { packageDeps, packageDevDeps, tryDependencies, nodeImports: nodejs.imports });
|
|
102
105
|
|
|
103
106
|
ref.composition.required_thirdparty = thirdPartyDependencies;
|
|
107
|
+
ref.composition.required_subpath = Object.fromEntries(subpathImportsDependencies);
|
|
104
108
|
ref.composition.unused.push(...unusedDependencies);
|
|
105
109
|
ref.composition.missing.push(...missingDependencies);
|
|
106
110
|
ref.composition.required_files = filesDependencies;
|
|
@@ -1,40 +1,71 @@
|
|
|
1
|
-
// Import Third-party Dependencies
|
|
2
|
-
import difference from "lodash.difference";
|
|
3
|
-
import builtins from "builtins";
|
|
4
|
-
|
|
5
|
-
// Import Internal Dependencies
|
|
6
|
-
import { getPackageName } from "./getPackageName.js";
|
|
7
|
-
|
|
8
|
-
// CONSTANTS
|
|
9
|
-
const kNodeModules = new Set(builtins({ experimental: true }));
|
|
10
|
-
const kExternalModules = new Set(["http", "https", "net", "http2", "dgram", "child_process"]);
|
|
11
|
-
|
|
12
|
-
export function analyzeDependencies(dependencies, deps = {}) {
|
|
13
|
-
const { packageDeps, packageDevDeps, tryDependencies } = deps;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
.filter((name) =>
|
|
18
|
-
.
|
|
19
|
-
|
|
20
|
-
.filter((name) => !
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
1
|
+
// Import Third-party Dependencies
|
|
2
|
+
import difference from "lodash.difference";
|
|
3
|
+
import builtins from "builtins";
|
|
4
|
+
|
|
5
|
+
// Import Internal Dependencies
|
|
6
|
+
import { getPackageName } from "./getPackageName.js";
|
|
7
|
+
|
|
8
|
+
// CONSTANTS
|
|
9
|
+
const kNodeModules = new Set(builtins({ experimental: true }));
|
|
10
|
+
const kExternalModules = new Set(["http", "https", "net", "http2", "dgram", "child_process"]);
|
|
11
|
+
|
|
12
|
+
export function analyzeDependencies(dependencies, deps = {}) {
|
|
13
|
+
const { packageDeps, packageDevDeps, tryDependencies, nodeImports = {} } = deps;
|
|
14
|
+
|
|
15
|
+
// See: https://nodejs.org/api/packages.html#subpath-imports
|
|
16
|
+
const subpathImportsDependencies = dependencies
|
|
17
|
+
.filter((name) => isAliasDependency(name) && name in nodeImports)
|
|
18
|
+
.map((name) => buildSubpathDependency(name, nodeImports));
|
|
19
|
+
const thirdPartyDependenciesAliased = new Set(
|
|
20
|
+
subpathImportsDependencies.flat().filter((name) => !isAliasDependency(name))
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const thirdPartyDependencies = dependencies
|
|
24
|
+
.map((name) => (packageDeps.includes(name) ? name : getPackageName(name)))
|
|
25
|
+
.filter((name) => !name.startsWith("."))
|
|
26
|
+
.filter((name) => !isNodeCoreModule(name))
|
|
27
|
+
.filter((name) => !packageDevDeps.includes(name))
|
|
28
|
+
.filter((name) => !tryDependencies.has(name));
|
|
29
|
+
|
|
30
|
+
const unusedDependencies = difference(
|
|
31
|
+
packageDeps.filter((name) => !name.startsWith("@types")),
|
|
32
|
+
[...thirdPartyDependencies, ...thirdPartyDependenciesAliased]
|
|
33
|
+
);
|
|
34
|
+
const missingDependencies = [...new Set(difference(thirdPartyDependencies, packageDeps))]
|
|
35
|
+
.filter((name) => !(name in nodeImports));
|
|
36
|
+
const nodeDependencies = dependencies.filter((name) => isNodeCoreModule(name));
|
|
37
|
+
|
|
38
|
+
return {
|
|
39
|
+
nodeDependencies,
|
|
40
|
+
thirdPartyDependencies: [...new Set(thirdPartyDependencies)],
|
|
41
|
+
subpathImportsDependencies,
|
|
42
|
+
unusedDependencies,
|
|
43
|
+
missingDependencies,
|
|
44
|
+
|
|
45
|
+
flags: {
|
|
46
|
+
hasExternalCapacity: nodeDependencies.some((depName) => kExternalModules.has(depName)),
|
|
47
|
+
hasMissingOrUnusedDependency: unusedDependencies.length > 0 || missingDependencies.length > 0
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* @param {!string} moduleName
|
|
54
|
+
* @returns {boolean}
|
|
55
|
+
*/
|
|
56
|
+
function isNodeCoreModule(moduleName) {
|
|
57
|
+
const cleanModuleName = moduleName.startsWith("node:") ? moduleName.slice(5) : moduleName;
|
|
58
|
+
|
|
59
|
+
// Note: We need to also check moduleName because builtins package only return true for 'node:test'.
|
|
60
|
+
return kNodeModules.has(cleanModuleName) || kNodeModules.has(moduleName);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function isAliasDependency(moduleName) {
|
|
64
|
+
return moduleName.charAt(0) === "#";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function buildSubpathDependency(alias, nodeImports) {
|
|
68
|
+
const importedDependency = nodeImports[alias].node ?? nodeImports[alias].default;
|
|
69
|
+
|
|
70
|
+
return [alias, importedDependency];
|
|
71
|
+
}
|
package/src/utils/warnings.js
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
// Import Third-party Dependencies
|
|
2
|
-
import { getToken, taggedString } from "@nodesecure/i18n";
|
|
3
|
-
|
|
4
|
-
// CONSTANTS
|
|
5
|
-
const kDetectedDep = taggedString`The dependency '${0}' has been detected in the dependency Tree.`;
|
|
6
|
-
const kWarningsMessages = Object.freeze({
|
|
7
|
-
"@scarf/scarf": getToken("warnings.disable_scarf"),
|
|
8
|
-
iohook: getToken("warnings.keylogging")
|
|
9
|
-
});
|
|
10
|
-
const kPackages = new Set(Object.keys(kWarningsMessages));
|
|
11
|
-
const kAuthors = new Set(["marak", "marak.squires@gmail.com"]);
|
|
12
|
-
|
|
13
|
-
function getWarning(depName) {
|
|
14
|
-
return `${kDetectedDep(depName)} ${kWarningsMessages[depName]}`;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function getDependenciesWarnings(dependencies) {
|
|
18
|
-
const warnings = [];
|
|
19
|
-
for (const depName of kPackages) {
|
|
20
|
-
if (dependencies.has(depName)) {
|
|
21
|
-
warnings.push(getWarning(depName));
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// TODO: optimize with @nodesecure/author later
|
|
26
|
-
for (const [packageName, dependency] of dependencies) {
|
|
27
|
-
for (const { name, email } of dependency.metadata.maintainers) {
|
|
28
|
-
if (kAuthors.has(name) || kAuthors.has(email)) {
|
|
29
|
-
warnings.push(`'Marak Squires' package '${packageName}' has been detected in the dependency tree`);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return warnings;
|
|
35
|
-
}
|
|
36
|
-
|
|
1
|
+
// Import Third-party Dependencies
|
|
2
|
+
import { getToken, taggedString } from "@nodesecure/i18n";
|
|
3
|
+
|
|
4
|
+
// CONSTANTS
|
|
5
|
+
const kDetectedDep = taggedString`The dependency '${0}' has been detected in the dependency Tree.`;
|
|
6
|
+
const kWarningsMessages = Object.freeze({
|
|
7
|
+
"@scarf/scarf": getToken("warnings.disable_scarf"),
|
|
8
|
+
iohook: getToken("warnings.keylogging")
|
|
9
|
+
});
|
|
10
|
+
const kPackages = new Set(Object.keys(kWarningsMessages));
|
|
11
|
+
const kAuthors = new Set(["marak", "marak.squires@gmail.com"]);
|
|
12
|
+
|
|
13
|
+
function getWarning(depName) {
|
|
14
|
+
return `${kDetectedDep(depName)} ${kWarningsMessages[depName]}`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function getDependenciesWarnings(dependencies) {
|
|
18
|
+
const warnings = [];
|
|
19
|
+
for (const depName of kPackages) {
|
|
20
|
+
if (dependencies.has(depName)) {
|
|
21
|
+
warnings.push(getWarning(depName));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// TODO: optimize with @nodesecure/author later
|
|
26
|
+
for (const [packageName, dependency] of dependencies) {
|
|
27
|
+
for (const { name, email } of dependency.metadata.maintainers) {
|
|
28
|
+
if (kAuthors.has(name) || kAuthors.has(email)) {
|
|
29
|
+
warnings.push(`'Marak Squires' package '${packageName}' has been detected in the dependency tree`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return warnings;
|
|
35
|
+
}
|
|
36
|
+
|
package/types/scanner.d.ts
CHANGED
|
@@ -42,6 +42,15 @@ declare namespace Scanner {
|
|
|
42
42
|
description: string;
|
|
43
43
|
/** Author of the package. This information is not trustable and can be empty. */
|
|
44
44
|
author: Maintainer;
|
|
45
|
+
engines: {
|
|
46
|
+
node?: string;
|
|
47
|
+
npm?: string;
|
|
48
|
+
};
|
|
49
|
+
repository: {
|
|
50
|
+
type: string;
|
|
51
|
+
url: string;
|
|
52
|
+
};
|
|
53
|
+
scripts: Record<string, string>;
|
|
45
54
|
/**
|
|
46
55
|
* JS-X-Ray warnings
|
|
47
56
|
*
|
|
@@ -58,6 +67,7 @@ declare namespace Scanner {
|
|
|
58
67
|
required_files: string[];
|
|
59
68
|
required_thirdparty: string[];
|
|
60
69
|
required_nodejs: string[];
|
|
70
|
+
required_subpath: string[];
|
|
61
71
|
unused: string[];
|
|
62
72
|
missing: string[];
|
|
63
73
|
};
|
|
@@ -156,6 +166,7 @@ declare namespace Scanner {
|
|
|
156
166
|
* @default 4
|
|
157
167
|
*/
|
|
158
168
|
readonly maxDepth?: number;
|
|
169
|
+
readonly registry?: string | URL;
|
|
159
170
|
/**
|
|
160
171
|
* Use root package-lock.json. This will have the effect of triggering the Arborist package.
|
|
161
172
|
*
|