diff options
author | Runxi Yu <me@runxiyu.org> | 2025-02-11 17:25:01 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-02-11 17:25:01 +0800 |
commit | f6937cf286bb568241e9e71e54c1462d792f1987 (patch) | |
tree | 3219b325d5f1205817879a94c0e1ce0408038e5c /misc | |
parent | clog: Non-structured functions (diff) | |
download | go-lindenii-common-f6937cf286bb568241e9e71e54c1462d792f1987.tar.gz go-lindenii-common-f6937cf286bb568241e9e71e54c1462d792f1987.tar.zst go-lindenii-common-f6937cf286bb568241e9e71e54c1462d792f1987.zip |
misc: Add sanitize_path
Diffstat (limited to 'misc')
-rw-r--r-- | misc/path.go | 27 |
1 files changed, 27 insertions, 0 deletions
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 +} |