Skip to content

Commit 9f3c8f1

Browse files
Merge pull request #110 from SteamServerUI/fix-zipslips-round2
This is attempt 2 to fix path traversal issues within steamcmd-helper… working still as expected
2 parents 29642b6 + 3e14b63 commit 9f3c8f1

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

src/steamcmd/steamcmd-helper.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ func isSymlinkInsideRoot(name, link, root string) bool {
4444
return !strings.HasPrefix(rel, "..") && !strings.HasPrefix(abs, string(os.PathSeparator))
4545
}
4646

47+
func isPathInsideRoot(path, root string) bool {
48+
cleanPath := filepath.Clean(path)
49+
rel, err := filepath.Rel(root, cleanPath)
50+
if err != nil {
51+
return false
52+
}
53+
return !strings.HasPrefix(rel, "..") && !strings.HasPrefix(cleanPath, string(os.PathSeparator))
54+
}
55+
4756
// createSteamCMDDirectory creates the SteamCMD directory.
4857
func createSteamCMDDirectory(steamCMDDir string) error {
4958
if err := os.MkdirAll(steamCMDDir, os.ModePerm); err != nil {
@@ -183,6 +192,9 @@ func untar(dest string, r io.Reader) error {
183192
return fmt.Errorf("failed to create directory %s: %v", target, err)
184193
}
185194
case tar.TypeReg:
195+
if !isPathInsideRoot(target, dest) {
196+
return fmt.Errorf("invalid file path attempts to write outside root directory: %s", target)
197+
}
186198
outFile, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY, os.FileMode(header.Mode))
187199
if err != nil {
188200
return fmt.Errorf("failed to create file %s: %v", target, err)
@@ -230,6 +242,9 @@ func Unzip(zipReader io.ReaderAt, size int64, dest string) error {
230242
fpath := filepath.Join(dest, f.Name)
231243

232244
// Ensure the file path is within the destination directory
245+
if !isPathInsideRoot(fpath, dest) {
246+
return fmt.Errorf("invalid file path attempts to write outside root directory: %s", fpath)
247+
}
233248
relPath, err := filepath.Rel(dest, fpath)
234249
if err != nil || strings.HasPrefix(relPath, "..") || strings.HasPrefix(relPath, string(os.PathSeparator)) {
235250
return fmt.Errorf("invalid file path: %s", fpath)

0 commit comments

Comments
 (0)