(baseDir, name string)
| 596 | } |
| 597 | |
| 598 | func safeJoinWithinBase(baseDir, name string) (string, error) { |
| 599 | base := filepath.Clean(baseDir) |
| 600 | candidate := strings.TrimSpace(name) |
| 601 | candidate = strings.ReplaceAll(candidate, "\\", "/") |
| 602 | candidate = filepath.Clean(filepath.FromSlash(candidate)) |
| 603 | if candidate == "" || candidate == "." { |
| 604 | return "", fmt.Errorf("invalid path: empty") |
| 605 | } |
| 606 | if filepath.IsAbs(candidate) { |
| 607 | return "", fmt.Errorf("invalid path %q: absolute path is not allowed", name) |
| 608 | } |
| 609 | if candidate == ".." || strings.HasPrefix(candidate, ".."+string(filepath.Separator)) { |
| 610 | return "", fmt.Errorf("invalid path %q: path escapes base directory", name) |
| 611 | } |
| 612 | resolved := filepath.Clean(filepath.Join(base, candidate)) |
| 613 | rel, err := filepath.Rel(base, resolved) |
| 614 | if err != nil { |
| 615 | return "", fmt.Errorf("resolve path %q failed, err: %v", name, err) |
| 616 | } |
| 617 | if rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { |
| 618 | return "", fmt.Errorf("invalid path %q: path escapes base directory", name) |
| 619 | } |
| 620 | return resolved, nil |
| 621 | } |
| 622 | |
| 623 | func stepRestoreComposeFiles(recoverCtx *composeRecoverContext) error { |
| 624 | if recoverCtx.targetDir != "" { |
no test coverage detected