Skip to content

Commit

Permalink
assert download file path is subpath of server dir
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Jan 31, 2023
1 parent 2b4e675 commit a1f424b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ var rootCmd = &cobra.Command{
if err != nil {
log.Fatalln(err)
}

for _, file := range index.Files {
ok, err := util.PathIsSubpath(file.Path, serverDir)
if err != nil {
log.Println(err)
}
if err != nil || !ok {
log.Fatalln("File path is not safe: " + file.Path)
}
}

fmt.Println("Installing:", index.Name)
fmt.Printf("Flavor dependencies: %+v\n", index.Dependencies)

Expand Down
11 changes: 11 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ var updateCmd = &cobra.Command{
if err != nil {
log.Fatalln(err)
}

for path, _ := range newModPackInfo.File {
ok, err := util.PathIsSubpath(string(path), serverDir)
if err != nil {
log.Println(err)
}
if err != nil || !ok {
log.Fatalln("File path is not safe: " + path)
}
}

err = newModPackInfo.Write(path.Join(serverDir, "modpack.json.update"))
if err != nil {
log.Fatalln(err)
Expand Down
14 changes: 14 additions & 0 deletions util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ func PathIsDir(path string) bool {
return info.Mode().IsDir()
}

func PathIsSubpath(path string, basePath string) (bool, error) {
absBasePath, err := filepath.Abs(basePath)
if err != nil {
return false, err
}

_, err = filepath.Rel(absBasePath, path)
if err != nil {
return false, err
}

return true, nil
}

func FileDetection(hash string, path string) DetectType {
_, err := os.Stat(path)
if err != nil {
Expand Down

0 comments on commit a1f424b

Please sign in to comment.