mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-26 20:09:47 +01:00
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:
parent
669d9216ec
commit
c5a29983a5
2 changed files with 24 additions and 0 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue