Lindenii Project Forge
Login

go-lindenii-common

Common library for the Lindenii Project
Commit info
ID
f6937cf286bb568241e9e71e54c1462d792f1987
Author
Runxi Yu <me@runxiyu.org>
Author date
Tue, 11 Feb 2025 17:25:01 +0800
Committer
Runxi Yu <me@runxiyu.org>
Committer date
Tue, 11 Feb 2025 17:25:01 +0800
Actions
misc: Add sanitize_path
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
}