Skip to content

Commit 39b871b

Browse files
authored
fix(search-mcp): admin GitLab links locate only files the reader may open (#10445)
A pasted GitLab file link was located with an administrator source's token before the reader's access was checked, so the reply revealed whether a private project existed and named its default branch. An administrator session now locates a link only when the reader may open it; outages, rate limits and credential failures are still reported.
1 parent ee7fa91 commit 39b871b

2 files changed

Lines changed: 120 additions & 5 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @vitest-environment node
3+
*/
4+
import { beforeEach, describe, expect, it, vi } from 'vitest'
5+
import type { ResolvedLiveAccount } from '@/lib/sim-search/live/accounts'
6+
import type { DocumentLink } from '@/lib/sim-search/live/links'
7+
8+
const { adminVerify, projectLookup } = vi.hoisted(() => ({
9+
adminVerify: vi.fn(),
10+
projectLookup: vi.fn(),
11+
}))
12+
13+
vi.mock('@/lib/sim-search/live/provider-rollout', () => ({
14+
isSearchProviderEnabled: vi.fn(async () => true),
15+
}))
16+
vi.mock('@/lib/sim-search/live/http', async (importOriginal) => ({
17+
...(await importOriginal<typeof import('@/lib/sim-search/live/http')>()),
18+
createNativeClient: () => ({ json: projectLookup }),
19+
}))
20+
vi.mock('@/lib/sim-search/live/gitlab-admin', () => ({
21+
createAdminGitLabSession: vi.fn(async () => ({ verify: adminVerify })),
22+
}))
23+
24+
import { openLiveAccountSession } from '@/lib/sim-search/live/account-session'
25+
import { NativeSearchError } from '@/lib/sim-search/live/http'
26+
27+
const fileLink: DocumentLink = {
28+
provider: 'gitlab',
29+
host: 'gitlab.example.com',
30+
repository: 'private-group/private-project',
31+
blob: ['feature', 'src', 'a.ts'],
32+
}
33+
34+
async function adminSession() {
35+
return openLiveAccountSession({
36+
owner: { organizationId: 'org-1' },
37+
userId: 'member-1',
38+
resolved: {
39+
account: {
40+
id: 'gitlab-source:s1',
41+
provider: 'gitlab',
42+
providerId: 'gitlab',
43+
displayName: 'GitLab',
44+
type: 'admin_source',
45+
scopes: [],
46+
},
47+
accessToken: 'admin-token',
48+
origin: 'https://gitlab.example.com',
49+
adminSource: { id: 's1', config: {} },
50+
} satisfies ResolvedLiveAccount,
51+
policies: {},
52+
signal: new AbortController().signal,
53+
})
54+
}
55+
56+
describe('administrator GitLab link location', () => {
57+
beforeEach(() => {
58+
projectLookup.mockResolvedValue({ id: 7, default_branch: 'release-2026' })
59+
})
60+
61+
it('does not name the branch of a project the administrator token sees', async () => {
62+
adminVerify.mockResolvedValue(false)
63+
const session = await adminSession()
64+
await expect(session.locate(fileLink)).resolves.toBeUndefined()
65+
})
66+
67+
it('locates a default-branch file only once the reader may open it', async () => {
68+
adminVerify.mockResolvedValue(false)
69+
const session = await adminSession()
70+
await expect(
71+
session.locate({ ...fileLink, blob: ['release-2026', 'src', 'a.ts'] })
72+
).resolves.toBeUndefined()
73+
74+
adminVerify.mockResolvedValue(true)
75+
await expect(
76+
session.locate({ ...fileLink, blob: ['release-2026', 'src', 'a.ts'] })
77+
).resolves.toMatchObject({ id: 'src/a.ts', container: '7', kind: 'code' })
78+
})
79+
80+
it('still reports a GitLab outage instead of calling the link unreachable', async () => {
81+
projectLookup.mockRejectedValue(
82+
new NativeSearchError('unavailable', 'GitLab is unavailable.', undefined, 503)
83+
)
84+
const session = await adminSession()
85+
await expect(session.locate(fileLink)).rejects.toThrow('GitLab is unavailable.')
86+
})
87+
})

‎apps/sim/lib/sim-search/live/account-session.ts‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ type Reference = Pick<
4242
'id' | 'container' | 'kind' | 'revision' | 'threadId' | 'accessMetadata' | 'connectionBinding'
4343
>
4444

45+
/**
46+
* Whether a failed link lookup is an answer about the project itself (not found, or on another
47+
* branch) rather than an outage, rate limit, or credential failure that says nothing about it.
48+
*/
49+
function isProjectAnswer(error: unknown): boolean {
50+
return (
51+
error instanceof NativeSearchError &&
52+
error.status === 'unavailable' &&
53+
(error.httpStatus === undefined || error.httpStatus < 500)
54+
)
55+
}
56+
4557
/** One member account's provider clients and the source boundary that bounds its results. */
4658
export interface LiveAccountSession {
4759
/** The effective policy: the service source's settings in service mode, else the member's. */
@@ -219,6 +231,12 @@ export async function openLiveAccountSession(
219231
throw error
220232
})
221233

234+
const verify: LiveAccountSession['verify'] = async (document) => {
235+
if (connectionBinding && document.connectionBinding !== connectionBinding) return false
236+
if (!(await boundary.verify(document))) return false
237+
return !admin || admin.verify(document, document.accessMetadata)
238+
}
239+
222240
return {
223241
async close() {
224242
await mcp?.close()
@@ -242,11 +260,7 @@ export async function openLiveAccountSession(
242260
: searchOnce(request))
243261
return connectionBinding ? { ...page, documents: page.documents.map(bind) } : page
244262
},
245-
async verify(document) {
246-
if (connectionBinding && document.connectionBinding !== connectionBinding) return false
247-
if (!(await boundary.verify(document))) return false
248-
return !admin || admin.verify(document, document.accessMetadata)
249-
},
263+
verify,
250264
async read(reference, filters) {
251265
if (admin) return admin.read(reference)
252266
if (client)
@@ -263,6 +277,20 @@ export async function openLiveAccountSession(
263277
},
264278
async locate(link) {
265279
if (link.provider !== provider) return undefined
280+
if (admin) {
281+
/**
282+
* The administrator's token sees projects the reader may not, so a link reads as
283+
* unreachable unless the reader may open it: no project's existence or branch leaks.
284+
*/
285+
const located = await locateDocumentLink(link, { client, origin }).catch(
286+
(error: unknown) => {
287+
signal.throwIfAborted()
288+
if (!isProjectAnswer(error)) throw error
289+
return undefined
290+
}
291+
)
292+
return located && (await verify(located)) ? located : undefined
293+
}
266294
const located = await locateDocumentLink(link, { client, origin })
267295
return located && (connectionBinding ? { ...located, connectionBinding } : located)
268296
},

0 commit comments

Comments
 (0)