mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-09-19 12:16:24 +08:00
fix(security): directory traversal (#744)
* fix(security): Directory traversal * chore: . * 优化 --------- Co-authored-by: j2rong4cn <j2rong@qq.com>
This commit is contained in:
@ -10,7 +10,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
NotImplement = errors.New("not implement")
|
NotImplement = errors.New("not implement")
|
||||||
NotSupport = errors.New("not support")
|
NotSupport = errors.New("not support")
|
||||||
RelativePath = errors.New("access using relative path is not allowed")
|
RelativePath = errors.New("using relative path is not allowed")
|
||||||
|
|
||||||
MoveBetweenTwoStorages = errors.New("can't move files between two storages, try to copy")
|
MoveBetweenTwoStorages = errors.New("can't move files between two storages, try to copy")
|
||||||
UploadNotSupported = errors.New("upload not supported")
|
UploadNotSupported = errors.New("upload not supported")
|
||||||
|
@ -75,20 +75,20 @@ func EncodePath(path string, all ...bool) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func JoinBasePath(basePath, reqPath string) (string, error) {
|
func JoinBasePath(basePath, reqPath string) (string, error) {
|
||||||
/** relative path:
|
reqPath, err := CheckRelativePath(reqPath)
|
||||||
* 1. ..
|
if err != nil {
|
||||||
* 2. ../
|
return "", err
|
||||||
* 3. /..
|
}
|
||||||
* 4. /../
|
return stdpath.Join(FixAndCleanPath(basePath), reqPath), nil
|
||||||
* 5. /a/b/..
|
}
|
||||||
*/
|
|
||||||
if reqPath == ".." ||
|
func CheckRelativePath(path string) (string, error) {
|
||||||
strings.HasSuffix(reqPath, "/..") ||
|
isRelativePath := strings.Contains(path, "..")
|
||||||
strings.HasPrefix(reqPath, "../") ||
|
path = FixAndCleanPath(path)
|
||||||
strings.Contains(reqPath, "/../") {
|
if isRelativePath && !strings.Contains(path, "..") {
|
||||||
return "", errs.RelativePath
|
return "", errs.RelativePath
|
||||||
}
|
}
|
||||||
return stdpath.Join(FixAndCleanPath(basePath), FixAndCleanPath(reqPath)), nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetFullPath(mountPath, path string) string {
|
func GetFullPath(mountPath, path string) string {
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/OpenListTeam/OpenList/v4/internal/model"
|
"github.com/OpenListTeam/OpenList/v4/internal/model"
|
||||||
"github.com/OpenListTeam/OpenList/v4/internal/op"
|
"github.com/OpenListTeam/OpenList/v4/internal/op"
|
||||||
"github.com/OpenListTeam/OpenList/v4/pkg/generic"
|
"github.com/OpenListTeam/OpenList/v4/pkg/generic"
|
||||||
|
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
|
||||||
"github.com/OpenListTeam/OpenList/v4/server/common"
|
"github.com/OpenListTeam/OpenList/v4/server/common"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@ -173,6 +174,11 @@ func FsBatchRename(c *gin.Context) {
|
|||||||
if renameObject.SrcName == "" || renameObject.NewName == "" {
|
if renameObject.SrcName == "" || renameObject.NewName == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
renameObject.NewName, err = utils.CheckRelativePath(renameObject.NewName)
|
||||||
|
if err != nil {
|
||||||
|
common.ErrorResp(c, err, 403)
|
||||||
|
return
|
||||||
|
}
|
||||||
filePath := fmt.Sprintf("%s/%s", reqPath, renameObject.SrcName)
|
filePath := fmt.Sprintf("%s/%s", reqPath, renameObject.SrcName)
|
||||||
if err := fs.Rename(c.Request.Context(), filePath, renameObject.NewName); err != nil {
|
if err := fs.Rename(c.Request.Context(), filePath, renameObject.NewName); err != nil {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
@ -228,10 +234,13 @@ func FsRegexRename(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
|
|
||||||
if srcRegexp.MatchString(file.GetName()) {
|
if srcRegexp.MatchString(file.GetName()) {
|
||||||
|
newFileName, err := utils.CheckRelativePath(srcRegexp.ReplaceAllString(file.GetName(), req.NewNameRegex))
|
||||||
|
if err != nil {
|
||||||
|
common.ErrorResp(c, err, 403)
|
||||||
|
return
|
||||||
|
}
|
||||||
filePath := fmt.Sprintf("%s/%s", reqPath, file.GetName())
|
filePath := fmt.Sprintf("%s/%s", reqPath, file.GetName())
|
||||||
newFileName := srcRegexp.ReplaceAllString(file.GetName(), req.NewNameRegex)
|
|
||||||
if err := fs.Rename(c.Request.Context(), filePath, newFileName); err != nil {
|
if err := fs.Rename(c.Request.Context(), filePath, newFileName); err != nil {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
return
|
return
|
||||||
|
@ -204,6 +204,9 @@ func FsRename(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
reqPath, err := user.JoinPath(req.Path)
|
reqPath, err := user.JoinPath(req.Path)
|
||||||
|
if err == nil {
|
||||||
|
req.Name, err = utils.CheckRelativePath(req.Name)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.ErrorResp(c, err, 403)
|
common.ErrorResp(c, err, 403)
|
||||||
return
|
return
|
||||||
|
Reference in New Issue
Block a user