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

import (
	"path/filepath"
	"strings"
)

// SanitizePath adapts filepath.Clean for FUSE paths.
// 1) A leading slash is 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 {
	if len(path) == 0 {
		return ""
	}
	// Drop leading slash
	if path[0] == '/' {
		path = path[1:]
	}
	clean := filepath.Clean(path)
	if clean == "." {
		return ""
	}
	if clean == ".." || strings.HasPrefix(clean, "../") {
		return ""
	}
	return clean
}