Skip to content

Commit 6cccf32

Browse files
committed
Standardise cleanUploadFilename to more closely match git
See verify_path in: https://github.com/git/git/blob/7f4e64169352e03476b0ea64e7e2973669e491a2/read-cache.c#L951 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 03d5f29 commit 6cccf32

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

routers/repo/editor.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -576,12 +576,13 @@ func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) {
576576
}
577577

578578
func cleanUploadFileName(name string) string {
579-
name = strings.TrimLeft(name, "./\\")
580-
name = strings.Replace(name, "../", "", -1)
581-
name = strings.Replace(name, "..\\", "", -1)
582-
name = strings.TrimPrefix(path.Clean(name), ".git/")
583-
if name == ".git" {
584-
return ""
579+
// Rebase the filename
580+
name = strings.Trim(path.Clean("/"+name), " /")
581+
// Git disallows any filenames to have a .git directory in them.
582+
for _, part := range strings.Split(name, "/") {
583+
if strings.ToLower(part) == ".git" {
584+
return ""
585+
}
585586
}
586587
return name
587588
}

routers/repo/editor_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ func TestCleanUploadName(t *testing.T) {
1515
models.PrepareTestEnv(t)
1616

1717
var kases = map[string]string{
18-
".git/refs/master": "git/refs/master",
18+
".git/refs/master": "",
1919
"/root/abc": "root/abc",
2020
"./../../abc": "abc",
21-
"a/../.git": "a/.git",
22-
"a/../../../abc": "a/abc",
21+
"a/../.git": "",
22+
"a/../../../abc": "abc",
2323
"../../../acd": "acd",
24-
"../../.git/abc": "git/abc",
25-
"..\\..\\.git/abc": "git/abc",
24+
"../../.git/abc": "",
25+
"..\\..\\.git/abc": "..\\..\\.git/abc",
26+
"abc/../def": "def",
2627
}
2728
for k, v := range kases {
2829
assert.EqualValues(t, v, cleanUploadFileName(k))

0 commit comments

Comments
 (0)