Skip to content

Commit 13f1bb2

Browse files
authored
Merge pull request #564 from allisonlarson/b-chmod-failed-tar
Remove failed decompressed files
2 parents cfc3a4f + c0ef527 commit 13f1bb2

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

decompress_tar_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package getter
66
import (
77
"archive/tar"
88
"bytes"
9+
"errors"
910
"os"
1011
"path/filepath"
1112
"runtime"
@@ -184,3 +185,29 @@ func TestDecompressTarPermissions(t *testing.T) {
184185
expected["directory/setuid"] = masked
185186
testDecompressorPermissions(t, d, input, expected, os.FileMode(060000000))
186187
}
188+
189+
func TestDecompressTarPermissionsFailed(t *testing.T) {
190+
d := new(TarDecompressor)
191+
input := "./test-fixtures/decompress-tar/bad.tar"
192+
193+
td := t.TempDir()
194+
195+
// Destination is always joining result so that we have a new path
196+
dst := filepath.Join(td, "subdir", "result")
197+
198+
err := d.Decompress(dst, input, true, os.FileMode(0))
199+
if err == nil {
200+
t.Fatalf("expected error when decompressing bad tar file but got none")
201+
}
202+
203+
expectedDst := filepath.Join(dst, "directory/setuid2")
204+
// Attempt to get file information
205+
_, err = os.Stat(expectedDst)
206+
207+
if !errors.Is(err, os.ErrNotExist) {
208+
if err != nil {
209+
t.Fatalf("unexpected error when checking for file '%s': %s", expectedDst, err)
210+
}
211+
t.Fatalf("expected file '%s' to not exist", expectedDst)
212+
}
213+
}

get_file_copy.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func Copy(ctx context.Context, dst io.Writer, src io.Reader) (int64, error) {
3535

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

4848
_, err = io.Copy(dstF, src)
4949
if err != nil {
50+
// Close & remove the file in case of partial write
51+
_ = dstF.Close()
52+
_ = os.Remove(dst)
5053
return err
5154
}
5255

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

77-
dstF, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, fmode)
80+
dstF, err := os.OpenFile(dst, os.O_RDWR|os.O_CREATE|os.O_TRUNC, mode(fmode, umask))
7881
if err != nil {
7982
return 0, err
8083
}
8184
defer func() { _ = dstF.Close() }()
8285

8386
count, err := Copy(ctx, dstF, srcF)
8487
if err != nil {
88+
// Close & remove the file in case of partial write
89+
_ = dstF.Close()
90+
_ = os.Remove(dst)
8591
return 0, err
8692
}
8793

8.79 KB
Binary file not shown.

0 commit comments

Comments
 (0)