fix(jellyfin): reject oversized raw avatar uploads before resizing

postUserImage capped the read for base64 inflation but never checked the
decoded body against MaxImageUploadSize, so a raw upload up to ~33% over
the limit reached SetAvatar, whose io.LimitReader silently truncated it.
Add the same explicit size check postItemImage already uses.
This commit is contained in:
Deluan 2026-09-07 20:14:13 -04:00
commit c5a29983a5
2 changed files with 24 additions and 0 deletions

View file

@ -6,6 +6,7 @@ import (
"net/http"
"strings"
"github.com/dustin/go-humanize"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/core/artwork"
"github.com/navidrome/navidrome/log"
@ -131,6 +132,13 @@ func (api *Router) postUserImage(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Bad Request", http.StatusBadRequest)
return
}
// Raw (non-base64) bodies skip the inflation math above, so the decoded size still needs its own check.
if int64(len(imgBytes)) > limit {
log.Warn(ctx, "Jellyfin API: avatar upload rejected: image exceeds MaxImageUploadSize",
"user", usr.UserName, "size", humanize.Bytes(uint64(len(imgBytes))), "limit", humanize.Bytes(uint64(limit)))
http.Error(w, "file too large", http.StatusBadRequest)
return
}
filename, err := api.imgUpload.SetAvatar(ctx, usr.ID, usr.UserName, usr.UploadedImagePath(), bytes.NewReader(imgBytes), ext)
if err != nil {

View file

@ -12,6 +12,7 @@ import (
"net/http/httptest"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/navidrome/navidrome/conf"
@ -186,6 +187,21 @@ var _ = Describe("POST /userimage and DELETE /userimage", func() {
Expect(w.Code).To(Equal(http.StatusNoContent))
})
It("rejects raw bytes over MaxImageUploadSize instead of letting SetAvatar truncate them", func() {
img := pngBytes()
// One byte under the image size: still well inside the base64-inflation read cap
// (limit*4/3+4), so only the post-decode size check can catch this.
conf.Server.MaxImageUploadSize = strconv.Itoa(len(img)-1) + "B"
r := authenticatedRequestWithBody("POST", "/userimage", bytes.NewReader(img))
r.Header.Set("Content-Type", "image/png")
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
Expect(w.Code).To(Equal(http.StatusBadRequest))
usr, _ := ds.User(context.Background()).Get("u1")
Expect(usr.UploadedImage).To(BeEmpty())
})
It("rejects an unknown content type", func() {
r := authenticatedRequestWithBody("POST", "/userimage", strings.NewReader("x"))
r.Header.Set("Content-Type", "text/plain")