forked from IBM/audit-ci
/
yarn-auditer.spec.js
374 lines (369 loc) · 11.3 KB
/
yarn-auditer.spec.js
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// @ts-check
const { expect } = require("chai");
const childProcess = require("child_process");
const semver = require("semver");
const { default: audit } = require("../dist/audit");
const { default: Allowlist } = require("../dist/allowlist");
const {
summaryWithDefault,
config: baseConfig,
testDirectory,
} = require("./common");
function config(additions) {
return baseConfig({ ...additions, "package-manager": "yarn" });
}
const canRunYarnBerry = semver.gte(
childProcess.execSync("node -v").toString().replace("\n", ""),
"12.13.0"
);
// To modify what slow times are, need to use
// function() {} instead of () => {}
describe("yarn-auditer", function testYarnAuditer() {
this.slow(3000);
it("prints full report with critical severity", async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-critical"),
levels: { critical: true },
"report-type": "full",
}),
(_summary) => _summary
);
expect(summary).to.eql(
summaryWithDefault({
failedLevelsFound: ["critical"],
advisoriesFound: ["GHSA-28xh-wpgr-7fm8"],
advisoryPathsFound: ["GHSA-28xh-wpgr-7fm8|open"],
})
);
});
it("does not report critical severity if it set to false", async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-critical"),
levels: { critical: false },
}),
(_summary) => _summary
);
expect(summary).to.eql(summaryWithDefault());
});
it("reports summary with high severity", async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-high"),
levels: { high: true },
"report-type": "summary",
}),
(_summary) => _summary
);
expect(summary).to.eql(
summaryWithDefault({
failedLevelsFound: ["high"],
advisoriesFound: ["GHSA-38f5-ghc2-fcmv"],
advisoryPathsFound: ["GHSA-38f5-ghc2-fcmv|cryo"],
})
);
});
it("reports important info with moderate severity", async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-moderate"),
levels: { moderate: true },
"report-type": "important",
}),
(_summary) => _summary
);
expect(summary).to.eql(
summaryWithDefault({
failedLevelsFound: ["moderate"],
advisoriesFound: ["GHSA-rvg8-pwq2-xj7q"],
advisoryPathsFound: ["GHSA-rvg8-pwq2-xj7q|base64url"],
})
);
});
it("does not report moderate severity if it set to false", async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-moderate"),
levels: { moderate: false },
}),
(_summary) => _summary
);
expect(summary).to.eql(summaryWithDefault());
});
it("ignores an advisory if it is allowlisted", async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-moderate"),
levels: { moderate: true },
allowlist: new Allowlist(["GHSA-rvg8-pwq2-xj7q"]),
}),
(_summary) => _summary
);
expect(summary).to.eql(
summaryWithDefault({
allowlistedAdvisoriesFound: ["GHSA-rvg8-pwq2-xj7q"],
})
);
});
it("does not ignore an advisory that is not allowlisted", async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-moderate"),
levels: { moderate: true },
allowlist: new Allowlist(["GHSA-cff4-rrq6-h78w"]),
}),
(_summary) => _summary
);
expect(summary).to.eql(
summaryWithDefault({
allowlistedAdvisoriesNotFound: ["GHSA-cff4-rrq6-h78w"],
failedLevelsFound: ["moderate"],
advisoriesFound: ["GHSA-rvg8-pwq2-xj7q"],
advisoryPathsFound: ["GHSA-rvg8-pwq2-xj7q|base64url"],
})
);
});
it("reports low severity", async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-low"),
levels: { low: true },
}),
(_summary) => _summary
);
expect(summary).to.eql(
summaryWithDefault({
failedLevelsFound: ["low"],
advisoriesFound: ["GHSA-c6rq-rjc2-86v2"],
advisoryPathsFound: ["GHSA-c6rq-rjc2-86v2|chownr"],
})
);
});
it("passes with no vulnerabilities", async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-none"),
levels: { low: true },
}),
(_summary) => _summary
);
expect(summary).to.eql(summaryWithDefault());
});
it("doesn't use the registry flag since it's not supported in Yarn yet", () =>
audit(
config({
directory: testDirectory("yarn-low"),
levels: { low: true },
registry: "https://example.com",
}),
(_summary) => _summary
));
(canRunYarnBerry ? it : it.skip)(
"[Yarn Berry] reports important info with moderate severity",
async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-berry-moderate"),
levels: { moderate: true },
"report-type": "important",
}),
(_summary) => _summary
);
expect(summary).to.eql(
summaryWithDefault({
failedLevelsFound: ["moderate"],
advisoriesFound: ["GHSA-rvg8-pwq2-xj7q"],
advisoryPathsFound: ["GHSA-rvg8-pwq2-xj7q|base64url"],
})
);
}
);
(canRunYarnBerry ? it : it.skip)(
"[Yarn Berry] does not report moderate severity if it set to false",
async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-berry-moderate"),
levels: { moderate: false },
}),
(_summary) => _summary
);
expect(summary).to.eql(summaryWithDefault());
}
);
(canRunYarnBerry ? it : it.skip)(
"[Yarn Berry] ignores an advisory if it is allowlisted",
async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-berry-moderate"),
levels: { moderate: true },
allowlist: new Allowlist(["GHSA-rvg8-pwq2-xj7q"]),
}),
(_summary) => _summary
);
expect(summary).to.eql(
summaryWithDefault({
allowlistedAdvisoriesFound: ["GHSA-rvg8-pwq2-xj7q"],
})
);
}
);
it("reports summary with no vulnerabilities when critical devDependency and skip-dev is true", async () => {
const summary = await audit(
config({
directory: testDirectory(
canRunYarnBerry ? "yarn-berry-skip-dev" : "yarn-skip-dev"
),
"skip-dev": true,
"report-type": "important",
}),
(_summary) => _summary
);
expect(summary).to.eql(summaryWithDefault());
});
it("reports summary with no vulnerabilities in yarn v1 workspace", async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-workspace-empty"),
levels: { moderate: true },
"report-type": "important",
}),
(_summary) => _summary
);
expect(summary).to.eql(summaryWithDefault());
});
it("reports summary with vulnerabilities in yarn v1 workspaces", async () => {
// TODO: There's a bug with yarn classic workspaces and failing to audit
// devDependencies:
// https://github.com/yarnpkg/yarn/issues/7047
// It doesn't report any vulnerabilities at all. The following directory should
// contain a critical vulnerability in devDependencies.
const summary = await audit(
config({
directory: testDirectory("yarn-workspace"),
levels: { moderate: true },
"report-type": "important",
}),
(_summary) => _summary
);
expect(summary).to.eql(
summaryWithDefault({
failedLevelsFound: ["high", "moderate"],
advisoriesFound: ["GHSA-38f5-ghc2-fcmv", "GHSA-rvg8-pwq2-xj7q"],
advisoryPathsFound: [
"GHSA-38f5-ghc2-fcmv|audit-ci-yarn-workspace-high-vulnerability>cryo",
"GHSA-rvg8-pwq2-xj7q|audit-ci-yarn-workspace-moderate-vulnerability>base64url",
],
})
);
});
(canRunYarnBerry ? it : it.skip)(
"reports summary with no vulnerabilities in yarn berry workspace",
async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-berry-workspace-empty"),
levels: { moderate: true },
"report-type": "important",
}),
(_summary) => _summary
);
expect(summary).to.eql(summaryWithDefault());
}
);
(canRunYarnBerry ? it : it.skip)(
"reports summary with vulnerabilities in yarn berry workspaces",
async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-berry-workspace"),
levels: { moderate: true },
"report-type": "important",
}),
(_summary) => _summary
);
expect(summary).to.eql(
summaryWithDefault({
failedLevelsFound: ["critical", "high", "moderate"],
advisoriesFound: [
"GHSA-38f5-ghc2-fcmv",
"GHSA-rvg8-pwq2-xj7q",
"GHSA-28xh-wpgr-7fm8",
],
advisoryPathsFound: [
"GHSA-38f5-ghc2-fcmv|cryo",
"GHSA-rvg8-pwq2-xj7q|base64url",
"GHSA-28xh-wpgr-7fm8|open",
],
})
);
}
);
(canRunYarnBerry ? it : it.skip)(
"reports summary with vulnerabilities in yarn berry workspaces with skip-dev=true",
async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-berry-workspace"),
levels: { moderate: true },
"skip-dev": true,
"report-type": "important",
}),
(_summary) => _summary
);
expect(summary).to.eql(
summaryWithDefault({
failedLevelsFound: ["high", "moderate"],
advisoriesFound: ["GHSA-38f5-ghc2-fcmv", "GHSA-rvg8-pwq2-xj7q"],
advisoryPathsFound: [
"GHSA-38f5-ghc2-fcmv|cryo",
"GHSA-rvg8-pwq2-xj7q|base64url",
],
})
);
}
);
it("does not report duplicate paths", async () => {
const summary = await audit(
config({
directory: testDirectory("yarn-duplicate-paths"),
levels: { high: true },
"report-type": "summary",
}),
(_summary) => _summary
);
expect(summary.advisoryPathsFound).to.eql([
...new Set(summary.advisoryPathsFound),
]);
});
// it('prints unexpected https://registry.yarnpkg.com 503 error message', () => {
// const directory = testDirectory('yarn-503');
// const errorMessagePath = path.resolve(directory, 'error-message');
// const errorMessage = require(errorMessagePath); // eslint-disable-line
// return audit(
// config({
// directory,
// _yarn: path.join(directory, 'yarn'),
// })
// )
// .then(() => {
// // Since we expect an error the promise should never resolve
// throw new Error();
// })
// .catch(err => {
// expect(err.toString()).to.contain(errorMessage);
// });
// });
// it("passes using --pass-enoaudit", () => {
// const directory = testDirectory("yarn-503");
// return audit(
// config({
// directory,
// "pass-enoaudit": true,
// _yarn: path.join(directory, "yarn"),
// })
// );
// });
});