fix(fs): rename bug (#832)

* fix(fs): rename bug

* chore

* fix bug

* .

---------

Co-authored-by: j2rong4cn <j2rong@qq.com>
This commit is contained in:
hshpy
2025-07-25 13:42:39 +08:00
committed by GitHub
parent b273232f87
commit 6134574dac
3 changed files with 16 additions and 16 deletions

View File

@ -75,20 +75,12 @@ func EncodePath(path string, all ...bool) string {
}
func JoinBasePath(basePath, reqPath string) (string, error) {
reqPath, err := CheckRelativePath(reqPath)
if err != nil {
return "", err
}
return stdpath.Join(FixAndCleanPath(basePath), reqPath), nil
}
func CheckRelativePath(path string) (string, error) {
isRelativePath := strings.Contains(path, "..")
path = FixAndCleanPath(path)
if isRelativePath && !strings.Contains(path, "..") {
isRelativePath := strings.Contains(reqPath, "..")
reqPath = FixAndCleanPath(reqPath)
if isRelativePath && !strings.Contains(reqPath, "..") {
return "", errs.RelativePath
}
return path, nil
return stdpath.Join(FixAndCleanPath(basePath), reqPath), nil
}
func GetFullPath(mountPath, path string) string {

View File

@ -11,7 +11,6 @@ import (
"github.com/OpenListTeam/OpenList/v4/internal/model"
"github.com/OpenListTeam/OpenList/v4/internal/op"
"github.com/OpenListTeam/OpenList/v4/pkg/generic"
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
"github.com/OpenListTeam/OpenList/v4/server/common"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
@ -174,7 +173,7 @@ func FsBatchRename(c *gin.Context) {
if renameObject.SrcName == "" || renameObject.NewName == "" {
continue
}
renameObject.NewName, err = utils.CheckRelativePath(renameObject.NewName)
err = checkRelativePath(renameObject.NewName)
if err != nil {
common.ErrorResp(c, err, 403)
return
@ -235,7 +234,8 @@ func FsRegexRename(c *gin.Context) {
for _, file := range files {
if srcRegexp.MatchString(file.GetName()) {
newFileName, err := utils.CheckRelativePath(srcRegexp.ReplaceAllString(file.GetName(), req.NewNameRegex))
newFileName := srcRegexp.ReplaceAllString(file.GetName(), req.NewNameRegex)
err := checkRelativePath(newFileName)
if err != nil {
common.ErrorResp(c, err, 403)
return

View File

@ -3,6 +3,7 @@ package handles
import (
"fmt"
stdpath "path"
"strings"
"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/OpenListTeam/OpenList/v4/internal/task"
@ -205,7 +206,7 @@ func FsRename(c *gin.Context) {
}
reqPath, err := user.JoinPath(req.Path)
if err == nil {
req.Name, err = utils.CheckRelativePath(req.Name)
err = checkRelativePath(req.Name)
}
if err != nil {
common.ErrorResp(c, err, 403)
@ -227,6 +228,13 @@ func FsRename(c *gin.Context) {
common.SuccessResp(c)
}
func checkRelativePath(path string) error {
if strings.ContainsAny(path, "/\\") || path == "" || path == "." || path == ".." {
return errs.RelativePath
}
return nil
}
type RemoveReq struct {
Dir string `json:"dir"`
Names []string `json:"names"`