summaryrefslogtreecommitdiff
path: root/internal/ctlsocksrv/sanitize.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2020-05-09 17:36:41 +0200
committerJakob Unterwurzacher2020-05-09 17:36:41 +0200
commit16221facb9066ccf03015ccfe9e7ca784b0d2099 (patch)
tree46abce23a8592542563deb463a98d4318deb24af /internal/ctlsocksrv/sanitize.go
parent3ef563493a9d0774513ec0d6aab4cfbc0f6334e5 (diff)
ctlsock: create exported ctlsock client library
The former interal ctlsock server package is renamed to ctlsocksrv.
Diffstat (limited to 'internal/ctlsocksrv/sanitize.go')
-rw-r--r--internal/ctlsocksrv/sanitize.go32
1 files changed, 32 insertions, 0 deletions
diff --git a/internal/ctlsocksrv/sanitize.go b/internal/ctlsocksrv/sanitize.go
new file mode 100644
index 0000000..4333872
--- /dev/null
+++ b/internal/ctlsocksrv/sanitize.go
@@ -0,0 +1,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
+}