@nodesecure/scanner 2.2.0 → 3.2.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.
@@ -1,14 +1,16 @@
1
- export * from "./getTarballComposition.js";
2
- export * from "./isSensitiveFile.js";
3
- export * from "./getPackageName.js";
4
- export * from "./mergeDependencies.js";
5
- export * from "./semver.js";
6
- export * from "./dirname.js";
7
- export * from "./warnings.js";
8
-
9
- export const constants = {
10
- NPM_TOKEN: typeof process.env.NODE_SECURE_TOKEN === "string" ? { token: process.env.NODE_SECURE_TOKEN } : {},
11
- NPM_SCRIPTS: new Set(["preinstall", "postinstall", "preuninstall", "postuninstall"]),
12
- EXT_DEPS: new Set(["http", "https", "net", "http2", "dgram", "child_process"]),
13
- EXT_JS: new Set([".js", ".mjs", ".cjs"])
14
- };
1
+ export * from "./getTarballComposition.js";
2
+ export * from "./isSensitiveFile.js";
3
+ export * from "./isGitDependency.js";
4
+ export * from "./getPackageName.js";
5
+ export * from "./mergeDependencies.js";
6
+ export * from "./semver.js";
7
+ export * from "./dirname.js";
8
+ export * from "./warnings.js";
9
+ export * from "./filterDependencyKind.js";
10
+ export * from "./analyzeDependencies.js";
11
+ export * from "./booleanToFlags.js";
12
+ export * from "./addMissingVersionFlags.js";
13
+
14
+ export const NPM_TOKEN = typeof process.env.NODE_SECURE_TOKEN === "string" ?
15
+ { token: process.env.NODE_SECURE_TOKEN } :
16
+ {};
@@ -0,0 +1,20 @@
1
+ const kGitVersionVariants = ["git:", "git+", "github:"];
2
+
3
+ /**
4
+ * @example isGitDependency("github:NodeSecure/scanner") // => true
5
+ * @example isGitDependency("git+ssh://git@github.com:npm/cli#semver:^5.0") // => true
6
+ * @example isGitDependency(">=1.0.2 <2.1.2") // => false
7
+ * @example isGitDependency("http://asdf.com/asdf.tar.gz") // => false
8
+ * @param {string} version
9
+ * @returns {boolean}
10
+ */
11
+ export function isGitDependency(version) {
12
+ for (const variant of kGitVersionVariants) {
13
+ if (version.startsWith(variant)) {
14
+ return true;
15
+ }
16
+ }
17
+
18
+ return false;
19
+ }
20
+
@@ -5,6 +5,12 @@ import path from "path";
5
5
  const kSensitiveFileName = new Set([".npmrc", ".env"]);
6
6
  const kSensitiveFileExtension = new Set([".key", ".pem"]);
7
7
 
