Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(desktop): enforce the GFS preview download ceiling in main (R1-H4)
Diego Zacarias57ae4681bbbd
diff --git a/desktop-app/src/__tests__/ipc.gfsPreviewLimit.test.ts b/desktop-app/src/__tests__/ipc.gfsPreviewLimit.test.tsnew file mode 100644index 000000000..b7476115e--- /dev/null+++ b/desktop-app/src/__tests__/ipc.gfsPreviewLimit.test.ts@@ -0,0 +1,72 @@+/**+ * R1-H4 — the preview download ceiling is authoritative in main. The renderer+ * is untrusted: it may request a smaller per-type limit, but a request above+ * GFS_PREVIEW_MAX_BYTES is rejected at the IPC boundary before it reaches the+ * service, instead of being obeyed unbounded.+ */+import { afterEach, beforeAll, describe, expect, it, vi } from 'vitest'+import type { AppService } from '../appService.js'+import { GFS_PREVIEW_MAX_BYTES } from '../gfs/previewLimits.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 IPC handler registered for ${channel}`)+ return registered+}++beforeAll(async () => {+ const { registerIpcHandlers } = await import('../ipc.js')+ registerIpcHandlers(service as unknown as AppService)+})++afterEach(() => {+ service.downloadGfsUri.mockClear()+})++describe('gfs:downloadPreview maxBytes ceiling', () => {+ it.each([GFS_PREVIEW_MAX_BYTES + 1, Number.MAX_SAFE_INTEGER])(+ 'rejects an over-cap maxBytes (%i) without calling the service',+ async maxBytes => {+ await expect(handler('gfs:downloadPreview')(trusted, { uri, maxBytes })).rejects.toThrow(+ 'preview download limit exceeds the allowed maximum'+ )+ expect(service.downloadGfsUri).not.toHaveBeenCalled()+ }+ )++ it.each([+ ['at the ceiling', GFS_PREVIEW_MAX_BYTES],+ ['a smaller per-type limit (image, 10 MiB)', 10 * 1024 * 1024],+ ])('forwards %s to the service', async (_label, maxBytes) => {+ await handler('gfs:downloadPreview')(trusted, { uri, maxBytes })+ expect(service.downloadGfsUri).toHaveBeenCalledWith(uri, maxBytes)+ })+})diff --git a/desktop-app/src/gfs/previewLimits.ts b/desktop-app/src/gfs/previewLimits.tsnew file mode 100644index 000000000..c654a3f4f--- /dev/null+++ b/desktop-app/src/gfs/previewLimits.ts@@ -0,0 +1,16 @@+/**+ * Authoritative, main-process ceiling for any GFS preview download (100 MiB).+ *+ * The renderer is the untrusted side: it may ask for a finer per-type limit+ * (e.g. 10 MiB for an image, 2 MiB for markdown), but it can never raise the+ * cap above this value. 100 MiB is the largest of the per-type preview limits+ * (video — GFS_VIDEO_PREVIEW_MAX_BYTES in the renderer constants); no legitimate+ * preview needs more, so a request exceeding it is a renderer bug or a tampered+ * payload and is rejected at the IPC boundary. If a per-type limit is ever+ * raised past this, raise this ceiling too or main will reject that preview.+ *+ * Kept in this dependency-free leaf module (not in boundedDownload.ts, which+ * pulls in the HTTP client graph) so the IPC layer can import the ceiling+ * without dragging that graph into its module evaluation.+ */+export const GFS_PREVIEW_MAX_BYTES = 100 * 1024 * 1024diff --git a/desktop-app/src/ipc.ts b/desktop-app/src/ipc.tsindex e072288de..88b88ba2e 100644--- a/desktop-app/src/ipc.ts+++ b/desktop-app/src/ipc.ts@@ -12,6 +12,7 @@ import { promises as fs } from 'node:fs'import path from 'node:path'import { AppService } from './appService.js'import { requireChatStore } from './chatStoreBinding.js'+import { GFS_PREVIEW_MAX_BYTES } from './gfs/previewLimits.js'import { assertSafeRouteSegment } from './pathSafety.js'import {PLUGIN_SDK_CAPABILITIES_CHANNEL,@@ -420,6 +421,13 @@ export function registerIpcHandlers(service: AppService): void {if (maxBytes === undefined) {throw new Error('maxBytes is required for a preview download')}+ // The renderer is untrusted: it may ask for a smaller per-type limit, but+ // the ceiling is enforced here in main. Reject (not clamp) an over-cap+ // request — a value above the app maximum is a bug or a tampered payload,+ // consistent with the fail-loud missing-maxBytes guard above.+ if (maxBytes > GFS_PREVIEW_MAX_BYTES) {+ throw new Error('preview download limit exceeds the allowed maximum')+ }return service.downloadGfsUri(sanitizeString(payload?.uri), maxBytes)})