From f6937cf286bb568241e9e71e54c1462d792f1987 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Tue, 11 Feb 2025 17:25:01 +0800 Subject: [PATCH] misc: Add sanitize_path --- misc/path.go | 27 +++++++++++++++++++++++++++ diff --git a/misc/path.go b/misc/path.go new file mode 100644 index 0000000000000000000000000000000000000000..c7013d8a6833fbdc33364468ac3eee15b0a4b6ed --- /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 +} -- 2.48.1