Skip to content

Commit fa1077b

Browse files
knqyf263Copilot
andauthored
fix(redhat): trim invalid suffix from content_sets in manifest parsing (#8818)
Co-authored-by: Copilot <[email protected]>
1 parent e322f21 commit fa1077b

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

pkg/detector/ospkg/redhat/redhat.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package redhat
33
import (
44
"context"
55
"fmt"
6+
"regexp"
67
"slices"
78
"sort"
89
"strings"
910
"time"
1011

1112
version "github.com/knqyf263/go-rpm-version"
13+
"github.com/samber/lo"
1214
"golang.org/x/xerrors"
1315

1416
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
@@ -109,6 +111,9 @@ func (s *Scanner) detect(osVer string, pkg ftypes.Package) ([]types.DetectedVuln
109111
nvr = fmt.Sprintf("%s-%s", pkg.BuildInfo.Nvr, pkg.BuildInfo.Arch)
110112
}
111113

114+
// Clean content sets from generic suffixes (__8, __9, etc.)
115+
contentSets = cleanContentSets(contentSets)
116+
112117
advisories, err := s.vs.Get(pkgName, contentSets, []string{nvr})
113118
if err != nil {
114119
return nil, xerrors.Errorf("failed to get Red Hat advisories: %w", err)
@@ -197,3 +202,23 @@ func addModularNamespace(name, label string) string {
197202
}
198203
return name
199204
}
205+
206+
// Match generic version suffixes like "__8", "__9", "__10", but preserve EUS suffixes like "__9_DOT_2".
207+
// Examples:
208+
// - Matches: "repo__8", "repo__10"
209+
// - Does not match: "repo__9_DOT_2", "repo__10_DOT_1"
210+
var genericSuffixPattern = regexp.MustCompile(`__\d+$`)
211+
212+
// cleanContentSets removes generic suffixes like "__8" from content sets.
213+
// These are Red Hat image build artifacts and not valid repository names.
214+
// Examples:
215+
//
216+
// Input: []string{"repo__8", "repo__9_DOT_2", "repo__10"}
217+
// Output: []string{"repo", "repo__9_DOT_2", "repo"}
218+
//
219+
// cf. https://github.com/aquasecurity/trivy-db/issues/435
220+
func cleanContentSets(contentSets []string) []string {
221+
return lo.Map(contentSets, func(cs string, _ int) string {
222+
return genericSuffixPattern.ReplaceAllString(cs, "")
223+
})
224+
}

pkg/detector/ospkg/redhat/redhat_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,56 @@ func TestScanner_Detect(t *testing.T) {
420420
},
421421
wantErr: true,
422422
},
423+
{
424+
name: "content sets with invalid suffix",
425+
fixtures: []string{
426+
"testdata/fixtures/redhat.yaml",
427+
"testdata/fixtures/cpe.yaml",
428+
},
429+
args: args{
430+
osVer: "8.3",
431+
pkgs: []ftypes.Package{
432+
{
433+
Name: "vim-minimal",
434+
Version: "7.4.160",
435+
Release: "5.el8",
436+
Epoch: 2,
437+
Arch: "x86_64",
438+
SrcName: "vim",
439+
SrcVersion: "7.4.160",
440+
SrcRelease: "5.el8",
441+
SrcEpoch: 2,
442+
Layer: ftypes.Layer{
443+
DiffID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
444+
},
445+
BuildInfo: &ftypes.BuildInfo{
446+
ContentSets: []string{
447+
"rhel-8-for-x86_64-baseos-rpms__8",
448+
"rhel-8-for-x86_64-appstream-rpms__8",
449+
},
450+
},
451+
},
452+
},
453+
},
454+
want: []types.DetectedVulnerability{
455+
{
456+
VulnerabilityID: "CVE-2019-12735",
457+
VendorIDs: []string{
458+
"RHSA-2019:1619",
459+
},
460+
PkgName: "vim-minimal",
461+
InstalledVersion: "2:7.4.160-5.el8",
462+
FixedVersion: "2:7.4.160-7.el8_7",
463+
SeveritySource: vulnerability.RedHat,
464+
Vulnerability: dbTypes.Vulnerability{
465+
Severity: dbTypes.SeverityMedium.String(),
466+
},
467+
Layer: ftypes.Layer{
468+
DiffID: "sha256:932da51564135c98a49a34a193d6cd363d8fa4184d957fde16c9d8527b3f3b02",
469+
},
470+
},
471+
},
472+
},
423473
}
424474
for _, tt := range tests {
425475
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)