Skip to content

Commit

Permalink
exporter: validate null config metadata from gateway
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
(cherry picked from commit ef536af15b2d351b8f0459022decc2a4955b1cb2)
(cherry picked from commit a8a6bc5180696624b18b5dc4ed4f9cf1a278ef27)
  • Loading branch information
tonistiigi committed Jan 31, 2024
1 parent 369b850 commit 83edaef
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func TestIntegration(t *testing.T) {
testMultipleRecordsWithSameLayersCacheImportExport,
testExportLocalNoPlatformSplit,
testExportLocalNoPlatformSplitOverwrite,
testValidateNullConfig,
)
}

Expand Down
50 changes: 50 additions & 0 deletions client/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package client

import (
"context"
"io"
"testing"

"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/frontend/gateway/client"
"github.com/moby/buildkit/util/testutil/integration"
"github.com/stretchr/testify/require"
)

func testValidateNullConfig(t *testing.T, sb integration.Sandbox) {
requiresLinux(t)

ctx := sb.Context()

c, err := New(ctx, sb.Address())
require.NoError(t, err)
defer c.Close()

b := func(ctx context.Context, c client.Client) (*client.Result, error) {
def, err := llb.Scratch().Marshal(ctx)
if err != nil {
return nil, err
}

res, err := c.Solve(ctx, client.SolveRequest{
Evaluate: true,
Definition: def.ToPB(),
})
if err != nil {
return nil, err
}
res.AddMeta("containerimage.config", []byte("null"))
return res, nil
}

_, err = c.Build(ctx, SolveOpt{
Exports: []ExportEntry{
{
Type: ExporterOCI,
Output: fixedWriteCloser(nopWriteCloser{io.Discard}),
},
},
}, "", b, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "invalid null image config for export")
}
9 changes: 9 additions & 0 deletions exporter/containerimage/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,20 @@ func parseHistoryFromConfig(dt []byte) ([]ocispecs.History, error) {
}

func patchImageConfig(dt []byte, descs []ocispecs.Descriptor, history []ocispecs.History, cache []byte, epoch *time.Time) ([]byte, error) {
var img ocispecs.Image
if err := json.Unmarshal(dt, &img); err != nil {
return nil, errors.Wrap(err, "invalid image config for export")
}

m := map[string]json.RawMessage{}
if err := json.Unmarshal(dt, &m); err != nil {
return nil, errors.Wrap(err, "failed to parse image config for patch")
}

if m == nil {
return nil, errors.Errorf("invalid null image config for export")
}

var rootFS ocispecs.RootFS
rootFS.Type = "layers"
for _, desc := range descs {
Expand Down

0 comments on commit 83edaef

Please sign in to comment.