Skip to content

Commit 05dd6ce

Browse files
committed
fix: Stop using SecureJoin which messes up symlinks
1 parent 164252d commit 05dd6ce

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module github.com/kluctl/go-embed-python
33
go 1.19
44

55
require (
6-
github.com/cyphar/filepath-securejoin v0.2.4
76
github.com/gobwas/glob v0.2.3
87
github.com/klauspost/compress v1.17.7
98
github.com/rogpeppe/go-internal v1.12.0

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
github.com/cyphar/filepath-securejoin v0.2.4 h1:Ugdm7cg7i6ZK6x3xDF1oEu1nfkyfH53EtKeQYTC3kyg=
2-
github.com/cyphar/filepath-securejoin v0.2.4/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
31
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
42
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
53
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

internal/tar.go

+13-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package internal
33
import (
44
"archive/tar"
55
"fmt"
6-
securejoin "github.com/cyphar/filepath-securejoin"
76
"io"
87
"io/fs"
98
"os"
@@ -22,12 +21,13 @@ func ExtractTarStream(r io.Reader, targetPath string) error {
2221
return fmt.Errorf("ExtractTarStream: Next() failed: %w", err)
2322
}
2423

25-
header.Name = filepath.FromSlash(header.Name)
26-
27-
p, err := securejoin.SecureJoin(targetPath, header.Name)
28-
if err != nil {
29-
return err
24+
if !validRelPath(header.Name) {
25+
return fmt.Errorf("tar contained invalid name error %q", header.Name)
3026
}
27+
28+
p := filepath.FromSlash(header.Name)
29+
p = filepath.Join(targetPath, p)
30+
3131
err = os.MkdirAll(filepath.Dir(p), 0755)
3232
if err != nil {
3333
return err
@@ -67,6 +67,13 @@ func ExtractTarStream(r io.Reader, targetPath string) error {
6767
return nil
6868
}
6969

70+
func validRelPath(p string) bool {
71+
if p == "" || strings.Contains(p, `\`) || strings.HasPrefix(p, "/") || strings.Contains(p, "../") {
72+
return false
73+
}
74+
return true
75+
}
76+
7077
func AddToTar(tw *tar.Writer, pth string, name string, filter func(h *tar.Header, size int64) (*tar.Header, error)) error {
7178
fi, err := os.Lstat(pth)
7279
if err != nil {

0 commit comments

Comments
 (0)