Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop): fence GFS dispatch during team hops

Alfredo Lopezdb67601af18e
diff --git a/desktop-app/src/__tests__/appService.rpc.test.ts b/desktop-app/src/__tests__/appService.rpc.test.ts
index 78272c6a6..622e9131f 100644
--- a/desktop-app/src/__tests__/appService.rpc.test.ts
+++ b/desktop-app/src/__tests__/appService.rpc.test.ts
@@ -205,6 +205,51 @@ describe('AppService.invokeHostMessage', () => {
expect(service.authClient.switchTeam).toHaveBeenNthCalledWith(2, 'team-b-token', 'team-a')
})
+ it('blocks new GFS dispatches while a transient team token is installed', async () => {
+ const service = new AppService() as any
+ service.sessionToken = 'team-a-token'
+ service.me = {
+ id: 'user-1',
+ email: '[email protected]',
+ name: 'Test User',
+ picture: null,
+ teamId: 'team-a',
+ teamName: 'Team A',
+ role: 'member',
+ }
+ service.gfsScopeIdentity = {
+ ownerId: 'user-1',
+ teamId: 'team-a',
+ environmentKey: 'test-env',
+ baseUrl: 'https://external.example',
+ }
+ service.gfsDispatchBlocked = false
+ service.authClient = {
+ getMe: vi
+ .fn()
+ .mockResolvedValueOnce({ ...service.me, teamId: 'team-b', teamName: 'Team B' })
+ .mockResolvedValueOnce(service.me),
+ switchTeam: vi.fn(async (_token: string, teamId: string) => ({
+ token: `${teamId}-token`,
+ team: { id: teamId, name: teamId, role: 'member' },
+ })),
+ }
+ service.tokenStore = { setSessionToken: vi.fn() }
+
+ await expect(
+ service.runWithTeamContext('team-b', async () => {
+ await expect(
+ service.startGfsFileUpload('parent-a', 'upload.bin', '/tmp/upload.bin')
+ ).rejects.toThrow('GFS upload dispatch is unavailable during a transient team hop')
+ return 'team-b-operation'
+ })
+ ).resolves.toBe('team-b-operation')
+
+ expect(service.authClient.switchTeam).toHaveBeenNthCalledWith(1, 'team-a-token', 'team-b')
+ expect(service.authClient.switchTeam).toHaveBeenNthCalledWith(2, 'team-b-token', 'team-a')
+ expect(service.gfsTransientTeamHopDepth).toBe(0)
+ })
+
it('fences the old GFS scope if a replacement team token cannot be refreshed', async () => {
const service = new AppService() as any
service.sessionToken = 'team-a-token'
@@ -248,6 +293,51 @@ describe('AppService.invokeHostMessage', () => {
expect(service.clearAuthenticatedSessionState).toHaveBeenCalledTimes(1)
})
+ it('clears and revokes a replacement token even when GFS fence persistence fails', async () => {
+ const service = new AppService() as any
+ service.sessionToken = 'team-a-token'
+ service.me = {
+ id: 'user-1',
+ email: '[email protected]',
+ name: 'Test User',
+ picture: null,
+ teamId: 'team-a',
+ teamName: 'Team A',
+ role: 'member',
+ }
+ service.gfsScopeIdentity = {
+ ownerId: 'user-1',
+ teamId: 'team-a',
+ environmentKey: 'test-env',
+ baseUrl: 'https://external.example',
+ }
+ service.authClient = {
+ switchTeam: vi.fn().mockResolvedValue({
+ token: 'team-b-token',
+ team: { id: 'team-b', name: 'Team B', role: 'member' },
+ }),
+ getMe: vi.fn().mockRejectedValue(new Error('replacement session rejected')),
+ }
+ service.tokenStore = {
+ setSessionToken: vi.fn(),
+ clearSessionToken: vi.fn(),
+ }
+ service.rpcTokenManager = { clear: vi.fn() }
+ service.suspendDesktopGfsUploadsForAuthBoundary = vi
+ .fn()
+ .mockRejectedValue(new Error('state file is read-only'))
+ service.clearAuthenticatedSessionState = vi.fn()
+
+ await expect(service.switchSessionToTeam('team-b', 'team-a-token')).rejects.toThrow(
+ 'replacement session rejected'
+ )
+
+ expect(service.clearAuthenticatedSessionState).toHaveBeenCalledTimes(1)
+ expect(service.tokenStore.clearSessionToken).toHaveBeenCalledWith(expect.any(String), {
+ legacyEnvKeys: expect.any(Array),
+ })
+ })
+
it('fences GFS uploads only after an explicit switchTeam succeeds', async () => {
const service = new AppService() as any
service.sessionToken = 'team-a-token'
@@ -266,6 +356,7 @@ describe('AppService.invokeHostMessage', () => {
environmentKey: 'test-env',
baseUrl: 'https://external.example',
}
+ service.gfsDispatchBlocked = false
service.authClient = {
switchTeam: vi.fn().mockResolvedValue({
token: 'team-b-token',
@@ -275,7 +366,11 @@ describe('AppService.invokeHostMessage', () => {
}
service.tokenStore = { setSessionToken: vi.fn() }
service.rpcTokenManager = { clear: vi.fn() }
- service.suspendDesktopGfsUploadsForAuthBoundary = vi.fn().mockResolvedValue(undefined)
+ service.suspendDesktopGfsUploadsForAuthBoundary = vi.fn(async () => {
+ await expect(
+ service.startGfsFileUpload('parent-a', 'upload.bin', '/tmp/upload.bin')
+ ).rejects.toThrow('GFS upload dispatch is unavailable during a transient team hop')
+ })
service.activateGfsAuthScope = vi.fn()
service.stopAllStreams = vi.fn()
service.gfsUploadJobs = new Map([['upload-1', { job: { suspendForAuth: vi.fn() } }]])
diff --git a/desktop-app/src/appService.ts b/desktop-app/src/appService.ts
index 29d57184d..b7f2656be 100644
--- a/desktop-app/src/appService.ts
+++ b/desktop-app/src/appService.ts
@@ -818,6 +818,10 @@ export class AppService {
private logoutInProgress = false
private gfsAuthEpoch = 0
private gfsDispatchBlocked = true
+ // A transient runWithTeamContext hop temporarily installs another team's
+ // token in sessionToken. Existing GFS jobs keep their captured token and
+ // scope, but new GFS operations must not capture that temporary token.
+ private gfsTransientTeamHopDepth = 0
// A finite RPC may temporarily borrow another team session. Keep the
// deliberate user-owned GFS scope stable across that hop so in-flight jobs
// are not invalidated by the transient `me` value.
@@ -992,47 +996,77 @@ export class AppService {
}
private async switchSessionToTeam(teamId: string, token = this.requireSessionToken()) {
+ const releaseTransientHop = this.enterGfsTransientTeamHop()
const targetTeamId = String(teamId || '').trim()
- if (!targetTeamId) throw new Error('teamId is required')
- const switchGeneration = this.sessionGeneration
- if (this.sessionToken !== token || !this.me) {
- throw new Error('stale_auth_epoch: authenticated team scope changed before switch dispatch')
- }
- const previousIdentity =
- this.gfsScopeIdentity ?? (this.me ? desktopGfsUploadIdentity(this.me) : undefined)
- let switchedToken: string | null = null
try {
- const switched = await this.authClient.switchTeam(token, targetTeamId)
- switchedToken = switched.token
- if (this.sessionGeneration !== switchGeneration || this.sessionToken !== token) {
- throw new Error('stale_auth_epoch: authenticated team scope changed during switch')
+ if (!targetTeamId) throw new Error('teamId is required')
+ const switchGeneration = this.sessionGeneration
+ if (this.sessionToken !== token || !this.me) {
+ throw new Error('stale_auth_epoch: authenticated team scope changed before switch dispatch')
}
- await this.commitSessionToken(switched.token, { refreshMe: true })
- if (!this.me) throw new Error('Team switch ended without an authenticated session')
- return switched.token
- } catch (error) {
- if (!switchedToken && this.sessionToken === token && this.me) {
- // A failed transient hop leaves the deliberate GFS scope untouched.
- } else {
- // Once the auth service has issued a replacement token, the previous
- // authenticated scope is no longer safe to continue. Fence and
- // persist its uploads before clearing the partially switched session;
- // otherwise a failed refresh/restore could leave a job running with a
- // token whose team ownership is unknown.
- await this.suspendDesktopGfsUploadsForAuthBoundary(previousIdentity)
- this.clearAuthenticatedSessionState()
- try {
- await this.tokenStore.clearSessionToken(getActiveEnvKey(), {
- legacyEnvKeys: getActiveLegacyEnvKeys(),
- })
- } catch (clearError) {
- console.warn(
- '[AppService] Failed to clear a partially switched team session:',
- clearError
- )
+ const previousIdentity =
+ this.gfsScopeIdentity ?? (this.me ? desktopGfsUploadIdentity(this.me) : undefined)
+ let switchedToken: string | null = null
+ try {
+ const switched = await this.authClient.switchTeam(token, targetTeamId)
+ switchedToken = switched.token
+ if (this.sessionGeneration !== switchGeneration || this.sessionToken !== token) {
+ throw new Error('stale_auth_epoch: authenticated team scope changed during switch')
+ }
+ await this.commitSessionToken(switched.token, { refreshMe: true })
+ if (!this.me) throw new Error('Team switch ended without an authenticated session')
+ return switched.token
+ } catch (error) {
+ if (!switchedToken && this.sessionToken === token && this.me) {
+ // A failed transient hop leaves the deliberate GFS scope untouched.
+ } else {
+ // Once the auth service has issued a replacement token, the previous
+ // authenticated scope is no longer safe to continue. Fence and
+ // persist its uploads before clearing the partially switched session;
+ // otherwise a failed refresh/restore could leave a job running with a
+ // token whose team ownership is unknown. The fence is best effort:
+ // clearing the in-memory scope and revoking the persisted token must
+ // still happen when its state write is unavailable.
+ try {
+ await this.suspendDesktopGfsUploadsForAuthBoundary(previousIdentity)
+ } catch (fenceError) {
+ console.warn(
+ '[AppService] Failed to persist the GFS authentication fence during team switch:',
+ fenceError
+ )
+ }
+ this.clearAuthenticatedSessionState()
+ try {
+ await this.tokenStore.clearSessionToken(getActiveEnvKey(), {
+ legacyEnvKeys: getActiveLegacyEnvKeys(),
+ })
+ } catch (clearError) {
+ console.warn(
+ '[AppService] Failed to clear a partially switched team session:',
+ clearError
+ )
+ }
}
+ throw error
}
- throw error
+ } finally {
+ releaseTransientHop()
+ }
+ }
+
+ private enterGfsTransientTeamHop(): () => void {
+ this.gfsTransientTeamHopDepth += 1
+ let released = false
+ return () => {
+ if (released) return
+ released = true
+ this.gfsTransientTeamHopDepth = Math.max(0, this.gfsTransientTeamHopDepth - 1)
+ }
+ }
+
+ private assertGfsDispatchAllowed(): void {
+ if (this.gfsTransientTeamHopDepth > 0) {
+ throw new Error('GFS upload dispatch is unavailable during a transient team hop')
}
}
@@ -1066,28 +1100,33 @@ export class AppService {
let activeToken = originalToken
const shouldSwitch = originalTeamId !== targetTeamId
const shouldRestore = Boolean(originalTeamId && shouldSwitch)
-
- if (shouldSwitch) {
- activeToken = await this.switchSessionToTeam(targetTeamId, originalToken)
- }
+ const releaseTransientHop = shouldSwitch ? this.enterGfsTransientTeamHop() : undefined
try {
- return await operation(activeToken)
- } catch (error) {
- operationError = error
- throw error
- } finally {
- if (shouldRestore) {
- try {
- await this.switchSessionToTeam(originalTeamId, this.requireSessionToken())
- } catch (restoreError) {
- if (!operationError) throw restoreError
- console.warn(
- '[AppService] Failed to restore team context after operation:',
- restoreError
- )
+ if (shouldSwitch) {
+ activeToken = await this.switchSessionToTeam(targetTeamId, originalToken)
+ }
+
+ try {
+ return await operation(activeToken)
+ } catch (error) {
+ operationError = error
+ throw error
+ } finally {
+ if (shouldRestore) {
+ try {
+ await this.switchSessionToTeam(originalTeamId, this.requireSessionToken())
+ } catch (restoreError) {
+ if (!operationError) throw restoreError
+ console.warn(
+ '[AppService] Failed to restore team context after operation:',
+ restoreError
+ )
+ }
}
}
+ } finally {
+ releaseTransientHop?.()
}
} finally {
releaseQueue()
@@ -1176,10 +1215,14 @@ export class AppService {
this.gfsScopeIdentity = identityOverride ?? (this.me ? desktopGfsUploadIdentity(this.me) : null)
}
- private currentDesktopGfsUploadScope(driveValue: string | undefined): DesktopGfsUploadScope {
+ private currentDesktopGfsUploadScope(
+ driveValue: string | undefined,
+ options: { allowTransientTeamHop?: boolean } = {}
+ ): DesktopGfsUploadScope {
if (this.gfsDispatchBlocked || !this.sessionToken || !this.me) {
throw new Error('GFS upload dispatch is unavailable without an active authenticated scope')
}
+ if (!options.allowTransientTeamHop) this.assertGfsDispatchAllowed()
const identity = this.gfsScopeIdentity
if (!identity) {
throw new Error('GFS upload dispatch is unavailable before scope activation')
@@ -1192,7 +1235,9 @@ export class AppService {
}
private assertCurrentDesktopGfsUploadScope(scope: DesktopGfsUploadScope): void {
- const current = this.currentDesktopGfsUploadScope(scope.drive)
+ // Existing jobs retain their original token and scope while a finite RPC
+ // borrows another team session. Only new user-facing dispatches are gated.
+ const current = this.currentDesktopGfsUploadScope(scope.drive, { allowTransientTeamHop: true })
if (!sameDesktopGfsUploadScope(scope, current)) {
throw new Error('stale_auth_epoch: GFS upload no longer belongs to the active scope')
}
@@ -2776,29 +2821,38 @@ export class AppService {
if (!targetTeamId) throw new Error('teamId is required')
const previousIdentity =
this.gfsScopeIdentity ?? (this.me ? desktopGfsUploadIdentity(this.me) : undefined)
- // §4.5-3 (GAP-N4 main half): a deliberate team switch tears down every live
- // stream — the old team's progress/activity/status/notification sockets must
- // not survive it. Done AFTER the switch SUCCEEDS (silently), mirroring the
- // renderer, which only calls `resetChat()`/`releaseAll()` once the `team.switch`
- // IPC resolves (so a FAILED switch leaves both the streams and the trackers
- // intact rather than a frozen half-torn-down state). Tasks stay alive
- // server-side and reconverge via reconcile when the user returns to that team.
- // NOTE: only the user-initiated `switchTeam` closes streams — the transient
- // per-operation team hops in `runWithTeamContext`/`switchSessionToTeam` (e.g.
- // minting a cross-team RPC token to decide an approval) must NOT.
- await this.switchSessionToTeam(targetTeamId)
- if (!this.me) throw new Error('Team switch ended without an authenticated session')
- // A deliberate user switch is the only team-context boundary that fences
- // in-flight uploads. Transient runWithTeamContext hops intentionally use
- // switchSessionToTeam directly and must leave those jobs untouched.
- await this.suspendDesktopGfsUploadsForAuthBoundary(previousIdentity)
- this.activateGfsAuthScope()
- this.stopAllStreams()
- // Grants are keyed by userId, not by team, so they carry over — but every
- // cached org/agents/contexts answer is now about the wrong team. Drop the
- // cache and tell mounted plugins to refetch (spec §6.2).
- tryGetPluginSdkRuntime()?.notifySessionChanged(true)
- return { authenticated: true, me: this.me }
+ // Keep the new team token fenced until the old GFS jobs have been
+ // suspended. `switchSessionToTeam` has its own nested gate, but releases
+ // it when the token exchange returns; this outer gate closes that small
+ // interval before the auth-boundary persistence completes.
+ const releaseTransientHop = this.enterGfsTransientTeamHop()
+ try {
+ // §4.5-3 (GAP-N4 main half): a deliberate team switch tears down every live
+ // stream — the old team's progress/activity/status/notification sockets must
+ // not survive it. Done AFTER the switch SUCCEEDS (silently), mirroring the
+ // renderer, which only calls `resetChat()`/`releaseAll()` once the `team.switch`
+ // IPC resolves (so a FAILED switch leaves both the streams and the trackers
+ // intact rather than a frozen half-torn-down state). Tasks stay alive
+ // server-side and reconverge via reconcile when the user returns to that team.
+ // NOTE: only the user-initiated `switchTeam` closes streams — the transient
+ // per-operation team hops in `runWithTeamContext`/`switchSessionToTeam` (e.g.
+ // minting a cross-team RPC token to decide an approval) must NOT.
+ await this.switchSessionToTeam(targetTeamId)
+ if (!this.me) throw new Error('Team switch ended without an authenticated session')
+ // A deliberate user switch is the only team-context boundary that fences
+ // in-flight uploads. Transient runWithTeamContext hops intentionally use
+ // switchSessionToTeam directly and must leave those jobs untouched.
+ await this.suspendDesktopGfsUploadsForAuthBoundary(previousIdentity)
+ this.activateGfsAuthScope()
+ this.stopAllStreams()
+ // Grants are keyed by userId, not by team, so they carry over — but every
+ // cached org/agents/contexts answer is now about the wrong team. Drop the
+ // cache and tell mounted plugins to refetch (spec §6.2).
+ tryGetPluginSdkRuntime()?.notifySessionChanged(true)
+ return { authenticated: true, me: this.me }
+ } finally {
+ releaseTransientHop()
+ }
}
async refreshAccessCatalog(): Promise<AccessCatalog> {