MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / safeJoinWithinBase

Function safeJoinWithinBase

agent/app/service/backup_compose.go:598–621  ·  view source on GitHub ↗
(baseDir, name string)

Source from the content-addressed store, hash-verified

596}
597
598func 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
623func stepRestoreComposeFiles(recoverCtx *composeRecoverContext) error {
624 if recoverCtx.targetDir != "" {

Callers 2

handleComposeRecoverFunction · 0.85
stepRestoreComposeFilesFunction · 0.85

Calls 1

CleanMethod · 0.65

Tested by

no test coverage detected