From 7c1e68a1e4e68e7257affba4f25cd9cb545c7016 Mon Sep 17 00:00:00 2001 From: ump45nose <52391318+ump45nose@users.noreply.github.com> Date: Wed, 4 Nov 2026 00:26:14 +0800 Subject: [PATCH] fix(subsonic): reject setRating values outside the 0-5 range - #6411 (#6413) --- .../e2e/subsonic_media_annotation_test.go | 9 ++++ server/subsonic/media_annotation.go | 11 ++++- server/subsonic/media_annotation_test.go | 48 +++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/server/subsonic/e2e/subsonic_media_annotation_test.go b/server/subsonic/e2e/subsonic_media_annotation_test.go index 4b90dd143..9abf8c4a3 100644 --- a/server/subsonic/e2e/subsonic_media_annotation_test.go +++ b/server/subsonic/e2e/subsonic_media_annotation_test.go @@ -137,6 +137,15 @@ var _ = Describe("Media Annotation Endpoints", Ordered, func() { resp = doReq("setRating", "id", songID) Expect(resp.Status).To(Equal(responses.StatusFailed)) }) + + It("rejects ratings outside the 0-5 range", func() { + for _, rating := range []string{"-1", "6"} { + resp := doReq("setRating", "id", songID, "rating", rating) + + Expect(resp.Status).To(Equal(responses.StatusFailed)) + Expect(resp.Error).ToNot(BeNil()) + } + }) }) Describe("Scrobble", func() { diff --git a/server/subsonic/media_annotation.go b/server/subsonic/media_annotation.go index d6d160570..164cafccb 100644 --- a/server/subsonic/media_annotation.go +++ b/server/subsonic/media_annotation.go @@ -17,16 +17,25 @@ import ( "github.com/navidrome/navidrome/utils/req" ) +// maxRating is the highest value accepted by the Subsonic spec for setRating. 0 clears the rating. +const maxRating = 5 + func (api *Router) SetRating(r *http.Request) (*responses.Subsonic, error) { p := req.Params(r) id, err := p.String("id") if err != nil { return nil, err } - rating, err := p.Int("rating") + rating64, err := p.Int64("rating") if err != nil { return nil, err } + // Out-of-range values would otherwise land in average_rating, which every user sees. + // Check before narrowing to int, so a value like 4294967296 cannot wrap into range on 32-bit builds. + if rating64 < 0 || rating64 > maxRating { + return nil, newError(responses.ErrorGeneric, "Invalid rating: %d. Must be between 0 and %d", rating64, maxRating) + } + rating := int(rating64) log.Debug(r, "Setting rating", "rating", rating, "id", id) err = api.setRating(r.Context(), id, rating) diff --git a/server/subsonic/media_annotation_test.go b/server/subsonic/media_annotation_test.go index 9948929e3..aa571c035 100644 --- a/server/subsonic/media_annotation_test.go +++ b/server/subsonic/media_annotation_test.go @@ -2,6 +2,7 @@ package subsonic import ( "context" + "errors" "fmt" "net/http" "time" @@ -233,6 +234,53 @@ var _ = Describe("MediaAnnotationController", func() { }) }) + Describe("SetRating range validation", func() { + var plRepo *tests.MockPlaylistRepo + + BeforeEach(func() { + plRepo = tests.CreateMockPlaylistRepo() + plRepo.SetData(model.Playlists{{ID: "pl-1", Name: "My Playlist", OwnerID: "u1"}}) + ds.(*tests.MockDataStore).MockedPlaylist = plRepo + }) + + DescribeTable("rejects ratings outside the 0-5 range allowed by the Subsonic spec", + func(rating string) { + _, err := router.SetRating(newGetRequest("id=pl-1", "rating=4")) + Expect(err).ToNot(HaveOccurred()) + + r := newGetRequest("id=pl-1", "rating="+rating) + + _, err = router.SetRating(r) + + Expect(err).To(MatchError(errSubsonic)) + var subErr subError + Expect(errors.As(err, &subErr)).To(BeTrue()) + Expect(subErr.code).To(Equal(responses.ErrorGeneric)) + Expect(plRepo.Ratings).To(HaveKeyWithValue("pl-1", 4)) + }, + Entry("above maximum", "6"), + Entry("well above maximum", "1000"), + Entry("negative", "-3"), + // These wrap to 0 and 5 when narrowed to a 32-bit int. + Entry("2^32 (wraps to 0 on 32-bit)", "4294967296"), + Entry("2^32+5 (wraps to 5 on 32-bit)", "4294967301"), + ) + + DescribeTable("accepts ratings inside the 0-5 range", + func(rating int) { + r := newGetRequest("id=pl-1", fmt.Sprintf("rating=%d", rating)) + + _, err := router.SetRating(r) + + Expect(err).ToNot(HaveOccurred()) + Expect(plRepo.Ratings).To(HaveKeyWithValue("pl-1", rating)) + }, + Entry("zero clears the rating", 0), + Entry("middle of the range", 3), + Entry("maximum", 5), + ) + }) + Describe("Star with an unresolvable id", func() { It("skips the id without broadcasting an empty (wildcard) refresh", func() { r := newGetRequest("id=does-not-exist")