Skip to content

Commit 804ea4a

Browse files
authored
fix(nodejs): use the default ID format to match licenses in pnpm packages. (#9661)
1 parent 35db88c commit 804ea4a

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"golang.org/x/xerrors"
1313

14+
"github.com/aquasecurity/trivy/pkg/dependency"
1415
"github.com/aquasecurity/trivy/pkg/dependency/parser/nodejs/packagejson"
1516
"github.com/aquasecurity/trivy/pkg/dependency/parser/nodejs/pnpm"
1617
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
@@ -65,8 +66,11 @@ func (a pnpmAnalyzer) PostAnalyze(ctx context.Context, input analyzer.PostAnalys
6566
}
6667

6768
// Fill licenses
68-
for i, lib := range app.Packages {
69-
if l, ok := licenses[lib.ID]; ok {
69+
for i, pkg := range app.Packages {
70+
// We use snapshots for pnpm package IDs.
71+
// But to match licenses, we need to use the ID-building logic as for `package.json` files.
72+
id := dependency.ID(types.NodePkg, pkg.Name, pkg.Version)
73+
if l, ok := licenses[id]; ok {
7074
app.Packages[i].Licenses = l
7175
}
7276
}

pkg/fanal/analyzer/language/nodejs/pnpm/pnpm_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ func Test_pnpmPkgLibraryAnalyzer_Analyze(t *testing.T) {
3939
},
4040
},
4141
},
42+
{
43+
name: "V9 with node_modules",
44+
dir: "testdata/happy-v9",
45+
want: &analyzer.AnalysisResult{
46+
Applications: []types.Application{
47+
{
48+
Type: types.Pnpm,
49+
FilePath: "pnpm-lock.yaml",
50+
Packages: types.Packages{
51+
{
52+
53+
Name: "vue-router",
54+
Version: "4.5.1",
55+
Licenses: []string{"MIT"},
56+
Relationship: types.RelationshipDirect,
57+
},
58+
},
59+
},
60+
},
61+
},
62+
},
4263
{
4364
name: "without node_modules",
4465
dir: "testdata/no-node_modules",

pkg/fanal/analyzer/language/nodejs/pnpm/testdata/happy-v9/node_modules/vue-router/package.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/fanal/analyzer/language/nodejs/pnpm/testdata/happy-v9/pnpm-lock.yaml

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)