Skip to content

Commit f8e42c5

Browse files
rally25rsarcanis
authored andcommitted
fix(audit) Report vulnerabilities in workspace package dependencies (#6639)
* added failing test for #6625 * fix(audit): Add workspaceAggregator dependencies to root manifest to be audited. This change takes the packages that are included as part of the workspace and adds them to what is sent to the npm audit api. fixes #6625 * update changelog for PR 6639
1 parent 124a2ef commit f8e42c5

File tree

7 files changed

+140
-1
lines changed

7 files changed

+140
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa
66

77
**Important:** This release contains a cache bump. It will cause the very first install following the upgrade to take slightly more time, especially if you don't use the [Offline Mirror](https://yarnpkg.com/blog/2016/11/24/offline-mirror/) feature. After that everything will be back to normal.
88

9+
- Fixes an issue with `yarn audit` when using workspaces
10+
11+
[#6625](https://github.com/yarnpkg/yarn/pull/6639) - [**Jeff Valore**](https://twitter.com/codingwithspike)
12+
913
- Uses `NODE_OPTIONS` to instruct Node to load the PnP hook, instead of raw CLI arguments
1014

1115
**Caveat:** This change might cause issues for PnP users having a space inside their cwd (cf [nodejs/node#24065](https://github.com/nodejs/node/pull/24065))

__tests__/commands/audit.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ test('calls reporter auditAdvisory with correct data', () => {
105105
});
106106
});
107107

108-
// *** Test temporarily removed due to inability to correctly puggest actions to the user.
108+
// *** Test temporarily removed due to inability to correctly suggest actions to the user.
109109
// test('calls reporter auditAction with correct data', () => {
110110
// return runAudit([], {}, 'single-vulnerable-dep-installed', (config, reporter) => {
111111
// const apiResponse = getAuditResponse(config);
@@ -162,3 +162,61 @@ test('calls reporter auditSummary with correct data for private package', () =>
162162
expect(reporter.auditSummary).toBeCalledWith(apiResponse.metadata);
163163
});
164164
});
165+
166+
test.concurrent('sends correct dependency map to audit api for workspaces.', () => {
167+
const expectedApiPost = {
168+
dependencies: {
169+
'balanced-match': {
170+
dependencies: {},
171+
integrity: 'sha1-ibTRmasr7kneFk6gK4nORi1xt2c=',
172+
requires: {},
173+
version: '1.0.0',
174+
},
175+
'brace-expansion': {
176+
dependencies: {},
177+
integrity: 'sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==',
178+
requires: {
179+
'balanced-match': '^1.0.0',
180+
'concat-map': '0.0.1',
181+
},
182+
version: '1.1.11',
183+
},
184+
'concat-map': {
185+
dependencies: {},
186+
integrity: 'sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=',
187+
requires: {},
188+
version: '0.0.1',
189+
},
190+
minimatch: {
191+
dependencies: {},
192+
integrity: 'sha1-UjYVelHk8ATBd/s8Un/33Xjw74M=',
193+
requires: {
194+
'brace-expansion': '^1.0.0',
195+
},
196+
version: '3.0.0',
197+
},
198+
prj1: {
199+
dependencies: {},
200+
integrity: '',
201+
requires: {
202+
minimatch: '3.0.0',
203+
},
204+
version: '0.0.0',
205+
},
206+
},
207+
install: [],
208+
metadata: {},
209+
name: 'yarn-test',
210+
remove: [],
211+
requires: {
212+
prj1: '0.0.0',
213+
},
214+
version: '1.0.0',
215+
};
216+
217+
return runAudit([], {}, 'workspace', async config => {
218+
const calledWithPipe = config.requestManager.request.mock.calls[0][0].body;
219+
const calledWith = JSON.parse(await gunzip(calledWithPipe));
220+
expect(calledWith).toEqual(expectedApiPost);
221+
});
222+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"actions": [],
3+
"advisories": {},
4+
"muted": [],
5+
"metadata": {
6+
"vulnerabilities": {
7+
"info": 0,
8+
"low": 0,
9+
"moderate": 0,
10+
"high": 0,
11+
"critical": 0
12+
},
13+
"dependencies": 0,
14+
"devDependencies": 0,
15+
"optionalDependencies": 0,
16+
"totalDependencies": 0
17+
}
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "yarn-test",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"private": true,
7+
"workspaces": ["packages/*"]
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "prj1",
3+
"private": true,
4+
"version": "0.0.0",
5+
"dependencies": {
6+
"minimatch": "3.0.0"
7+
}
8+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
balanced-match@^1.0.0:
6+
version "1.0.0"
7+
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
8+
integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c=
9+
10+
brace-expansion@^1.0.0:
11+
version "1.1.11"
12+
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
13+
integrity sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==
14+
dependencies:
15+
balanced-match "^1.0.0"
16+
concat-map "0.0.1"
17+
18+
19+
version "0.0.1"
20+
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
21+
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=
22+
23+
24+
version "3.0.0"
25+
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.0.tgz#5236157a51e4f004c177fb3c527ff7dd78f0ef83"
26+
integrity sha1-UjYVelHk8ATBd/s8Un/33Xjw74M=
27+
dependencies:
28+
brace-expansion "^1.0.0"

src/cli/commands/audit.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,27 @@ export default class Audit {
229229
return responseJson;
230230
}
231231

232+
_insertWorkspacePackagesIntoManifest(manifest: Object, resolver: PackageResolver) {
233+
if (resolver.workspaceLayout) {
234+
const workspaceAggregatorName = resolver.workspaceLayout.virtualManifestName;
235+
const workspaceManifest = resolver.workspaceLayout.workspaces[workspaceAggregatorName].manifest;
236+
237+
manifest.dependencies = Object.assign(manifest.dependencies || {}, workspaceManifest.dependencies);
238+
manifest.devDependencies = Object.assign(manifest.devDependencies || {}, workspaceManifest.devDependencies);
239+
manifest.optionalDependencies = Object.assign(
240+
manifest.optionalDependencies || {},
241+
workspaceManifest.optionalDependencies,
242+
);
243+
}
244+
}
245+
232246
async performAudit(
233247
manifest: Object,
234248
resolver: PackageResolver,
235249
linker: PackageLinker,
236250
patterns: Array<string>,
237251
): Promise<AuditVulnerabilityCounts> {
252+
this._insertWorkspacePackagesIntoManifest(manifest, resolver);
238253
const hoistedTrees = await hoistedTreeBuilder(resolver, linker, patterns);
239254
const auditTree = this._mapHoistedTreesToAuditTree(manifest, hoistedTrees);
240255
this.auditData = await this._fetchAudit(auditTree);

0 commit comments

Comments
 (0)