fix(subsonic): a handler without a caller answers error 50 everywhere (#8002)

Sixteen handlers answered a missing caller with error 50 "Not
authenticated"; the star, unstar and scrobble handlers answered error 0
"Unauthenticated". Middleware stops such a request first, so no app saw
it, but the next handler copied would pick one at random. requireUser
now holds the check, the same shape as /api's, at all 21 sites.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-26 12:44:05 -05:00
co-authored by Claude Opus 5.5
parent 0811775af6
commit cfdd6b916f
10 changed files with 32 additions and 42 deletions
+11
View File
@@ -37,6 +37,17 @@ func UserFromContext(ctx context.Context) (dbq.User, bool) {
return u, ok
}
// requireUser returns the caller Middleware put in the context. Without one
// (a handler mounted outside Middleware) it answers error 50 and returns
// false, and the handler must return at once.
func requireUser(w http.ResponseWriter, r *http.Request) (dbq.User, bool) {
user, ok := UserFromContext(r.Context())
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
}
return user, ok
}
// Middleware authenticates the request against users.api_token_hash (apiKey) or
// users.subsonic_password (t/s or p). On failure it writes a Subsonic failed
// envelope using the request's f= format; downstream handlers never see an
+2 -4
View File
@@ -350,18 +350,16 @@ func (b *browseHandlers) listAlbums(w http.ResponseWriter, r *http.Request, q *d
FromYear: int32(from), ToYear: int32(to), Lim: int32(size), Off: int32(offset),
})
case "starred", "highest":
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return nil, false
}
albums, err = q.ListLikedAlbumRows(r.Context(), dbq.ListLikedAlbumRowsParams{
UserID: user.ID, Limit: int32(size), Offset: int32(offset),
})
case "recent", "frequent":
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return nil, false
}
if listType == "recent" {
+2 -4
View File
@@ -63,9 +63,8 @@ func (b *browseHandlers) getSongsByGenre(w http.ResponseWriter, r *http.Request)
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: genre")
return
}
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
count := clampInt(atoiDefault(params.Get("count"), 10), 1, 500)
@@ -114,9 +113,8 @@ func (b *browseHandlers) getTopSongs(w http.ResponseWriter, r *http.Request) {
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: artist")
return
}
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
count := clampInt(atoiDefault(params.Get("count"), 50), 1, 500)
+4 -8
View File
@@ -91,9 +91,8 @@ func playlistRef(p *playlists.PlaylistRow) PlaylistRef {
// what Minstrel makes fresh each day; within each group the service's order
// (most recently changed first) stands.
func (h *pickHandlers) getPlaylists(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
rows, err := h.playlists.List(r.Context(), user.ID)
@@ -126,9 +125,8 @@ func (h *pickHandlers) getPlaylists(w http.ResponseWriter, r *http.Request) {
// whose file is missing, or that has left the library, keeps its place in
// Minstrel but is not listed here, since a Subsonic app would try to play it.
func (h *pickHandlers) getPlaylist(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
raw := r.URL.Query().Get("id")
@@ -272,9 +270,8 @@ type RandomSongsResponse struct {
// size defaults to 10, as the Subsonic API defines it, and is capped at the
// server's radio length. genre, fromYear and toYear narrow the library first.
func (h *pickHandlers) getRandomSongs(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
params := r.URL.Query()
@@ -404,9 +401,8 @@ func (h *pickHandlers) getSimilarSongs2(w http.ResponseWriter, r *http.Request)
// album id; otherwise only an artist id is a seed. It writes the failure
// itself and reports false when there is nothing to answer.
func (h *pickHandlers) similar(w http.ResponseWriter, r *http.Request, anyKind bool) ([]SongRef, bool) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return nil, false
}
params := r.URL.Query()
+3 -6
View File
@@ -22,9 +22,8 @@ import (
// playlistId, replaces that playlist's songs (and its name, when name is
// given). It answers with the playlist, as getPlaylist does.
func (h *pickHandlers) createPlaylist(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
params := r.URL.Query()
@@ -87,9 +86,8 @@ func (h *pickHandlers) createPlaylist(w http.ResponseWriter, r *http.Request) {
// songIdToAdd at the end. Each index counts the songs as getPlaylist lists
// them before this call, so the song removed is the one the app showed.
func (h *pickHandlers) updatePlaylist(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
params := r.URL.Query()
@@ -166,9 +164,8 @@ func (h *pickHandlers) updatePlaylist(w http.ResponseWriter, r *http.Request) {
// deletePlaylist deletes the playlist id.
func (h *pickHandlers) deletePlaylist(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
raw := r.URL.Query().Get("id")
+2 -4
View File
@@ -44,9 +44,8 @@ type PlayQueueResponse struct {
// in the library drops out of it; if that was the current song, the queue
// has no current song.
func (b *browseHandlers) getPlayQueue(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
q := dbq.New(b.pool)
@@ -83,9 +82,8 @@ func (b *browseHandlers) getPlayQueue(w http.ResponseWriter, r *http.Request) {
// position the milliseconds into it. With no id the saved queue is cleared,
// as OpenSubsonic specifies.
func (b *browseHandlers) savePlayQueue(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
params := r.URL.Query()
+1 -2
View File
@@ -67,9 +67,8 @@ func (s *scanHandlers) getScanStatus(w http.ResponseWriter, r *http.Request) {
// at once: the scan records itself a moment after it starts, so the latest
// scan run alone would still read as finished.
func (s *scanHandlers) startScan(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
if !user.IsAdmin {
+4 -8
View File
@@ -18,9 +18,8 @@ import (
// albumId, artistId. All entities are validated before any insert; a single
// missing entity fails the whole call with Subsonic error 70.
func (m *mediaHandlers) handleStar(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrGeneric, "Unauthenticated")
return
}
params := r.URL.Query()
@@ -70,9 +69,8 @@ func (m *mediaHandlers) handleStar(w http.ResponseWriter, r *http.Request) {
// as a no-op rather than an error — Subsonic clients commonly call unstar
// on entities the server has never seen (cross-server library moves).
func (m *mediaHandlers) handleUnstar(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrGeneric, "Unauthenticated")
return
}
params := r.URL.Query()
@@ -137,9 +135,8 @@ func resolveStarID(ctx context.Context, q *dbq.Queries, raw string, kind entityK
}
func (m *mediaHandlers) handleGetStarred(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrGeneric, "Unauthenticated")
return
}
q := dbq.New(m.pool)
@@ -159,9 +156,8 @@ func (m *mediaHandlers) handleGetStarred(w http.ResponseWriter, r *http.Request)
}
func (m *mediaHandlers) handleGetStarred2(w http.ResponseWriter, r *http.Request) {
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrGeneric, "Unauthenticated")
return
}
q := dbq.New(m.pool)
+1 -2
View File
@@ -196,9 +196,8 @@ func (m *mediaHandlers) handleScrobble(w http.ResponseWriter, r *http.Request) {
WriteFail(w, r, ErrDataNotFound, "Track not found")
return
}
user, ok := UserFromContext(r.Context())
user, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrGeneric, "Unauthenticated")
return
}
at := time.Now().UTC()
+2 -4
View File
@@ -63,9 +63,8 @@ type UsersContainer struct {
// getUsers lists every user, for an admin only; anyone else gets
// ErrNotAuthorized, as the spec has it.
func (h *userHandlers) getUsers(w http.ResponseWriter, r *http.Request) {
caller, ok := UserFromContext(r.Context())
caller, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
if !caller.IsAdmin {
@@ -89,9 +88,8 @@ func (h *userHandlers) getUsers(w http.ResponseWriter, r *http.Request) {
// unknown username is ErrDataNotFound. Per spec a non-admin asking about
// another user gets ErrNotAuthorized.
func (h *userHandlers) getUser(w http.ResponseWriter, r *http.Request) {
caller, ok := UserFromContext(r.Context())
caller, ok := requireUser(w, r)
if !ok {
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
return
}
requested := r.URL.Query().Get("username")