Skip to content

Commit

Permalink
chunked: refactor value into const
Browse files Browse the repository at this point in the history
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
  • Loading branch information
giuseppe authored and kwilczynski committed Nov 25, 2024
1 parent 02927a3 commit 2f80f63
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
14 changes: 10 additions & 4 deletions pkg/chunked/compression_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import (
expMaps "golang.org/x/exp/maps"
)

const (
// maxTocSize is the maximum size of a blob that we will attempt to process.
// It is used to prevent DoS attacks from layers that embed a very large TOC file.
maxTocSize = (1 << 20) * 50
)

var typesToTar = map[string]byte{
TypeReg: tar.TypeReg,
TypeLink: tar.TypeLink,
Expand Down Expand Up @@ -74,7 +80,7 @@ func readEstargzChunkedManifest(blobStream ImageSourceSeekable, blobSize int64,

size := int64(blobSize - footerSize - tocOffset)
// set a reasonable limit
if size > (1<<20)*50 {
if size > maxTocSize {
return nil, 0, errors.New("manifest too big")
}

Expand Down Expand Up @@ -103,7 +109,7 @@ func readEstargzChunkedManifest(blobStream ImageSourceSeekable, blobSize int64,
return err
}
// set a reasonable limit
if header.Size > (1<<20)*50 {
if header.Size > maxTocSize {
return errors.New("manifest too big")
}

Expand Down Expand Up @@ -163,10 +169,10 @@ func readZstdChunkedManifest(blobStream ImageSourceSeekable, tocDigest digest.Di
}

// set a reasonable limit
if manifestChunk.Length > (1<<20)*50 {
if manifestChunk.Length > maxTocSize {
return nil, nil, nil, 0, errors.New("manifest too big")
}
if manifestLengthUncompressed > (1<<20)*50 {
if manifestLengthUncompressed > maxTocSize {
return nil, nil, nil, 0, errors.New("manifest too big")
}

Expand Down
17 changes: 13 additions & 4 deletions pkg/chunked/storage_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ func TestGetBlobAtWithErrors(t *testing.T) {

is := &mockImageSource{streams: streams, errors: errorsC}

resultChan, err := getBlobAt(is)
chunks := []ImageSourceChunk{
{Offset: 0, Length: 1},
{Offset: 1, Length: 1},
}
resultChan, err := getBlobAt(is, chunks...)
require.NoError(t, err)

expectedErrors := []string{"error1", "error2"}
Expand All @@ -149,13 +153,18 @@ func TestGetBlobAtMixedStreamsAndErrors(t *testing.T) {
errorsC := make(chan error, 1)

streams <- mockReadCloserFromContent("stream1")
streams <- mockReadCloserFromContent("stream2")
errorsC <- errors.New("error1")
close(streams)
close(errorsC)

is := &mockImageSource{streams: streams, errors: errorsC}

resultChan, err := getBlobAt(is)
chunks := []ImageSourceChunk{
{Offset: 0, Length: 1},
{Offset: 1, Length: 1},
}
resultChan, err := getBlobAt(is, chunks...)
require.NoError(t, err)

var receivedStreams int
Expand All @@ -167,6 +176,6 @@ func TestGetBlobAtMixedStreamsAndErrors(t *testing.T) {
receivedStreams++
}
}
assert.Equal(t, 0, receivedStreams)
assert.Equal(t, 2, receivedErrors)
assert.Equal(t, 2, receivedStreams)
assert.Equal(t, 1, receivedErrors)
}

0 comments on commit 2f80f63

Please sign in to comment.