🤯 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): resolve model ownership from the caller's own row (#23413)
Innei4f1818a02fe0
diff --git a/packages/openapi/src/common/base.service.ts b/packages/openapi/src/common/base.service.tsindex 4617b0a7a0f..24c16a65049 100644--- a/packages/openapi/src/common/base.service.ts+++ b/packages/openapi/src/common/base.service.ts@@ -367,11 +367,16 @@ export abstract class BaseService implements IBaseService {return workspaceModel?.userId;}- const targetModel = await this.db.query.aiModels.findFirst({+ // A model id names a model, not a row: `ai_models` is unique on+ // (id, providerId, userId), so other accounts hold their own rows under+ // the same id. Only the caller's row says anything about ownership; with+ // none, the model is unowned for this caller (e.g. a built-in one) and+ // must not be attributed to whichever account's row the lookup hits.+ const ownModel = await this.db.query.aiModels.findFirst({columns: { userId: true },- where: eq(aiModels.id, target.targetModelId),+ where: and(eq(aiModels.id, target.targetModelId), eq(aiModels.userId, this.userId)),});- return targetModel?.userId;+ return ownModel?.userId;}default: {diff --git a/packages/openapi/src/routes/models.route.test.ts b/packages/openapi/src/routes/models.route.test.tsindex 2950a0a9367..95e9c9d5ec0 100644--- a/packages/openapi/src/routes/models.route.test.ts+++ b/packages/openapi/src/routes/models.route.test.ts@@ -10,9 +10,11 @@ import { ModelListSchema, ModelSchema } from '../types/model.type';const db = await getTestDB();vi.mock('@/database/core/db-adaptor', () => ({ getServerDB: async () => db }));+// Every scope is granted unless a test narrows it; codes end in `:all` / `:owner`.+let grants: (code: string) => boolean = () => true;vi.mock('@/database/models/rbac', () => ({RbacModel: class {- hasAnyPermission = async () => true;+ hasAnyPermission = async (codes: string[]) => codes.some((code) => grants(code));},}));vi.mock('../middleware/auth', () => ({@@ -42,6 +44,7 @@ const request = (path: string, init: { body?: unknown; method?: string } = {}) =const json = async (res: Response) => ((await res.json()) as { data: any }).data;beforeEach(async () => {+ grants = () => true;await db.delete(aiModels);await db.delete(users);await db.insert(users).values({ id: USER });@@ -77,3 +80,59 @@ describe('model routes answer their documented payloads', () => {expect((await request('/my-provider/missing')).status).toBe(404);});});++/**+ * A model id is not an owner: `ai_models` is unique on (id, providerId, userId),+ * so every account that enables `gpt-4o` holds its own `gpt-4o` row. Resolving+ * ownership by id alone picked whichever row came first and, when that was+ * another account's, answered an owner-scoped caller with 403 — which is what+ * made `POST /api/v1/chat` fail for personal accounts on shared model ids.+ */+describe('model ownership with a model id other accounts also hold', () => {+ const OTHER = 'model-route-other-user';++ beforeEach(async () => {+ grants = (code) => code.endsWith(':owner');+ await db.insert(users).values({ id: OTHER });+ // The other account's row first, so an id-only lookup finds it.+ await db.insert(aiModels).values([+ { displayName: 'Theirs', id: 'shared-model', providerId: 'openai', userId: OTHER },+ { displayName: 'Mine', id: 'shared-model', providerId: 'openai', userId: USER },+ ]);+ });++ it('lets an owner-scoped caller read their own row', async () => {+ const res = await request('/openai/shared-model');++ expect(res.status).toBe(200);+ expect((await json(res)).displayName).toBe('Mine');+ });++ it('updates only the caller’s row', async () => {+ const res = await request('/openai/shared-model', {+ body: { displayName: 'Renamed' },+ method: 'PATCH',+ });++ expect(res.status).toBe(200);+ const rows = await db.query.aiModels.findMany({ columns: { displayName: true, userId: true } });+ expect(rows).toEqual(+ expect.arrayContaining([+ { displayName: 'Renamed', userId: USER },+ { displayName: 'Theirs', userId: OTHER },+ ]),+ );+ });++ it('still finds nothing when only another account holds the id', async () => {+ await db.delete(aiModels);+ await db.insert(aiModels).values({+ displayName: 'Theirs',+ id: 'their-model',+ providerId: 'openai',+ userId: OTHER,+ });++ expect((await request('/openai/their-model')).status).toBe(404);+ });+});