| 1 | package fileutil |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | func confinedRelativePath(rel string) (string, error) { |
| 10 | rel = filepath.Clean(strings.TrimSpace(rel)) |
| 11 | if rel == "" || rel == "." || filepath.IsAbs(rel) { |
| 12 | return "", fmt.Errorf("path must be relative to the workspace") |
| 13 | } |
| 14 | if rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { |
| 15 | return "", fmt.Errorf("path escapes the workspace") |
| 16 | } |
| 17 | return rel, nil |
| 18 | } |
| 19 |