Skip to content

Commit 0936bc6

Browse files
authored
Merge pull request #65 from DataDog/jmachado/backport-remote-artifact-type
fix(image): don't reject remote images whose ArtifactType is just a config media type
2 parents 5d0baf7 + 7c9b4aa commit 0936bc6

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

pkg/fanal/image/remote.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/google/go-containerregistry/pkg/name"
99
v1 "github.com/google/go-containerregistry/pkg/v1"
10+
v1types "github.com/google/go-containerregistry/pkg/v1/types"
1011
"golang.org/x/xerrors"
1112

1213
"github.com/aquasecurity/trivy/pkg/fanal/types"
@@ -21,9 +22,11 @@ func tryRemote(ctx context.Context, imageName string, ref name.Reference, option
2122
if err != nil {
2223
return nil, cleanup, err
2324
}
24-
// ArtifactType being non-empty indicates this is not a regular container image
25-
// (e.g., Helm charts, WASM modules, or other OCI artifacts)
26-
if desc.ArtifactType != "" {
25+
26+
// An empty ArtifactType or an ArtifactType with a config media type indicates a
27+
// regular container image. Any other ArtifactType is treated as a non-image
28+
// artifact (e.g., Helm charts, WASM modules, or other OCI artifacts).
29+
if desc.ArtifactType != "" && !v1types.MediaType(desc.ArtifactType).IsConfig() {
2730
return nil, cleanup, xerrors.Errorf("unsupported artifact type %q for image %q", desc.ArtifactType, imageName)
2831
}
2932
img, err := desc.Image()

pkg/fanal/image/remote_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,29 @@ func Test_tryRemote(t *testing.T) {
144144
},
145145
wantErr: "unsupported artifact type",
146146
},
147+
{
148+
// Regression test: a single-arch image manifest (as opposed to a
149+
// multi-arch manifest list/index) has a Config field, and when the
150+
// manifest itself doesn't set an explicit `artifactType`,
151+
// go-containerregistry falls back to using the config's media type
152+
// as the descriptor's ArtifactType. A regular container image config
153+
// media type must NOT be treated as an unsupported artifact type.
154+
name: "regular image with explicit docker config media type",
155+
imageName: "test/dockerconfig:latest",
156+
setupImage: func(t *testing.T, ref name.Reference) {
157+
configFile, err := img.ConfigFile()
158+
require.NoError(t, err)
159+
160+
imageToWrite, err := mutate.Config(img, configFile.Config)
161+
require.NoError(t, err)
162+
163+
imageToWrite = mutate.ConfigMediaType(imageToWrite, "application/vnd.docker.container.image.v1+json")
164+
165+
err = remote.Write(ref, imageToWrite)
166+
require.NoError(t, err)
167+
},
168+
wantName: "/test/dockerconfig:latest",
169+
},
147170
{
148171
name: "image not found",
149172
imageName: "test/notfound:latest",

0 commit comments

Comments
 (0)