From a778a926107568eb491457f94b9079f8b0d2def9 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Mon, 16 Nov 2026 20:08:39 -0800 Subject: [PATCH] fix(search): validate GitLab group scope before permission refresh --- apps/docs/content/docs/search/gitlab.mdx | 4 +- .../gitlab/permission-config/capability.ts | 7 +- .../gitlab-group.integration.ts | 78 ++++++++++++++++--- 3 files changed, 77 insertions(+), 12 deletions(-) diff --git a/apps/docs/content/docs/search/gitlab.mdx b/apps/docs/content/docs/search/gitlab.mdx index d2dc87b5662..0556c5cb8c5 100644 --- a/apps/docs/content/docs/search/gitlab.mdx +++ b/apps/docs/content/docs/search/gitlab.mdx @@ -45,11 +45,11 @@ Sim checks returned content against these restrictions independently of the quer ## Add a group -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. +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. 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. -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. +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. 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. diff --git a/apps/sim/connectors/gitlab/permission-config/capability.ts b/apps/sim/connectors/gitlab/permission-config/capability.ts index 52dc1c7973a..ca84374577e 100644 --- a/apps/sim/connectors/gitlab/permission-config/capability.ts +++ b/apps/sim/connectors/gitlab/permission-config/capability.ts @@ -5,6 +5,7 @@ import { OutboundRoutingError } from '@/lib/core/network/routing' import { OrchestrationError } from '@/lib/core/orchestration/types' import type { ConnectorPermissionConfigCapability } from '@/lib/knowledge/connectors/permission-config' import { writeConnectorPermissions } from '@/lib/knowledge/connectors/permission-store' +import { gitLabGroupMatchesConfig } from '@/connectors/gitlab/group' import { prepareGitLabPermissionChange } from '@/connectors/gitlab/permission-config/prepare' import { gitLabPermissionData, @@ -47,7 +48,11 @@ export const gitLabPermissionConfig: ConnectorPermissionConfigCapability = { ) } ) - if (discovered.host !== stored.host || discovered.group.id !== stored.group.id) + if ( + discovered.host !== stored.host || + discovered.group.id !== stored.group.id || + !gitLabGroupMatchesConfig(discovered, source.sourceConfig) + ) throw new OrchestrationError( 'validation', 'The GitLab group identity changed. Update the connection before replacing permissions.' diff --git a/apps/sim/lib/knowledge/__integration__/gitlab-group.integration.ts b/apps/sim/lib/knowledge/__integration__/gitlab-group.integration.ts index 29873b741f1..e0cbc42dc4c 100644 --- a/apps/sim/lib/knowledge/__integration__/gitlab-group.integration.ts +++ b/apps/sim/lib/knowledge/__integration__/gitlab-group.integration.ts @@ -40,7 +40,10 @@ import { readSearchSourcePermissions, replaceSearchSourcePermissions, } from '@/lib/knowledge/application/source-permissions' -import { readConnectorPermissionGrants } from '@/lib/knowledge/connectors/permission-store' +import { + loadConnectorPermissionSnapshot, + readConnectorPermissionGrants, +} from '@/lib/knowledge/connectors/permission-store' import { readLiveDocument, searchLiveKnowledge } from '@/lib/sim-search/live/application' import { decodeLiveReference } from '@/lib/sim-search/live/reference' import { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry' @@ -68,7 +71,7 @@ describe('GitLab group Search permission boundary', () => { group: 'team', contentTypes: 'issues', } - const permissions = (revoke = false) => ({ + const permissions = (revoke = false, namespace = 'team') => ({ provider: 'gitlab' as const, userMapping: { filename: 'users.csv', @@ -76,7 +79,7 @@ describe('GitLab group Search permission boundary', () => { }, projectPermissions: { filename: 'permissions.csv', - content: `project_path,user_id\nteam/a,${revoke ? 2 : 1}\nteam/b,2${added ? '\nteam/c,1' : ''}`, + content: `project_path,user_id\n${namespace}/a,${revoke ? 2 : 1}\n${namespace}/b,2${added ? `\n${namespace}/c,1` : ''}`, }, }) const server = createServer((request, response) => { @@ -92,16 +95,19 @@ describe('GitLab group Search permission boundary', () => { ) return reply(401, {}) if (providerState === 'revoked') return reply(401, {}) + const namespace = providerState === 'renamed' ? 'renamed-team' : 'team' if (path === '/api/v4/user') return reply(200, { id: 50, state: 'active', locked: false }) + if (path === '/api/v4/groups/team' && providerState === 'renamed') + return reply(301, {}, { location: '/api/v4/groups/7' }) if (path === '/api/v4/groups/team' || path === '/api/v4/groups/7') - return reply(200, { id: 7, full_path: providerState === 'renamed' ? 'renamed-team' : 'team' }) + return reply(200, { id: 7, full_path: namespace }) if (path === '/api/v4/groups/7/projects') { if (url.searchParams.get('with_shared') !== 'false') return reply(400, {}) const projects = [ - { id: 11, path_with_namespace: 'team/a' }, - { id: 12, path_with_namespace: 'team/b' }, + { id: 11, path_with_namespace: `${namespace}/a` }, + { id: 12, path_with_namespace: `${namespace}/b` }, ] - if (added) projects.push({ id: 13, path_with_namespace: 'team/c' }) + if (added) projects.push({ id: 13, path_with_namespace: `${namespace}/c` }) const page = url.searchParams.get('page') ?? '1' if (page === '1') return reply(200, projects.slice(0, 1), { @@ -118,7 +124,13 @@ describe('GitLab group Search permission boundary', () => { if (!match) return reply(404, {}) const id = Number(match[1]) const projectPath = - id === 11 ? (moved ? 'outside/a' : 'team/a') : id === 12 ? 'team/b' : 'team/c' + id === 11 + ? moved + ? 'outside/a' + : `${namespace}/a` + : id === 12 + ? `${namespace}/b` + : `${namespace}/c` const issue = (iid: number) => ({ project_id: id, iid, @@ -247,7 +259,7 @@ describe('GitLab group Search permission boundary', () => { boundary: 'Application use cases, real PostgreSQL and HTTP GitLab API fixture; TLS/SSRF transport substituted', checks, - passed: checks.length === 8, + passed: checks.length === 10, }, null, 2 @@ -538,4 +550,52 @@ describe('GitLab group Search permission boundary', () => { ).rejects.toMatchObject({ code: 'forbidden' }) checks.push('Organization administration boundary') }) + + it.each(['team', '7'])( + 'refreshes renamed groups only when saved scope %s still matches', + async (group) => { + const { connector } = await createKnowledgeConnector.execute({ + principal: principal(ids.aliceId), + input: { + knowledgeBaseId: ids.knowledgeBaseId, + assertedOrganizationId: ids.organizationId, + connectorType: 'gitlab', + accessMode: 'admin', + apiKey: token, + sourceConfig: { ...config, group }, + permissionConfig: { ...permissions(), mode: 'csv' }, + syncIntervalMinutes: 60, + }, + }) + const before = await loadConnectorPermissionSnapshot(connector.id) + providerState = 'renamed' + try { + const refresh = replaceSearchSourcePermissions.execute({ + principal: principal(ids.aliceId), + input: { + organizationId: ids.organizationId, + sourceId: connector.id, + expectedRevision: 1, + permissions: permissions(false, 'renamed-team'), + }, + }) + if (group === 'team') { + await expect(refresh).rejects.toMatchObject({ code: 'validation' }) + expect(await loadConnectorPermissionSnapshot(connector.id)).toEqual(before) + providerState = 'healthy' + } else { + expect(await refresh).toEqual({ revision: 2 }) + } + const results = (await search(ids.aliceId, '11', undefined, 20, connector.id)).results + expect(results.length).toBeGreaterThan(0) + const first = results[0] + if (!first) throw new Error('Missing allowed fixture result') + expect((await read(ids.aliceId, first.documentId)).chunks.length).toBeGreaterThan(0) + expect((await search(ids.bobId, '11', undefined, 20, connector.id)).results).toEqual([]) + checks.push(`Renamed group refresh preserves permissions for configured scope ${group}`) + } finally { + providerState = 'healthy' + } + } + ) })