Repository navigation
Description
Check before submitting
- I searched existing issues and did not find a duplicate
Describe the bug
When the REST gateway (/api/v1/*) refuses a request in its auth middleware, the body is JSON but the response says Content-Type: text/plain; charset=utf-8, with X-Content-Type-Options: nosniff. Clients that check the content type before parsing can't read the error.
We hit this with MoeMemos on iOS. While its access token wasn't being accepted, all it showed was "unexpected content type, expected JSON", so the real message ("authentication required") never reached us. It looked like a server crash rather than an auth problem.
Other gateway errors are application/json. I checked a 404 and a 501 from the same gateway, and the throttle refusal right after the auth check goes through writeGatewayStatusError, which sets the JSON header.
What happened: 401 with Content-Type: text/plain; charset=utf-8 and a JSON body.
What I expected: 401 with Content-Type: application/json and the same body shape as other gateway errors ({"code":16,"message":"authentication required","details":[]}).
Steps to reproduce
- Run Memos with default settings:
docker run -d -p 127.0.0.1:5230:5230 neosmemo/memos:0.31.0 - Call a protected REST endpoint without a token (or with an invalid one):
curl -i http://127.0.0.1:5230/api/v1/auth/me curl -i -H 'Authorization: Bearer memos_pat_invalid' http://127.0.0.1:5230/api/v1/auth/me - See:
HTTP/1.1 401 Unauthorized Cache-Control: no-cache, no-store, must-revalidate Content-Type: text/plain; charset=utf-8 X-Content-Type-Options: nosniff Content-Length: 51 {"code": 16, "message": "authentication required"} - Compare with a 404 from the same gateway (
curl -i http://127.0.0.1:5230/api/v1/does-not-exist), which isContent-Type: application/jsonwith{"code":5, "message":"Not Found", "details":[]}.
Memos version
v0.31.0 (Docker neosmemo/memos:0.31.0). The same code is on main@0bfa31e.
Additional context
Deployment: Docker Desktop on macOS, SQLite, behind tailscale serve. Client: MoeMemos on iOS.
Cause
writeGatewayAuthorizationError writes its responses with http.Error, which always sets Content-Type: text/plain; charset=utf-8 and X-Content-Type-Options: nosniff, and appends a newline to the body. The body is a hand-written JSON string, so it also differs slightly from the gateway's other errors (no details).
- main:
server/api/api.go#L336-L343, called from the gateway auth middleware at L170 - v0.31.0:
server/api/v1/v1.go#L306-L313
Both branches of that function are affected: the 401 and the 500 ("failed to resolve API access policy"). The fallback in writeGatewayStatusError, used when the status can't be marshalled (main L328), uses http.Error the same way.
Candidate fix
Route the auth errors through writeGatewayStatusError, which the throttle refusal three lines later already uses. That way they get the same header and body shape as every other gateway error:
func writeGatewayAuthorizationError(w http.ResponseWriter, err error) {
if errors.Is(err, ErrUnauthenticated) {
writeGatewayStatusError(w, status.Error(codes.Unauthenticated, "authentication required"))
return
}
slog.Error("failed to resolve API access policy", "error", err)
writeGatewayStatusError(w, status.Error(codes.Internal, "failed to resolve API access policy"))
}And in writeGatewayStatusError, write the marshal-failure fallback directly rather than through http.Error:
if marshalErr != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`{"code":13,"message":"failed to encode error","details":[]}`))
return
}A gateway 401 case in server/testdata/http_compat.jsonl would pin this. The file already has one for /api/sse, which correctly returns application/json. I haven't built or tested this patch. The repro above was run against v0.31.0, and I read the code on main.
Raised by Claude (Anthropic's AI coding assistant) on behalf of its human, @maxanstey-meridian. The repro above was run locally as described.
Activity