Skip to content
This repository was archived by the owner on Mar 24, 2023. It is now read-only.

Commit 63da032

Browse files
committed
allow writing files to the os tempdir
1 parent 651bad5 commit 63da032

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

pkg/lifecycle/render/dockerlayer/layer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (u *Unpacker) Execute(
7070

7171
err = util.IsLegalPath(basePath)
7272
if err != nil {
73-
return errors.Wrap(err, "write github asset")
73+
return errors.Wrap(err, "write docker layer")
7474
}
7575

7676
debug.Log(

pkg/util/legal_path.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,28 @@ package util
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
67
"strings"
78

89
"github.com/pkg/errors"
910
)
1011

11-
// IsLegalPath checks if the provided path is a relative path within the current working directory. If it is not, it returns an error.
12+
// IsLegalPath checks if the provided path is a relative path within the current working directory or within the os tempdir.
13+
// If it is not, it returns an error.
1214
func IsLegalPath(path string) error {
1315

1416
if filepath.IsAbs(path) {
17+
relAbsPath, err := filepath.Rel(os.TempDir(), path)
18+
if err != nil {
19+
return fmt.Errorf("cannot write to an absolute path: %s, got error finding relative path from tempdir: %s", path, err.Error())
20+
}
21+
22+
// subdirectories of the os tempdir are fine
23+
if !strings.Contains(relAbsPath, "..") {
24+
return nil
25+
}
26+
1527
return fmt.Errorf("cannot write to an absolute path: %s", path)
1628
}
1729

pkg/util/legal_path_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package util
22

3-
import "testing"
3+
import (
4+
"os"
5+
"path/filepath"
6+
"testing"
7+
)
48

59
func TestIsLegalPath(t *testing.T) {
610
tests := []struct {
@@ -28,6 +32,11 @@ func TestIsLegalPath(t *testing.T) {
2832
path: "./happy/../../../unhappy/path",
2933
wantErr: true,
3034
},
35+
{
36+
name: "absolute path to tempdir",
37+
path: filepath.Join(os.TempDir(), "mydir"),
38+
wantErr: false,
39+
},
3140
}
3241
for _, tt := range tests {
3342
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)