Skip to content

Commit 3ac6388

Browse files
authored
fix(nodejs): use project dir when searching for workspaces for Yarn.lock files (aquasecurity#6102)
1 parent 3c1601b commit 3ac6388

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@test/bar-generators",
3+
"version": "0.0.1",
4+
"dependencies": {
5+
"hoek": "6.1.3"
6+
}
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "@test/foo",
3+
"version": "1.0.0",
4+
"repository": "ssh://[email protected]/test.git/foo",
5+
"workspaces": [
6+
"bar/*"
7+
],
8+
"main": "index.js",
9+
"license": "MIT"
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
6+
version "6.1.3"
7+
resolved "https://registry.yarnpkg.com/hoek/-/hoek-6.1.3.tgz#73b7d33952e01fe27a38b0457294b79dd8da242c"
8+
integrity sha512-YXXAAhmF9zpQbC7LEcREFtXfGq5K1fmd+4PHkBq8NUqmzW3G+Dq10bI/i0KucLRwss3YYFQ0fSfoxBZYiGUqtQ==

pkg/fanal/analyzer/language/nodejs/yarn/yarn.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (a yarnAnalyzer) parsePackageJsonDependencies(fsys fs.FS, filePath string)
268268
devDependencies := rootPkg.DevDependencies
269269

270270
if len(rootPkg.Workspaces) > 0 {
271-
pkgs, err := a.traverseWorkspaces(fsys, rootPkg.Workspaces)
271+
pkgs, err := a.traverseWorkspaces(fsys, path.Dir(filePath), rootPkg.Workspaces)
272272
if err != nil {
273273
return nil, nil, xerrors.Errorf("traverse workspaces error: %w", err)
274274
}
@@ -281,7 +281,7 @@ func (a yarnAnalyzer) parsePackageJsonDependencies(fsys fs.FS, filePath string)
281281
return dependencies, devDependencies, nil
282282
}
283283

284-
func (a yarnAnalyzer) traverseWorkspaces(fsys fs.FS, workspaces []string) ([]packagejson.Package, error) {
284+
func (a yarnAnalyzer) traverseWorkspaces(fsys fs.FS, dir string, workspaces []string) ([]packagejson.Package, error) {
285285
var pkgs []packagejson.Package
286286

287287
required := func(path string, _ fs.DirEntry) bool {
@@ -298,6 +298,9 @@ func (a yarnAnalyzer) traverseWorkspaces(fsys fs.FS, workspaces []string) ([]pac
298298
}
299299

300300
for _, workspace := range workspaces {
301+
// We need to add the path to the `package.json` file
302+
// to properly get the pattern to search in `fs`
303+
workspace = path.Join(dir, workspace)
301304
matches, err := fs.Glob(fsys, workspace)
302305
if err != nil {
303306
return nil, err

pkg/fanal/analyzer/language/nodejs/yarn/yarn_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,31 @@ func Test_yarnLibraryAnalyzer_Analyze(t *testing.T) {
127127
},
128128
},
129129
},
130+
{
131+
name: "Project with workspace placed in sub dir",
132+
dir: "testdata/project-with-workspace-in-subdir",
133+
want: &analyzer.AnalysisResult{
134+
Applications: []types.Application{
135+
{
136+
Type: types.Yarn,
137+
FilePath: "foo/yarn.lock",
138+
Libraries: types.Packages{
139+
{
140+
141+
Name: "hoek",
142+
Version: "6.1.3",
143+
Locations: []types.Location{
144+
{
145+
StartLine: 5,
146+
EndLine: 8,
147+
},
148+
},
149+
},
150+
},
151+
},
152+
},
153+
},
154+
},
130155
{
131156
name: "no package.json",
132157
dir: "testdata/no-packagejson",

0 commit comments

Comments
 (0)