@nodesecure/scanner 3.3.0 → 3.5.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 +97 -95
- package/index.js +1 -1
- package/package.json +19 -19
- package/src/class/dependency.class.js +102 -99
- package/src/depWalker.js +23 -9
- package/src/manifest.js +58 -57
- package/src/npmRegistry.js +74 -68
- package/src/tarball.js +6 -3
- package/src/utils/analyzeDependencies.js +71 -40
- package/types/api.d.ts +1 -1
- package/types/scanner.d.ts +193 -185
package/src/manifest.js
CHANGED
|
@@ -1,57 +1,58 @@
|
|
|
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
|
-
} = await read(location);
|
|
42
|
-
|
|
43
|
-
const packageDeps = Object.keys(dependencies);
|
|
44
|
-
const packageDevDeps = Object.keys(devDependencies);
|
|
45
|
-
const hasNativePackage = [...packageDevDeps, ...packageDeps]
|
|
46
|
-
.some((pkg) => kNativeNpmPackages.has(pkg));
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
author: typeof author === "string" ? parseManifestAuthor(author) : author,
|
|
50
|
-
description,
|
|
51
|
-
hasScript: Object.keys(scripts)
|
|
52
|
-
.some((value) => kUnsafeNpmScripts.has(value.toLowerCase())),
|
|
53
|
-
packageDeps,
|
|
54
|
-
packageDevDeps,
|
|
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
|
+
imports = {}
|
|
41
|
+
} = await read(location);
|
|
42
|
+
|
|
43
|
+
const packageDeps = Object.keys(dependencies);
|
|
44
|
+
const packageDevDeps = Object.keys(devDependencies);
|
|
45
|
+
const hasNativePackage = [...packageDevDeps, ...packageDeps]
|
|
46
|
+
.some((pkg) => kNativeNpmPackages.has(pkg));
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
author: typeof author === "string" ? parseManifestAuthor(author) : author,
|
|
50
|
+
description,
|
|
51
|
+
hasScript: Object.keys(scripts)
|
|
52
|
+
.some((value) => kUnsafeNpmScripts.has(value.toLowerCase())),
|
|
53
|
+
packageDeps,
|
|
54
|
+
packageDevDeps,
|
|
55
|
+
nodejs: { imports },
|
|
56
|
+
hasNativeElements: hasNativePackage || gypfile
|
|
57
|
+
};
|
|
58
|
+
}
|
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
|
@@ -65,7 +65,9 @@ export async function scanDirOrArchive(name, version, options) {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
// Read the package.json at the root of the directory or archive.
|
|
68
|
-
const {
|
|
68
|
+
const {
|
|
69
|
+
packageDeps, packageDevDeps, author, description, hasScript, hasNativeElements, nodejs
|
|
70
|
+
} = await manifest.readAnalyze(dest);
|
|
69
71
|
ref.author = author;
|
|
70
72
|
ref.description = description;
|
|
71
73
|
|
|
@@ -97,10 +99,11 @@ export async function scanDirOrArchive(name, version, options) {
|
|
|
97
99
|
const minifiedFiles = fileAnalysisResults.filter((row) => row.isMinified).flatMap((row) => row.file);
|
|
98
100
|
|
|
99
101
|
const {
|
|
100
|
-
nodeDependencies, thirdPartyDependencies, missingDependencies, unusedDependencies, flags
|
|
101
|
-
} = analyzeDependencies(dependencies, { packageDeps, packageDevDeps, tryDependencies });
|
|
102
|
+
nodeDependencies, thirdPartyDependencies, subpathImportsDependencies, missingDependencies, unusedDependencies, flags
|
|
103
|
+
} = analyzeDependencies(dependencies, { packageDeps, packageDevDeps, tryDependencies, nodeImports: nodejs.imports });
|
|
102
104
|
|
|
103
105
|
ref.composition.required_thirdparty = thirdPartyDependencies;
|
|
106
|
+
ref.composition.required_subpath = Object.fromEntries(subpathImportsDependencies);
|
|
104
107
|
ref.composition.unused.push(...unusedDependencies);
|
|
105
108
|
ref.composition.missing.push(...missingDependencies);
|
|
106
109
|
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/types/api.d.ts
CHANGED
|
@@ -11,5 +11,5 @@ export {
|
|
|
11
11
|
declare const ScannerLoggerEvents: LoggerEvents;
|
|
12
12
|
|
|
13
13
|
declare function cwd(location: string, options?: Scanner.Options, logger?: Logger): Promise<Scanner.Payload>;
|
|
14
|
-
declare function from(packageName: string, options?: Scanner.Options, logger?: Logger): Promise<Scanner.Payload>;
|
|
14
|
+
declare function from(packageName: string, options?: Omit<Scanner.Options, "includeDevDeps">, logger?: Logger): Promise<Scanner.Payload>;
|
|
15
15
|
declare function verify(packageName?: string | null): Promise<Scanner.VerifyPayload>;
|