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:
@@ -37,6 +37,17 @@ func UserFromContext(ctx context.Context) (dbq.User, bool) {
|
|||||||
return u, ok
|
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
|
// 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
|
// 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
|
// envelope using the request's f= format; downstream handlers never see an
|
||||||
|
|||||||
@@ -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),
|
FromYear: int32(from), ToYear: int32(to), Lim: int32(size), Off: int32(offset),
|
||||||
})
|
})
|
||||||
case "starred", "highest":
|
case "starred", "highest":
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
albums, err = q.ListLikedAlbumRows(r.Context(), dbq.ListLikedAlbumRowsParams{
|
albums, err = q.ListLikedAlbumRows(r.Context(), dbq.ListLikedAlbumRowsParams{
|
||||||
UserID: user.ID, Limit: int32(size), Offset: int32(offset),
|
UserID: user.ID, Limit: int32(size), Offset: int32(offset),
|
||||||
})
|
})
|
||||||
case "recent", "frequent":
|
case "recent", "frequent":
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
if listType == "recent" {
|
if listType == "recent" {
|
||||||
|
|||||||
@@ -63,9 +63,8 @@ func (b *browseHandlers) getSongsByGenre(w http.ResponseWriter, r *http.Request)
|
|||||||
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: genre")
|
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: genre")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
count := clampInt(atoiDefault(params.Get("count"), 10), 1, 500)
|
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")
|
WriteFail(w, r, ErrMissingParameter, "Missing required parameter: artist")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
count := clampInt(atoiDefault(params.Get("count"), 50), 1, 500)
|
count := clampInt(atoiDefault(params.Get("count"), 50), 1, 500)
|
||||||
|
|||||||
@@ -91,9 +91,8 @@ func playlistRef(p *playlists.PlaylistRow) PlaylistRef {
|
|||||||
// what Minstrel makes fresh each day; within each group the service's order
|
// what Minstrel makes fresh each day; within each group the service's order
|
||||||
// (most recently changed first) stands.
|
// (most recently changed first) stands.
|
||||||
func (h *pickHandlers) getPlaylists(w http.ResponseWriter, r *http.Request) {
|
func (h *pickHandlers) getPlaylists(w http.ResponseWriter, r *http.Request) {
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
rows, err := h.playlists.List(r.Context(), user.ID)
|
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
|
// 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.
|
// 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) {
|
func (h *pickHandlers) getPlaylist(w http.ResponseWriter, r *http.Request) {
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
raw := r.URL.Query().Get("id")
|
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
|
// 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.
|
// server's radio length. genre, fromYear and toYear narrow the library first.
|
||||||
func (h *pickHandlers) getRandomSongs(w http.ResponseWriter, r *http.Request) {
|
func (h *pickHandlers) getRandomSongs(w http.ResponseWriter, r *http.Request) {
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
params := r.URL.Query()
|
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
|
// album id; otherwise only an artist id is a seed. It writes the failure
|
||||||
// itself and reports false when there is nothing to answer.
|
// itself and reports false when there is nothing to answer.
|
||||||
func (h *pickHandlers) similar(w http.ResponseWriter, r *http.Request, anyKind bool) ([]SongRef, bool) {
|
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 {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
params := r.URL.Query()
|
params := r.URL.Query()
|
||||||
|
|||||||
@@ -22,9 +22,8 @@ import (
|
|||||||
// playlistId, replaces that playlist's songs (and its name, when name is
|
// playlistId, replaces that playlist's songs (and its name, when name is
|
||||||
// given). It answers with the playlist, as getPlaylist does.
|
// given). It answers with the playlist, as getPlaylist does.
|
||||||
func (h *pickHandlers) createPlaylist(w http.ResponseWriter, r *http.Request) {
|
func (h *pickHandlers) createPlaylist(w http.ResponseWriter, r *http.Request) {
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
params := r.URL.Query()
|
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
|
// 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.
|
// them before this call, so the song removed is the one the app showed.
|
||||||
func (h *pickHandlers) updatePlaylist(w http.ResponseWriter, r *http.Request) {
|
func (h *pickHandlers) updatePlaylist(w http.ResponseWriter, r *http.Request) {
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
params := r.URL.Query()
|
params := r.URL.Query()
|
||||||
@@ -166,9 +164,8 @@ func (h *pickHandlers) updatePlaylist(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// deletePlaylist deletes the playlist id.
|
// deletePlaylist deletes the playlist id.
|
||||||
func (h *pickHandlers) deletePlaylist(w http.ResponseWriter, r *http.Request) {
|
func (h *pickHandlers) deletePlaylist(w http.ResponseWriter, r *http.Request) {
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
raw := r.URL.Query().Get("id")
|
raw := r.URL.Query().Get("id")
|
||||||
|
|||||||
@@ -44,9 +44,8 @@ type PlayQueueResponse struct {
|
|||||||
// in the library drops out of it; if that was the current song, the queue
|
// in the library drops out of it; if that was the current song, the queue
|
||||||
// has no current song.
|
// has no current song.
|
||||||
func (b *browseHandlers) getPlayQueue(w http.ResponseWriter, r *http.Request) {
|
func (b *browseHandlers) getPlayQueue(w http.ResponseWriter, r *http.Request) {
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
q := dbq.New(b.pool)
|
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,
|
// position the milliseconds into it. With no id the saved queue is cleared,
|
||||||
// as OpenSubsonic specifies.
|
// as OpenSubsonic specifies.
|
||||||
func (b *browseHandlers) savePlayQueue(w http.ResponseWriter, r *http.Request) {
|
func (b *browseHandlers) savePlayQueue(w http.ResponseWriter, r *http.Request) {
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
params := r.URL.Query()
|
params := r.URL.Query()
|
||||||
|
|||||||
@@ -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
|
// at once: the scan records itself a moment after it starts, so the latest
|
||||||
// scan run alone would still read as finished.
|
// scan run alone would still read as finished.
|
||||||
func (s *scanHandlers) startScan(w http.ResponseWriter, r *http.Request) {
|
func (s *scanHandlers) startScan(w http.ResponseWriter, r *http.Request) {
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !user.IsAdmin {
|
if !user.IsAdmin {
|
||||||
|
|||||||
@@ -18,9 +18,8 @@ import (
|
|||||||
// albumId, artistId. All entities are validated before any insert; a single
|
// albumId, artistId. All entities are validated before any insert; a single
|
||||||
// missing entity fails the whole call with Subsonic error 70.
|
// missing entity fails the whole call with Subsonic error 70.
|
||||||
func (m *mediaHandlers) handleStar(w http.ResponseWriter, r *http.Request) {
|
func (m *mediaHandlers) handleStar(w http.ResponseWriter, r *http.Request) {
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrGeneric, "Unauthenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
params := r.URL.Query()
|
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
|
// as a no-op rather than an error — Subsonic clients commonly call unstar
|
||||||
// on entities the server has never seen (cross-server library moves).
|
// on entities the server has never seen (cross-server library moves).
|
||||||
func (m *mediaHandlers) handleUnstar(w http.ResponseWriter, r *http.Request) {
|
func (m *mediaHandlers) handleUnstar(w http.ResponseWriter, r *http.Request) {
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrGeneric, "Unauthenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
params := r.URL.Query()
|
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) {
|
func (m *mediaHandlers) handleGetStarred(w http.ResponseWriter, r *http.Request) {
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrGeneric, "Unauthenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
q := dbq.New(m.pool)
|
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) {
|
func (m *mediaHandlers) handleGetStarred2(w http.ResponseWriter, r *http.Request) {
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrGeneric, "Unauthenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
q := dbq.New(m.pool)
|
q := dbq.New(m.pool)
|
||||||
|
|||||||
@@ -196,9 +196,8 @@ func (m *mediaHandlers) handleScrobble(w http.ResponseWriter, r *http.Request) {
|
|||||||
WriteFail(w, r, ErrDataNotFound, "Track not found")
|
WriteFail(w, r, ErrDataNotFound, "Track not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user, ok := UserFromContext(r.Context())
|
user, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrGeneric, "Unauthenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
at := time.Now().UTC()
|
at := time.Now().UTC()
|
||||||
|
|||||||
@@ -63,9 +63,8 @@ type UsersContainer struct {
|
|||||||
// getUsers lists every user, for an admin only; anyone else gets
|
// getUsers lists every user, for an admin only; anyone else gets
|
||||||
// ErrNotAuthorized, as the spec has it.
|
// ErrNotAuthorized, as the spec has it.
|
||||||
func (h *userHandlers) getUsers(w http.ResponseWriter, r *http.Request) {
|
func (h *userHandlers) getUsers(w http.ResponseWriter, r *http.Request) {
|
||||||
caller, ok := UserFromContext(r.Context())
|
caller, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if !caller.IsAdmin {
|
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
|
// unknown username is ErrDataNotFound. Per spec a non-admin asking about
|
||||||
// another user gets ErrNotAuthorized.
|
// another user gets ErrNotAuthorized.
|
||||||
func (h *userHandlers) getUser(w http.ResponseWriter, r *http.Request) {
|
func (h *userHandlers) getUser(w http.ResponseWriter, r *http.Request) {
|
||||||
caller, ok := UserFromContext(r.Context())
|
caller, ok := requireUser(w, r)
|
||||||
if !ok {
|
if !ok {
|
||||||
WriteFail(w, r, ErrNotAuthorized, "Not authenticated")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
requested := r.URL.Query().Get("username")
|
requested := r.URL.Query().Get("username")
|
||||||
|
|||||||
Reference in New Issue
Block a user