Skip to content

Commit

Permalink
Fix PGET Expected Byte Check
Browse files Browse the repository at this point in the history
Tar files have padding. Verify the end of the file is null and only
error if there is non-null or we haven't matched content-length from
origin.

This behavior is an artifact of multi-readers if the final multireader
is potentially all null bytes. New test mimics observed behavior.
  • Loading branch information
tempusfrangit committed Dec 3, 2024
1 parent f56ac41 commit ab1ab9d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 5 additions & 4 deletions pkg/consumer/tar_extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package consumer_test
import (
"archive/tar"
"bytes"
"io"
"os"
"path"
"testing"
Expand Down Expand Up @@ -110,7 +111,7 @@ func TestTarExtractor_Consume(t *testing.T) {
r.NoError(err)

// Create a reader from the tar file bytes
reader := bytes.NewReader(tarFileBytes)
reader := io.MultiReader(bytes.NewReader(tarFileBytes), bytes.NewReader(make([]byte, 1024)))

// Create a temporary directory to extract the tar file
tmpDir, err := os.MkdirTemp("", "tarExtractorTest-")
Expand All @@ -120,15 +121,15 @@ func TestTarExtractor_Consume(t *testing.T) {

tarConsumer := consumer.TarExtractor{}
targetDir := path.Join(tmpDir, "extract")
r.NoError(tarConsumer.Consume(reader, targetDir, int64(len(tarFileBytes))))
r.NoError(tarConsumer.Consume(reader, targetDir, int64(len(tarFileBytes)+1024)))

// Check if the extraction was successful
checkTarExtraction(t, targetDir)

// Test with incorrect expectedBytes
_, _ = reader.Seek(0, 0)
reader = io.MultiReader(bytes.NewReader(tarFileBytes), bytes.NewReader(make([]byte, 1024)))
targetDir = path.Join(tmpDir, "extract-fail")
r.Error(tarConsumer.Consume(reader, targetDir, int64(len(tarFileBytes)-1)))
r.Error(tarConsumer.Consume(reader, targetDir, int64(len(tarFileBytes)+1024-1)))
}

func checkTarExtraction(t *testing.T, targetDir string) {
Expand Down
18 changes: 18 additions & 0 deletions pkg/extract/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ func TarFile(r *bufio.Reader, destDir string, overwrite bool) error {
return fmt.Errorf("error creating links: %w", err)
}

// Read the rest of the bytes from the archive and verify they are all null bytes
// This is for validation that the byte count is correct
padding := make([]byte, 1024)
for {
n, err := r.Read(padding)
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("error reading padding bytes: %w", err)
}
for _, b := range padding[:n] {
if b != 0x00 {
return fmt.Errorf("unexpected non-null byte in padding: %x", b)
}
}
}

elapsed := time.Since(startTime).Seconds()
logger.Debug().
Str("extractor", "tar").
Expand Down

0 comments on commit ab1ab9d

Please sign in to comment.