fix(subsonic): reject setRating values outside the 0-5 range - #6411 (#6413)

This commit is contained in:
ump45nose 2026-11-04 00:26:14 +08:00 committed by GitHub
parent 755da3ea59
commit 7c1e68a1e4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 67 additions and 1 deletions

View File

@ -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() {

View File

@ -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)

View File

@ -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")