71 lines
2.0 KiB
Go
71 lines
2.0 KiB
Go
package platform
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
func ValidateRestoreTarget(target, restoreRoot string, inPlace, confirmed bool) (string, error) {
|
|
if !filepath.IsAbs(target) {
|
|
return "", errors.New("validation: restore target must be absolute")
|
|
}
|
|
clean := filepath.Clean(target)
|
|
if clean == "/" || clean == "/boot" || clean == "/etc" || clean == "/usr" || clean == "/var" {
|
|
return "", errors.New("validation: protected system restore target")
|
|
}
|
|
if !inPlace {
|
|
if err := validateStagingRestoreTarget(clean, restoreRoot); err != nil {
|
|
return "", err
|
|
}
|
|
} else if !confirmed {
|
|
return "", errors.New("validation: in-place restore requires explicit confirmation")
|
|
}
|
|
if inPlace {
|
|
anchor := string(os.PathSeparator) + strings.Split(strings.TrimPrefix(clean, string(os.PathSeparator)), string(os.PathSeparator))[0]
|
|
if err := rejectSymlinks(anchor, clean); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
return clean, nil
|
|
}
|
|
|
|
func validateStagingRestoreTarget(target, restoreRoot string) error {
|
|
allowedRoots := []string{filepath.Clean(restoreRoot), "/mnt/user", "/mnt/disks", "/mnt/remotes"}
|
|
for _, root := range allowedRoots {
|
|
if root == "." || root == "" {
|
|
continue
|
|
}
|
|
if target == root || strings.HasPrefix(target, root+string(os.PathSeparator)) {
|
|
return rejectSymlinks(root, target)
|
|
}
|
|
}
|
|
return errors.New("validation: staging restore target must be below restoreRoot, /mnt/user, /mnt/disks, or /mnt/remotes")
|
|
}
|
|
|
|
func rejectSymlinks(root, target string) error {
|
|
relative, err := filepath.Rel(root, target)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
current := root
|
|
for _, component := range strings.Split(relative, string(os.PathSeparator)) {
|
|
if component == "." || component == "" {
|
|
continue
|
|
}
|
|
current = filepath.Join(current, component)
|
|
info, err := os.Lstat(current)
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if info.Mode()&os.ModeSymlink != 0 {
|
|
return errors.New("validation: restore target traverses a symlink")
|
|
}
|
|
}
|
|
return nil
|
|
}
|