Skip to content

Modern Docker Management, Designed for Everyone

fix(trivy): match scans by layer diff IDs

Kyle Mendellebb5a1ff6da4
diff --git a/backend/internal/vulnerability/children/scan/children/trivy/service.go b/backend/internal/vulnerability/children/scan/children/trivy/service.go
index 25864f123..970d4ab50 100644
--- a/backend/internal/vulnerability/children/scan/children/trivy/service.go
+++ b/backend/internal/vulnerability/children/scan/children/trivy/service.go
@@ -9,6 +9,7 @@ import (
"io"
"log/slog"
"os"
+ "slices"
"strings"
"sync"
"time"
@@ -336,8 +337,15 @@ func (s *Service) scanResult(ctx context.Context, dockerClient *client.Client, r
}
// The name can move to another image after it was selected; never store that image's findings under imageID.
- if scanned := report.Metadata.ImageID; mode == "exec" && scanned != "" && strings.TrimPrefix(scanned, "sha256:") != strings.TrimPrefix(imageID, "sha256:") {
- return nil, fmt.Errorf("%s now refers to image %s, not the selected image %s", imageName, scanned, imageID)
+ // Compare layer diff IDs: image IDs differ by image store (containerd uses the manifest digest).
+ if mode == "exec" {
+ inspect, inspectErr := dockerClient.ImageInspect(ctx, imageID)
+ if inspectErr != nil {
+ return nil, fmt.Errorf("selected image %s is no longer available: %w", imageID, inspectErr)
+ }
+ if len(report.Metadata.DiffIDs) > 0 && !slices.Equal(report.Metadata.DiffIDs, inspect.RootFS.Layers) {
+ return nil, fmt.Errorf("%s now refers to a different image than the selected image %s", imageName, imageID)
+ }
}
result := vulnerability.ConvertTrivyReportToScanResult(report, imageID, time.Now(), run.duration)
if len(raw) == 0 {