summaryrefslogtreecommitdiff
path: root/internal/ctlsocksrv/sanitize.go
blob: 4333872c3b9848ab30b90ae397a58add9c0c1722 (plain)
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
28
29
30
31
32
package ctlsocksrv

import (
	"path/filepath"
	"strings"
)

// SanitizePath adapts filepath.Clean for FUSE paths.
// 1) Leading slash(es) are dropped
// 2) It returns "" instead of "."
// 3) If the cleaned path points above CWD (start with ".."), an empty string
//    is returned
// See the TestSanitizePath testcases for examples.
func SanitizePath(path string) string {
	// (1)
	for len(path) > 0 && path[0] == '/' {
		path = path[1:]
	}
	if len(path) == 0 {
		return ""
	}
	clean := filepath.Clean(path)
	// (2)
	if clean == "." {
		return ""
	}
	// (3)
	if clean == ".." || strings.HasPrefix(clean, "../") {
		return ""
	}
	return clean
}