Skip to content

Commit 34ae79c

Browse files
committed
feat(serverHandler/upload): parse form file path manually
RFC 7578, Section 4.2 requires that if a filename is provided, the directory path information must not be used. Since Go 1.17, Part.FileName() will strip directory information. The directory information is needed for uploading, parse manually instead.
1 parent 98270dd commit 34ae79c

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/serverHandler/upload.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"../util"
55
"errors"
66
"io"
7+
"mime"
8+
"mime/multipart"
79
"net/http"
810
"os"
911
"path"
@@ -40,6 +42,17 @@ func getAvailableFilename(fsPrefix, filename string, mustAppendSuffix bool) stri
4042
return ""
4143
}
4244

45+
// RFC 7578, Section 4.2 requires that if a filename is provided, the
46+
// directory path information must not be used.
47+
// Since Go 1.17, Part.FileName() will strip directory information.
48+
// However, the directory information is needed for uploading.
49+
// Parse manually instead.
50+
func getPartFilePath(part *multipart.Part) string {
51+
cd := part.Header.Get("Content-Disposition")
52+
_, params, _ := mime.ParseMediaType(cd)
53+
return params["filename"]
54+
}
55+
4356
func (h *handler) saveUploadFiles(fsPrefix string, createDir, overwriteExists bool, aliasSubItems []os.FileInfo, r *http.Request) bool {
4457
errs := []error{}
4558

@@ -58,7 +71,7 @@ func (h *handler) saveUploadFiles(fsPrefix string, createDir, overwriteExists bo
5871
break
5972
}
6073

61-
inputPartFilePath := part.FileName()
74+
inputPartFilePath := getPartFilePath(part)
6275
if len(inputPartFilePath) == 0 {
6376
continue
6477
}

0 commit comments

Comments
 (0)