Skip to content

Commit c7667b3

Browse files
authored
Show restricted members only the connectors their roles reach (#1666)
1 parent 45e0fdf commit c7667b3

12 files changed

Lines changed: 1457 additions & 44 deletions

‎packages/backend/src/connectors/connectors.controller.ts‎

Lines changed: 148 additions & 21 deletions
Large diffs are not rendered by default.

‎packages/backend/src/connectors/connectors.member-visibility.integration.spec.ts‎

Lines changed: 324 additions & 0 deletions
Large diffs are not rendered by default.

‎packages/backend/src/connectors/connectors.member-visibility.spec.ts‎

Lines changed: 647 additions & 0 deletions
Large diffs are not rendered by default.

‎packages/backend/src/connectors/connectors.module.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { LicenseModule } from '../license/license.module';
3131
import { McpServersModule } from '../mcp-servers/mcp-servers.module';
3232
import { FileSearchCoreModule } from '../file-search/file-search-core.module';
3333
import { IdentityProvidersModule } from '../identity-providers/identity-providers.module';
34+
import { MemberVisibilityModule } from '../settings/member-visibility.module';
3435

3536
const ENGINES = [
3637
RestEngine,
@@ -53,7 +54,16 @@ const PARSERS = [OpenApiParser, WsdlParser, GraphqlParser, PostmanParser, CurlPa
5354
// not each log in.
5455
@Module({
5556
// IdentityProvidersModule: SsoService, for signing in on the connect page.
56-
imports: [McpServerModule, McpServersModule, LicenseModule, IdentityProvidersModule, FileSearchCoreModule],
57+
// MemberVisibilityModule (not SettingsModule): what a restricted member may
58+
// read on the connectors and tools routes.
59+
imports: [
60+
McpServerModule,
61+
McpServersModule,
62+
LicenseModule,
63+
IdentityProvidersModule,
64+
FileSearchCoreModule,
65+
MemberVisibilityModule,
66+
],
5767
controllers: [ConnectorsController, McpOAuthCallbackController, ShopeeAuthController, TikTokShopAuthController, MetaAuthController, WooCommerceAuthController, ToolsController, WebhookIngestController],
5868
providers: [
5969
ConnectorsService,

‎packages/backend/src/connectors/connectors.service.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
import { ConfigService } from '@nestjs/config';
1212
import { PrismaService } from '../common/prisma.service';
1313
import { Connector, ConnectorType, AuthType } from '../generated/prisma/client';
14+
import type { Prisma } from '../generated/prisma/client';
1415
import { RestEngine } from './engines/rest.engine';
1516
import { attachResponseMeta } from './engines/response-headers.util';
1617
import { SoapEngine, WsdlReadError } from './engines/soap.engine';
@@ -116,6 +117,28 @@ export class ConnectorsService {
116117
return rows.map((c) => ({ ...c, icon: resolveAdapterIcon(c) }));
117118
}
118119

120+
/**
121+
* `findByOrg` narrowed by an extra `where` (member visibility, see
122+
* settings/member-visibility.service.ts `connectorWhere`). The filter is
123+
* added to the workspace filter with AND, in the query, so `limit` and
124+
* `offset` page over visible connectors only. Each row keeps all its tools;
125+
* the caller filters them.
126+
*/
127+
async findVisibleByOrg(
128+
organizationId: string,
129+
visibility: Prisma.ConnectorWhereInput,
130+
opts?: { limit?: number; offset?: number },
131+
): Promise<Array<Connector & { icon: string | null }>> {
132+
const rows = await this.prisma.connector.findMany({
133+
where: { AND: [{ organizationId }, visibility] },
134+
include: { tools: true },
135+
orderBy: { createdAt: 'desc' },
136+
...(opts?.limit !== undefined ? { take: opts.limit } : {}),
137+
...(opts?.offset !== undefined ? { skip: opts.offset } : {}),
138+
});
139+
return rows.map((c) => ({ ...c, icon: resolveAdapterIcon(c) }));
140+
}
141+
119142
async findById(id: string): Promise<Connector & { icon: string | null }> {
120143
const connector = await this.prisma.connector.findUnique({
121144
where: { id },

‎packages/backend/src/connectors/tools.controller.ts‎

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ForbiddenException,
1313
BadRequestException,
1414
ConflictException,
15+
NotFoundException,
1516
} from '@nestjs/common';
1617
import { ApiTags, ApiOperation, ApiBearerAuth, ApiProperty, ApiPropertyOptional, ApiSecurity } from '@nestjs/swagger';
1718
import { JwtOrManagementKeyGuard } from '../auth/jwt-or-management-key.guard';
@@ -27,6 +28,12 @@ import {
2728
import { MAX_TOOL_NAME_LENGTH, trimToolName } from './parsers/tool-name.util';
2829
import { Transform, Type } from 'class-transformer';
2930
import { PrismaService } from '../common/prisma.service';
31+
import {
32+
MemberVisibilityService,
33+
canSeeConnector,
34+
toolWhere,
35+
type VisibilityScope,
36+
} from '../settings/member-visibility.service';
3037
import { McpServerService } from '../mcp-server/mcp-server.service';
3138
import { DynamicMcpTools } from '../mcp-server/dynamic-mcp-tools';
3239
import { ConnectorsService } from './connectors.service';
@@ -304,6 +311,16 @@ class PreviewMappingDto {
304311
sample?: unknown;
305312
}
306313

314+
const UNRESTRICTED_SCOPE: VisibilityScope = Object.freeze({ restricted: false as const });
315+
316+
/**
317+
* Member visibility (settings/member-visibility.service.ts) on the tools of a
318+
* connector: in a workspace set to `restricted`, a member who is not an admin
319+
* gets 404 (the body an unknown connector id gets) on every GET route of a
320+
* connector they cannot see, and the list holds only the tools their roles
321+
* allow unless they own the connector. Writes keep their owner-or-admin rule.
322+
* Admins and open workspaces take today's code path.
323+
*/
307324
@ApiTags('Tools')
308325
@ApiBearerAuth()
309326
@ApiSecurity('api-key')
@@ -317,8 +334,34 @@ export class ToolsController {
317334
private readonly edition: EditionService,
318335
private readonly licenseGuard: LicenseGuardService,
319336
private readonly toolExecutor: DynamicMcpTools,
337+
// Not @Optional: Nest refuses to boot without it. It is missing only in
338+
// unit specs that build the controller by hand and predate the setting.
339+
private readonly visibility?: MemberVisibilityService,
320340
) {}
321341

342+
/** The caller's member visibility; unrestricted for admins and open workspaces. */
343+
private scopeFor(req: any): Promise<VisibilityScope> {
344+
return this.visibility ? this.visibility.scopeFor(req.user) : Promise.resolve(UNRESTRICTED_SCOPE);
345+
}
346+
347+
/** A connector the scope may not see: exactly the 404 ConnectorsService.findById gives an unknown id. */
348+
private assertVisible(scope: VisibilityScope, connector: { userId: string; tools?: Array<{ id: string }> }, connectorId: string) {
349+
if (!canSeeConnector(scope, { userId: connector.userId, tools: connector.tools ?? [] })) {
350+
throw new NotFoundException(`Connector ${connectorId} not found`);
351+
}
352+
}
353+
354+
/**
355+
* For a restricted member: another workspace's connector stays the 403 it is
356+
* today, a hidden one is a 404. Nothing at all for admins and open workspaces.
357+
*/
358+
private async hideUnlessVisible(connectorId: string, req: any): Promise<void> {
359+
const scope = await this.scopeFor(req);
360+
if (!scope.restricted) return;
361+
const connector = await this.assertConnectorOrgMatch(connectorId, req);
362+
this.assertVisible(scope, connector as any, connectorId);
363+
}
364+
322365
/**
323366
* Validate and authorize a code tool at save time (shape + authority gates).
324367
* Shape: validateCodeToolMapping. Authority: `definer` is admin-only;
@@ -473,11 +516,24 @@ export class ToolsController {
473516
}
474517

475518
@Get()
476-
@ApiOperation({ summary: 'List tools for a connector' })
519+
@ApiOperation({
520+
summary: 'List tools for a connector',
521+
description:
522+
'In a workspace with restricted member visibility, a member who is not an admin gets 404 for a connector they ' +
523+
'cannot see, and only the tools their roles allow unless they own the connector.',
524+
})
477525
async list(@Req() req: any, @Param('connectorId') connectorId: string) {
478-
await this.assertConnectorOrgMatch(connectorId, req);
526+
const connector = await this.assertConnectorOrgMatch(connectorId, req);
527+
const scope = await this.scopeFor(req);
528+
if (!scope.restricted) {
529+
return this.prisma.mcpTool.findMany({
530+
where: { connectorId },
531+
orderBy: { createdAt: 'desc' },
532+
});
533+
}
534+
this.assertVisible(scope, connector as any, connectorId);
479535
return this.prisma.mcpTool.findMany({
480-
where: { connectorId },
536+
where: { AND: [{ connectorId }, toolWhere(scope, connector)] },
481537
orderBy: { createdAt: 'desc' },
482538
});
483539
}
@@ -715,13 +771,15 @@ export class ToolsController {
715771
'Returns `derived` (inferred from the connector: HTTP verb, ' +
716772
'query/mutation, readOnly flag, SQL text), `override` (the stored ' +
717773
'explicit value, if any) and `effective` (what MCP clients actually ' +
718-
'see). Also lists the supported keys.',
774+
'see). Also lists the supported keys. Connector owner or admin; in a workspace with restricted member ' +
775+
'visibility, a member who is not an admin gets 404 for a connector they cannot see.',
719776
})
720777
async getAnnotations(
721778
@Req() req: any,
722779
@Param('toolId') toolId: string,
723780
@Param('connectorId') connectorId: string,
724781
) {
782+
await this.hideUnlessVisible(connectorId, req);
725783
const connector = await this.assertCanWriteConnector(connectorId, req);
726784
const tool = await this.prisma.mcpTool.findFirst({
727785
where: { id: toolId, connectorId },
@@ -804,13 +862,15 @@ export class ToolsController {
804862
summary: 'Get the response mapping configured for a tool',
805863
description:
806864
'Returns the stored `transform` (or null), whether it is active, and ' +
807-
'whether a recent real response is available to preview it against.',
865+
'whether a recent real response is available to preview it against. Connector owner or admin; in a workspace ' +
866+
'with restricted member visibility, a member who is not an admin gets 404 for a connector they cannot see.',
808867
})
809868
async getResponseMapping(
810869
@Req() req: any,
811870
@Param('toolId') toolId: string,
812871
@Param('connectorId') connectorId: string,
813872
) {
873+
await this.hideUnlessVisible(connectorId, req);
814874
await this.assertCanWriteConnector(connectorId, req);
815875
const tool = await this.prisma.mcpTool.findFirst({
816876
where: { id: toolId, connectorId },

‎packages/backend/src/file-search/file-search.controller.ts‎

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Body, Controller, Delete, Get, Param, Post, Put, Query, Req, UseGuards
22
import { JwtOrManagementKeyGuard } from '../auth/jwt-or-management-key.guard';
33
import { ApiBearerAuth, ApiOperation, ApiTags, ApiSecurity } from '@nestjs/swagger';
44
import { FileSearchService } from './file-search.service';
5+
import { MemberVisibilityService } from '../settings/member-visibility.service';
56
import {
67
FileAccessRulesDto,
78
FileSearchActivateDto,
@@ -19,14 +20,30 @@ import {
1920
/**
2021
* File Search API (#1385): the setup wizard and the connector's Files tab.
2122
* Every route is scoped to the caller's workspace; setup routes need ADMIN.
23+
*
24+
* Member visibility (settings/member-visibility.service.ts): in a workspace
25+
* set to `restricted`, a member who is not an admin gets 404 (the body an
26+
* unknown id gets) on every route with a connector id they cannot see, before
27+
* the route's own checks. Admins and open workspaces: today's path. The
28+
* route-by-route rules are listed in connectors.member-visibility.spec.ts.
2229
*/
2330
@ApiTags('File Search')
2431
@ApiBearerAuth()
2532
@ApiSecurity('api-key')
2633
@UseGuards(JwtOrManagementKeyGuard)
2734
@Controller('api/file-search')
2835
export class FileSearchController {
29-
constructor(private readonly svc: FileSearchService) {}
36+
constructor(
37+
private readonly svc: FileSearchService,
38+
private readonly visibility: MemberVisibilityService,
39+
) {}
40+
41+
/** 404 for a connector a restricted member cannot see; nothing for admins and open workspaces. */
42+
private async hideUnlessVisible(req: any, id: string): Promise<void> {
43+
const scope = await this.visibility.scopeFor(req.user);
44+
if (!scope.restricted) return;
45+
await this.svc.assertVisible(req.user, id, scope);
46+
}
3047

3148
@Get('capabilities')
3249
@ApiOperation({ summary: 'Plan, limits, worker health, protocols and AI availability for the wizard' })
@@ -59,8 +76,12 @@ export class FileSearchController {
5976
}
6077

6178
@Get('connectors/:id')
62-
@ApiOperation({ summary: 'Get a File Search connector overview' })
63-
overview(@Req() req: any, @Param('id') id: string) {
79+
@ApiOperation({
80+
summary: 'Get a File Search connector overview',
81+
description: 'In a workspace with restricted member visibility, a member who is not an admin gets 404 for a connector they cannot see.',
82+
})
83+
async overview(@Req() req: any, @Param('id') id: string) {
84+
await this.hideUnlessVisible(req, id);
6485
return this.svc.overview(req.user, id);
6586
}
6687

@@ -77,8 +98,12 @@ export class FileSearchController {
7798
}
7899

79100
@Get('connectors/:id/scans/:scanId')
80-
@ApiOperation({ summary: 'Get the status and results of a scan' })
81-
getScan(@Req() req: any, @Param('id') id: string, @Param('scanId') scanId: string) {
101+
@ApiOperation({
102+
summary: 'Get the status and results of a scan',
103+
description: 'In a workspace with restricted member visibility, a member who is not an admin gets 404 for a connector they cannot see.',
104+
})
105+
async getScan(@Req() req: any, @Param('id') id: string, @Param('scanId') scanId: string) {
106+
await this.hideUnlessVisible(req, id);
82107
return this.svc.getScan(req.user, id, scanId);
83108
}
84109

@@ -107,26 +132,42 @@ export class FileSearchController {
107132
}
108133

109134
@Get('connectors/:id/tree')
110-
@ApiOperation({ summary: 'Get the folder tree of a connector' })
111-
tree(@Req() req: any, @Param('id') id: string) {
135+
@ApiOperation({
136+
summary: 'Get the folder tree of a connector',
137+
description: 'In a workspace with restricted member visibility, a member who is not an admin gets 404 for a connector they cannot see.',
138+
})
139+
async tree(@Req() req: any, @Param('id') id: string) {
140+
await this.hideUnlessVisible(req, id);
112141
return this.svc.tree(req.user, id);
113142
}
114143

115144
@Get('connectors/:id/problems')
116-
@ApiOperation({ summary: 'List indexing problems for a connector' })
117-
problems(@Req() req: any, @Param('id') id: string, @Query() query: PageQueryDto) {
145+
@ApiOperation({
146+
summary: 'List indexing problems for a connector',
147+
description: 'In a workspace with restricted member visibility, a member who is not an admin gets 404 for a connector they cannot see.',
148+
})
149+
async problems(@Req() req: any, @Param('id') id: string, @Query() query: PageQueryDto) {
150+
await this.hideUnlessVisible(req, id);
118151
return this.svc.problems(req.user, id, query);
119152
}
120153

121154
@Get('connectors/:id/runs')
122-
@ApiOperation({ summary: 'List sync runs for a connector' })
123-
runs(@Req() req: any, @Param('id') id: string) {
155+
@ApiOperation({
156+
summary: 'List sync runs for a connector',
157+
description: 'In a workspace with restricted member visibility, a member who is not an admin gets 404 for a connector they cannot see.',
158+
})
159+
async runs(@Req() req: any, @Param('id') id: string) {
160+
await this.hideUnlessVisible(req, id);
124161
return this.svc.runs(req.user, id);
125162
}
126163

127164
@Post('connectors/:id/sync')
128-
@ApiOperation({ summary: 'Trigger an immediate sync' })
129-
syncNow(@Req() req: any, @Param('id') id: string) {
165+
@ApiOperation({
166+
summary: 'Trigger an immediate sync',
167+
description: 'In a workspace with restricted member visibility, a member who is not an admin gets 404 for a connector they cannot see.',
168+
})
169+
async syncNow(@Req() req: any, @Param('id') id: string) {
170+
await this.hideUnlessVisible(req, id);
130171
return this.svc.syncNow(req.user, id);
131172
}
132173

@@ -155,8 +196,12 @@ export class FileSearchController {
155196
}
156197

157198
@Get('connectors/:id/access-rules')
158-
@ApiOperation({ summary: 'Get per-connector access rules' })
159-
getRules(@Req() req: any, @Param('id') id: string) {
199+
@ApiOperation({
200+
summary: 'Get per-connector access rules',
201+
description: 'In a workspace with restricted member visibility, a member who is not an admin gets 404 for a connector they cannot see.',
202+
})
203+
async getRules(@Req() req: any, @Param('id') id: string) {
204+
await this.hideUnlessVisible(req, id);
160205
return this.svc.getAccessRules(req.user, id);
161206
}
162207

0 commit comments

Comments
 (0)