Skip to content

Commit 098f230

Browse files
committed
sec(api/auth): return opaque 401 in login catch-all instead of err.Error() (closes #937)
The login() catch-all error path was forwarding raw service errors verbatim via NewClientError(401, err.Error()), potentially exposing internal account state (e.g. lock status, user IDs, timestamps) to unauthenticated callers. Replace it with the opaque hardcoded string "invalid credentials" so all non-sentinel auth failures (wrong password, account not found, locked, etc.) collapse to the same response, regardless of what the service returns. The typed-sentinel arms above (ErrMFARequired -> "mfa_required", ErrInvalidMFACode -> "invalid_mfa_code") are unchanged. Adds TestHandler_login_OpaqueError_HidesInternalMessage as a regression test that pins the service returning a distinctive internal message and asserts the 401 body is "invalid credentials" with no internal detail leaking through. Salvaged from closed #886.
1 parent c00445c commit 098f230

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎internal/api/handler_auth.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ func (h *Handler) login(ctx context.Context, req *events.LambdaFunctionURLReques
5050
if errors.Is(err, auth.ErrInvalidMFACode) {
5151
return nil, NewClientError(401, "invalid_mfa_code")
5252
}
53-
return nil, NewClientError(401, err.Error())
53+
// All other auth failures (wrong password, account not found, locked, etc.)
54+
// collapse to a single opaque 401. Never forward err.Error() verbatim - it
55+
// may reveal internal account state.
56+
return nil, NewClientError(401, "invalid credentials")
5457
}
5558

5659
return response, nil

‎internal/api/handler_auth_test.go‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,43 @@ func TestHandler_login_AuthError(t *testing.T) {
101101
assert.Equal(t, 401, ce.code)
102102
}
103103

104+
// TestHandler_login_OpaqueError_HidesInternalMessage is a regression test for
105+
// issue #937: the login catch-all 401 must return the opaque "invalid
106+
// credentials" string regardless of what the auth service returns. Internal
107+
// error messages (e.g. "internal: user 42 locked since 2025-01-01") must never
108+
// be forwarded to the client because they reveal account state.
109+
func TestHandler_login_OpaqueError_HidesInternalMessage(t *testing.T) {
110+
ctx := context.Background()
111+
mockAuth := new(MockAuthService)
112+
t.Cleanup(func() { mockAuth.AssertExpectations(t) })
113+
114+
// A service error containing distinctive internal detail that must never
115+
// reach the client.
116+
internalMsg := "internal: user 42 locked since 2025-01-01T00:00:00Z"
117+
mockAuth.On("Login", ctx, mock.Anything).Return((*LoginResponse)(nil), errors.New(internalMsg)).Once()
118+
119+
handler := &Handler{auth: mockAuth}
120+
121+
encodedPassword := base64.StdEncoding.EncodeToString([]byte("anypassword"))
122+
req := &events.LambdaFunctionURLRequest{
123+
Body: `{"email": "victim@example.com", "password": "` + encodedPassword + `"}`,
124+
}
125+
126+
result, err := handler.login(ctx, req)
127+
assert.Nil(t, result)
128+
require.Error(t, err)
129+
130+
ce, ok := IsClientError(err)
131+
require.True(t, ok, "login failure must be a ClientError")
132+
assert.Equal(t, 401, ce.code)
133+
// The opaque message must be returned.
134+
assert.Equal(t, "invalid credentials", ce.message)
135+
// The internal message must not leak to the client.
136+
assert.NotContains(t, ce.message, "internal:", "internal error detail must not be forwarded to the client")
137+
assert.NotContains(t, ce.message, "user 42", "user identifier must not appear in the 401 response")
138+
assert.NotContains(t, ce.message, "locked", "account state must not appear in the 401 response")
139+
}
140+
104141
func TestHandler_logout_Success(t *testing.T) {
105142
ctx := context.Background()
106143
mockAuth := new(MockAuthService)

0 commit comments

Comments
 (0)