Skip to content

Commit 21c70e1

Browse files
techknowlogicklunny
authored andcommitted
backport 5571 (#5573)
1 parent b45d588 commit 21c70e1

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

routers/repo/editor.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,17 @@ func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) {
559559
ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + branchName + "/" + form.TreePath)
560560
}
561561

562+
func cleanUploadFileName(name string) string {
563+
name = strings.TrimLeft(name, "./\\")
564+
name = strings.Replace(name, "../", "", -1)
565+
name = strings.Replace(name, "..\\", "", -1)
566+
name = strings.TrimPrefix(path.Clean(name), ".git/")
567+
if name == ".git" {
568+
return ""
569+
}
570+
return name
571+
}
572+
562573
// UploadFileToServer upload file to server file dir not git
563574
func UploadFileToServer(ctx *context.Context) {
564575
file, header, err := ctx.Req.FormFile("file")
@@ -591,7 +602,13 @@ func UploadFileToServer(ctx *context.Context) {
591602
}
592603
}
593604

594-
upload, err := models.NewUpload(header.Filename, buf, file)
605+
name := cleanUploadFileName(header.Filename)
606+
if len(name) == 0 {
607+
ctx.Error(500, "Upload file name is invalid")
608+
return
609+
}
610+
611+
upload, err := models.NewUpload(name, buf, file)
595612
if err != nil {
596613
ctx.Error(500, fmt.Sprintf("NewUpload: %v", err))
597614
return

routers/repo/editor_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2018 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package repo
6+
7+
import (
8+
"testing"
9+
10+
"code.gitea.io/gitea/models"
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestCleanUploadName(t *testing.T) {
15+
models.PrepareTestEnv(t)
16+
17+
var kases = map[string]string{
18+
".git/refs/master": "git/refs/master",
19+
"/root/abc": "root/abc",
20+
"./../../abc": "abc",
21+
"a/../.git": "a/.git",
22+
"a/../../../abc": "a/abc",
23+
"../../../acd": "acd",
24+
"../../.git/abc": "git/abc",
25+
"..\\..\\.git/abc": "git/abc",
26+
}
27+
for k, v := range kases {
28+
assert.EqualValues(t, v, cleanUploadFileName(k))
29+
}
30+
}

0 commit comments

Comments
 (0)