trivy icon indicating copy to clipboard operation
trivy copied to clipboard

feat(license): scan `vendor` directory for license for `go.mod` files

Open DmitriyLewen opened this issue 9 months ago • 4 comments

Description

There are cases when users use only vendor dir (go mod vendor) command.

For these cases we can't detect licenses from $GOPATH/pkg/mod dir.

New logic

graph TD
    A[Detect licenses for go.mod files] --> B{vendor direxists?}
    B -- yes --> C[use vendor dir to find licenses]
    B -- no --> D[use $GOPATH/pkg/mod]

Discussed in https://github.com/aquasecurity/trivy/discussions/8517

DmitriyLewen avatar Mar 11 '25 10:03 DmitriyLewen

Hi @DmitriyLewen !

Trivy reads go.mod files from $GOPATH/pkg/mod to collect dependencies (e.g. in gomodAnalyzer.collectDeps), but vendor packages don’t have that.

Is there a way to handle this case?

oneum20 avatar Apr 04 '25 07:04 oneum20

Hello @oneum20 What do you mean? Can you explain in more detail what you mean?

DmitriyLewen avatar Apr 04 '25 07:04 DmitriyLewen

Packages under $GOPATH/pkg/mod have their own go.mod files.

$ ls $GOPATH/pkg/mod/connectrpc.com/[email protected]
LICENSE                 README.md               buf.work.yaml           grpchealth.go
MAINTAINERS.md          SECURITY.md             go.mod                  grpchealth_test.go
Makefile                buf.gen.yaml            go.sum                  internal

Packages in the vendor directory do not have their own go.mod files.

$ ls  ./vendor/connectrpc.com/grpchealth 
LICENSE         Makefile        SECURITY.md     buf.work.yaml   internal
MAINTAINERS.md  README.md       buf.gen.yaml    grpchealth.go

In the following code, it looks like Trivy reads the package’s go.mod file to collect its dependencies.

https://github.com/aquasecurity/trivy/blob/bfa99d26faf31a9b41f0e09b13ec6a669c87d91b/pkg/fanal/analyzer/language/golang/mod/mod.go#L188-L191

oneum20 avatar Apr 04 '25 07:04 oneum20

hmm... You are right. Packages from vendor directory don't have go.mod files. This doesn't matter for license detection (we check LICENSE files), but it will affect the removal of unused dependencies.

But to avoid these problems - perhaps make senso to check both paths (i mean $GOPATH/pkg/mod and vendor dir)

DmitriyLewen avatar Apr 04 '25 09:04 DmitriyLewen

Added in #8689

DmitriyLewen avatar May 28 '25 10:05 DmitriyLewen