🤯 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
✨ feat(auth): add auth.getScope to report a credential's effective workspace scope (#22247)
AmAzing-785dddb7eaab
diff --git a/apps/server/src/routers/lambda/__tests__/auth.getScope.test.ts b/apps/server/src/routers/lambda/__tests__/auth.getScope.test.tsnew file mode 100644index 00000000000..4b74edf44b2--- /dev/null+++ b/apps/server/src/routers/lambda/__tests__/auth.getScope.test.ts@@ -0,0 +1,190 @@+// @vitest-environment node+import { API_KEY_PREFIX } from '@lobechat/utils/apiKey';+import { NextRequest } from 'next/server';+import { beforeEach, describe, expect, it, vi } from 'vitest';++import { createLambdaContext } from '@/libs/trpc/lambda/context';++import { lambdaRouter } from '../index';++const { mockFindByKey, mockGetSession, workspaceMembers } = vi.hoisted(() => ({+ mockFindByKey: vi.fn(),+ mockGetSession: vi.fn(),+ // `${workspaceId}:${userId}` pairs the membership check accepts+ workspaceMembers: new Set<string>(),+}));++vi.mock('@/auth', () => ({ auth: { api: { getSession: mockGetSession } } }));++vi.mock('@/database/core/db-adaptor', () => ({ getServerDB: vi.fn().mockResolvedValue({}) }));++vi.mock('@/database/models/apiKey', () => ({+ ApiKeyModel: Object.assign(+ vi.fn(function () {+ return { updateLastUsed: vi.fn().mockResolvedValue(undefined) };+ }),+ { findByKey: mockFindByKey },+ ),+}));++vi.mock('@/database/models/workspace', async (importOriginal) => ({+ ...(await importOriginal<Record<string, unknown>>()),+ hasActiveWorkspaceMembership: vi.fn(async () => true),+}));++vi.mock('@/business/server/workspaceApiKey', () => ({+ canUseWorkspaceApiKeys: vi.fn(async () => true),+}));++vi.mock('@/envs/auth', async (importOriginal) => ({+ ...(await importOriginal<Record<string, unknown>>()),+ authEnv: { ENABLE_OIDC: false },+}));++// The OSS stub leaves `ctx.workspaceId` untouched; the cloud override demotes a+// header naming a workspace the caller is not a member of to personal mode.+// Mirror that demotion so the procedure is exercised against the scope the+// middleware leaves, not the raw header.+vi.mock('@/business/server/trpc-middlewares/workspaceAuth', async (importOriginal) => {+ const { authedProcedure } = await import('@/libs/trpc/lambda');+ const { trpc } = await import('@/libs/trpc/lambda/init');++ const cloudWorkspaceAuth = trpc.middleware(async ({ ctx, next }) => {+ const isMember = !!ctx.workspaceId && workspaceMembers.has(`${ctx.workspaceId}:${ctx.userId}`);++ return next({ ctx: { workspaceId: isMember ? ctx.workspaceId : undefined } });+ });++ return {+ ...(await importOriginal<Record<string, unknown>>()),+ cloudWorkspaceAuth,+ wsCompatProcedure: authedProcedure.use(cloudWorkspaceAuth),+ };+});++const API_KEY = `${API_KEY_PREFIX}aaaaaaaaaaaaaaaa`;++const apiKeyRecord = (workspaceId: string | null, scopes: string[] | null = null) => ({+ accessedAt: new Date(),+ createdAt: new Date(),+ enabled: true,+ expiresAt: null,+ id: 'key-1',+ key: 'encrypted-key',+ keyHash: 'hashed-key',+ lastUsedAt: null,+ name: 'Test API Key',+ scopes,+ updatedAt: new Date(),+ userId: 'key-user',+ workspaceId,+});++const getScope = async (headers: Record<string, string>) => {+ const request = new NextRequest('https://example.com/trpc/lambda/auth.getScope', { headers });+ const caller = lambdaRouter.createCaller(await createLambdaContext(request));++ return caller.auth.getScope();+};++beforeEach(() => {+ vi.clearAllMocks();+ workspaceMembers.clear();+ // the key issuer is an active member of the key's workspace+ workspaceMembers.add('ws-key:key-user');+ mockGetSession.mockResolvedValue(null);+});++describe('auth.getScope', () => {+ describe('workspace API key', () => {+ beforeEach(() => {+ mockFindByKey.mockResolvedValue(apiKeyRecord('ws-key'));+ });++ it('reports the key workspace when no header is sent', async () => {+ await expect(getScope({ 'X-API-Key': API_KEY })).resolves.toEqual({+ scope: 'workspace',+ source: 'apiKey',+ workspaceId: 'ws-key',+ });+ });++ it('reports the key workspace when the header names the same workspace', async () => {+ await expect(getScope({ 'X-API-Key': API_KEY, 'X-Workspace-Id': 'ws-key' })).resolves.toEqual(+ { scope: 'workspace', source: 'apiKey', workspaceId: 'ws-key' },+ );+ });++ it('rejects a header naming another workspace instead of reporting either scope', async () => {+ await expect(+ getScope({ 'X-API-Key': API_KEY, 'X-Workspace-Id': 'ws-other' }),+ ).rejects.toMatchObject({ code: 'UNAUTHORIZED' });+ });++ it('stays reachable for a restricted key', async () => {+ mockFindByKey.mockResolvedValue(apiKeyRecord('ws-key', ['file:read']));++ await expect(getScope({ 'X-API-Key': API_KEY })).resolves.toEqual({+ scope: 'workspace',+ source: 'apiKey',+ workspaceId: 'ws-key',+ });+ });+ });++ describe('personal API key', () => {+ beforeEach(() => {+ mockFindByKey.mockResolvedValue(apiKeyRecord(null));+ });++ it('reports personal scope without a header', async () => {+ await expect(getScope({ 'X-API-Key': API_KEY })).resolves.toEqual({+ scope: 'personal',+ source: 'personal',+ workspaceId: null,+ });+ });++ it('rejects a workspace header instead of reporting either scope', async () => {+ await expect(+ getScope({ 'X-API-Key': API_KEY, 'X-Workspace-Id': 'ws-key' }),+ ).rejects.toMatchObject({ code: 'UNAUTHORIZED' });+ });+ });++ describe('session', () => {+ beforeEach(() => {+ mockGetSession.mockResolvedValue({ user: { id: 'session-user' } });+ });++ it('reports the header workspace for a member', async () => {+ workspaceMembers.add('ws-team:session-user');++ await expect(getScope({ 'X-Workspace-Id': 'ws-team' })).resolves.toEqual({+ scope: 'workspace',+ source: 'header',+ workspaceId: 'ws-team',+ });+ });++ it('reports personal scope when the header names a workspace the user is not in', async () => {+ await expect(getScope({ 'X-Workspace-Id': 'ws-team' })).resolves.toEqual({+ scope: 'personal',+ source: 'personal',+ workspaceId: null,+ });+ });++ it('reports personal scope without a header', async () => {+ await expect(getScope({})).resolves.toEqual({+ scope: 'personal',+ source: 'personal',+ workspaceId: null,+ });+ });+ });++ it('rejects an unauthenticated request', async () => {+ await expect(getScope({})).rejects.toMatchObject({ code: 'UNAUTHORIZED' });+ });+});diff --git a/apps/server/src/routers/lambda/auth.ts b/apps/server/src/routers/lambda/auth.tsnew file mode 100644index 00000000000..46ad822737c--- /dev/null+++ b/apps/server/src/routers/lambda/auth.ts@@ -0,0 +1,41 @@+import { wsCompatProcedure } from '@/business/server/trpc-middlewares/workspaceAuth';+import { router } from '@/libs/trpc/lambda';++/**+ * Where the effective workspace of a request came from:+ * - `apiKey`: a workspace API key, which binds every call to its own workspace+ * regardless of `X-Workspace-Id`;+ * - `header`: a session/OIDC request scoped by `X-Workspace-Id`;+ * - `personal`: no workspace is in effect.+ */+export type CredentialScopeSource = 'apiKey' | 'header' | 'personal';++export interface CredentialScope {+ scope: 'personal' | 'workspace';+ source: CredentialScopeSource;+ workspaceId: string | null;+}++export const authRouter = router({+ /**+ * The workspace scope the presented credential actually runs under, so a+ * client (e.g. `lh whoami`) reports it instead of guessing it locally.+ *+ * Runs behind `wsCompatProcedure`, the same middleware chain as the content+ * routers: the cloud `cloudWorkspaceAuth` demotes a header naming a workspace+ * the caller is not a member of to personal mode, and this reports the scope+ * left after that — not the raw header.+ */+ getScope: wsCompatProcedure.query(({ ctx }): CredentialScope => {+ const workspaceId = ctx.workspaceId ?? null;++ if (!workspaceId) return { scope: 'personal', source: 'personal', workspaceId: null };++ // `apiKeyScopes` is set (a list, or `null` for full access) only when the+ // request authenticated with an API key; `createLambdaContext` then pins+ // `workspaceId` to the key's own workspace.+ const source = ctx.apiKeyScopes !== undefined ? 'apiKey' : 'header';++ return { scope: 'workspace', source, workspaceId };+ }),+});diff --git a/apps/server/src/routers/lambda/index.ts b/apps/server/src/routers/lambda/index.tsindex f9ef80cfeed..8b546e09860 100644--- a/apps/server/src/routers/lambda/index.ts+++ b/apps/server/src/routers/lambda/index.ts@@ -45,6 +45,7 @@ import { aiModelRouter } from './aiModel';import { aiProviderRouter } from './aiProvider';import { apiKeyRouter } from './apiKey';import { asrRouter } from './asr';+import { authRouter } from './auth';import { botMessageRouter } from './botMessage';import { briefRouter } from './brief';import { canvasRouter } from './canvas';@@ -154,6 +155,7 @@ export const lambdaRouter = router({aiProvider: aiProviderRouter,apiKey: apiKeyRouter,asr: asrRouter,+ auth: authRouter,tts: ttsRouter,chunk: chunkRouter,comfyui: comfyuiRouter,diff --git a/packages/const/src/apiKeyScope.ts b/packages/const/src/apiKeyScope.tsindex 2b05f8ff28b..e1a166de898 100644--- a/packages/const/src/apiKeyScope.ts+++ b/packages/const/src/apiKeyScope.ts@@ -205,6 +205,9 @@ export const TRPC_NAMESPACE_API_KEY_RULES: Record<string, TrpcNamespaceScopeRule// keys must not mint or manage keysapiKey: 'blocked',asr: { any: 'model:invoke' },+ // `auth.getScope` only reports which scope the presented credential runs+ // under, so any key — however narrow — can tell where its calls land+ auth: 'open',artifactShare: rw('chat:read', null),// decrypts stored bot/messenger credentials and calls external channel APIsbotMessage: 'blocked',