From f6937cf286bb568241e9e71e54c1462d792f1987 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Tue, 11 Feb 2025 17:25:01 +0800 Subject: misc: Add sanitize_path --- misc/path.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 misc/path.go (limited to 'misc') diff --git a/misc/path.go b/misc/path.go new file mode 100644 index 0000000..c7013d8 --- /dev/null +++ b/misc/path.go @@ -0,0 +1,27 @@ +package misc + +import ( + "path/filepath" + "strings" +) + +func sanitize_path(path string) (string, bool) { + if path == "" { + return "", false + } + + path = strings.TrimLeft(path, "/") + + for _, part := range strings.Split(path, "/") { + if part == "." || part == ".." { + path = filepath.Clean(path) + break + } + } + + if path == ".." || strings.HasPrefix(path, "../") { + return "", false + } + + return path, true +} -- cgit v1.2.3