fix(subsonic): make getAvatar answer only for the logged-in user
Pipeline: Test, Lint, Build / Get version info (push) Canceled after 0s
Pipeline: Test, Lint, Build / Lint Go code (push) Canceled after 0s
Pipeline: Test, Lint, Build / Validate DB migrations (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test Go code (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test Go plugins (push) Canceled after 0s
Pipeline: Test, Lint, Build / Report coverage (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test Go code (Windows) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test JS code (push) Canceled after 0s
Pipeline: Test, Lint, Build / Lint i18n files (push) Canceled after 0s
Pipeline: Test, Lint, Build / Check Docker configuration (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/386) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/riscv64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (windows/386) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Push to GHCR (push) Canceled after 0s
Pipeline: Test, Lint, Build / Push to Docker Hub () (push) Canceled after 0s
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build Windows installers (push) Canceled after 0s
Pipeline: Test, Lint, Build / Package/Release (push) Canceled after 0s
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Canceled after 0s

getAvatar now follows the same rule as getUser and getUsers, which only
return the authenticated user. A request for any other username returns
error 50 (not authorized), for regular users and admins alike.

The handler now reads the email from the user the auth middleware already
put in the request context, so it no longer queries the database on every
call.

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan committed 2026-11-28 10:25:51 -05:00
1 parent 42c7046e9f
commit 62256712f5
2 files changed
+57 -5

No files matched your search

+9 -5
View File
@@ -12,6 +12,7 @@ import (
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/model/request"
"github.com/navidrome/navidrome/resources"
"github.com/navidrome/navidrome/server/imghttp"
"github.com/navidrome/navidrome/server/subsonic/responses"
@@ -29,15 +30,18 @@ func (api *Router) GetAvatar(w http.ResponseWriter, r *http.Request) (*responses
return nil, err
}
ctx := r.Context()
u, err := api.ds.User().FindByUsername(ctx, username)
if err != nil {
return nil, err
loggedUser, ok := request.UserFrom(ctx)
if !ok {
return nil, newError(responses.ErrorGeneric, "Internal error")
}
if u.Email == "" {
if !strings.EqualFold(username, loggedUser.UserName) {
return nil, newError(responses.ErrorAuthorizationFail)
}
if loggedUser.Email == "" {
log.Warn(ctx, "User needs an email for gravatar to work", "username", username)
return api.getPlaceHolderAvatar(w, r)
}
http.Redirect(w, r, gravatar.Url(u.Email, 0), http.StatusFound) //nolint:gosec // URL is not constructed from user input
http.Redirect(w, r, gravatar.Url(loggedUser.Email, 0), http.StatusFound)
return nil, nil
}
+48
View File
@@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"io"
"net/http"
"net/http/httptest"
"os"
"time"
@@ -18,6 +19,8 @@ import (
"github.com/navidrome/navidrome/model/request"
"github.com/navidrome/navidrome/persistence"
"github.com/navidrome/navidrome/persistence/persistencetest"
"github.com/navidrome/navidrome/server/subsonic/responses"
"github.com/navidrome/navidrome/utils/gravatar"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
@@ -172,6 +175,51 @@ var _ = Describe("MediaRetrievalController", func() {
})
})
Describe("GetAvatar", func() {
regular := model.User{ID: "u1", UserName: "regular", Email: "regular@example.com"}
other := model.User{ID: "u2", UserName: "other", Email: "other@example.com"}
admin := model.User{ID: "u3", UserName: "admin", IsAdmin: true}
BeforeEach(func() {
conf.Update(func(c *conf.Options) { c.EnableGravatar = true })
})
getAvatar := func(caller model.User, username string) error {
GinkgoHelper()
r := newGetRequest("username=" + username)
r = r.WithContext(request.WithUser(r.Context(), caller))
_, err := router.GetAvatar(w, r)
return err
}
expectNotAuthorized := func(err error) {
GinkgoHelper()
var subErr subError
Expect(errors.As(err, &subErr)).To(BeTrue())
Expect(subErr.code).To(Equal(responses.ErrorAuthorizationFail))
Expect(w.Header().Get("Location")).To(BeEmpty())
}
It("redirects to the caller's own gravatar", func() {
Expect(getAvatar(regular, "REGULAR")).To(Succeed())
Expect(w.Code).To(Equal(http.StatusFound))
Expect(w.Header().Get("Location")).To(Equal(gravatar.Url(regular.Email, 0)))
})
It("refuses a regular user another user's avatar", func() {
expectNotAuthorized(getAvatar(regular, other.UserName))
})
It("refuses an unknown username the same way", func() {
expectNotAuthorized(getAvatar(regular, "no-such-user"))
})
It("refuses an admin another user's avatar, like getUser", func() {
expectNotAuthorized(getAvatar(admin, other.UserName))
})
})
Describe("GetLyrics", func() {
It("should return data for given artist & title", func() {
r := newGetRequest("artist=Rick+Astley", "title=Never+Gonna+Give+You+Up")