feat(echo): support Echo Go package scanning (#11438)

Signed-off-by: Cursor <cursoragent@cursor.com>
Signed-off-by: Laiza Angrest <laiza.angrest@echo.ai>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: DmitriyLewen <dmitriy.lewen@smartforce.io>
This commit is contained in:
authored and GitHub committed 2026-11-10 08:07:08 +00:00
1 parent 10ab0f82f0
commit 0e40f8178a
13 files changed
+510 -9

No files matched your search

+1
View File
@@ -37,6 +37,7 @@ Trivy identifies them by the version suffix and uses Echo's own security advisor
| [Java](../language/java.md) | `+echo.N` | `org.springframework:spring-core` `6.1.21+echo.1` |
| [Node.js](../language/nodejs.md) | `+echo.N` | `nanoid` `3.3.6+echo.2` |
| [.NET](../language/dotnet.md) | `+echo.N` | `System.Security.Cryptography.Xml` `8.0.0+echo.1` |
| [Go](../language/golang.md) | `+echo.N` | `golang.org/x/net` `v0.22.0+echo.1` |
Packages without the Echo version suffix, and packages from unsupported ecosystems, are scanned against the upstream advisories as usual.
+59 -1
View File
@@ -1,6 +1,7 @@
package mod
import (
"bytes"
"context"
"fmt"
goversion "go/version"
@@ -12,6 +13,8 @@ import (
"github.com/samber/lo"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
"golang.org/x/xerrors"
"github.com/aquasecurity/trivy/pkg/dependency"
@@ -30,6 +33,10 @@ var (
// gopkg.in without user segment
// Example: gopkg.in/pkg.v3 -> github.com/go-pkg/pkg
VCSUrlGoPkgInRegexWithoutUser = regexp.MustCompile(`^gopkg\.in/([^.]+)\..*$`)
// echoVersionRegex matches Echo's non-zero-padded numeric build metadata
// segment, optionally followed by additional metadata identifiers.
echoVersionRegex = regexp.MustCompile(`\+echo\.(0|[1-9]\d*)(\.|$)`)
)
type Parser struct {
@@ -82,7 +89,7 @@ func (p *Parser) Parse(_ context.Context, r xio.ReadSeekerAt) ([]ftypes.Package,
return nil, nil, false, xerrors.Errorf("file read error: %w", err)
}
modFileParsed, err := modfile.Parse("go.mod", goModData, nil)
modFileParsed, err := parseModFile(goModData)
if err != nil {
return nil, nil, false, xerrors.Errorf("go.mod parse error: %w", err)
}
@@ -193,6 +200,57 @@ func (p *Parser) Parse(_ context.Context, r xio.ReadSeekerAt) ([]ftypes.Package,
return pkgSlice, deps, skipIndirect, nil
}
// parseModFile parses go.mod and keeps Echo versions (e.g. v0.55.0+echo.1
// and v0.55.0+echo.1.extra.1), which modfile.Parse otherwise strips as
// build metadata.
//
// The first parse uses a nil VersionFixer, which silently canonicalizes an
// Echo version and loses its build metadata. The original suffix therefore
// can't be recovered by patching the parsed file, so a second parse with a
// custom VersionFixer is required to preserve it.
func parseModFile(data []byte) (*modfile.File, error) {
f, err := modfile.Parse("go.mod", data, nil)
if err != nil {
return nil, err
}
// Only files with Echo versions need the second parse below.
if !bytes.Contains(data, []byte("+echo.")) {
return f, nil
}
// A VersionFixer also applies to retract directives, which Trivy doesn't
// use and which may contain version queries. Intentionally blank every
// retract directive before re-parsing, not only those containing "+echo.".
data = bytes.Clone(data)
for _, r := range f.Retract {
for i := r.Syntax.Start.Byte; i < r.Syntax.End.Byte; i++ {
data[i] = ' '
}
}
return modfile.Parse("go.mod", data, versionFixer)
}
// isEchoVersion reports whether version has Echo's non-zero-padded "+echo.N"
// build-metadata segment.
func isEchoVersion(version string) bool {
return echoVersionRegex.MatchString(version)
}
// versionFixer keeps versions recognized by a vendor (currently only Echo) that are
// otherwise valid SemVer, and canonicalizes all others, matching what modfile.Parse
// does with a nil VersionFixer.
func versionFixer(path, version string) (string, error) {
if semver.IsValid(version) && isEchoVersion(version) {
return version, nil
}
if canonical := module.CanonicalVersion(version); canonical != "" {
return canonical, nil
}
return "", xerrors.Errorf("invalid version %q of %s: must be of the form v1.2.3", version, path)
}
// lessThan checks if the Go version is less than `<majorVer>.<minorVer>`
func lessThan(ver string, majorVer, minorVer int) bool {
if ver == "" {
@@ -102,6 +102,21 @@ func TestParse(t *testing.T) {
wantDeps: defaultGoDepParserDeps,
wantSkipIndirect: false,
},
{
name: "Echo versions",
file: "testdata/echo/gomod",
replace: true,
wantPkgs: GoModEcho,
wantDeps: GoModEchoDeps,
wantSkipIndirect: false,
},
{
name: "Echo marker only in retract",
file: "testdata/echo-retract-only/gomod",
wantPkgs: GoModEchoRetractOnly,
wantDeps: GoModEchoRetractOnlyDeps,
wantSkipIndirect: false,
},
{
name: "go 1.16",
file: "testdata/go116/go.mod",
@@ -135,6 +150,98 @@ func TestParse(t *testing.T) {
}
}
func TestVersionFixer(t *testing.T) {
tests := []struct {
name string
version string
want string
wantErr bool
}{
{
name: "Echo version",
version: "v0.55.0+echo.1",
want: "v0.55.0+echo.1",
},
{
name: "Echo prerelease version",
version: "v1.2.3-rc.1+echo.2",
want: "v1.2.3-rc.1+echo.2",
},
{
name: "zero Echo revision",
version: "v1.2.3+echo.0",
want: "v1.2.3+echo.0",
},
{
name: "zero-padded Echo revision",
version: "v1.2.3+echo.01",
want: "v1.2.3",
},
{
name: "non-Echo build metadata",
version: "v1.2.3+build.1",
want: "v1.2.3",
},
{
name: "Echo version with later metadata identifiers",
version: "v1.2.3+echo.1.extra.1",
want: "v1.2.3+echo.1.extra.1",
},
{
name: "multiple Echo metadata segments",
version: "v1.2.3+echo.1.echo.2",
want: "v1.2.3+echo.1.echo.2",
},
{
name: "multiple build metadata delimiters",
version: "v1.2.3+echo.1+echo.2",
wantErr: true,
},
{
name: "Echo revision joined to another identifier",
version: "v1.2.3+echo.1-extra.1",
want: "v1.2.3",
},
{
name: "negative Echo revision",
version: "v1.2.3+echo.-1",
want: "v1.2.3",
},
{
name: "non-numeric Echo revision",
version: "v1.2.3+echo.x",
want: "v1.2.3",
},
{
name: "Echo-like identifier",
version: "v1.2.3+echo.1foo",
want: "v1.2.3",
},
{
name: "incompatible version",
version: "v2.0.0+incompatible",
want: "v2.0.0+incompatible",
},
{
name: "malformed Echo version",
version: "not-a-version+echo.1",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := versionFixer("example.com/module", tt.version)
if tt.wantErr {
require.ErrorContains(t, err, "must be of the form v1.2.3")
return
}
require.NoError(t, err)
require.Equal(t, tt.want, got)
})
}
}
func TestToolchainVersion(t *testing.T) {
tests := []struct {
name string
@@ -407,6 +407,85 @@ var (
},
}
GoModEcho = []ftypes.Package{
{
ID: "example.com/app",
Name: "example.com/app",
Relationship: ftypes.RelationshipRoot,
},
{
ID: "example.com/mismatch@v1.4.5+echo.1",
Name: "example.com/mismatch",
Version: "v1.4.5+echo.1",
Relationship: ftypes.RelationshipDirect,
},
{
ID: "example.com/ordinary@v1.5.6",
Name: "example.com/ordinary",
Version: "v1.5.6",
Relationship: ftypes.RelationshipDirect,
},
{
ID: "example.com/replacement@v1.3.4+echo.2",
Name: "example.com/replacement",
Version: "v1.3.4+echo.2",
Relationship: ftypes.RelationshipDirect,
},
{
ID: "example.com/v2module/v2@v2.0.0+echo.1",
Name: "example.com/v2module/v2",
Version: "v2.0.0+echo.1",
Relationship: ftypes.RelationshipDirect,
},
{
ID: "example.com/variant@v1.2.3+echo.2.variant.1",
Name: "example.com/variant",
Version: "v1.2.3+echo.2.variant.1",
Relationship: ftypes.RelationshipDirect,
},
{
ID: "golang.org/x/net@v0.55.0+echo.1",
Name: "golang.org/x/net",
Version: "v0.55.0+echo.1",
Relationship: ftypes.RelationshipDirect,
},
}
GoModEchoDeps = ftypes.Dependencies{
{
ID: "example.com/app",
DependsOn: []string{
"example.com/mismatch@v1.4.5+echo.1",
"example.com/ordinary@v1.5.6",
"example.com/replacement@v1.3.4+echo.2",
"example.com/v2module/v2@v2.0.0+echo.1",
"example.com/variant@v1.2.3+echo.2.variant.1",
"golang.org/x/net@v0.55.0+echo.1",
},
},
}
GoModEchoRetractOnly = []ftypes.Package{
{
ID: "example.com/retract-only",
Name: "example.com/retract-only",
Relationship: ftypes.RelationshipRoot,
},
{
ID: "example.com/ordinary@v1.2.3",
Name: "example.com/ordinary",
Version: "v1.2.3",
Relationship: ftypes.RelationshipDirect,
},
}
GoModEchoRetractOnlyDeps = ftypes.Dependencies{
{
ID: "example.com/retract-only",
DependsOn: []string{"example.com/ordinary@v1.2.3"},
},
}
// execute go mod tidy in go116 folder
GoMod116 = []ftypes.Package{
{
@@ -0,0 +1,7 @@
module example.com/retract-only
go 1.22.0
require example.com/ordinary v1.2.3+build.1
retract v1.0.0+echo.1
+22
View File
@@ -0,0 +1,22 @@
module example.com/app
go 1.22.0
require (
golang.org/x/net v0.55.0+echo.1
example.com/variant v1.2.3+echo.2.variant.1
example.com/replaced v1.3.4+echo.1
example.com/mismatch v1.4.5+echo.1
example.com/ordinary v1.5.6+build.1
example.com/v2module/v2 v2.0.0+echo.1
)
replace (
example.com/replaced v1.3.4+echo.1 => example.com/replacement v1.3.4+echo.2
example.com/mismatch v1.4.5+echo.2 => example.com/not-used v1.4.5+echo.3
)
retract (
master
[master, release-branch]
)
+118
View File
@@ -455,6 +455,124 @@ func TestDriver_Detect(t *testing.T) {
},
},
},
{
name: "plain go package uses upstream advisory",
fixtures: []string{
"testdata/fixtures/echo-go.yaml",
"testdata/fixtures/data-source.yaml",
},
libType: ftypes.GoModule,
args: args{
pkgName: "golang.org/x/net",
pkgVer: "v0.22.0",
},
want: []types.DetectedVulnerability{
{
VulnerabilityID: "CVE-2023-45288",
PkgName: "golang.org/x/net",
InstalledVersion: "v0.22.0",
FixedVersion: "0.23.0",
DataSource: &dbTypes.DataSource{
ID: vulnerability.GHSA,
Name: "GitHub Security Advisory Go",
URL: "https://github.com/advisories?query=type%3Areviewed+ecosystem%3Ago",
},
},
},
},
{
name: "echo go package below Echo fix",
fixtures: []string{
"testdata/fixtures/echo-go.yaml",
"testdata/fixtures/data-source.yaml",
},
libType: ftypes.GoModule,
args: args{
pkgName: "golang.org/x/net",
pkgVer: "v0.21.0+echo.1",
},
want: []types.DetectedVulnerability{
{
VulnerabilityID: "CVE-2023-45288",
VendorIDs: []string{
"GHSA-4v7x-pqxf-cx7m",
"ECHO-326d-6f7d-c967",
},
PkgName: "golang.org/x/net",
InstalledVersion: "v0.21.0+echo.1",
FixedVersion: "v0.22.0+echo.1",
DataSource: &dbTypes.DataSource{
ID: "echo-osv",
Name: "Echo OSV",
URL: "https://advisory.echohq.com/osv/all.zip",
},
},
},
},
{
name: "zero-padded Go revision stays on the upstream advisory",
fixtures: []string{
"testdata/fixtures/echo-go.yaml",
"testdata/fixtures/data-source.yaml",
},
libType: ftypes.GoBinary,
args: args{
pkgName: "golang.org/x/net",
pkgVer: "v0.22.0+echo.01",
},
want: []types.DetectedVulnerability{
{
VulnerabilityID: "CVE-2023-45288",
PkgName: "golang.org/x/net",
InstalledVersion: "v0.22.0+echo.01",
FixedVersion: "0.23.0",
DataSource: &dbTypes.DataSource{
ID: vulnerability.GHSA,
Name: "GitHub Security Advisory Go",
URL: "https://github.com/advisories?query=type%3Areviewed+ecosystem%3Ago",
},
},
},
},
{
name: "echo go package at Echo fix",
fixtures: []string{
"testdata/fixtures/echo-go.yaml",
"testdata/fixtures/data-source.yaml",
},
libType: ftypes.GoModule,
args: args{
pkgName: "golang.org/x/net",
pkgVer: "v0.22.0+echo.1",
},
want: nil,
},
{
name: "echo go package above Echo fix",
fixtures: []string{
"testdata/fixtures/echo-go.yaml",
"testdata/fixtures/data-source.yaml",
},
libType: ftypes.GoBinary,
args: args{
pkgName: "golang.org/x/net",
pkgVer: "v0.22.0+echo.2",
},
want: nil,
},
{
name: "echo go package with later metadata segments",
fixtures: []string{
"testdata/fixtures/echo-go.yaml",
"testdata/fixtures/data-source.yaml",
},
libType: ftypes.GoModule,
args: args{
pkgName: "golang.org/x/net",
pkgVer: "v0.22.0+echo.1.extra.1",
},
want: nil,
},
{
name: "echo pip package",
fixtures: []string{
+34 -6
View File
@@ -5,6 +5,8 @@ import (
"regexp"
"strings"
"golang.org/x/mod/semver"
"github.com/aquasecurity/go-version/pkg/version"
"github.com/aquasecurity/trivy-db/pkg/ecosystem"
"github.com/aquasecurity/trivy/pkg/detector/library"
@@ -13,25 +15,35 @@ import (
"github.com/aquasecurity/trivy/pkg/detector/library/compare/pep440"
)
// echoLocalSegmentRe matches the "+echo.N" version segment,
// e.g. "+echo.1" in "2.14.2+echo.1" or "2.14.2+echo.1.post1".
var echoLocalSegmentRe = regexp.MustCompile(`\+echo\.\d+([._-]|$)`)
var (
// echoLocalSegmentRe matches the "+echo.N" version segment used by the
// existing Echo ecosystems.
echoLocalSegmentRe = regexp.MustCompile(`\+echo\.\d+([._-]|$)`)
// echoSemverSegmentRe requires the numeric Echo revision to be a complete
// SemVer build identifier. Additional metadata identifiers remain valid,
// but a "-" joined identifier (e.g. "+echo.1-extra") is rejected, unlike
// echoLocalSegmentRe used by the other Echo ecosystems.
echoSemverSegmentRe = regexp.MustCompile(`\+echo\.(0|[1-9]\d*)(\.|$)`)
)
func init() {
library.RegisterSupplier(echoSupplier{
pipComparer: pep440.NewComparer(pep440.AllowLocalSpecifier()),
npmComparer: npm.NewComparer(npm.WithBuildMetadata()),
nugetComparer: compare.NewGenericComparer(compare.WithBuildMetadata(version.BuildMetadataOrderNodeSemver)),
goComparer: compare.NewGenericComparer(compare.WithBuildMetadata(version.BuildMetadataOrderNodeSemver)),
})
}
// echoSupplier matches language packages patched by Echo.
// Echo provides patched versions of Python (pip), Java (maven), JavaScript
// (npm), and .NET (NuGet) packages identified by a "+echo.N" version segment.
// (npm), .NET (NuGet), and Go packages identified by a "+echo.N" version segment.
type echoSupplier struct {
pipComparer compare.Comparer
npmComparer compare.Comparer
nugetComparer compare.Comparer
goComparer compare.Comparer
}
func (echoSupplier) Name() string {
@@ -43,6 +55,13 @@ func (echoSupplier) Name() string {
// string, where N is a numeric revision (e.g. "2.14.2+echo.1" or "2.14.2+echo.1.post1").
func (echoSupplier) Match(eco ecosystem.Type, _, pkgVer string) library.MatchResult {
switch eco {
case ecosystem.Go:
if strings.Contains(pkgVer, "+echo.") &&
echoSemverSegmentRe.MatchString(pkgVer) &&
isValidGoSemVer(pkgVer) {
return library.Matched
}
return library.NoMatch
case ecosystem.Pip, ecosystem.Maven, ecosystem.Npm, ecosystem.NuGet:
default:
return library.NoMatch
@@ -53,14 +72,21 @@ func (echoSupplier) Match(eco ecosystem.Type, _, pkgVer string) library.MatchRes
return library.NoMatch
}
// isValidGoSemVer gates the lenient go-version comparer used below: without
// it, a malformed version that merely contains a "+echo." substring would
// still be routed to the Echo bucket instead of the upstream advisory.
func isValidGoSemVer(ver string) bool {
return semver.IsValid(ver) || semver.IsValid("v"+ver)
}
// BucketPrefix returns the supplier-specific advisory bucket prefix.
func (e echoSupplier) BucketPrefix(eco ecosystem.Type) string {
return fmt.Sprintf("%s %s::", e.Name(), eco)
}
// Comparer returns a version comparer for the given ecosystem.
// pip, npm and NuGet get comparers that keep the "+echo.N" suffix significant,
// since PEP 440 local versions and SemVer build metadata are otherwise ignored;
// pip, npm, NuGet, and Go get comparers that keep the "+echo.N" suffix significant,
// PEP 440 local versions and SemVer build metadata are otherwise ignored;
// Maven's default comparer already orders it correctly.
func (e echoSupplier) Comparer(eco ecosystem.Type, defaultComparer compare.Comparer) compare.Comparer {
switch eco {
@@ -70,6 +96,8 @@ func (e echoSupplier) Comparer(eco ecosystem.Type, defaultComparer compare.Compa
return e.npmComparer
case ecosystem.NuGet:
return e.nugetComparer
case ecosystem.Go:
return e.goComparer
default:
return defaultComparer
}
+43 -1
View File
@@ -53,10 +53,52 @@ func TestEchoSupplier_Match(t *testing.T) {
want: library.Matched,
},
{
name: "go package is not supported",
name: "go package with +echo.1 suffix",
eco: ecosystem.Go,
pkgName: "golang.org/x/crypto",
pkgVer: "v0.26.0+echo.1",
want: library.Matched,
},
{
name: "go package without v prefix",
eco: ecosystem.Go,
pkgName: "golang.org/x/crypto",
pkgVer: "0.26.0+echo.1",
want: library.Matched,
},
{
name: "go package with later metadata identifiers",
eco: ecosystem.Go,
pkgName: "golang.org/x/crypto",
pkgVer: "v0.26.0+echo.1.extra.1",
want: library.Matched,
},
{
name: "go package with joined Echo revision",
eco: ecosystem.Go,
pkgName: "golang.org/x/crypto",
pkgVer: "v0.26.0+echo.1-extra.1",
want: library.NoMatch,
},
{
name: "go package with malformed core version",
eco: ecosystem.Go,
pkgName: "golang.org/x/crypto",
pkgVer: "v0.26.0.1+echo.1",
want: library.NoMatch,
},
{
name: "go package with empty trailing metadata identifier",
eco: ecosystem.Go,
pkgName: "golang.org/x/crypto",
pkgVer: "v0.26.0+echo.1.",
want: library.NoMatch,
},
{
name: "go package with zero-padded Echo revision",
eco: ecosystem.Go,
pkgName: "golang.org/x/crypto",
pkgVer: "v0.26.0+echo.01",
want: library.NoMatch,
},
{
+11
View File
@@ -114,6 +114,15 @@ func Test_lookupSupplier(t *testing.T) {
wantPrefix: "echo nuget::",
wantDefaultComparer: false,
},
{
name: "echo go package returns supplier prefix and build-aware comparer",
eco: ecosystem.Go,
pkgName: "golang.org/x/net",
pkgVer: "v0.55.0+echo.1",
wantMatch: library.Matched,
wantPrefix: "echo go::",
wantDefaultComparer: false,
},
{
name: "non-supplier pip package returns no match",
eco: ecosystem.Pip,
@@ -149,6 +158,8 @@ func Test_lookupSupplier(t *testing.T) {
assert.Equal(t, npm.NewComparer(npm.WithBuildMetadata()), comparer)
case ecosystem.NuGet:
assert.Equal(t, compare.NewGenericComparer(compare.WithBuildMetadata(version.BuildMetadataOrderNodeSemver)), comparer)
case ecosystem.Go:
assert.Equal(t, compare.NewGenericComparer(compare.WithBuildMetadata(version.BuildMetadataOrderNodeSemver)), comparer)
}
}
})
@@ -80,3 +80,8 @@
ID: "echo-osv"
Name: "Echo OSV"
URL: "https://advisory.echohq.com/osv/all.zip"
- key: "echo go::Echo OSV"
value:
ID: "echo-osv"
Name: "Echo OSV"
URL: "https://advisory.echohq.com/osv/all.zip"
+23
View File
@@ -0,0 +1,23 @@
- bucket: "go::GitHub Security Advisory Go"
pairs:
- bucket: golang.org/x/net
pairs:
- key: CVE-2023-45288
value:
PatchedVersions:
- "0.23.0"
VulnerableVersions:
- "<0.23.0"
- bucket: "echo go::Echo OSV"
pairs:
- bucket: golang.org/x/net
pairs:
- key: CVE-2023-45288
value:
VendorIDs:
- GHSA-4v7x-pqxf-cx7m
- ECHO-326d-6f7d-c967
PatchedVersions:
- "v0.22.0+echo.1"
VulnerableVersions:
- "<v0.22.0+echo.1"
@@ -34,7 +34,7 @@ func init() {
analyzer.RegisterPostAnalyzer(analyzer.TypeGoMod, newGoModAnalyzer)
}
const version = 2
const version = 3
var (
requiredFiles = []string{