Skip to content

Commit

Permalink
perf: 对文件名做了过滤。防止路径注入漏洞
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyLTY committed Jan 31, 2024
1 parent 3f95bee commit a92924f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
8 changes: 7 additions & 1 deletion internal/logic/container/delrestorelogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package container
import (
"context"
"os"
"regexp"

"github.com/onlyLTY/dockerCopilot/UGREEN/internal/svc"
"github.com/onlyLTY/dockerCopilot/UGREEN/internal/types"
Expand All @@ -26,7 +27,7 @@ func NewDelRestoreLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelRes

func (l *DelRestoreLogic) DelRestore(req *types.ContainerRestoreReq) (resp *types.Resp, err error) {
resp = &types.Resp{}
fileName := req.Filename
fileName := CleanFilename(req.Filename)
basePath := os.Getenv("BACKUP_DIR") // 从环境变量中获取备份目录
if basePath == "" {
basePath = "/data/backups" // 如果环境变量未设置,使用默认值
Expand All @@ -44,3 +45,8 @@ func (l *DelRestoreLogic) DelRestore(req *types.ContainerRestoreReq) (resp *type
resp.Data = map[string]interface{}{}
return resp, nil
}

func CleanFilename(filename string) string {
reg := regexp.MustCompile("[^a-zA-Z0-9-]+")
return reg.ReplaceAllString(filename, "")
}
3 changes: 2 additions & 1 deletion internal/logic/container/restorelogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ func NewRestoreLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RestoreLo
func (l *RestoreLogic) Restore(req *types.ContainerRestoreReq) (resp *types.Resp, err error) {
resp = &types.Resp{}
taskID := uuid.New().String()
fileName := CleanFilename(req.Filename)
go func() {
// Catch any panic and log the error
defer func() {
if r := recover(); r != nil {
l.Errorf("Recovered from panic in restoreContainer: %v", r)
}
}()
err := utiles.RestoreContainer(l.svcCtx, req.Filename, taskID)
err := utiles.RestoreContainer(l.svcCtx, fileName, taskID)
if err != nil {
l.Errorf("Error in restoreContainer: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/utiles/restorecontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func RestoreContainer(ctx *svc.ServiceContext, filename string, taskID string) e
if err != nil {
logx.Error("Failed to read file: %s", err)
oldProgress.Percentage = 0
oldProgress.Message = "读取文件失败或者未找到文件"
oldProgress.Message = "读取文件失败或者未找到文件。请确认文件名仅由大小写字母、数字和短横线组成"
oldProgress.DetailMsg = err.Error()
oldProgress.IsDone = true
ctx.UpdateProgress(taskID, oldProgress)
Expand Down

0 comments on commit a92924f

Please sign in to comment.