8
+ /**
9
+ * @see https://github.com/jandre/safe-commit-hook/blob/master/git-deny-patterns.json
10
+ *
11
+ * @param {!string} fileName
12
+ * @returns {boolean}
13
+ */
8
14
  export function isSensitiveFile(fileName) {
9
15
  return kSensitiveFileName.has(path.basename(fileName)) ||
10
16
  kSensitiveFileExtension.has(path.extname(fileName));
@@ -1,23 +1,26 @@
1
- export function mergeDependencies(manifest, types = ["dependencies"]) {
2
- const dependencies = new Map();
3
- const customResolvers = new Map();
4
-
5
- for (const fieldName of types) {
6
- if (!Reflect.has(manifest, fieldName)) {
7
- continue;
8
- }
9
- const dep = manifest[fieldName];
10
-
11
- for (const [name, version] of Object.entries(dep)) {
12
- // Version can be file:, github:, git+, ./...
13
- if (/^([a-zA-Z]+:|git\+|\.\\)/.test(version)) {
14
- customResolvers.set(name, version);
15
- continue;
16
- }
17
-
18
- dependencies.set(name, version);
19
- }
20
- }
21
-
22
- return { dependencies, customResolvers };
23
- }
1
+ export function mergeDependencies(manifest, types = ["dependencies"]) {
2
+ const dependencies = new Map();
3
+ const customResolvers = new Map();
4
+
5
+ for (const fieldName of types) {
6
+ if (!Reflect.has(manifest, fieldName)) {
7
+ continue;
8
+ }
9
+ const dep = manifest[fieldName];
10
+
11
+ for (const [name, version] of Object.entries(dep)) {
12
+ /**
13
+ * Version can be file:, github:, git:, git+, ./...
14
+ * @see https://docs.npmjs.com/cli/v7/configuring-npm/package-json#dependencies
15
+ */
16
+ if (/^([a-zA-Z]+:|git\+|\.\\)/.test(version)) {
17
+ customResolvers.set(name, version);
18
+ continue;
19
+ }
20
+
21
+ dependencies.set(name, version);
22
+ }
23
+ }
24
+
25
+ return { dependencies, customResolvers };
26
+ }
@@ -4,7 +4,7 @@ import semver from "semver";
4
4
  import { getLocalRegistryURL } from "@nodesecure/npm-registry-sdk";
5
5
 
6
6
  // Import Internal Dependencies
7
- import { constants } from "./index.js";
7
+ import { NPM_TOKEN } from "./index.js";
8
8
 
9
9
  /**
10
10
  * @param {!string} version semver range
@@ -31,11 +31,11 @@ export function cleanRange(version) {
31
31
  export async function getExpectedSemVer(depName, range) {
32
32
  try {
33
33
  const { versions, "dist-tags": { latest } } = await pacote.packument(depName, {
34
- ...constants.NPM_TOKEN, registry: getLocalRegistryURL()
34
+ ...NPM_TOKEN, registry: getLocalRegistryURL()
35
35
  });
36
36
  const currVersion = semver.maxSatisfying(Object.keys(versions), range);
37
37
 
38
- return [currVersion === null ? latest : currVersion, semver.eq(latest, currVersion)];
38
+ return currVersion === null ? [latest, true] : [currVersion, semver.eq(latest, currVersion)];
39
39
  }
40
40
  catch (err) {
41
41
  return [cleanRange(range), true];
@@ -8,6 +8,7 @@ const kWarningsMessages = Object.freeze({
8
8
  iohook: getToken("warnings.keylogging")
9
9
  });
10
10
  const kPackages = new Set(Object.keys(kWarningsMessages));
11
+ const kAuthors = new Set(["Marak Squires"]);
11
12
 
12
13
  function getWarning(depName) {
13
14
  return `${kDetectedDep(depName)} ${kWarningsMessages[depName]}`;
@@ -21,6 +22,14 @@ export function getDependenciesWarnings(dependencies) {
21
22
  }
22
23
  }
23
24
 
25
+ // TODO: optimize with @nodesecure/author later
26
+ for (const [packageName, dependency] of dependencies) {
27
+ const author = dependency.metadata.author?.name ?? null;
28
+ if (kAuthors.has(author)) {
29
+ warnings.push(`'${author}' package '${packageName}' has been detected in the dependency tree`);
30
+ }
31
+ }
32
+
24
33
  return warnings;
25
34
  }
26
35
 
package/types/api.d.ts CHANGED
@@ -1,12 +1,15 @@
1
1
  import Scanner from "./scanner";
2
- import { Logger } from "./logger";
2
+ import { Logger, LoggerEvents } from "./logger";
3
3
 
4
4
  export {
5
5
  cwd,
6
6
  from,
7
- verify
7
+ verify,
8
+ ScannerLoggerEvents
8
9
  }
9
10
 
11
+ declare const ScannerLoggerEvents: LoggerEvents;
12
+
10
13
  declare function cwd(path: string, options?: Scanner.Options, logger?: Logger): Promise<Scanner.Payload>;
11
14
  declare function from(packageName: string, options?: Scanner.Options, logger?: Logger): Promise<Scanner.Payload>;
12
15
  declare function verify(packageName: string): Promise<Scanner.VerifyPayload>;
package/types/logger.d.ts CHANGED
@@ -2,11 +2,27 @@ import { EventEmitter } from "events";
2
2
 
3
3
  export {
4
4
  Logger,
5
- LoggerEventData
5
+ LoggerEventData,
6
+ LoggerEvents
7
+ }
8
+
9
+ interface LoggerEvents {
10
+ readonly done: "depWalkerFinished";
11
+ readonly analysis: {
12
+ readonly tree: "walkTree";
13
+ readonly tarball: "tarball";
14
+ readonly registry: "registry";
15
+ };
16
+ readonly manifest: {
17
+ readonly read: "readManifest";
18
+ readonly fetch: "fetchManifest";
19
+ };
6
20
  }
7
21
 
8
22
  interface LoggerEventData {
23
+ /** UNIX Timestamp */
9
24
  startedAt: number;
25
+ /** Count of triggered event */
10
26
  count: number;
11
27
  }
12
28
 
