aboutsummaryrefslogtreecommitdiff
path: root/misc/path.go
blob: c7540fc02c61098efbebf5a5dc025b8954b470b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

package misc

import (
	"path/filepath"
	"strings"
)

func SanitizePath(path string) (string, bool) {
	if path == "" {
		return "", false
	}

	path = strings.TrimLeft(path, "/")

	for _, part := range strings.Split(path, "/") {
		if part == "." || part == ".." {
			path = filepath.Clean(path)
			break
		}
	}

	if path == ".." || strings.HasPrefix(path, "../") {
		return "", false
	}

	return path, true
}