mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-18 07:30:29 +00:00
Four independent bugs found while reviewing the HTTP layer:
initial_setup.go: createInitialAdminUser assigned the users.Put error to a
shadowed err, so the outer err (always nil by then, since a CountAll failure
panics) was returned instead. A failure to create the admin user was reported
as success, and initialSetup went on to commit the "setup complete" property
in the same transaction — so no admin user existed and initial setup was
skipped on every later boot.
auth.go: createAdminUser logged the Put error but returned nil, so createAdmin
fell through to doLogin and answered 401 "Invalid username or password"
instead of surfacing the real failure. It also logged the whole model.User,
which puts the new admin's password in the log in clear text; every other call
site logs user.UserName.
native_api.go: writeDeleteManyResponse did not return after http.Error when
marshaling failed, then wrote a nil body over the 500. It also built the
single-id body by hand with html.EscapeString, which does not escape
backslashes, so an id ending in one produced `{"id":"a\"}` — invalid JSON.
Both shapes now go through json.Marshal. A failed Write is now logged rather
than answered with http.Error, which could not work once the body had started.
handle_shares.go: handleM3U set Content-Type after WriteHeader, so it was
never sent and shared playlists were served with a sniffed type.
Signed-off-by: zapisanchez <zapisanchez@gmail.com>
51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package nativeapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
var _ = Describe("writeDeleteManyResponse", func() {
|
|
var w *httptest.ResponseRecorder
|
|
|
|
write := func(ids ...string) map[string]any {
|
|
w = httptest.NewRecorder()
|
|
writeDeleteManyResponse(w, httptest.NewRequest("DELETE", "/missing", nil), ids)
|
|
|
|
var body map[string]any
|
|
Expect(json.Unmarshal(w.Body.Bytes(), &body)).To(Succeed(), "response body must be valid JSON: %s", w.Body.String())
|
|
return body
|
|
}
|
|
|
|
It("returns a single id as an object", func() {
|
|
Expect(write("abc123")).To(HaveKeyWithValue("id", "abc123"))
|
|
})
|
|
|
|
It("returns multiple ids as a list", func() {
|
|
Expect(write("a", "b")).To(HaveKeyWithValue("ids", ConsistOf("a", "b")))
|
|
})
|
|
|
|
// ids come straight from the query string. html.EscapeString, used previously to
|
|
// build the single-id body, does not escape backslashes, so `{"id":"a\"}` went out.
|
|
It("stays valid JSON when the id contains a backslash", func() {
|
|
Expect(write(`a\`)).To(HaveKeyWithValue("id", `a\`))
|
|
})
|
|
|
|
It("stays valid JSON when the id contains a quote", func() {
|
|
Expect(write(`a"b`)).To(HaveKeyWithValue("id", `a"b`))
|
|
})
|
|
|
|
It("does not HTML-escape the id into entities", func() {
|
|
Expect(write("a&b")).To(HaveKeyWithValue("id", "a&b"))
|
|
})
|
|
|
|
It("responds 200 on success", func() {
|
|
write("abc123")
|
|
Expect(w.Code).To(Equal(http.StatusOK))
|
|
})
|
|
})
|