Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop-app): r1-l3 remove the mount cookie when the session changes during its write
Diego Zacariasde4e18652109
diff --git a/desktop-app/src/__tests__/sandboxUiDriverLifecycle.test.ts b/desktop-app/src/__tests__/sandboxUiDriverLifecycle.test.tsindex e2a69aa1c..59932aea6 100644--- a/desktop-app/src/__tests__/sandboxUiDriverLifecycle.test.ts+++ b/desktop-app/src/__tests__/sandboxUiDriverLifecycle.test.ts@@ -70,9 +70,17 @@ const electronMocks = vi.hoisted(() => {}}+ // Partition cookie jar keyed by `${url}|${name}`, the identity Electron's+ // `cookies.remove(url, name)` matches on.+ const cookieJar = new Map<string, string>()const sessionObject = {cookies: {- set: vi.fn(async (_details?: { value: string }): Promise<void> => undefined),+ set: vi.fn(+ async (_details?: { url: string; name: string; value: string }): Promise<void> => undefined+ ),+ remove: vi.fn(async (url: string, name: string): Promise<void> => {+ cookieJar.delete(`${url}|${name}`)+ }),},setPermissionRequestHandler: vi.fn(),setPermissionCheckHandler: vi.fn(),@@ -87,6 +95,7 @@ const electronMocks = vi.hoisted(() => {getDisplayMatching: vi.fn(() => ({ scaleFactor: 1 })),touchSandboxUiPartition: vi.fn(async () => undefined),sessionObject,+ cookieJar,views,}})@@ -167,9 +176,25 @@ function mountArgs(overrides: MountSandboxUiViewOverrides): MountSandboxUiViewArbeforeEach(() => {vi.clearAllMocks()electronMocks.views.length = 0+ electronMocks.cookieJar.clear()electronMocks.sessionObject.cookies.set.mockResolvedValue(undefined)})+/** Holds every cookie write pending; `release()` lands them in the jar. */+function holdCookieWrites(): { release: () => void } {+ const pending: Array<() => void> = []+ electronMocks.sessionObject.cookies.set.mockImplementation(+ details =>+ new Promise<void>(resolve => {+ pending.push(() => {+ if (details) electronMocks.cookieJar.set(`${details.url}|${details.name}`, details.value)+ resolve()+ })+ })+ )+ return { release: () => pending.splice(0).forEach(land => land()) }+}+afterEach(async () => {await unmountSandboxUiView()})@@ -420,6 +445,55 @@ describe('mountSandboxUiView lifecycle cleanup', () => {expect(parentWindow.listenerCount('closed')).toBe(0)})+ it('removes the cookie it wrote when the session changes during the write', async () => {+ const parentWindow = new FakeParentWindow()+ const cookieWrites = holdCookieWrites()+ let sessionCurrent = true++ const mount = mountSandboxUiView(mountArgs({ parentWindow, isCurrent: () => sessionCurrent }))+ await vi.waitFor(() => {+ expect(electronMocks.sessionObject.cookies.set).toHaveBeenCalledOnce()+ })++ sessionCurrent = false+ cookieWrites.release()+ await mount++ expect([...electronMocks.cookieJar.keys()]).toEqual([])+ expect(electronMocks.views).toHaveLength(0)+ expect(parentWindow.contentView.addChildView).not.toHaveBeenCalled()+ })++ it('leaves the cookie alone when a newer mount superseded the write', async () => {+ const parentWindow = new FakeParentWindow()+ const cookieWrites = holdCookieWrites()+ let sessionCurrent = true++ const mount = mountSandboxUiView(mountArgs({ parentWindow, isCurrent: () => sessionCurrent }))+ await vi.waitFor(() => {+ expect(electronMocks.sessionObject.cookies.set).toHaveBeenCalledOnce()+ })++ const newerMount = mountSandboxUiView(+ mountArgs({+ parentWindow,+ setCookie:+ 'clerum_sandbox_ui_session=tok-newer;' ++ ' Path=/api/v1/sandbox-ui/sandbox-recipes/task-board/; HttpOnly',+ })+ )+ await vi.waitFor(() => {+ expect(electronMocks.sessionObject.cookies.set).toHaveBeenCalledTimes(2)+ })+ sessionCurrent = false+ cookieWrites.release()+ await Promise.all([mount, newerMount])++ expect(electronMocks.sessionObject.cookies.remove).not.toHaveBeenCalled()+ expect([...electronMocks.cookieJar.values()]).toEqual(['tok-newer'])+ expect(electronMocks.views).toHaveLength(1)+ })+it('encodes the canonical client route before handing it to the mounted app', async () => {const parentWindow = new FakeParentWindow()diff --git a/desktop-app/src/sandboxUiDriver.ts b/desktop-app/src/sandboxUiDriver.tsindex 64793b71d..4194bf7d5 100644--- a/desktop-app/src/sandboxUiDriver.ts+++ b/desktop-app/src/sandboxUiDriver.ts@@ -296,6 +296,25 @@ async function writeSandboxUiCookie(args: {})}+/**+ * Undo `writeSandboxUiCookie`: removes the cookie it would set for the same+ * arguments. Removal matches by name and by the URL the cookie is sent to, so+ * it also clears an older value left at that Path by a previous mount.+ */+async function removeSandboxUiCookie(args: {+ rpcProxyOrigin: string+ recipeNs: string+ recipeName: string+ setCookie: string | string[]+ partition: string+}): Promise<void> {+ const { rpcProxyOrigin, recipeNs, recipeName, setCookie, partition } = args+ const cookiePath = extractSandboxUiPath(setCookie, recipeNs, recipeName)+ await session+ .fromPartition(partition)+ .cookies.remove(`${rpcProxyOrigin}${cookiePath}`, SANDBOX_UI_COOKIE_NAME)+}+/*** Refresh the active partition's session cookie. No-op when nothing is* mounted (refresh can race with teardown). Throws on a missing cookie@@ -396,14 +415,19 @@ export async function mountSandboxUiView(args: MountSandboxUiArgs): Promise<void// navigation request carries it. The proxy enforces the recipe binding// claim on every request — installing on a stale partition would still// fail closed at rpc-proxy.- await writeSandboxUiCookie({- rpcProxyOrigin: proxyOriginUrl,- recipeNs,- recipeName,- setCookie,- partition,- })- if (generation !== mountGeneration || !isCurrent()) return+ const cookieArgs = { rpcProxyOrigin: proxyOriginUrl, recipeNs, recipeName, setCookie, partition }+ await writeSandboxUiCookie(cookieArgs)+ if (generation !== mountGeneration) return+ if (!isCurrent()) {+ // The partition is keyed per recipe, not per user, and a session change+ // does not clear it: the cookie just written would outlive the session+ // that minted it. Only when this mount is still the newest one — a newer+ // mount may already have written its own cookie at the same Path.+ await removeSandboxUiCookie(cookieArgs).catch(err => {+ console.warn('[SandboxUI] could not remove a stale session cookie:', err)+ })+ return+ }if (parentWindow.isDestroyed()) {throw new Error('parent window is destroyed')}