(pathStr string)
| 148 | } |
| 149 | |
| 150 | func ExpandHomeDir(pathStr string) (string, error) { |
| 151 | if pathStr != "~" && !strings.HasPrefix(pathStr, "~/") && (!strings.HasPrefix(pathStr, `~\`) || runtime.GOOS != "windows") { |
| 152 | return filepath.Clean(pathStr), nil |
| 153 | } |
| 154 | homeDir := GetHomeDir() |
| 155 | if pathStr == "~" { |
| 156 | return homeDir, nil |
| 157 | } |
| 158 | expandedPath := filepath.Clean(filepath.Join(homeDir, pathStr[2:])) |
| 159 | absPath, err := filepath.Abs(filepath.Join(homeDir, expandedPath)) |
| 160 | if err != nil || !strings.HasPrefix(absPath, homeDir) { |
| 161 | return "", fmt.Errorf("potential path traversal detected for path %s", pathStr) |
| 162 | } |
| 163 | return expandedPath, nil |
| 164 | } |
| 165 | |
| 166 | func ExpandHomeDirSafe(pathStr string) string { |
| 167 | path, _ := ExpandHomeDir(pathStr) |
no test coverage detected