Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions apps/sim/lib/sim-search/live/account-session.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* @vitest-environment node
*/
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { ResolvedLiveAccount } from '@/lib/sim-search/live/accounts'
import type { DocumentLink } from '@/lib/sim-search/live/links'

const { adminVerify, projectLookup } = vi.hoisted(() => ({
adminVerify: vi.fn(),
projectLookup: vi.fn(),
}))

vi.mock('@/lib/sim-search/live/provider-rollout', () => ({
isSearchProviderEnabled: vi.fn(async () => true),
}))
vi.mock('@/lib/sim-search/live/http', async (importOriginal) => ({
...(await importOriginal<typeof import('@/lib/sim-search/live/http')>()),
createNativeClient: () => ({ json: projectLookup }),
}))
vi.mock('@/lib/sim-search/live/gitlab-admin', () => ({
createAdminGitLabSession: vi.fn(async () => ({ verify: adminVerify })),
}))

import { openLiveAccountSession } from '@/lib/sim-search/live/account-session'
import { NativeSearchError } from '@/lib/sim-search/live/http'

const fileLink: DocumentLink = {
provider: 'gitlab',
host: 'gitlab.example.com',
repository: 'private-group/private-project',
blob: ['feature', 'src', 'a.ts'],
}

async function adminSession() {
return openLiveAccountSession({
owner: { organizationId: 'org-1' },
userId: 'member-1',
resolved: {
account: {
id: 'gitlab-source:s1',
provider: 'gitlab',
providerId: 'gitlab',
displayName: 'GitLab',
type: 'admin_source',
scopes: [],
},
accessToken: 'admin-token',
origin: 'https://gitlab.example.com',
adminSource: { id: 's1', config: {} },
} satisfies ResolvedLiveAccount,
policies: {},
signal: new AbortController().signal,
})
}

describe('administrator GitLab link location', () => {
beforeEach(() => {
projectLookup.mockResolvedValue({ id: 7, default_branch: 'release-2026' })
})

it('does not name the branch of a project the administrator token sees', async () => {
adminVerify.mockResolvedValue(false)
const session = await adminSession()
await expect(session.locate(fileLink)).resolves.toBeUndefined()
})

it('locates a default-branch file only once the reader may open it', async () => {
adminVerify.mockResolvedValue(false)
const session = await adminSession()
await expect(
session.locate({ ...fileLink, blob: ['release-2026', 'src', 'a.ts'] })
).resolves.toBeUndefined()

adminVerify.mockResolvedValue(true)
await expect(
session.locate({ ...fileLink, blob: ['release-2026', 'src', 'a.ts'] })
).resolves.toMatchObject({ id: 'src/a.ts', container: '7', kind: 'code' })
})

it('still reports a GitLab outage instead of calling the link unreachable', async () => {
projectLookup.mockRejectedValue(
new NativeSearchError('unavailable', 'GitLab is unavailable.', undefined, 503)
)
const session = await adminSession()
await expect(session.locate(fileLink)).rejects.toThrow('GitLab is unavailable.')
})
})
38 changes: 33 additions & 5 deletions apps/sim/lib/sim-search/live/account-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ type Reference = Pick<
'id' | 'container' | 'kind' | 'revision' | 'threadId' | 'accessMetadata' | 'connectionBinding'
>

/**
* Whether a failed link lookup is an answer about the project itself (not found, or on another
* branch) rather than an outage, rate limit, or credential failure that says nothing about it.
*/
function isProjectAnswer(error: unknown): boolean {
return (
error instanceof NativeSearchError &&
error.status === 'unavailable' &&
(error.httpStatus === undefined || error.httpStatus < 500)
)
}

/** One member account's provider clients and the source boundary that bounds its results. */
export interface LiveAccountSession {
/** The effective policy: the service source's settings in service mode, else the member's. */
Expand Down Expand Up @@ -219,6 +231,12 @@ export async function openLiveAccountSession(
throw error
})

const verify: LiveAccountSession['verify'] = async (document) => {
if (connectionBinding && document.connectionBinding !== connectionBinding) return false
if (!(await boundary.verify(document))) return false
return !admin || admin.verify(document, document.accessMetadata)
}

return {
async close() {
await mcp?.close()
Expand All @@ -242,11 +260,7 @@ export async function openLiveAccountSession(
: searchOnce(request))
return connectionBinding ? { ...page, documents: page.documents.map(bind) } : page
},
async verify(document) {
if (connectionBinding && document.connectionBinding !== connectionBinding) return false
if (!(await boundary.verify(document))) return false
return !admin || admin.verify(document, document.accessMetadata)
},
verify,
async read(reference, filters) {
if (admin) return admin.read(reference)
if (client)
Expand All @@ -263,6 +277,20 @@ export async function openLiveAccountSession(
},
async locate(link) {
if (link.provider !== provider) return undefined
if (admin) {
/**
* The administrator's token sees projects the reader may not, so a link reads as
* unreachable unless the reader may open it: no project's existence or branch leaks.
*/
const located = await locateDocumentLink(link, { client, origin }).catch(
(error: unknown) => {
signal.throwIfAborted()
if (!isProjectAnswer(error)) throw error
return undefined
}
)
return located && (await verify(located)) ? located : undefined
}
const located = await locateDocumentLink(link, { client, origin })
return located && (connectionBinding ? { ...located, connectionBinding } : located)
},
Expand Down
Loading