Skip to content

Commit 1b9fdd9

Browse files
authored
fix(search): validate GitLab group scope before permission refresh (#10117)
1 parent 4a30494 commit 1b9fdd9

3 files changed

Lines changed: 77 additions & 12 deletions

File tree

‎apps/docs/content/docs/search/gitlab.mdx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ Sim checks returned content against these restrictions independently of the quer
4545

4646
## Add a group
4747

48-
Choose **Group** in the source's **Scope** field and enter the numeric group ID or full group path. Enable **Include subgroups** only if descendant projects should be included. Sim discovers up to 1,000 projects visible to the source token. Projects shared from outside the group are excluded.
48+
Choose **Group** in the source's **Scope** field and enter the numeric group ID or full group path. Enable **Include subgroups** only if descendant projects should be included. Sim supports groups with at most 1,000 projects visible to the source token; larger groups need narrower sources. Projects shared from outside the group are excluded.
4949

5050
The same permission strategies apply. Group membership alone never grants access to every project: administrator mode checks each project's current permissions, and non-admin mode requires a CSV grant for each project and reader. The source token must be able to read every project you want to search.
5151

52-
In CSV mode, each successful upload rediscovers the group's projects and replaces all grants atomically. Newly discovered projects become searchable only for users explicitly listed in the uploaded CSV. Updating and saving source settings also refreshes discovery. A failed discovery or stale revision preserves the previous configuration and grants.
52+
In CSV mode, each successful upload rediscovers the group's projects and replaces all grants atomically. Newly discovered projects become searchable only for users explicitly listed in the uploaded CSV. Updating and saving source settings also refreshes discovery. A failed discovery or stale revision preserves the previous configuration and grants. If a group configured by path is renamed, save the new path and an updated project-permissions CSV together in source settings. Numeric group IDs remain valid across renames.
5353

5454
Search visits project pages within a bounded request budget. Continuation cursors cover remaining projects and results; targeting a project narrows the request. Renamed or moved projects are blocked until a successful refresh; projects outside the configured group remain excluded. Group setup uses the existing project search APIs, so it does not require group-wide advanced search.
5555

‎apps/sim/connectors/gitlab/permission-config/capability.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { OutboundRoutingError } from '@/lib/core/network/routing'
55
import { OrchestrationError } from '@/lib/core/orchestration/types'
66
import type { ConnectorPermissionConfigCapability } from '@/lib/knowledge/connectors/permission-config'
77
import { writeConnectorPermissions } from '@/lib/knowledge/connectors/permission-store'
8+
import { gitLabGroupMatchesConfig } from '@/connectors/gitlab/group'
89
import { prepareGitLabPermissionChange } from '@/connectors/gitlab/permission-config/prepare'
910
import {
1011
gitLabPermissionData,
@@ -47,7 +48,11 @@ export const gitLabPermissionConfig: ConnectorPermissionConfigCapability = {
4748
)
4849
}
4950
)
50-
if (discovered.host !== stored.host || discovered.group.id !== stored.group.id)
51+
if (
52+
discovered.host !== stored.host ||
53+
discovered.group.id !== stored.group.id ||
54+
!gitLabGroupMatchesConfig(discovered, source.sourceConfig)
55+
)
5156
throw new OrchestrationError(
5257
'validation',
5358
'The GitLab group identity changed. Update the connection before replacing permissions.'

‎apps/sim/lib/knowledge/__integration__/gitlab-group.integration.ts‎

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ import {
4040
readSearchSourcePermissions,
4141
replaceSearchSourcePermissions,
4242
} from '@/lib/knowledge/application/source-permissions'
43-
import { readConnectorPermissionGrants } from '@/lib/knowledge/connectors/permission-store'
43+
import {
44+
loadConnectorPermissionSnapshot,
45+
readConnectorPermissionGrants,
46+
} from '@/lib/knowledge/connectors/permission-store'
4447
import { readLiveDocument, searchLiveKnowledge } from '@/lib/sim-search/live/application'
4548
import { decodeLiveReference } from '@/lib/sim-search/live/reference'
4649
import { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry'
@@ -68,15 +71,15 @@ describe('GitLab group Search permission boundary', () => {
6871
group: 'team',
6972
contentTypes: 'issues',
7073
}
71-
const permissions = (revoke = false) => ({
74+
const permissions = (revoke = false, namespace = 'team') => ({
7275
provider: 'gitlab' as const,
7376
userMapping: {
7477
filename: 'users.csv',
7578
content: `user_id,email\n1,${ids.aliceId}@fixture.test\n2,${ids.bobId}@fixture.test`,
7679
},
7780
projectPermissions: {
7881
filename: 'permissions.csv',
79-
content: `project_path,user_id\nteam/a,${revoke ? 2 : 1}\nteam/b,2${added ? '\nteam/c,1' : ''}`,
82+
content: `project_path,user_id\n${namespace}/a,${revoke ? 2 : 1}\n${namespace}/b,2${added ? `\n${namespace}/c,1` : ''}`,
8083
},
8184
})
8285
const server = createServer((request, response) => {
@@ -92,16 +95,19 @@ describe('GitLab group Search permission boundary', () => {
9295
)
9396
return reply(401, {})
9497
if (providerState === 'revoked') return reply(401, {})
98+
const namespace = providerState === 'renamed' ? 'renamed-team' : 'team'
9599
if (path === '/api/v4/user') return reply(200, { id: 50, state: 'active', locked: false })
100+
if (path === '/api/v4/groups/team' && providerState === 'renamed')
101+
return reply(301, {}, { location: '/api/v4/groups/7' })
96102
if (path === '/api/v4/groups/team' || path === '/api/v4/groups/7')
97-
return reply(200, { id: 7, full_path: providerState === 'renamed' ? 'renamed-team' : 'team' })
103+
return reply(200, { id: 7, full_path: namespace })
98104
if (path === '/api/v4/groups/7/projects') {
99105
if (url.searchParams.get('with_shared') !== 'false') return reply(400, {})
100106
const projects = [
101-
{ id: 11, path_with_namespace: 'team/a' },
102-
{ id: 12, path_with_namespace: 'team/b' },
107+
{ id: 11, path_with_namespace: `${namespace}/a` },
108+
{ id: 12, path_with_namespace: `${namespace}/b` },
103109
]
104-
if (added) projects.push({ id: 13, path_with_namespace: 'team/c' })
110+
if (added) projects.push({ id: 13, path_with_namespace: `${namespace}/c` })
105111
const page = url.searchParams.get('page') ?? '1'
106112
if (page === '1')
107113
return reply(200, projects.slice(0, 1), {
@@ -118,7 +124,13 @@ describe('GitLab group Search permission boundary', () => {
118124
if (!match) return reply(404, {})
119125
const id = Number(match[1])
120126
const projectPath =
121-
id === 11 ? (moved ? 'outside/a' : 'team/a') : id === 12 ? 'team/b' : 'team/c'
127+
id === 11
128+
? moved
129+
? 'outside/a'
130+
: `${namespace}/a`
131+
: id === 12
132+
? `${namespace}/b`
133+
: `${namespace}/c`
122134
const issue = (iid: number) => ({
123135
project_id: id,
124136
iid,
@@ -247,7 +259,7 @@ describe('GitLab group Search permission boundary', () => {
247259
boundary:
248260
'Application use cases, real PostgreSQL and HTTP GitLab API fixture; TLS/SSRF transport substituted',
249261
checks,
250-
passed: checks.length === 8,
262+
passed: checks.length === 10,
251263
},
252264
null,
253265
2
@@ -538,4 +550,52 @@ describe('GitLab group Search permission boundary', () => {
538550
).rejects.toMatchObject({ code: 'forbidden' })
539551
checks.push('Organization administration boundary')
540552
})
553+
554+
it.each(['team', '7'])(
555+
'refreshes renamed groups only when saved scope %s still matches',
556+
async (group) => {
557+
const { connector } = await createKnowledgeConnector.execute({
558+
principal: principal(ids.aliceId),
559+
input: {
560+
knowledgeBaseId: ids.knowledgeBaseId,
561+
assertedOrganizationId: ids.organizationId,
562+
connectorType: 'gitlab',
563+
accessMode: 'admin',
564+
apiKey: token,
565+
sourceConfig: { ...config, group },
566+
permissionConfig: { ...permissions(), mode: 'csv' },
567+
syncIntervalMinutes: 60,
568+
},
569+
})
570+
const before = await loadConnectorPermissionSnapshot(connector.id)
571+
providerState = 'renamed'
572+
try {
573+
const refresh = replaceSearchSourcePermissions.execute({
574+
principal: principal(ids.aliceId),
575+
input: {
576+
organizationId: ids.organizationId,
577+
sourceId: connector.id,
578+
expectedRevision: 1,
579+
permissions: permissions(false, 'renamed-team'),
580+
},
581+
})
582+
if (group === 'team') {
583+
await expect(refresh).rejects.toMatchObject({ code: 'validation' })
584+
expect(await loadConnectorPermissionSnapshot(connector.id)).toEqual(before)
585+
providerState = 'healthy'
586+
} else {
587+
expect(await refresh).toEqual({ revision: 2 })
588+
}
589+
const results = (await search(ids.aliceId, '11', undefined, 20, connector.id)).results
590+
expect(results.length).toBeGreaterThan(0)
591+
const first = results[0]
592+
if (!first) throw new Error('Missing allowed fixture result')
593+
expect((await read(ids.aliceId, first.documentId)).chunks.length).toBeGreaterThan(0)
594+
expect((await search(ids.bobId, '11', undefined, 20, connector.id)).results).toEqual([])
595+
checks.push(`Renamed group refresh preserves permissions for configured scope ${group}`)
596+
} finally {
597+
providerState = 'healthy'
598+
}
599+
}
600+
)
541601
})

0 commit comments

Comments
 (0)