Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): bound folder-zip transfers producer-side by remaining budget (R1-H2)
M60d896fa8914
diff --git a/desktop-app/src/__tests__/ipc.gfsDownloadLimit.test.ts b/desktop-app/src/__tests__/ipc.gfsDownloadLimit.test.tsnew file mode 100644index 000000000..ceaee48d3--- /dev/null+++ b/desktop-app/src/__tests__/ipc.gfsDownloadLimit.test.ts@@ -0,0 +1,83 @@+/**+ * R1-H2 — an optionally bounded 'gfs:download' is validated in main. The+ * renderer (folder-zip walk) may request a per-transfer bound up to+ * GFS_DOWNLOAD_MAX_BYTES_CEILING; a larger or invalid bound is rejected at the+ * IPC boundary before it reaches the service, and an absent bound keeps the+ * save-to-disk path uncapped.+ */+import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'+import type { AppService } from '../appService.js'+import { GFS_DOWNLOAD_MAX_BYTES_CEILING } from '../gfs/downloadLimits.js'++type Handler = (event: unknown, payload: unknown) => Promise<unknown>++const electron = vi.hoisted(() => ({ handlers: new Map<string, Handler>() }))++vi.mock('electron', () => ({+ BrowserWindow: { fromWebContents: vi.fn(), getAllWindows: vi.fn(() => []) },+ Notification: vi.fn(),+ app: { on: vi.fn(), getPath: vi.fn(), getVersion: vi.fn() },+ clipboard: { writeText: vi.fn() },+ dialog: { showSaveDialog: vi.fn() },+ ipcMain: {+ handle: vi.fn((channel: string, handler: Handler) => {+ electron.handlers.set(channel, handler)+ }),+ on: vi.fn(),+ removeHandler: vi.fn(),+ },+}))+// ipc.ts only uses AppService as a type at registration; the real module would+// pull in the whole main-process graph.+vi.mock('../appService.js', () => ({ AppService: class {} }))++const service = {+ downloadGfsUri: vi.fn(async () => new ArrayBuffer(0)),+}+const trusted = { senderFrame: { url: 'file:///app/index.html' } }+const uri = 'gfs://drive/resource'++function handler(channel: string): Handler {+ const registered = electron.handlers.get(channel)+ if (!registered) throw new Error(`no handler registered for ${channel}`)+ return registered+}++describe('gfs:download maxBytes boundary (R1-H2)', () => {+ beforeAll(async () => {+ vi.mocked(await import('../appService.js'))+ const { registerIpcHandlers } = await import('../ipc.js')+ registerIpcHandlers(service as unknown as AppService)+ })++ afterEach(() => {+ vi.clearAllMocks()+ })++ it('passes a within-ceiling bound through to the service', async () => {+ await handler('gfs:download')(trusted, { uri, maxBytes: 1024 })+ expect(service.downloadGfsUri).toHaveBeenCalledWith(uri, 1024)+ })++ it('stays uncapped when no bound is requested', async () => {+ await handler('gfs:download')(trusted, { uri })+ expect(service.downloadGfsUri).toHaveBeenCalledWith(uri, undefined)+ })++ it('rejects a bound above the ceiling before reaching the service', async () => {+ await expect(+ handler('gfs:download')(trusted, { uri, maxBytes: GFS_DOWNLOAD_MAX_BYTES_CEILING + 1 })+ ).rejects.toThrow('download limit exceeds the allowed maximum')+ expect(service.downloadGfsUri).not.toHaveBeenCalled()+ })++ it('rejects an invalid bound instead of silently ignoring it', async () => {+ await expect(handler('gfs:download')(trusted, { uri, maxBytes: 0 })).rejects.toThrow(+ 'Invalid maxBytes'+ )+ await expect(handler('gfs:download')(trusted, { uri, maxBytes: 'big' })).rejects.toThrow(+ 'Invalid maxBytes'+ )+ expect(service.downloadGfsUri).not.toHaveBeenCalled()+ })+})diff --git a/desktop-app/src/gfs/downloadLimits.ts b/desktop-app/src/gfs/downloadLimits.tsnew file mode 100644index 000000000..d651b3f00--- /dev/null+++ b/desktop-app/src/gfs/downloadLimits.ts@@ -0,0 +1,8 @@+/**+ * Main-side ceiling for an optionally bounded 'gfs:download'. The renderer is+ * untrusted: it may request a per-download bound up to this size (the+ * folder-zip walk derives one from its remaining byte budget), but never a+ * larger one. Requests without a bound keep the save-to-disk path's uncapped+ * semantics.+ */+export const GFS_DOWNLOAD_MAX_BYTES_CEILING = 2 * 1024 * 1024 * 1024diff --git a/desktop-app/src/ipc.ts b/desktop-app/src/ipc.tsindex 4b9fc50aa..fd631e7a6 100644--- a/desktop-app/src/ipc.ts+++ b/desktop-app/src/ipc.ts@@ -17,6 +17,7 @@ import {requireChatStore,requireChatStoreForBindingGeneration,} from './chatStoreBinding.js'+import { GFS_DOWNLOAD_MAX_BYTES_CEILING } from './gfs/downloadLimits.js'import { GFS_PREVIEW_MAX_BYTES } from './gfs/previewLimits.js'import { assertSafeRouteSegment } from './pathSafety.js'import {@@ -479,9 +480,17 @@ export function registerIpcHandlers(service: AppService): void {assertTrustedSender(event)return service.resolveGfsUri(sanitizeString(payload?.uri))})- ipcMain.handle('gfs:download', async (event, payload: { uri: string }) => {+ ipcMain.handle('gfs:download', async (event, payload: { uri: string; maxBytes?: number }) => {assertTrustedSender(event)- return service.downloadGfsUri(sanitizeString(payload?.uri))+ // Optional producer-side bound (folder-zip walk): when present it is+ // validated here in main so an over-large body is rejected before it+ // materializes in either process. Absent keeps the single-file+ // save-to-disk path uncapped.+ const maxBytes = sanitizeOptionalPositiveInteger(payload?.maxBytes, 'maxBytes')+ if (maxBytes !== undefined && maxBytes > GFS_DOWNLOAD_MAX_BYTES_CEILING) {+ throw new Error('download limit exceeds the allowed maximum')+ }+ return service.downloadGfsUri(sanitizeString(payload?.uri), maxBytes)})ipcMain.handle('gfs:downloadPreview',diff --git a/desktop-app/src/preload.ts b/desktop-app/src/preload.tsindex 59abcb5b3..2e4290488 100644--- a/desktop-app/src/preload.ts+++ b/desktop-app/src/preload.ts@@ -143,7 +143,8 @@ const clerum = Object.freeze({},gfs: {resolve: (uri: string) => ipcRenderer.invoke('gfs:resolve', { uri }),- download: (uri: string) => ipcRenderer.invoke('gfs:download', { uri }),+ download: (uri: string, options?: { maxBytes?: number }) =>+ ipcRenderer.invoke('gfs:download', { uri, maxBytes: options?.maxBytes }),downloadPreview: (uri: string, maxBytes: number) =>ipcRenderer.invoke('gfs:downloadPreview', { uri, maxBytes }),listAccessible: (drive?: string, cursor?: string) =>diff --git a/desktop-app/src/renderer.d.ts b/desktop-app/src/renderer.d.tsindex 338a01182..3a3b7edd0 100644--- a/desktop-app/src/renderer.d.ts+++ b/desktop-app/src/renderer.d.ts@@ -131,7 +131,10 @@ declare global {bytes?: numberupdatedAt?: string}>- download: (uri: string) => Promise<{+ download: (+ uri: string,+ options?: { maxBytes?: number }+ ) => Promise<{resource: {drive: stringresourceId: stringdiff --git a/desktop-app/ui/src/lib/__tests__/gfsFolderZip.test.ts b/desktop-app/ui/src/lib/__tests__/gfsFolderZip.test.tsindex 517ffb289..87520d9ff 100644--- a/desktop-app/ui/src/lib/__tests__/gfsFolderZip.test.ts+++ b/desktop-app/ui/src/lib/__tests__/gfsFolderZip.test.ts@@ -54,7 +54,10 @@ function folder(function depsFor(pages: Record<string, Array<{ items: GfsZipChildItem[]; nextCursor: string | null }>>,- downloads: Record<string, () => Promise<{ bytes: ArrayBuffer }>> = {}+ downloads: Record<+ string,+ (uri: string, options?: { maxBytes?: number }) => Promise<{ bytes: ArrayBuffer }>+ > = {}): GfsFolderZipDeps {return {listChildren: vi.fn(async (resourceId: string, _drive: string, cursor?: string) => {@@ -65,9 +68,9 @@ function depsFor(if (!page) throw new Error(`no page ${index} for ${resourceId}`)return { items: page.items, nextCursor: page.nextCursor }}),- download: vi.fn(async (uri: string) => {+ download: vi.fn(async (uri: string, options?: { maxBytes?: number }) => {const produce = downloads[uri]- if (produce) return produce()+ if (produce) return produce(uri, options)return { bytes: bytesOf(uri) }}),// Spacing itself has a dedicated manual-clock test below the walk; here it@@ -346,6 +349,79 @@ describe('createGfsFolderZip', () => {).rejects.toThrow(/exceeded the 1 KiB folder-zip limit while downloading/)})+ it('bounds each transfer producer-side by the remaining budget (R1-H2)', async () => {+ // Declared size missing (older server) + a dishonestly large body: the+ // bounded fetch must refuse BEFORE the body materializes — the walk's+ // only obligation after that is mapping the refusal to the limit error.+ const deps = depsFor(+ {+ root: [+ {+ items: [+ folder({+ resourceId: 'ghost',+ name: 'ghost.bin',+ bytes: undefined as unknown as number,+ }),+ ],+ nextCursor: null,+ },+ ],+ },+ {+ 'gfs://main/ghost': async (uri: string, options?: { maxBytes?: number }) => {+ expect(options?.maxBytes).toBe(1024)+ throw new Error('gfs download exceeds the 1024-byte limit httpStatus=413')+ },+ }+ )++ await expect(+ createGfsFolderZip(+ { resourceId: 'root', drive: 'main', name: 'Ghost' },+ { deps, limits: { maxTotalBytes: 1024 } }+ )+ ).rejects.toThrow(/exceeded the 1 KiB folder-zip limit while downloading/)+ })++ it('carries a near-limit folder through download, zip and save-sized output (R1-H2)', async () => {+ // Two honest files at half the ceiling each: the second transfer is+ // bounded by exactly the remaining budget and both land in the archive.+ const seenMaxBytes: Array<number | undefined> = []+ const deps = depsFor(+ {+ root: [+ {+ items: [+ folder({ resourceId: 'half-a', name: 'half-a.bin', bytes: 512 }),+ folder({ resourceId: 'half-b', name: 'half-b.bin', bytes: 512 }),+ ],+ nextCursor: null,+ },+ ],+ },+ {+ 'gfs://main/half-a': async (_uri: string, options?: { maxBytes?: number }) => {+ seenMaxBytes.push(options?.maxBytes)+ return { bytes: new ArrayBuffer(512) }+ },+ 'gfs://main/half-b': async (_uri: string, options?: { maxBytes?: number }) => {+ seenMaxBytes.push(options?.maxBytes)+ return { bytes: new ArrayBuffer(512) }+ },+ }+ )++ const result = await createGfsFolderZip(+ { resourceId: 'root', drive: 'main', name: 'Brink' },+ { deps, limits: { maxTotalBytes: 1024 } }+ )+ expect(result.fileCount).toBe(2)+ expect(seenMaxBytes).toEqual([1024, 512])+ // The archive itself exists and parses back.+ expect(zipEntryNames(result.bytes).length).toBe(2)+ })+it('backs off once on a 429 using the server-provided retry hint', async () => {let attempts = 0const deps: GfsFolderZipDeps = {diff --git a/desktop-app/ui/src/lib/gfsFolderZip.ts b/desktop-app/ui/src/lib/gfsFolderZip.tsindex 10ccc2765..4d5b3dd40 100644--- a/desktop-app/ui/src/lib/gfsFolderZip.ts+++ b/desktop-app/ui/src/lib/gfsFolderZip.ts@@ -26,8 +26,14 @@ import { createZipWriter } from '@lib/zipWriter'* them while the Files browsing surface shares the same classes.*/export const GFS_ZIP_READS_PER_MINUTE = 120-/** Refuse before buffering: total uncompressed bytes across the folder. */-export const GFS_ZIP_MAX_TOTAL_BYTES = 1024 * 1024 * 1024+/**+ * Refuse before buffering: total bytes across the folder. 512 MiB is the+ * documented end-to-end bound (spec: ZIP budget model): with the producer-side+ * per-transfer maxBytes and the single pre-sized archive buffer, peak renderer+ * memory at the ceiling is ~2x this cap (archive + save-time Blob) plus one+ * bounded in-flight transfer.+ */+export const GFS_ZIP_MAX_TOTAL_BYTES = 512 * 1024 * 1024/** Refuse before buffering: total file entries across the folder. */export const GFS_ZIP_MAX_ENTRIES = 2000@@ -115,7 +121,12 @@ export interface GfsZipChildrenPage {export interface GfsFolderZipDeps {listChildren(resourceId: string, drive: string, cursor?: string): Promise<GfsZipChildrenPage>- download(uri: string): Promise<{ bytes: ArrayBuffer }>+ /**+ * Downloads one file. `options.maxBytes` is the producer-side bound for the+ * transfer (R1-H2): the bounded fetch rejects before an over-budget body+ * materializes in either process.+ */+ download(uri: string, options?: { maxBytes?: number }): Promise<{ bytes: ArrayBuffer }>throttle: GfsReadThrottlesleep: (ms: number) => Promise<void>}@@ -237,7 +248,8 @@ export async function createGfsFolderZip(options.deps?.listChildren ??((resourceId, drive, cursor) => window.clerum.gfs.listChildren(resourceId, drive, cursor))const download: GfsFolderZipDeps['download'] =- options.deps?.download ?? (uri => window.clerum.gfs.download(uri))+ options.deps?.download ??+ ((uri, requestOptions) => window.clerum.gfs.download(uri, requestOptions))const sleep =options.deps?.sleep ?? ((ms: number) => new Promise<void>(resolve => setTimeout(resolve, ms)))const throttle = options.deps?.throttle ?? createGfsReadThrottle()@@ -396,10 +408,21 @@ export async function createGfsFolderZip(await withAbort(throttle.acquire())let bytes: ArrayBuffertry {- bytes = (await withRateLimitRetry(() => download(file.uri))).bytes+ // R1-H2: bound the transfer producer-side by the remaining budget so an+ // over-limit body (dishonest or missing declared size) is rejected+ // before it materializes in the renderer; the receipt check below stays+ // as the backstop.+ const remainingBytes = maxTotalBytes - addedBytes+ bytes = (await withRateLimitRetry(() => download(file.uri, { maxBytes: remainingBytes })))+ .bytes} catch (error) {if (isFolderZipAbortError(error)) throw errorconst message = toMessage(error)+ if (parseHttpStatus(message) === 413) {+ throw new GfsFolderZipLimitError(+ `"${folder.name}" exceeded the ${formatZipBytes(maxTotalBytes)} folder-zip limit while downloading. Download smaller subfolders individually.`+ )+ }if (isAccessDenied(message)) {skipped.push({ path: file.path, reason: 'Permission denied' })continuediff --git a/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx b/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsxindex 29836de55..3db634dc7 100644--- a/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx+++ b/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx@@ -3379,7 +3379,7 @@ describe('FilesPage', () => {expect(screen.queryByTestId('gfs-zip-progress')).toBeNull()expect(listChildren).toHaveBeenCalledWith('folder-1', 'main', undefined)- expect(download).toHaveBeenCalledWith('gfs://main/doc-1')+ expect(download).toHaveBeenCalledWith('gfs://main/doc-1', { maxBytes: 512 * 1024 * 1024 })expect(createObjectURL).toHaveBeenCalledWith(expect.any(Blob))expect(click).toHaveBeenCalled()const savedName = (click.mock.instances[0] as HTMLAnchorElement | undefined)?.download@@ -3455,7 +3455,7 @@ describe('FilesPage', () => {expect(download).not.toHaveBeenCalled()expect(click).not.toHaveBeenCalled()expect(pushToast).toHaveBeenCalledWith(- expect.stringContaining('exceeds the 1 GiB folder-zip limit'),+ expect.stringContaining('exceeds the 512 MiB folder-zip limit'),'error')})