fix: check ParseMultipartForm errors, reduce maxMemory, handle 413 properly

This commit is contained in:
2026-07-10 12:06:12 +01:00
parent ef3cfe8c4f
commit 3a9bc02796
2 changed files with 19 additions and 4 deletions
+10 -2
View File
@@ -11,6 +11,7 @@ import (
"crussell/mw"
"database/sql"
"encoding/json"
"errors"
"fmt"
"io"
"log"
@@ -705,8 +706,15 @@ func UploadImage(w http.ResponseWriter, r *http.Request) {
return
}
//nolint:errcheck // parse errors are non-fatal; form values may still be available
_ = r.ParseMultipartForm(50 << 20)
if err := r.ParseMultipartForm(10 << 20); err != nil {
var maxBytesErr *http.MaxBytesError
if errors.As(err, &maxBytesErr) {
http.Error(w, "Upload too large", http.StatusRequestEntityTooLarge)
return
}
// Non-size parse errors: log but continue — form values (tags) may be available
log.Printf("Warning: ParseMultipartForm: %v", err)
}
tagsStr := r.FormValue("tags")
tags := []string{}
+9 -2
View File
@@ -1002,8 +1002,15 @@ func UploadProfilePictureHandler(w http.ResponseWriter, r *http.Request) {
return
}
//nolint:errcheck // parse errors are non-fatal; form values may still be available
_ = r.ParseMultipartForm(10 << 20)
if err := r.ParseMultipartForm(1 << 20); err != nil {
var maxBytesErr *http.MaxBytesError
if errors.As(err, &maxBytesErr) {
http.Error(w, "Profile picture too large", http.StatusRequestEntityTooLarge)
return
}
// Non-size parse errors: log but continue — form values may still be available
log.Printf("Warning: ParseMultipartForm: %v", err)
}
file, _, err := r.FormFile("file")
if err != nil {