Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cvetools/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ func parseSocketFromRepo(repo string) (string, string) {
return "", repo
}

func dockerImageSaveID(id string) string {
if len(id) == 64 {
return "sha256:" + id
}
return id
}

Comment on lines +64 to +70

@pohanhuang pohanhuang Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func dockerImageSaveID(id string) string {
if len(id) == 64 {
return "sha256:" + id
}
return id
}
const sha256Prefix = "sha256:"
func dockerImageSaveID(id string) string {
if d, err := goDigest.Parse(id); err == nil {
return d.String()
}
if d, err := goDigest.Parse(sha256Prefix + id); err == nil {
return d.String()
}
return id
}

func (s *ScanTools) GetLocalImageMeta(ctx context.Context, repository, tag string) (*container.ImageMeta, share.ScanErrorCode) {
sock, repo := parseSocketFromRepo(repository)
if sock == "" {
Expand Down Expand Up @@ -119,7 +126,7 @@ func (s *ScanTools) LoadLocalImage(ctx context.Context, repository, tag, imgPath
return nil, nil, nil, share.ScanErrorCode_ScanErrContainerAPI
}

file, err := rt.GetImageFile(meta.ID)
file, err := rt.GetImageFile(dockerImageSaveID(meta.ID))
if err != nil {
log.WithFields(log.Fields{"error": err}).Error("Failed to get image")
if errdefs.IsNotFound(err) {
Expand Down Expand Up @@ -862,3 +869,4 @@ func getSignatureDataForImage(ctx context.Context, rc *scan.RegClient, repo, dig
s.Manifest = string(info.RawManifest)
return s, share.ScanErrorCode_ScanErrNone
}

36 changes: 36 additions & 0 deletions cvetools/image_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cvetools

import "testing"

func TestDockerImageSaveID(t *testing.T) {
tests := []struct {
name string
id string
want string
}{
{
name: "bare sha256",
id: "fd791d74b68913cbb027c6546007b3f0d3bc45125f797758156952bc2d6daf40",
want: "sha256:fd791d74b68913cbb027c6546007b3f0d3bc45125f797758156952bc2d6daf40",
},
{
name: "canonical sha256",
id: "sha256:fd791d74b68913cbb027c6546007b3f0d3bc45125f797758156952bc2d6daf40",
want: "sha256:fd791d74b68913cbb027c6546007b3f0d3bc45125f797758156952bc2d6daf40",
},
{
name: "short image ID",
id: "fd791d74b689",
want: "fd791d74b689",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := dockerImageSaveID(tt.id); got != tt.want {
t.Fatalf("dockerImageSaveID(%q) = %q, want %q", tt.id, got, tt.want)
}
})
}
}