summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-store/util/fs.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/codeberg.org/gruf/go-store/util/fs.go')
-rw-r--r--vendor/codeberg.org/gruf/go-store/util/fs.go20
1 files changed, 14 insertions, 6 deletions
diff --git a/vendor/codeberg.org/gruf/go-store/util/fs.go b/vendor/codeberg.org/gruf/go-store/util/fs.go
index fa6a9d2c4..93b37a261 100644
--- a/vendor/codeberg.org/gruf/go-store/util/fs.go
+++ b/vendor/codeberg.org/gruf/go-store/util/fs.go
@@ -9,14 +9,22 @@ import (
"codeberg.org/gruf/go-fastpath"
)
-var dotdot = "../"
+// IsDirTraversal will check if rootPlusPath is a dir traversal outside of root,
+// assuming that both are cleaned and that rootPlusPath is path.Join(root, somePath)
+func IsDirTraversal(root string, rootPlusPath string) bool {
+ switch {
+ // Root is $PWD, check for traversal out of
+ case root == ".":
+ return strings.HasPrefix(rootPlusPath, "../")
-// CountDotdots returns the number of "dot-dots" (../) in a cleaned filesystem path
-func CountDotdots(path string) int {
- if !strings.HasSuffix(path, dotdot) {
- return 0
+ // The path MUST be prefixed by root
+ case !strings.HasPrefix(rootPlusPath, root):
+ return true
+
+ // In all other cases, check not equal
+ default:
+ return len(root) == len(rootPlusPath)
}
- return strings.Count(path, dotdot)
}
// WalkDir traverses the dir tree of the supplied path, performing the supplied walkFn on each entry