Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions experimental/clashapi/server_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (s *Server) downloadZIP(body io.Reader, output string) error {
}
defer reader.Close()
trimDir := zipIsInSingleDirectory(reader.File)
cleanOutput := filepath.Clean(output) + string(os.PathSeparator)
for _, file := range reader.File {
if file.FileInfo().IsDir() {
continue
Expand All @@ -118,6 +119,10 @@ func (s *Server) downloadZIP(body io.Reader, output string) error {
return err
}
savePath := filepath.Join(saveDirectory, pathElements[len(pathElements)-1])
cleanSavePath := filepath.Clean(savePath)
if !strings.HasPrefix(cleanSavePath, cleanOutput) {
return os.ErrPermission
}
err = downloadZIPEntry(s.ctx, file, savePath)
if err != nil {
return err
Expand Down
46 changes: 46 additions & 0 deletions experimental/clashapi/server_resources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package clashapi

import (
"archive/zip"
"bytes"
"context"
"os"
"path/filepath"
"testing"
)

func TestDownloadZIPZipSlip(t *testing.T) {
tempDir := t.TempDir()

var buf bytes.Buffer

zw := zip.NewWriter(&buf)

_, err := zw.Create("../../../pwned.txt")
if err != nil {
t.Fatal(err)
}

err = zw.Close()
if err != nil {
t.Fatal(err)
}

server := &Server{
ctx: context.Background(),
}

err = server.downloadZIP(bytes.NewReader(buf.Bytes()), tempDir)

if err == nil {
t.Fatal("expected zip slip error")
}

outsidePath := filepath.Join(tempDir, "..", "..", "..", "pwned.txt")

_, err = os.Stat(outsidePath)

if !os.IsNotExist(err) {
t.Fatal("zip slip created file outside target directory")
}
}