Skip to content

Commit ca54293

Browse files
DmitriyLewenNimit Johri
authored andcommitted
fix: also check filepath when removing duplicate packages (aquasecurity#9142)
1 parent c75ed21 commit ca54293

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

pkg/fanal/applier/docker.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,9 @@ func ApplyLayers(layers []ftypes.BlobInfo) ftypes.ArtifactDetail {
238238
// De-duplicate same debian packages from different dirs
239239
// cf. https://github.com/aquasecurity/trivy/issues/8297
240240
mergedLayer.Packages = xslices.ZeroToNil(lo.UniqBy(mergedLayer.Packages, func(pkg ftypes.Package) string {
241-
return cmp.Or(pkg.ID, fmt.Sprintf("%s@%s", pkg.Name, utils.FormatVersion(pkg)))
241+
id := cmp.Or(pkg.ID, fmt.Sprintf("%s@%s", pkg.Name, utils.FormatVersion(pkg)))
242+
// To avoid deduplicating packages with the same ID but from different locations (e.g. RPM archives), check the file path.
243+
return fmt.Sprintf("%s/%s", id, pkg.FilePath)
242244
}))
243245

244246
for _, app := range mergedLayer.Applications {

pkg/fanal/applier/docker_test.go

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ func TestApplyLayers(t *testing.T) {
258258
},
259259
},
260260
{
261-
name: "happy path with duplicate of debian packages",
261+
name: "happy path with duplicate packages",
262262
inputLayers: []types.BlobInfo{
263263
{
264264
SchemaVersion: 2,
@@ -276,6 +276,36 @@ func TestApplyLayers(t *testing.T) {
276276
},
277277
},
278278
},
279+
{
280+
FilePath: "foo/socat-1.7.3.2-2.el7.x86_64.rpm",
281+
Packages: types.Packages{
282+
{
283+
Name: "socat",
284+
Version: "1.7.3.2",
285+
Release: "2.el7",
286+
Arch: "x86_64",
287+
SrcName: "socat",
288+
SrcVersion: "1.7.3.2",
289+
SrcRelease: "2.el7",
290+
FilePath: "foo/socat-1.7.3.2-2.el7.x86_64.rpm",
291+
},
292+
},
293+
},
294+
{
295+
FilePath: "bar/socat-1.7.3.2-2.el7.x86_64.rpm",
296+
Packages: types.Packages{
297+
{
298+
Name: "socat",
299+
Version: "1.7.3.2",
300+
Release: "2.el7",
301+
Arch: "x86_64",
302+
SrcName: "socat",
303+
SrcVersion: "1.7.3.2",
304+
SrcRelease: "2.el7",
305+
FilePath: "bar/socat-1.7.3.2-2.el7.x86_64.rpm",
306+
},
307+
},
308+
},
279309
},
280310
},
281311
{
@@ -311,6 +341,38 @@ func TestApplyLayers(t *testing.T) {
311341
DiffID: "sha256:96e320b34b5478d8b369ca43ffaa88ff6dd9499ec72b792ca21b1e8b0c55670f",
312342
},
313343
},
344+
{
345+
Name: "socat",
346+
Version: "1.7.3.2",
347+
Release: "2.el7",
348+
Arch: "x86_64",
349+
SrcName: "socat",
350+
SrcVersion: "1.7.3.2",
351+
SrcRelease: "2.el7",
352+
FilePath: "bar/socat-1.7.3.2-2.el7.x86_64.rpm",
353+
Layer: types.Layer{
354+
DiffID: "sha256:96e320b34b5478d8b369ca43ffaa88ff6dd9499ec72b792ca21b1e8b0c55670f",
355+
},
356+
Identifier: types.PkgIdentifier{
357+
UID: "bfb68335f6284b36",
358+
},
359+
},
360+
{
361+
Name: "socat",
362+
Version: "1.7.3.2",
363+
Release: "2.el7",
364+
Arch: "x86_64",
365+
SrcName: "socat",
366+
SrcVersion: "1.7.3.2",
367+
SrcRelease: "2.el7",
368+
FilePath: "foo/socat-1.7.3.2-2.el7.x86_64.rpm",
369+
Layer: types.Layer{
370+
DiffID: "sha256:96e320b34b5478d8b369ca43ffaa88ff6dd9499ec72b792ca21b1e8b0c55670f",
371+
},
372+
Identifier: types.PkgIdentifier{
373+
UID: "4d8db4fac0caf460",
374+
},
375+
},
314376
},
315377
},
316378
},

0 commit comments

Comments
 (0)