aboutsummaryrefslogtreecommitdiff
path: root/internal/ctlsocksrv/sanitize.go
blob: 227294331d31ade82b844caac6ed32ff91c2c9bf (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
33
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
}