fix: enforce sensitive file sources and persistent history provenance (#14344)

This commit is contained in:
蘭
2026-11-08 22:41:30 +08:00
committed by GitHub
parent c32f7e0a6c
commit 00feb6e23b
2 changed files with 453 additions and 28 deletions
+243 -28
View File
@@ -287,11 +287,8 @@ func (f *FileService) Create(op request.FileCreate) error {
}
func (f *FileService) Delete(op request.FileDelete) error {
if op.IsDir {
excludeDir := global.Dir.DataDir
if path.Base(op.Path) == ".1panel_clash" || op.Path == excludeDir {
return buserr.New("ErrPathNotDelete")
}
if err := files.CheckFileDeletePaths(op.Path); err != nil {
return err
}
fo := files.NewFileOp()
recycleBinStatus, _ := settingRepo.Get(settingRepo.WithByKey("FileRecycleBin"))
@@ -307,6 +304,9 @@ func (f *FileService) Delete(op request.FileDelete) error {
}
}
if op.ForceDelete {
if err := files.CheckFileDeletePaths(op.Path); err != nil {
return err
}
var err error
if op.IsDir {
err = fo.DeleteDir(op.Path)
@@ -324,6 +324,9 @@ func (f *FileService) Delete(op request.FileDelete) error {
}
info, _ := fo.Fs.Stat(op.Path)
if info == nil || files.IsSymlink(info.Mode()) {
if err := files.CheckFileDeletePaths(op.Path); err != nil {
return err
}
if err := os.Remove(op.Path); err != nil {
return err
}
@@ -331,6 +334,9 @@ func (f *FileService) Delete(op request.FileDelete) error {
return nil
}
if err := files.CheckFileDeletePaths(op.Path); err != nil {
return err
}
if err := NewIRecycleBinService().Create(request.RecycleBinCreate{SourcePath: op.Path}); err != nil {
return err
}
@@ -338,35 +344,119 @@ func (f *FileService) Delete(op request.FileDelete) error {
}
func (f *FileService) BatchDelete(op request.FileBatchDelete) error {
plans, err := f.prepareBatchFileDelete(op)
if err != nil {
return err
}
fo := files.NewFileOp()
if op.IsDir {
for _, file := range op.Paths {
targets, err := f.collectPermanentDeleteTargets(file, true)
if err != nil {
return err
}
if err := fo.DeleteDir(file); err != nil {
return err
}
if err := cleanupTrashInfoByEntryPath(file); err != nil {
global.LOG.Warnf("cleanup trashinfo failed for %s: %v", file, err)
}
f.cleanupPermanentDeleteHistory(targets)
for _, plan := range plans {
if err := files.CheckFileDeletePaths(plan.path); err != nil {
return err
}
} else {
for _, file := range op.Paths {
if err := fo.DeleteFile(file); err != nil {
return err
}
if err := cleanupTrashInfoByEntryPath(file); err != nil {
global.LOG.Warnf("cleanup trashinfo failed for %s: %v", file, err)
}
f.cleanupPermanentDeleteHistory([]string{file})
if op.IsDir {
err = fo.DeleteDir(plan.path)
} else {
err = fo.DeleteFile(plan.path)
}
if err != nil {
return err
}
if err := cleanupTrashInfoByEntryPath(plan.path); err != nil {
global.LOG.Warnf("cleanup trashinfo failed for %s: %v", plan.path, err)
}
f.cleanupPermanentDeleteHistory(plan.historyTargets)
}
return nil
}
type batchFileDeletePlan struct {
path string
historyTargets []string
absolute string
physical string
info os.FileInfo
}
func (f *FileService) prepareBatchFileDelete(op request.FileBatchDelete) ([]batchFileDeletePlan, error) {
if err := files.CheckFileDeletePaths(op.Paths...); err != nil {
return nil, err
}
plans := make([]batchFileDeletePlan, 0, len(op.Paths))
for _, target := range op.Paths {
info, err := os.Lstat(target)
if err != nil {
return nil, err
}
absolute, err := filepath.Abs(target)
if err != nil {
return nil, err
}
physicalInput := target
if !filepath.IsAbs(physicalInput) {
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
physicalInput = cwd + string(filepath.Separator) + physicalInput
}
physical, resolveErr := filepath.EvalSymlinks(physicalInput)
if resolveErr != nil && !os.IsNotExist(resolveErr) {
return nil, resolveErr
}
if resolveErr == nil {
physical, err = filepath.Abs(physical)
if err != nil {
return nil, err
}
}
plan := batchFileDeletePlan{path: target, absolute: absolute, physical: physical, info: info}
for _, previous := range plans {
if fileDeletePlansOverlap(previous, plan) {
return nil, buserr.New("ErrInvalidParams")
}
}
// Keep isDir=false as Remove, rather than silently upgrading to recursion.
if !op.IsDir && info.IsDir() {
entries, err := os.ReadDir(target)
if err != nil {
return nil, err
}
if len(entries) > 0 {
return nil, &os.PathError{Op: "remove", Path: target, Err: syscall.ENOTEMPTY}
}
}
historyTargets, err := f.collectPermanentDeleteTargets(target, op.IsDir)
if err != nil {
return nil, err
}
plan.historyTargets = historyTargets
plans = append(plans, plan)
}
return plans, nil
}
func fileDeletePlansOverlap(a, b batchFileDeletePlan) bool {
contains := func(parent, child string) bool {
if parent == "" || child == "" {
return false
}
relative, err := filepath.Rel(parent, child)
return err == nil && relative != ".." && !strings.HasPrefix(relative, ".."+string(filepath.Separator))
}
if contains(a.absolute, b.absolute) || contains(b.absolute, a.absolute) {
return true
}
if a.info.IsDir() && contains(a.physical, b.physical) || b.info.IsDir() && contains(b.physical, a.physical) {
return true
}
if os.SameFile(a.info, b.info) && strings.EqualFold(filepath.Base(a.absolute), filepath.Base(b.absolute)) {
parentA, errA := os.Stat(filepath.Dir(a.absolute))
parentB, errB := os.Stat(filepath.Dir(b.absolute))
return errA == nil && errB == nil && os.SameFile(parentA, parentB)
}
return false
}
func (f *FileService) ChangeMode(op request.FileCreate) error {
fo := files.NewFileOp()
if err := fo.ChmodR(op.Path, op.Mode, op.Sub); err != nil {
@@ -879,6 +969,9 @@ func (f *FileService) SaveContent(edit request.FileEdit) error {
}
func (f *FileService) ChangeName(req request.FileRename) error {
if err := files.CheckFileDeletePaths(req.OldName, req.NewName); err != nil {
return err
}
if hasInvalidFileName(req.NewName) {
return buserr.New("ErrInvalidChar")
}
@@ -894,6 +987,9 @@ func (f *FileService) ChangeName(req request.FileRename) error {
}
fo := files.NewFileOp()
content, mode, shouldRecordHistory := readEditableFileHistoryContent(req.OldName)
if err := files.CheckFileDeletePaths(req.OldName, req.NewName); err != nil {
return err
}
if err := fo.Rename(req.OldName, req.NewName); err != nil {
return err
}
@@ -987,6 +1083,9 @@ func (f *FileService) StopMvFile(taskID string) error {
}
func validateFileMove(fo files.FileOp, m request.FileMove) error {
if err := checkFileMoveDeletePaths(m); err != nil {
return err
}
if !fo.Stat(m.NewPath) {
return buserr.New("ErrPathNotFound")
}
@@ -1004,6 +1103,9 @@ func validateFileMove(fo files.FileOp, m request.FileMove) error {
}
func (f *FileService) moveFileWithContext(ctx context.Context, m request.FileMove) error {
if err := checkFileMoveDeletePaths(m); err != nil {
return err
}
fo := files.NewFileOp()
type moveSnapshot struct {
path string
@@ -1023,6 +1125,9 @@ func (f *FileService) moveFileWithContext(ctx context.Context, m request.FileMov
if err := ctx.Err(); err != nil {
return err
}
if err := checkFileMoveDeletePaths(request.FileMove{Type: "cut", CoverPaths: []string{src}, NewPath: m.NewPath}); err != nil {
return err
}
if err := fo.CopyAndReNameWithContext(ctx, src, m.NewPath, "", true); err != nil {
errs = append(errs, err)
global.LOG.Errorf("cut copy file [%s] to [%s] failed, err: %s", src, m.NewPath, err.Error())
@@ -1031,6 +1136,9 @@ func (f *FileService) moveFileWithContext(ctx context.Context, m request.FileMov
if err := ctx.Err(); err != nil {
return err
}
if err := files.CheckFileDeletePaths(src); err != nil {
return err
}
if err := fo.DeleteDir(src); err != nil {
removeErr := fmt.Errorf("remove merged source [%s] failed: %w", src, err)
errs = append(errs, removeErr)
@@ -1038,7 +1146,14 @@ func (f *FileService) moveFileWithContext(ctx context.Context, m request.FileMov
}
}
}
if err := fo.CutWithContext(ctx, m.OldPaths, m.NewPath, m.Name, m.Cover); err != nil {
cutSources, cutDestination, err := absoluteFileCutOperands(m.OldPaths, m.NewPath)
if err != nil {
return err
}
if err := checkFileMoveDeletePaths(request.FileMove{Type: "cut", OldPaths: cutSources, NewPath: cutDestination, Name: m.Name, Cover: m.Cover}); err != nil {
return err
}
if err := fo.CutWithContext(ctx, cutSources, cutDestination, m.Name, m.Cover); err != nil {
return err
}
for _, snapshot := range snapshots {
@@ -1053,6 +1168,9 @@ func (f *FileService) moveFileWithContext(ctx context.Context, m request.FileMov
}
if m.Type == "copy" {
for _, src := range m.OldPaths {
if err := checkFileMoveDeletePaths(request.FileMove{Type: "copy", OldPaths: []string{src}, NewPath: m.NewPath, Name: m.Name, Cover: m.Cover}); err != nil {
return err
}
if err := fo.CopyAndReNameWithContext(ctx, src, m.NewPath, m.Name, m.Cover); err != nil {
errs = append(errs, err)
global.LOG.Errorf("copy file [%s] to [%s] failed, err: %s", src, m.NewPath, err.Error())
@@ -1060,6 +1178,9 @@ func (f *FileService) moveFileWithContext(ctx context.Context, m request.FileMov
}
if len(m.CoverPaths) > 0 {
for _, src := range m.CoverPaths {
if err := checkFileMoveDeletePaths(request.FileMove{Type: "copy", CoverPaths: []string{src}, NewPath: m.NewPath}); err != nil {
return err
}
if err := fo.CopyAndReNameWithContext(ctx, src, m.NewPath, "", true); err != nil {
errs = append(errs, err)
global.LOG.Errorf("copy file [%s] to [%s] failed, err: %s", src, m.NewPath, err.Error())
@@ -1071,6 +1192,100 @@ func (f *FileService) moveFileWithContext(ctx context.Context, m request.FileMov
return aggregateFileMoveErrors(errs)
}
func absoluteFileCutOperands(sources []string, destination string) ([]string, string, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, "", err
}
absolute := func(value string) string {
if filepath.IsAbs(value) {
return value
}
return cwd + string(filepath.Separator) + value
}
result := make([]string, len(sources))
for i, source := range sources {
result[i] = absolute(source)
}
return result, absolute(destination), nil
}
func checkFileMoveDeletePaths(m request.FileMove) error {
if m.Type != "cut" && m.Type != "copy" {
return nil
}
if m.Type == "cut" {
if err := files.CheckFileDeletePaths(append(append([]string{}, m.OldPaths...), m.CoverPaths...)...); err != nil {
return err
}
}
var destinations []string
commandTarget := func(operand, source string) (string, error) {
info, err := os.Stat(operand)
if err != nil && !os.IsNotExist(err) {
return "", err
}
if err == nil && info.IsDir() {
return operand + string(filepath.Separator) + filepath.Base(filepath.Clean(source)), nil
}
return operand, nil
}
copyTarget := func(source, name string, cover bool) (string, error) {
source = path.Clean("/" + source)
destination := path.Clean("/" + m.NewPath)
info, err := os.Stat(source)
if err != nil {
return "", err
}
operand := destination
if info.IsDir() {
if name != "" && !cover {
operand = filepath.Join(destination, name)
}
} else {
operand = filepath.Join(destination, name)
if cover {
operand = destination
}
}
return commandTarget(operand, source)
}
if m.Type == "cut" && len(m.OldPaths) > 0 {
operand := m.NewPath
if m.Name != "" {
operand = filepath.Join(m.NewPath, m.Name)
if _, err := os.Stat(operand); err == nil {
operand = m.NewPath
} else if !os.IsNotExist(err) {
return err
}
}
for _, source := range m.OldPaths {
target, err := commandTarget(operand, source)
if err != nil {
return err
}
destinations = append(destinations, target)
}
} else if m.Type == "copy" {
for _, source := range m.OldPaths {
target, err := copyTarget(source, m.Name, m.Cover)
if err != nil {
return err
}
destinations = append(destinations, target)
}
}
for _, source := range m.CoverPaths {
target, err := copyTarget(source, "", true)
if err != nil {
return err
}
destinations = append(destinations, target)
}
return files.CheckFileDeletePaths(destinations...)
}
func readEditableFileHistoryContent(filePath string) ([]byte, os.FileMode, bool) {
info, err := os.Lstat(filePath)
if err != nil {
+210
View File
@@ -0,0 +1,210 @@
package files
import (
"os"
"path/filepath"
"strings"
"syscall"
"github.com/1Panel-dev/1Panel/agent/buserr"
"github.com/1Panel-dev/1Panel/agent/global"
)
type fileDeletePolicy struct {
dataDir, trashRoot string
fixedRoots []string
}
type deletePathViews struct {
lexical, physical string
info os.FileInfo
}
func CheckFileDeletePaths(paths ...string) error {
return (fileDeletePolicy{dataDir: global.Dir.DataDir, trashRoot: global.Dir.RecycleBinDir, fixedRoots: protectedPaths}).check(paths...)
}
func (policy fileDeletePolicy) check(paths ...string) error {
var fixed []deletePathViews
for _, root := range policy.fixedRoots {
view, err := newDeletePathViews(root)
if err != nil {
return buserr.New("ErrPathNotDelete")
}
fixed = append(fixed, view)
}
var data, trash *deletePathViews
for _, setting := range []struct {
path string
destination **deletePathViews
}{{policy.dataDir, &data}, {policy.trashRoot, &trash}} {
if strings.TrimSpace(setting.path) == "" {
continue
}
view, err := newDeletePathViews(setting.path)
if err != nil {
return buserr.New("ErrPathNotDelete")
}
*setting.destination = &view
}
for _, path := range paths {
target, err := newDeletePathViews(path)
if err != nil {
return buserr.New("ErrPathNotDelete")
}
for _, root := range fixed {
if sameDeleteRoot(target, root) {
return buserr.New("ErrPathNotDelete")
}
}
if data != nil && deleteTargetContainsRoot(target, *data) {
return buserr.New("ErrPathNotDelete")
}
if strings.EqualFold(filepath.Base(target.lexical), ".1panel_clash") || strings.EqualFold(filepath.Base(target.physical), ".1panel_clash") || trash != nil && sameDeleteRoot(target, *trash) {
return buserr.New("ErrPathNotDelete")
}
}
return nil
}
func newDeletePathViews(value string) (deletePathViews, error) {
if strings.TrimSpace(value) == "" || strings.IndexByte(value, 0) >= 0 {
return deletePathViews{}, syscall.EINVAL
}
absolute, err := filepath.Abs(value)
if err != nil {
return deletePathViews{}, err
}
physicalInput := value
if !filepath.IsAbs(physicalInput) {
cwd, err := os.Getwd()
if err != nil {
return deletePathViews{}, err
}
physicalInput = cwd + string(filepath.Separator) + physicalInput
}
physical, err := filepath.EvalSymlinks(physicalInput)
if os.IsNotExist(err) {
physical, err = resolveMissingDeletePath(value)
}
if err != nil {
return deletePathViews{}, err
}
physical, err = filepath.Abs(physical)
if err != nil {
return deletePathViews{}, err
}
info, err := os.Stat(physical)
if err != nil && !os.IsNotExist(err) {
return deletePathViews{}, err
}
return deletePathViews{lexical: absolute, physical: physical, info: info}, nil
}
func sameDeleteRoot(target, root deletePathViews) bool {
for _, candidate := range []string{target.lexical, target.physical} {
for _, protected := range []string{root.lexical, root.physical} {
if candidate == protected {
return true
}
}
}
return sameDeleteDirectory(target.info, root.info)
}
func sameDeleteDirectory(a, b os.FileInfo) bool {
return a != nil && b != nil && a.IsDir() && b.IsDir() && os.SameFile(a, b)
}
func deleteTargetContainsRoot(target, root deletePathViews) bool {
if sameDeleteRoot(target, root) {
return true
}
for _, candidate := range []string{target.lexical, target.physical} {
for _, protected := range []string{root.lexical, root.physical} {
relative, err := filepath.Rel(candidate, protected)
if err == nil && relative != ".." && !strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
return true
}
}
}
if target.info != nil && target.info.IsDir() {
for ancestor := root.physical; ; ancestor = filepath.Dir(ancestor) {
info, err := os.Stat(ancestor)
if err == nil && sameDeleteDirectory(target.info, info) {
return true
}
if filepath.Dir(ancestor) == ancestor {
break
}
}
}
return false
}
func resolveMissingDeletePath(value string) (string, error) {
if !filepath.IsAbs(value) {
cwd, err := os.Getwd()
if err != nil {
return "", err
}
value = cwd + string(filepath.Separator) + value
}
pending := strings.Split(filepath.ToSlash(value), "/")
current := string(filepath.Separator)
var currentInfo os.FileInfo
missing, links := false, 0
for len(pending) > 0 {
component := pending[0]
pending = pending[1:]
if component == "" {
continue
}
if currentInfo != nil && !currentInfo.IsDir() {
return "", syscall.ENOTDIR
}
if component == "." {
continue
}
if component == ".." {
if missing {
return "", os.ErrNotExist
}
current = filepath.Dir(current)
currentInfo = nil
continue
}
next := filepath.Join(current, component)
if missing {
current = next
continue
}
info, err := os.Lstat(next)
if os.IsNotExist(err) {
missing = true
current = next
currentInfo = nil
continue
}
if err != nil {
return "", err
}
if info.Mode()&os.ModeSymlink != 0 {
links++
if links > 40 {
return "", syscall.ELOOP
}
link, err := os.Readlink(next)
if err != nil {
return "", err
}
if filepath.IsAbs(link) {
current = string(filepath.Separator)
}
currentInfo = nil
pending = append(strings.Split(filepath.ToSlash(link), "/"), pending...)
continue
}
current = next
currentInfo = info
}
return current, nil
}