Skip to content

Commit 5de8398

Browse files
authored
feat: Add path protection mechanism to prevent deletion of system dir (#11130)
* feat: Add path protection mechanism to prevent deletion of critical system directories * feat: Enhance recycle bin service with path protection for deletion requests
1 parent c0220db commit 5de8398

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

agent/app/service/recycle_bin.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ func (r RecycleBinService) Page(search dto.PageInfo) (int64, []response.RecycleB
7676
}
7777

7878
func (r RecycleBinService) Create(create request.RecycleBinCreate) error {
79+
if files.IsProtected(create.SourcePath) {
80+
return buserr.New("ErrPathNotDelete")
81+
}
7982
op := files.NewFileOp()
8083
if !op.Stat(create.SourcePath) {
8184
return buserr.New("ErrLinkPathNotFound")

agent/utils/files/file_op.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,40 @@ import (
3333
"github.com/spf13/afero"
3434
)
3535

36+
var protectedPaths = []string{
37+
"/",
38+
"/bin",
39+
"/sbin",
40+
"/etc",
41+
"/boot",
42+
"/usr",
43+
"/lib",
44+
"/lib64",
45+
"/dev",
46+
"/proc",
47+
"/sys",
48+
"/root",
49+
}
50+
51+
func IsProtected(path string) bool {
52+
real, err := filepath.EvalSymlinks(path)
53+
if err == nil {
54+
path = real
55+
}
56+
57+
abs, err := filepath.Abs(path)
58+
if err == nil {
59+
path = abs
60+
}
61+
62+
for _, p := range protectedPaths {
63+
if path == p {
64+
return true
65+
}
66+
}
67+
return false
68+
}
69+
3670
type FileOp struct {
3771
Fs afero.Fs
3872
}
@@ -104,6 +138,9 @@ func (f FileOp) LinkFile(source string, dst string, isSymlink bool) error {
104138
}
105139

106140
func (f FileOp) DeleteDir(dst string) error {
141+
if IsProtected(dst) {
142+
return buserr.New("ErrPathNotDelete")
143+
}
107144
return f.Fs.RemoveAll(dst)
108145
}
109146

@@ -113,14 +150,23 @@ func (f FileOp) Stat(dst string) bool {
113150
}
114151

115152
func (f FileOp) DeleteFile(dst string) error {
153+
if IsProtected(dst) {
154+
return buserr.New("ErrPathNotDelete")
155+
}
116156
return f.Fs.Remove(dst)
117157
}
118158

119159
func (f FileOp) CleanDir(dst string) error {
160+
if IsProtected(dst) {
161+
return buserr.New("ErrPathNotDelete")
162+
}
120163
return cmd.RunDefaultBashCf("rm -rf %s/*", dst)
121164
}
122165

123166
func (f FileOp) RmRf(dst string) error {
167+
if IsProtected(dst) {
168+
return buserr.New("ErrPathNotDelete")
169+
}
124170
return cmd.RunDefaultBashCf("rm -rf %s", dst)
125171
}
126172

0 commit comments

Comments
 (0)