Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

fix(api): return JSON media type for authorization errors (#6504)

manuaudioe0b336ce0b67
diff --git a/server/api/api.go b/server/api/api.go
index 63664e827..00d65e2bd 100644
--- a/server/api/api.go
+++ b/server/api/api.go
@@ -323,7 +323,7 @@ func writeGatewayStatusError(w http.ResponseWriter, err error) {
w.Header().Add(key, value)
}
}
- body, marshalErr := protojson.Marshal(st.Proto())
+ body, marshalErr := (protojson.MarshalOptions{EmitDefaultValues: true}).Marshal(st.Proto())
if marshalErr != nil {
http.Error(w, `{"code": 13, "message": "failed to encode error"}`, http.StatusInternalServerError)
return
@@ -335,9 +335,9 @@ func writeGatewayStatusError(w http.ResponseWriter, err error) {
func writeGatewayAuthorizationError(w http.ResponseWriter, err error) {
if errors.Is(err, ErrUnauthenticated) {
- http.Error(w, `{"code": 16, "message": "authentication required"}`, http.StatusUnauthorized)
+ writeGatewayStatusError(w, status.Error(codes.Unauthenticated, "authentication required"))
return
}
slog.Error("failed to resolve API access policy", "error", err)
- http.Error(w, `{"code": 13, "message": "failed to resolve API access policy"}`, http.StatusInternalServerError)
+ writeGatewayStatusError(w, status.Error(codes.Internal, "failed to resolve API access policy"))
}
diff --git a/server/api/authz_test.go b/server/api/authz_test.go
index 14b93ddb9..433233fc4 100644
--- a/server/api/authz_test.go
+++ b/server/api/authz_test.go
@@ -2,6 +2,7 @@ package api
import (
"context"
+ "encoding/json"
"errors"
"net/http"
"net/http/httptest"
@@ -22,14 +23,51 @@ func (s stubAnonymousAccessStore) AllowsAnonymousAccess(context.Context) (bool,
}
func TestWriteGatewayAuthorizationErrorMapsFailureClass(t *testing.T) {
- unauthenticated := httptest.NewRecorder()
- writeGatewayAuthorizationError(unauthenticated, ErrUnauthenticated)
- assert.Equal(t, http.StatusUnauthorized, unauthenticated.Code)
+ cases := []struct {
+ name string
+ err error
+ wantStatus int
+ wantCode int
+ wantMsg string
+ }{
+ {
+ name: "unauthenticated",
+ err: ErrUnauthenticated,
+ wantStatus: http.StatusUnauthorized,
+ wantCode: 16,
+ wantMsg: "authentication required",
+ },
+ {
+ name: "internal authorization failure",
+ err: errors.New("database unavailable"),
+ wantStatus: http.StatusInternalServerError,
+ wantCode: 13,
+ wantMsg: "failed to resolve API access policy",
+ },
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ response := httptest.NewRecorder()
+ writeGatewayAuthorizationError(response, tc.err)
+
+ assert.Equal(t, tc.wantStatus, response.Code)
+ assert.Equal(t, "application/json", response.Header().Get("Content-Type"))
- internal := httptest.NewRecorder()
- writeGatewayAuthorizationError(internal, errors.New("database unavailable"))
- assert.Equal(t, http.StatusInternalServerError, internal.Code)
- assert.NotContains(t, internal.Body.String(), "database unavailable")
+ var body struct {
+ Code int `json:"code"`
+ Message string `json:"message"`
+ Details []json.RawMessage `json:"details"`
+ }
+ if assert.NoError(t, json.Unmarshal(response.Body.Bytes(), &body)) {
+ assert.Equal(t, tc.wantCode, body.Code)
+ assert.Equal(t, tc.wantMsg, body.Message)
+ assert.NotNil(t, body.Details)
+ assert.Empty(t, body.Details)
+ }
+ assert.NotContains(t, response.Body.String(), "database unavailable")
+ })
+ }
}
// TestAuthorizerCheckAccess exercises the method-level access policy matrix.