Skip to content

🤯 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.

🐛 fix(creds): preserve workspace scope in sandbox injection (#17639)

AmAzing-678529b1182f
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/__tests__/creds.test.ts b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/creds.test.ts
new file mode 100644
index 00000000000..5a917f1cac0
--- /dev/null
+++ b/apps/server/src/services/toolExecution/serverRuntimes/__tests__/creds.test.ts
@@ -0,0 +1,87 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { MarketService } from '@/server/services/market';
+
+import { type ToolExecutionContext } from '../../types';
+import { credsRuntime } from '../creds';
+
+const { getMember } = vi.hoisted(() => ({
+ getMember: vi.fn(),
+}));
+
+vi.mock('@/database/models/workspaceMember', () => ({
+ WorkspaceMemberModel: vi.fn().mockImplementation(() => ({ getMember })),
+}));
+
+vi.mock('@/server/services/market', () => ({
+ MarketService: vi.fn(),
+}));
+
+describe('credsRuntime', () => {
+ const serverDB = {} as NonNullable<ToolExecutionContext['serverDB']>;
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ getMember.mockResolvedValue({ role: 'member' });
+ });
+
+ it('signs verified workspace context into the Market trusted-client identity', async () => {
+ await credsRuntime.factory({
+ serverDB,
+ toolManifestMap: {},
+ topicId: 'topic-1',
+ userId: 'user-1',
+ workspaceId: 'workspace-1',
+ });
+
+ expect(getMember).toHaveBeenCalledWith('workspace-1', 'user-1');
+ expect(MarketService).toHaveBeenCalledWith({
+ userInfo: { userId: 'user-1', workspaceId: 'workspace-1' },
+ });
+ });
+
+ it('rejects workspace context without an active membership', async () => {
+ getMember.mockResolvedValue(undefined);
+
+ await expect(
+ credsRuntime.factory({
+ serverDB,
+ toolManifestMap: {},
+ userId: 'user-1',
+ workspaceId: 'workspace-1',
+ }),
+ ).rejects.toThrow('Workspace membership is required for workspace Creds execution');
+ expect(MarketService).not.toHaveBeenCalled();
+ });
+
+ it('fails closed when workspace membership cannot be queried', async () => {
+ await expect(
+ credsRuntime.factory({
+ toolManifestMap: {},
+ userId: 'user-1',
+ workspaceId: 'workspace-1',
+ }),
+ ).rejects.toThrow('serverDB is required for workspace Creds execution');
+ expect(getMember).not.toHaveBeenCalled();
+ expect(MarketService).not.toHaveBeenCalled();
+ });
+
+ it('keeps personal runtime identity outside a workspace', async () => {
+ await credsRuntime.factory({
+ toolManifestMap: {},
+ topicId: 'topic-1',
+ userId: 'user-1',
+ });
+
+ expect(getMember).not.toHaveBeenCalled();
+ expect(MarketService).toHaveBeenCalledWith({
+ userInfo: { userId: 'user-1', workspaceId: undefined },
+ });
+ });
+
+ it('rejects runtime creation without a user identity', async () => {
+ await expect(credsRuntime.factory({ toolManifestMap: {} })).rejects.toThrow(
+ 'userId is required for Creds execution',
+ );
+ });
+});
diff --git a/apps/server/src/services/toolExecution/serverRuntimes/creds.ts b/apps/server/src/services/toolExecution/serverRuntimes/creds.ts
index 0adff7b833b..3a2469a82ed 100644
--- a/apps/server/src/services/toolExecution/serverRuntimes/creds.ts
+++ b/apps/server/src/services/toolExecution/serverRuntimes/creds.ts
@@ -5,6 +5,7 @@ import {
} from '@lobechat/builtin-tool-creds';
import debug from 'debug';
+import { WorkspaceMemberModel } from '@/database/models/workspaceMember';
import { MarketService } from '@/server/services/market';
import { type ServerRuntimeRegistration } from './types';
@@ -111,10 +112,8 @@ class ServerCredsService implements ICredsService {
}> {
log('injectCreds: keys=%O, topicId=%s', params.keys, params.topicId);
- // NOTE: stays on the personal `market.creds.inject` even inside a workspace.
- // The Market SDK's org-scoped creds service has no `inject`/`injectForSkill`
- // equivalent yet (see LOBE-10978) — sandbox injection cannot be routed to the
- // workspace's organization credentials until Market adds that endpoint.
+ // Market's generic inject endpoint resolves organization credentials from
+ // the workspaceId signed into this service's trusted-client token.
const result = await this.marketService.market.creds.inject({
keys: params.keys,
sandbox: params.sandbox,
@@ -161,11 +160,25 @@ class ServerCredsService implements ICredsService {
* Per-request runtime (needs userId, topicId)
*/
export const credsRuntime: ServerRuntimeRegistration = {
- factory: (context) => {
+ factory: async (context) => {
if (!context.userId) {
throw new Error('userId is required for Creds execution');
}
+ if (context.workspaceId) {
+ if (!context.serverDB) {
+ throw new Error('serverDB is required for workspace Creds execution');
+ }
+
+ const membership = await new WorkspaceMemberModel(context.serverDB, context.userId).getMember(
+ context.workspaceId,
+ context.userId,
+ );
+ if (!membership) {
+ throw new Error('Workspace membership is required for workspace Creds execution');
+ }
+ }
+
log(
'Creating CredsExecutionRuntime for userId=%s, topicId=%s, workspaceId=%s',
context.userId,
@@ -173,7 +186,9 @@ export const credsRuntime: ServerRuntimeRegistration = {
context.workspaceId,
);
- const marketService = new MarketService({ userInfo: { userId: context.userId } });
+ const marketService = new MarketService({
+ userInfo: { userId: context.userId, workspaceId: context.workspaceId },
+ });
const credsService = new ServerCredsService(marketService, context.workspaceId);
return new CredsExecutionRuntime(credsService, {