Repository navigation
Expand file tree
/
Copy pathnpm.test.ts
More file actions
357 lines (311 loc) · 13.6 KB
/
Copy pathnpm.test.ts
File metadata and controls
357 lines (311 loc) · 13.6 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
189
190
191
192
193
194
195
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
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
356
357
import assert from "node:assert/strict";
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { after, before, test } from "node:test";
import { NpmDetector } from "../src/detectors/npm/index.js";
import { copyFixtureTree } from "./fixture-copies.js";
let enginesFixtures: string;
let typesStubFixtures: string;
let nodeSassDeprecatedFixtures: string;
const stubTypeRule = /^npm\.package\.types\.[^.]+\.deprecated$/;
const nodeSassDeprecatedRule = "npm.package.node-sass.deprecated";
const temporaryRoots: string[] = [];
before(async () => {
enginesFixtures = await copyFixtureTree(resolve("tests/fixtures/nodejs/engines"), temporaryRoots);
typesStubFixtures = await copyFixtureTree(resolve("tests/fixtures/npm/types-stubs"), temporaryRoots);
nodeSassDeprecatedFixtures = await copyFixtureTree(
resolve("tests/fixtures/npm/node-sass-deprecated"),
temporaryRoots,
);
});
after(async () => {
await Promise.all(temporaryRoots.map((root) => rm(root, { recursive: true, force: true })));
});
async function repository(files: Record<string, string>): Promise<string> {
const root = await mkdtemp(join(tmpdir(), "offing-npm-"));
temporaryRoots.push(root);
await Promise.all(
Object.entries(files).map(async ([path, contents]) => {
const absolutePath = join(root, path);
await mkdir(dirname(absolutePath), { recursive: true });
await writeFile(absolutePath, contents);
}),
);
return root;
}
test("reports EOL Node.js ranges and deprecated direct packages from package-lock", async () => {
const root = await repository({
"package.json": JSON.stringify(
{
engines: { node: "^20.0.0" },
dependencies: { request: "^2.88.0" },
devDependencies: { "node-sass": "^9.0.0" },
},
null,
2,
),
"package-lock.json": JSON.stringify({
lockfileVersion: 3,
packages: {
"node_modules/request": { version: "2.88.2" },
"node_modules/node-sass": { version: "9.0.0" },
},
}),
"src/client.ts": `import request from "request";\nrequest.get("https://example.com");\n`,
"src/styles.js": `const sass = require("node-sass");\nsass.renderSync({ file: "app.scss" });\n`,
});
const findings = await new NpmDetector().scan({ root });
assert.equal(findings.length, 4);
assert.match(findings[0]?.summary ?? "", /Node\.js 20/);
assert.equal(findings[0]?.severity, "medium");
assert.match(findings[1]?.summary ?? "", /resolved: 2\.88\.2/);
const requestFinding = findings.find((finding) => finding.ruleId === "npm.package.request.deprecated");
const sassEolFinding = findings.find((finding) => finding.ruleId === "npm.package.node-sass.eol");
const sassDeprecatedFinding = findings.find((finding) => finding.ruleId === nodeSassDeprecatedRule);
assert.ok(sassEolFinding);
assert.ok(sassDeprecatedFinding);
assert.match(sassEolFinding?.summary ?? "", /resolved: 9\.0\.0/);
assert.match(sassDeprecatedFinding?.summary ?? "", /resolved: 9\.0\.0/);
assert.equal(requestFinding?.impact?.usageCount, 2);
assert.equal(sassEolFinding?.impact?.usageCount, 2);
assert.equal(sassDeprecatedFinding?.impact?.usageCount, 2);
assert.equal(requestFinding?.usages?.[1]?.location.path, "src/client.ts");
});
test("reads pinned runtimes and reports them with higher severity", async () => {
const root = await repository({
"package.json": JSON.stringify({ volta: { node: "18.20.0" } }, null, 2),
".nvmrc": "20\n",
});
const findings = await new NpmDetector().scan({ root });
assert.equal(findings.length, 2);
assert.ok(findings.every((finding) => finding.severity === "high"));
assert.deepEqual(findings.map((finding) => finding.location.path).sort(), [".nvmrc", "package.json"]);
});
test("discovers resolved versions in pnpm and Yarn lockfiles", async () => {
const root = await repository({
"package.json": JSON.stringify({
dependencies: { request: "^2.88.0", "left-pad": "^1.3.0" },
}),
"pnpm-lock.yaml": `lockfileVersion: '9.0'\npackages:\n request@2.88.2: {}\n`,
"yarn.lock": `left-pad@^1.3.0:\n version "1.3.0"\n`,
});
const findings = await new NpmDetector().scan({ root });
assert.equal(findings.length, 2);
assert.ok(findings.some((finding) => finding.summary.includes("resolved: 2.88.2")));
assert.ok(findings.some((finding) => finding.summary.includes("resolved: 1.3.0")));
});
test("does not report supported Node.js ranges or transitive-only deprecated packages", async () => {
const root = await repository({
"package.json": JSON.stringify({ engines: { node: ">=22" } }),
"package-lock.json": JSON.stringify({
lockfileVersion: 3,
packages: { "node_modules/request": { version: "2.88.2" } },
}),
});
const findings = await new NpmDetector().scan({ root });
assert.deepEqual(findings, []);
});
test("stub-type deprecations exclude older typed releases and respect resolved versions", async () => {
for (const [version, affected] of [
["8.1.0", false],
["9.0.0", true],
] as const) {
const root = await repository({
"package.json": JSON.stringify({ dependencies: { "@types/glob": `^${version}` } }),
"package-lock.json": JSON.stringify({
lockfileVersion: 3,
packages: { "node_modules/@types/glob": { version } },
}),
});
const result = await new NpmDetector().scan({ root });
assert.equal(
result.some((finding) => finding.ruleId === "npm.package.types.glob.deprecated"),
affected,
);
}
});
test("types-stubs positive fixture reports the deprecated @types/glob stub", async () => {
const findings = await new NpmDetector().scan({ root: resolve(typesStubFixtures, "positive") });
const stubFindings = findings.filter((finding) => stubTypeRule.test(finding.ruleId));
assert.equal(stubFindings.length, 1);
assert.equal(stubFindings[0]?.ruleId, "npm.package.types.glob.deprecated");
assert.equal(stubFindings[0]?.location.path, "package.json");
});
test("types-stubs negative fixture produces no stub-type findings on lookalikes", async () => {
const findings = await new NpmDetector().scan({ root: resolve(typesStubFixtures, "negative") });
assert.deepEqual(
findings.filter((finding) => stubTypeRule.test(finding.ruleId)),
[],
);
});
test("node-sass-deprecated positive fixture reports the deprecated node-sass package", async () => {
const findings = await new NpmDetector().scan({ root: resolve(nodeSassDeprecatedFixtures, "positive") });
const deprecated = findings.filter((finding) => finding.ruleId === nodeSassDeprecatedRule);
assert.equal(deprecated.length, 1);
assert.equal(deprecated[0]?.location.path, "package.json");
});
test("node-sass-deprecated negative fixture produces no deprecated findings on lookalikes", async () => {
const findings = await new NpmDetector().scan({ root: resolve(nodeSassDeprecatedFixtures, "negative") });
assert.deepEqual(
findings.filter((finding) => finding.ruleId === nodeSassDeprecatedRule),
[],
);
});
test("node-sass deprecated rule does not match an unpublished resolved version", async () => {
const root = await repository({
"package.json": JSON.stringify({ dependencies: { "node-sass": "^99.0.0" } }),
"package-lock.json": JSON.stringify({
lockfileVersion: 3,
packages: { "node_modules/node-sass": { version: "99.0.0" } },
}),
});
const findings = await new NpmDetector().scan({ root });
assert.equal(
findings.some((finding) => finding.ruleId === nodeSassDeprecatedRule),
false,
);
});
test("version-specific npm rules match declared ranges when no lockfile is available", async () => {
for (const [range, affected] of [
["^8.1.0", false],
["^9.0.0", true],
["*", true],
["workspace:*", false],
] as const) {
const root = await repository({ "package.json": JSON.stringify({ dependencies: { "@types/glob": range } }) });
const findings = await new NpmDetector().scan({ root });
assert.equal(
findings.some((finding) => finding.ruleId === "npm.package.types.glob.deprecated"),
affected,
range,
);
}
});
test("a supported resolved release takes precedence over a broad manifest range", async () => {
const root = await repository({
"package.json": JSON.stringify({ dependencies: { "@types/glob": "*" } }),
"package-lock.json": JSON.stringify({
lockfileVersion: 3,
packages: { "node_modules/@types/glob": { version: "8.1.0" } },
}),
});
const findings = await new NpmDetector().scan({ root });
assert.equal(
findings.some((finding) => finding.ruleId === "npm.package.types.glob.deprecated"),
false,
);
});
test("reports the line of the declaration that was matched, in the section it came from", async () => {
const root = await repository({
"package.json": [
"{",
' "overrides": {',
' "request": "2.88.2"',
" },",
' "dependencies": {',
' "request": "^2.88.0"',
" },",
' "devDependencies": {',
' "request": "^2.88.0"',
" }",
"}",
].join("\n"),
});
const findings = await new NpmDetector().scan({ root });
const requestFindings = findings.filter((finding) => finding.ruleId === "npm.package.request.deprecated");
assert.equal(requestFindings.length, 1);
assert.equal(requestFindings[0]?.location.line, 9);
assert.match(requestFindings[0]?.summary ?? "", /declared in devDependencies as \^2\.88\.0/);
});
test("does not report a package declared only as a peer dependency, even when the lockfile resolves it", async () => {
for (const range of ["*", "^8.0.0 || ^9.0.0"]) {
const root = await repository({
"package.json": JSON.stringify({ peerDependencies: { "@types/glob": range } }),
"package-lock.json": JSON.stringify({
lockfileVersion: 3,
packages: { "node_modules/@types/glob": { version: "9.0.0" } },
}),
});
const findings = await new NpmDetector().scan({ root });
assert.deepEqual(
findings.filter((finding) => finding.ruleId === "npm.package.types.glob.deprecated"),
[],
range,
);
}
});
test("a package in dependencies and peerDependencies reports the dependencies line", async () => {
const root = await repository({
"package.json": [
"{",
' "peerDependencies": {',
' "@types/glob": "*"',
" },",
' "dependencies": {',
' "@types/glob": "^9.0.0"',
" }",
"}",
].join("\n"),
});
const findings = await new NpmDetector().scan({ root });
const globFindings = findings.filter((finding) => finding.ruleId === "npm.package.types.glob.deprecated");
assert.equal(globFindings.length, 1);
assert.equal(globFindings[0]?.location.line, 6);
assert.match(globFindings[0]?.summary ?? "", /declared in dependencies as \^9\.0\.0/);
});
test("a scheduled Node.js line is reported only for a declaration that stays on it", async () => {
const reported = async (files: Record<string, string>) =>
(await new NpmDetector().scan({ root: await repository(files) })).filter(
(finding) => finding.ruleId === "nodejs.runtime.eol-22",
);
for (const value of ["22", "22.x", "^22.1.0", "~22.4.0", ">=22.0.0 <23"]) {
const findings = await reported({ "package.json": JSON.stringify({ engines: { node: value } }) });
assert.equal(findings.length, 1, value);
assert.equal(findings[0]!.severity, "low", value);
assert.equal(findings[0]!.deadline, "2027-04-30", value);
}
const pinned = await reported({ "package.json": "{}", ".nvmrc": "v22.11.0\n" });
assert.deepEqual(
pinned.map((finding) => [finding.severity, finding.location.path]),
[["low", ".nvmrc"]],
);
// A range that merely allows Node.js 22 is not a problem with the declaration, and neither is one that starts below it.
for (const value of [">=22", ">=22.0.0", "^20 || ^22", ">=20", "24", "*"]) {
assert.deepEqual(await reported({ "package.json": JSON.stringify({ engines: { node: value } }) }), [], value);
}
});
test("a scheduled Node.js line does not make a range report the ended lines it allows", async () => {
const root = await repository({ "package.json": JSON.stringify({ engines: { node: ">=22" } }) });
assert.deepEqual(await new NpmDetector().scan({ root }), []);
});
test("engines.node fixtures: concrete EOL pin is reported; lookalikes are not", async () => {
const positive = await new NpmDetector().scan({ root: resolve(enginesFixtures, "positive") });
assert.deepEqual(
positive.map((finding) => [finding.ruleId, finding.location.path, finding.location.line]),
[["nodejs.runtime.eol-18", "package.json", 5]],
);
const negative = await new NpmDetector().scan({ root: resolve(enginesFixtures, "negative") });
assert.deepEqual(
negative.filter((finding) => finding.ruleId.startsWith("nodejs.runtime.")),
[],
);
});
test("a workspace package.json that is not a JSON object is skipped and the other packages are still scanned", async () => {
for (const hostile of ["null", "[]", "42", '"text"']) {
const root = await repository({
"package.json": JSON.stringify({ private: true, workspaces: ["packages/*"] }),
"packages/bad/package.json": hostile,
"packages/good/package.json": JSON.stringify({ engines: { node: ">=14" } }),
});
const findings = await new NpmDetector().scan({ root });
assert.ok(
findings.some((finding) => finding.location.path === "packages/good/package.json"),
`the good workspace is still reported next to ${hostile}`,
);
}
});
test("a root package.json that is not a JSON object is an error, like malformed JSON", async () => {
const root = await repository({ "package.json": "null" });
await assert.rejects(new NpmDetector().scan({ root }), /package\.json must contain a JSON object/);
});