mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-05 23:44:22 +00:00
Add file size validation in UploadFile handler to prevent excessive memory allocation
This commit is contained in:
@@ -372,6 +372,19 @@ func (h *Handler) UploadFile() http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate file size to prevent excessive memory allocation
|
||||||
|
// TODO: Make this configurable
|
||||||
|
const maxFileSize = 100 * 1024 * 1024 // 100MB
|
||||||
|
if formFile.Size > maxFileSize {
|
||||||
|
log.Debug("file too large",
|
||||||
|
"fileName", formFile.Filename,
|
||||||
|
"fileSize", formFile.Size,
|
||||||
|
"maxSize", maxFileSize,
|
||||||
|
)
|
||||||
|
respondError(w, "File too large", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Open the uploaded file
|
// Open the uploaded file
|
||||||
file, err := formFile.Open()
|
file, err := formFile.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -391,9 +404,8 @@ func (h *Handler) UploadFile() http.HandlerFunc {
|
|||||||
|
|
||||||
filePath := decodedPath + "/" + formFile.Filename
|
filePath := decodedPath + "/" + formFile.Filename
|
||||||
|
|
||||||
content := make([]byte, formFile.Size)
|
content, err := io.ReadAll(file)
|
||||||
_, err = file.Read(content)
|
if err != nil {
|
||||||
if err != nil && err != io.EOF {
|
|
||||||
log.Error("failed to read uploaded file",
|
log.Error("failed to read uploaded file",
|
||||||
"filePath", filePath,
|
"filePath", filePath,
|
||||||
"error", err.Error(),
|
"error", err.Error(),
|
||||||
|
|||||||
Reference in New Issue
Block a user