aboutsummaryrefslogtreecommitdiff
path: root/misc/path.go
blob: 1b2e8a3bdffc8e3ef490090c6f5f57127731cf80 (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 Sanitize_path(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
}