Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(nodejs): do not detect lock file in node_modules as an app #4949

Merged
merged 2 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/fanal/analyzer/language/nodejs/npm/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/utils/fsutils"
xpath "github.com/aquasecurity/trivy/pkg/x/path"
)

func init() {
Expand Down Expand Up @@ -85,7 +86,7 @@ func (a npmLibraryAnalyzer) PostAnalyze(_ context.Context, input analyzer.PostAn

func (a npmLibraryAnalyzer) Required(filePath string, _ os.FileInfo) bool {
fileName := filepath.Base(filePath)
if fileName == types.NpmPkgLock {
if fileName == types.NpmPkgLock && !xpath.Contains(filePath, "node_modules") {
return true
}
// The file path to package.json - */node_modules/<package_name>/package.json
Expand Down Expand Up @@ -122,7 +123,7 @@ func (a npmLibraryAnalyzer) parseNpmPkgLock(fsys fs.FS, path string) (*types.App
}

func (a npmLibraryAnalyzer) findLicenses(fsys fs.FS, lockPath string) (map[string]string, error) {
dir := filepath.Dir(lockPath)
dir := path.Dir(lockPath)
root := path.Join(dir, "node_modules")
if _, err := fs.Stat(fsys, root); errors.Is(err, fs.ErrNotExist) {
log.Logger.Infof(`To collect the license information of packages in %q, "npm install" needs to be performed beforehand`, lockPath)
Expand Down
5 changes: 5 additions & 0 deletions pkg/fanal/analyzer/language/nodejs/npm/npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ func Test_nodePkgLibraryAnalyzer_Required(t *testing.T) {
filePath: "npm/node_modules/package.json",
want: false,
},
{
name: "lock file in node_modules",
filePath: "npm/node_modules/html2canvas/package-lock.json",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
13 changes: 13 additions & 0 deletions pkg/x/path/path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package path

import (
"strings"

"golang.org/x/exp/slices"
)

// Contains reports whether the path contains the subpath.
func Contains(filePath, subpath string) bool {
ss := strings.Split(filePath, "/")
return slices.Contains(ss, subpath)
}
50 changes: 50 additions & 0 deletions pkg/x/path/path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package path

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestContains(t *testing.T) {
type args struct {
filePath string
subpath string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "file",
args: args{
filePath: "go.mod",
subpath: "go.mod",
},
want: true,
},
{
name: "dir",
args: args{
filePath: "app/node_modules/express/package.json",
subpath: "node_modules",
},
want: true,
},
{
name: "path",
args: args{
filePath: "app/node_modules/express/package.json",
subpath: "app/node_modules",
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Contains(tt.args.filePath, tt.args.subpath)
assert.Equal(t, tt.want, got)
})
}
}