@@ -1,86 +1,185 @@
1
- import { Warning, Dependency, BaseWarning } from "@nodesecure/js-x-ray";
2
- import { license as License } from "@nodesecure/ntlp";
3
- import { Strategy, NpmStrategy, NodeStrategy } from "@nodesecure/vuln";
4
- import { Flags } from "@nodesecure/flags";
5
- import { Maintainer } from "@npm/types";
6
-
7
- export = Scanner;
8
-
9
- declare namespace Scanner {
10
- export interface Publisher {
11
- name: string;
12
- version: string;
13
- at: string;
14
- }
15
-
16
- export interface VersionDescriptor {
17
- metadata: {
18
- dependencyCount: number;
19
- publishedCount: number;
20
- lastUpdateAt: number;
21
- lastVersion: number;
22
- hasChangedAuthor: boolean;
23
- hasManyPublishers: boolean;
24
- hasReceivedUpdateInOneYear: boolean;
25
- author: Maintainer;
26
- homepage: string | null;
27
- maintainers: Maintainer[];
28
- publishers: Publisher[];
29
- }
30
- versions: string[];
31
- vulnerabilities: (NpmStrategy.Vulnerability | NodeStrategy.Vulnerability)[];
32
- [version: string]: {
33
- id: number;
34
- usedBy: Record<string, string>;
35
- size: number;
36
- description: string;
37
- author: Maintainer;
38
- warnings: Warning<BaseWarning>[];
39
- composition: {
40
- extensions: string[];
41
- files: string[];
42
- minified: string[];
43
- required_files: string[];
44
- required_thirdparty: string[];
45
- required_nodejs: string[];
46
- unused: string[];
47
- missing: string[];
48
- };
49
- license: string | License[];
50
- flags: Flags;
51
- gitUrl: null | string;
52
- };
53
- }
54
-
55
- export interface Payload {
56
- id: string;
57
- rootDependencyName: string;
58
- warnings: [];
59
- dependencies: Record<string, VersionDescriptor>;
60
- version: string;
61
- vulnerabilityStrategy: Strategy.Kind;
62
- }
63
-
64
- export interface VerifyPayload {
65
- files: {
66
- list: string[];
67
- extensions: string[];
68
- minified: string[];
69
- };
70
- directorySize: number;
71
- uniqueLicenseIds: string[];
72
- licenses: License[];
73
- ast: {
74
- dependencies: Record<string, Dependency>;
75
- warnings: Warning<BaseWarning>[];
76
- };
77
- }
78
-
79
- export interface Options {
80
- readonly maxDepth?: number;
81
- readonly usePackageLock?: boolean;
82
- readonly vulnerabilityStrategy: Strategy.Kind;
83
- readonly forceRootAnalysis?: boolean;
84
- readonly fullLockMode?: boolean;
85
- }
86
- }
1
+ // Import NodeSecure Dependencies
2
+ import * as JSXRay from "@nodesecure/js-x-ray";
3
+ import { license as License } from "@nodesecure/ntlp";
4
+ import * as Vuln from "@nodesecure/vuln";
5
+ import { Flags } from "@nodesecure/flags";
6
+
7
+ // Import Third-party Dependencies
8
+ import { Maintainer } from "@npm/types";
9
+
10
+ export = Scanner;
11
+
12
+ declare namespace Scanner {
13
+ export interface Publisher {
14
+ /**
15
+ * Publisher npm user name.
16
+ */
17
+ name: string;
18
+ /**
19
+ * Publisher npm user email.
20
+ */
21
+ email: string;
22
+ /**
23
+ * First version published.
24
+ */
25
+ version: string;
26
+ /**
27
+ * Date of the first publication
28
+ * @example 2021-08-10T20:45:08.342Z
29
+ */
30
+ at: string;
31
+ }
32
+
33
+ export interface DependencyVersion {
34
+ /** Id of the package (useful for usedBy relation) */
35
+ id: number;
36
+ /** By whom (id) is used the package */
37
+ usedBy: Record<string, string>;
38
+ /** Size on disk of the extracted tarball (in bytes) */
39
+ size: number;
40
+ /** Package description */
41
+ description: string;
42
+ /** Author of the package. This information is not trustable and can be empty. */
43
+ author: Maintainer;
44
+ /**
45
+ * JS-X-Ray warnings
46
+ *
47
+ * @see https://github.com/NodeSecure/js-x-ray/blob/master/WARNINGS.md
48
+ */
49
+ warnings: JSXRay.Warning<JSXRay.BaseWarning>[];
50
+ /** Tarball composition (files and dependencies) */
51
+ composition: {
52
+ /** Files extensions (.js, .md, .exe etc..) */
53
+ extensions: string[];
54
+ files: string[];
55
+ /** Minified files (foo.min.js etc..) */
56
+ minified: string[];
57
+ required_files: string[];
58
+ required_thirdparty: string[];
59
+ required_nodejs: string[];
60
+ unused: string[];
61
+ missing: string[];
62
+ };
63
+ /**
64
+ * Package licenses with SPDX expression.
65
+ *
66
+ * @see https://github.com/NodeSecure/licenses-conformance
67
+ * @see https://github.com/NodeSecure/npm-tarball-license-parser
68
+ */
69
+ license: License[];
70
+ /**
71
+ * Flags (Array of string)
72
+ *
73
+ * @see https://github.com/NodeSecure/flags/blob/main/FLAGS.md
74
+ */
75
+ flags: Flags[];
76
+ /**
77
+ * If the dependency is a GIT repository
78
+ */
79
+ gitUrl: null | string;
80
+ }
81
+
82
+ export interface Dependency {
83
+ /** NPM Registry metadata */
84
+ metadata: {
85
+ /** Count of dependencies */
86
+ dependencyCount: number;
87
+ /** Number of releases published on npm */
88
+ publishedCount: number;
89
+ lastUpdateAt: number;
90
+ /** Last version SemVer */
91
+ lastVersion: number;
92
+ hasChangedAuthor: boolean;
93
+ hasManyPublishers: boolean;
94
+ hasReceivedUpdateInOneYear: boolean;
95
+ /** Author of the package. This information is not trustable and can be empty. */
96
+ author: Maintainer;
97
+ /** Package home page */
98
+ homepage: string | null;
99
+ /**
100
+ * List of maintainers (list of people in the organization related to the package)
101
+ */
102
+ maintainers: { name: string, email: string }[];
103
+ /**
104
+ * List of people who published this package
105
+ */
106
+ publishers: Publisher[];
107
+ }
108
+ /** List of versions of this package available in the dependency tree (In the payload) */
109
+ versions: Record<string, DependencyVersion>;
110
+ /**
111
+ * Vulnerabilities fetched dependending on the selected vulnerabilityStrategy
112
+ *
113
+ * @see https://github.com/NodeSecure/vuln
114
+ */
115
+ vulnerabilities: Vuln.Strategy.StandardVulnerability[];
116
+ }
117
+
118
+ export type GlobalWarning = string[];
119
+ export type Dependencies = Record<string, Dependency>;
120
+
121
+ export interface Payload {
122
+ /** Payload unique id */
123
+ id: string;
124
+ /** Name of the analyzed package */
125
+ rootDependencyName: string;
126
+ /** Global warnings list */
127
+ warnings: GlobalWarning[];
128
+ /** All the dependencies of the package (flattened) */
129
+ dependencies: Dependencies;
130
+ /** Version of the scanner used to generate the result */
131
+ scannerVersion: string;
132
+ /** Vulnerability strategy name (npm, snyk, node) */
133
+ vulnerabilityStrategy: Vuln.Strategy.Kind;
134
+ }
135
+
136
+ export interface VerifyPayload {
137
+ files: {
138
+ list: string[];
139
+ extensions: string[];
140
+ minified: string[];
141
+ };
142
+ directorySize: number;
143
+ uniqueLicenseIds: string[];
144
+ licenses: License[];
145
+ ast: {
146
+ dependencies: Record<string, JSXRay.Dependency>;
147
+ warnings: JSXRay.Warning<JSXRay.BaseWarning>[];
148
+ };
149
+ }
150
+
151
+ export interface Options {
152
+ /**
153
+ * Maximum tree depth
154
+ *
155
+ * @default 4
156
+ */
157
+ readonly maxDepth?: number;
158
+ /**
159
+ * Use root package-lock.json. This will have the effect of triggering the Arborist package.
160
+ *
161
+ * @default false for from() API
162
+ * @default true for cwd() API
163
+ */
164
+ readonly usePackageLock?: boolean;
165
+ /**
166
+ * Vulnerability strategy name (npm, snyk, node)
167
+ *
168
+ * @default NONE
169
+ */
170
+ readonly vulnerabilityStrategy: Vuln.Strategy.Kind;
171
+ /**
172
+ * Analyze root package.
173
+ *
174
+ * @default false for from() API
175
+ * @default true for cwd() API
176
+ */
177
+ readonly forceRootAnalysis?: boolean;
178
+ /**
179
+ * Deeper dependencies analysis with cwd() API.
180
+ *
181
+ * @default false
182
+ */
183
+ readonly fullLockMode?: boolean;
184
+ }
185
+ }