Skip to content
Open
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
27 changes: 27 additions & 0 deletions decompress_tar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package getter
import (
"archive/tar"
"bytes"
"errors"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -184,3 +185,29 @@ func TestDecompressTarPermissions(t *testing.T) {
expected["directory/setuid"] = masked
testDecompressorPermissions(t, d, input, expected, os.FileMode(060000000))
}

func TestDecompressTarPermissionsFailed(t *testing.T) {
d := new(TarDecompressor)
input := "./test-fixtures/decompress-tar/bad.tar"

td := t.TempDir()

// Destination is always joining result so that we have a new path
dst := filepath.Join(td, "subdir", "result")

err := d.Decompress(dst, input, true, os.FileMode(0))
if err == nil {
t.Fatalf("expected error when decompressing bad tar file but got none")
}

expectedDst := filepath.Join(dst, "directory/setuid2")
// Attempt to get file information
_, err = os.Stat(expectedDst)

if !errors.Is(err, os.ErrNotExist) {
if err != nil {
t.Fatalf("unexpected error when checking for file '%s': %s", expectedDst, err)
}
t.Fatalf("expected file '%s' to not exist", expectedDst)
}
}
10 changes: 8 additions & 2 deletions get_file_copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Copy(ctx context.Context, dst io.Writer, src io.Reader) (int64, error) {

// copyReader copies from an io.Reader into a file, using umask to create the dst file
func copyReader(dst string, src io.Reader, fmode, umask os.FileMode, fileSizeLimit int64) error {
dstF, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fmode)
dstF, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode(fmode, umask))
if err != nil {
return err
}
Expand All @@ -47,6 +47,9 @@ func copyReader(dst string, src io.Reader, fmode, umask os.FileMode, fileSizeLim

_, err = io.Copy(dstF, src)
if err != nil {
// Close & remove the file in case of partial write
_ = dstF.Close()
Copy link
Member

Choose a reason for hiding this comment

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

We already call close in a deferred function on line 42, so do we need this? In any case, having a second call won't cause any errors as we are throwing away the returned error.

Copy link
Member Author

Choose a reason for hiding this comment

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

The windows test case would fail here because the file was reported as being in use at the time of removal. Adding in the explicit file closed fixed it and allowed the file to be removed, so it seems like windows has a different requirement/handling that needs the file to be explicitly closed before removing it. I figured it's better to close the file an extra time as opposed to trying to check.

_ = os.Remove(dst)
return err
}

Expand Down Expand Up @@ -74,14 +77,17 @@ func copyFile(ctx context.Context, dst, src string, disableSymlinks bool, fmode,
}
defer func() { _ = srcF.Close() }()

dstF, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fmode)
dstF, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode(fmode, umask))
if err != nil {
return 0, err
}
defer func() { _ = dstF.Close() }()

count, err := Copy(ctx, dstF, srcF)
if err != nil {
// Close & remove the file in case of partial write
_ = dstF.Close()
Copy link
Member

Choose a reason for hiding this comment

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

We already call close in a deferred function on line 84, so do we need this? In any case, having a second call won't cause any errors as we are throwing away the returned error.

Copy link
Member Author

Choose a reason for hiding this comment

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

Same reasoning as above, keeping the way these are handled in sync.

_ = os.Remove(dst)
return 0, err
}

Expand Down
Binary file added test-fixtures/decompress-tar/bad.tar
Binary file not shown.
Loading