Skip to content

Commit 4e096c2

Browse files
committed
fix: validate embedded app paths with filepath.IsLocal
1 parent 2db0a2d commit 4e096c2

1 file changed

Lines changed: 3 additions & 45 deletions

File tree

‎embed.go‎

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ import (
1010
"io/fs"
1111
"log"
1212
"os"
13-
"path"
1413
"path/filepath"
1514
"runtime"
16-
"strings"
1715
"time"
1816
)
1917

@@ -75,11 +73,10 @@ func untar(dir string) (err error) {
7573
// Ignore it.
7674
continue
7775
}
78-
rel, err := nativeRelPath(f.Name)
79-
if err != nil {
80-
return fmt.Errorf("tar file contained invalid name %q: %v", f.Name, err)
76+
if !filepath.IsLocal(f.Name) {
77+
return fmt.Errorf("tar file contained invalid name %q", f.Name)
8178
}
82-
abs := filepath.Join(dir, rel)
79+
abs := filepath.Join(dir, f.Name)
8380

8481
fi := f.FileInfo()
8582
mode := fi.Mode()
@@ -155,42 +152,3 @@ func untar(dir string) (err error) {
155152
}
156153
return nil
157154
}
158-
159-
// nativeRelPath verifies that p is a non-empty relative path
160-
// using either slashes or the buildlet's native path separator,
161-
// and returns it canonicalized to the native path separator.
162-
func nativeRelPath(p string) (string, error) {
163-
if p == "" {
164-
return "", errors.New("path not provided")
165-
}
166-
167-
if filepath.Separator != '/' && strings.Contains(p, string(filepath.Separator)) {
168-
clean := filepath.Clean(p)
169-
if filepath.IsAbs(clean) {
170-
return "", fmt.Errorf("path %q is not relative", p)
171-
}
172-
if clean == ".." || strings.HasPrefix(clean, ".."+string(filepath.Separator)) {
173-
return "", fmt.Errorf("path %q refers to a parent directory", p)
174-
}
175-
if strings.HasPrefix(p, string(filepath.Separator)) || filepath.VolumeName(clean) != "" {
176-
// On Windows, this catches semi-relative paths like "C:" (meaning “the
177-
// current working directory on volume C:”) and "\windows" (meaning “the
178-
// windows subdirectory of the current drive letter”).
179-
return "", fmt.Errorf("path %q is relative to volume", p)
180-
}
181-
return p, nil
182-
}
183-
184-
clean := path.Clean(p)
185-
if path.IsAbs(clean) {
186-
return "", fmt.Errorf("path %q is not relative", p)
187-
}
188-
if clean == ".." || strings.HasPrefix(clean, "../") {
189-
return "", fmt.Errorf("path %q refers to a parent directory", p)
190-
}
191-
canon := filepath.FromSlash(p)
192-
if filepath.VolumeName(canon) != "" {
193-
return "", fmt.Errorf("path %q begins with a native volume name", p)
194-
}
195-
return canon, nil
196-
}

0 commit comments

Comments
 (0)