π€― LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
π fix(openapi): make messageCount opt-in on GET /users/me (#23119)
LiJian911a1fde4e81
diff --git a/packages/openapi/src/controllers/user.controller.test.ts b/packages/openapi/src/controllers/user.controller.test.tsindex 2d14771963c..f47e6ec1fed 100644--- a/packages/openapi/src/controllers/user.controller.test.ts+++ b/packages/openapi/src/controllers/user.controller.test.ts@@ -83,40 +83,45 @@ describe('UserController.getCurrentUser', () => {});});- it('withholds the count from a restricted key without chat:read, without failing', async () => {+ it('skips the count unless the caller opts in, even for full-access callers', async () => {+ for (const auth of [{ authType: 'apikey', scopes: ['*'] }, { authType: 'oidc' }]) {+ await new UserController().getCurrentUser(contextFor(auth));+ expect(getCurrentUser).toHaveBeenLastCalledWith(false);++ for (const query of ['includeCount=0', 'includeCount=false', 'includeCount=yes']) {+ await new UserController().getCurrentUser(contextFor(auth, query));+ expect(getCurrentUser).toHaveBeenLastCalledWith(false);+ }+ }+ });++ it('withholds an opted-in count from a restricted key without chat:read, without failing', async () => {const res = await new UserController().getCurrentUser(- contextFor({- authType: 'apikey',- scopes: ['agent:read'],- }),+ contextFor({ authType: 'apikey', scopes: ['agent:read'] }, 'includeCount=1'),);expect(res.status).toBe(200);expect(getCurrentUser).toHaveBeenCalledWith(false);});- it('returns the count to a restricted key holding chat:read', async () => {+ it('returns an opted-in count to a restricted key holding chat:read', async () => {await new UserController().getCurrentUser(- contextFor({ authType: 'apikey', scopes: ['chat:read'] }),+ contextFor({ authType: 'apikey', scopes: ['chat:read'] }, 'includeCount=1'),);expect(getCurrentUser).toHaveBeenCalledWith(true);});- it('returns the count to full-access keys and session callers', async () => {- await new UserController().getCurrentUser(contextFor({ authType: 'apikey', scopes: ['*'] }));- expect(getCurrentUser).toHaveBeenLastCalledWith(true);-- await new UserController().getCurrentUser(contextFor({ authType: 'apikey', scopes: null }));- expect(getCurrentUser).toHaveBeenLastCalledWith(true);-- await new UserController().getCurrentUser(contextFor({ authType: 'oidc' }));- expect(getCurrentUser).toHaveBeenLastCalledWith(true);- });-- it('still honours an explicit includeCount=0 for permitted callers', async () => {- await new UserController().getCurrentUser(contextFor({ authType: 'oidc' }, 'includeCount=0'));-- expect(getCurrentUser).toHaveBeenCalledWith(false);+ it('returns an opted-in count to full-access keys and session callers', async () => {+ for (const auth of [+ { authType: 'apikey', scopes: ['*'] },+ { authType: 'apikey', scopes: null },+ { authType: 'oidc' },+ ]) {+ for (const query of ['includeCount=1', 'includeCount=true']) {+ await new UserController().getCurrentUser(contextFor(auth, query));+ expect(getCurrentUser).toHaveBeenLastCalledWith(true);+ }+ }});});diff --git a/packages/openapi/src/controllers/user.controller.ts b/packages/openapi/src/controllers/user.controller.tsindex 83c2585cb50..286a510208b 100644--- a/packages/openapi/src/controllers/user.controller.ts+++ b/packages/openapi/src/controllers/user.controller.ts@@ -32,16 +32,20 @@ export class UserController extends BaseController {* - without `user:read` the response is narrowed to the id, keeping the* caller's email, phone, preferences and role names out of a key that was* only granted, say, `model:invoke`- * - `messageCount` is chat-usage metadata and additionally needs `chat:read`+ * - `messageCount` is opt-in (`?includeCount=1`) and additionally needs+ * `chat:read`: counting a user's messages scans every row they own, which+ * takes tens of seconds for heavy users, while most callers (`lh login`,+ * the agent gateway verifying an API key) only need identity. Callers that+ * want the number can also use `GET /api/v1/messages/count`.*- * Narrowed rather than rejected throughout: `includeCount` defaults to true,- * so answering with 403 would make identity resolution depend on a query- * parameter β the exact trap this endpoint just came out of.+ * Narrowed rather than rejected throughout: answering with 403 would make+ * identity resolution depend on a query parameter β the exact trap this+ * endpoint just came out of.*/async getCurrentUser(c: Context): Promise<Response> {try {const includeCountQuery = c.req.query('includeCount');- const countRequested = includeCountQuery !== '0' && includeCountQuery !== 'false';+ const countRequested = includeCountQuery === '1' || includeCountQuery === 'true';const includeCount = countRequested && this.callerHasScope(c, 'chat:read');// Get database connection and create service instancediff --git a/packages/openapi/src/routes/users.route.ts b/packages/openapi/src/routes/users.route.tsindex 9af3a90471f..f88ad22cfc1 100644--- a/packages/openapi/src/routes/users.route.ts+++ b/packages/openapi/src/routes/users.route.ts@@ -35,7 +35,8 @@ UserRoutes.get(//// Reachability is not disclosure: the payload is scoped inside// `UserController.getCurrentUser` β a key without `user:read` receives only- // `{ id }`, and `messageCount` additionally needs `chat:read`. Widen there,+ // `{ id }`, and `messageCount` is opt-in (`?includeCount=1`) and additionally+ // needs `chat:read`. Widen there,// under a scope check, rather than by gating the route.async (c) => {const userController = new UserController();diff --git a/packages/openapi/src/services/user.service.ts b/packages/openapi/src/services/user.service.tsindex 18801dde372..1b5de3e2d7d 100644--- a/packages/openapi/src/services/user.service.ts+++ b/packages/openapi/src/services/user.service.ts@@ -42,7 +42,7 @@ export class UserService extends BaseService {* @param userId User ID* @returns User info and role info*/- private async getUserWithRoles(userId: string, includeCount = true): Promise<UserWithRoles> {+ private async getUserWithRoles(userId: string, includeCount = false): Promise<UserWithRoles> {// Use subquery approach to avoid complex GROUP BYconst user = await this.db.query.users.findFirst({where: eq(users.id, userId),@@ -90,7 +90,7 @@ export class UserService extends BaseService {* Get the currently logged-in user info* @returns User info*/- async getCurrentUser(includeCount = true): ServiceResult<UserWithRoles> {+ async getCurrentUser(includeCount = false): ServiceResult<UserWithRoles> {this.log('info', 'θ·εε½εη»ε½η¨ζ·δΏ‘ζ―εθ§θ²δΏ‘ζ―');// Query basic user info