summaryrefslogtreecommitdiff
path: root/internal/ctlsock/sanitize.go
blob: 7cf77a59db3fb32fdad5304169f837e66f869cf1 (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 ctlsock

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
}