Skip to content

Commit 8ed4fcc

Browse files
hshpyj2rong4cn
authored andcommitted
fix(fs): rename bug (#832)
* fix(fs): rename bug * chore * fix bug * . --------- Co-authored-by: j2rong4cn <[email protected]>
1 parent db5e775 commit 8ed4fcc

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

pkg/utils/path.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,12 @@ func EncodePath(path string, all ...bool) string {
7575
}
7676

7777
func JoinBasePath(basePath, reqPath string) (string, error) {
78-
reqPath, err := CheckRelativePath(reqPath)
79-
if err != nil {
80-
return "", err
81-
}
82-
return stdpath.Join(FixAndCleanPath(basePath), reqPath), nil
83-
}
84-
85-
func CheckRelativePath(path string) (string, error) {
86-
isRelativePath := strings.Contains(path, "..")
87-
path = FixAndCleanPath(path)
88-
if isRelativePath && !strings.Contains(path, "..") {
78+
isRelativePath := strings.Contains(reqPath, "..")
79+
reqPath = FixAndCleanPath(reqPath)
80+
if isRelativePath && !strings.Contains(reqPath, "..") {
8981
return "", errs.RelativePath
9082
}
91-
return path, nil
83+
return stdpath.Join(FixAndCleanPath(basePath), reqPath), nil
9284
}
9385

9486
func GetFullPath(mountPath, path string) string {

server/handles/fsbatch.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/OpenListTeam/OpenList/v4/internal/model"
1212
"github.com/OpenListTeam/OpenList/v4/internal/op"
1313
"github.com/OpenListTeam/OpenList/v4/pkg/generic"
14-
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
1514
"github.com/OpenListTeam/OpenList/v4/server/common"
1615
"github.com/gin-gonic/gin"
1716
"github.com/pkg/errors"
@@ -174,7 +173,7 @@ func FsBatchRename(c *gin.Context) {
174173
if renameObject.SrcName == "" || renameObject.NewName == "" {
175174
continue
176175
}
177-
renameObject.NewName, err = utils.CheckRelativePath(renameObject.NewName)
176+
err = checkRelativePath(renameObject.NewName)
178177
if err != nil {
179178
common.ErrorResp(c, err, 403)
180179
return
@@ -235,7 +234,8 @@ func FsRegexRename(c *gin.Context) {
235234

236235
for _, file := range files {
237236
if srcRegexp.MatchString(file.GetName()) {
238-
newFileName, err := utils.CheckRelativePath(srcRegexp.ReplaceAllString(file.GetName(), req.NewNameRegex))
237+
newFileName := srcRegexp.ReplaceAllString(file.GetName(), req.NewNameRegex)
238+
err := checkRelativePath(newFileName)
239239
if err != nil {
240240
common.ErrorResp(c, err, 403)
241241
return

server/handles/fsmanage.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handles
33
import (
44
"fmt"
55
stdpath "path"
6+
"strings"
67

78
"github.com/OpenListTeam/OpenList/v4/internal/conf"
89
"github.com/OpenListTeam/OpenList/v4/internal/task"
@@ -205,7 +206,7 @@ func FsRename(c *gin.Context) {
205206
}
206207
reqPath, err := user.JoinPath(req.Path)
207208
if err == nil {
208-
req.Name, err = utils.CheckRelativePath(req.Name)
209+
err = checkRelativePath(req.Name)
209210
}
210211
if err != nil {
211212
common.ErrorResp(c, err, 403)
@@ -227,6 +228,13 @@ func FsRename(c *gin.Context) {
227228
common.SuccessResp(c)
228229
}
229230

231+
func checkRelativePath(path string) error {
232+
if strings.ContainsAny(path, "/\\") || path == "" || path == "." || path == ".." {
233+
return errs.RelativePath
234+
}
235+
return nil
236+
}
237+
230238
type RemoveReq struct {
231239
Dir string `json:"dir"`
232240
Names []string `json:"names"`

0 commit comments

Comments
 (0)