Skip to content

The agent that grows with you

fix(webapp): refuse a pasted image the browser cannot send instead of attaching it

emozillaea7b4476f0c8
diff --git a/apps/desktop/src/lib/browser-uploads.test.ts b/apps/desktop/src/lib/browser-uploads.test.ts
new file mode 100644
index 00000000000..ba83fb2a6bf
--- /dev/null
+++ b/apps/desktop/src/lib/browser-uploads.test.ts
@@ -0,0 +1,56 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { $connection } from '@/store/session'
+import { clearWebappSession, seedWebappSession } from '@/test/webapp-session'
+
+import { installBrowserDesktopBridge } from './browser-desktop-bridge'
+import { BROWSER_UPLOAD_MAX_BYTES } from './browser-uploads'
+
+afterEach(() => {
+ clearWebappSession()
+ Reflect.deleteProperty(window, 'hermesDesktop')
+ document.documentElement.removeAttribute('data-hermes-desktop-host')
+ $connection.set(null)
+ vi.unstubAllGlobals()
+ vi.restoreAllMocks()
+})
+
+function png(size: number): Uint8Array {
+ const bytes = new Uint8Array(size)
+ bytes.set([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
+
+ return bytes
+}
+
+describe('browser image paste size', () => {
+ // A pasted or dropped image is staged on the host, and sending the message reads it back through
+ // /api/fs/read-data-url, which stops at 16 MiB. An image over that limit is refused at the paste,
+ // with the limit named, instead of attaching an image the message can never send.
+ it('refuses an image larger than the browser can send, before uploading it', async () => {
+ seedWebappSession('served-token')
+ const fetchMock = vi.fn()
+ vi.stubGlobal('fetch', fetchMock)
+ expect(installBrowserDesktopBridge()).toBe(true)
+
+ await expect(window.hermesDesktop!.saveImageBuffer(png(BROWSER_UPLOAD_MAX_BYTES + 1), '.png')).rejects.toThrow(
+ 'The image is larger than 16 MiB, the largest image the browser can send.'
+ )
+ expect(fetchMock).not.toHaveBeenCalled()
+ })
+
+ it('stages an image of exactly the limit', async () => {
+ seedWebappSession('served-token')
+
+ const fetchMock = vi.fn().mockResolvedValue(
+ new Response(JSON.stringify({ path: '/srv/hermes/cache/images/upload.png' }), { status: 200 })
+ )
+
+ vi.stubGlobal('fetch', fetchMock)
+ expect(installBrowserDesktopBridge()).toBe(true)
+
+ await expect(window.hermesDesktop!.saveImageBuffer(png(BROWSER_UPLOAD_MAX_BYTES), '.png')).resolves.toBe(
+ '/srv/hermes/cache/images/upload.png'
+ )
+ expect((fetchMock.mock.calls[0] as [URL])[0].pathname).toBe('/api/chat/image-upload')
+ })
+})
diff --git a/apps/desktop/src/lib/browser-uploads.ts b/apps/desktop/src/lib/browser-uploads.ts
index db44d2a18e2..651cc3b6e53 100644
--- a/apps/desktop/src/lib/browser-uploads.ts
+++ b/apps/desktop/src/lib/browser-uploads.ts
@@ -4,7 +4,9 @@ import { type BrowserBootstrap, browserFetch } from '@/lib/browser-transport'
const STAGED_UPLOAD_CACHE_LIMIT = 256
-/** The largest file the browser stages on the host; mirrors WEBAPP_ATTACHMENT_MAX_BYTES (hermes_constants.py). */
+/** The largest file the browser stages on the host, and the largest image it can send: sending reads
+ * a staged image back through /api/fs/read-data-url. Mirrors WEBAPP_ATTACHMENT_MAX_BYTES
+ * (hermes_constants.py), the cap on both. */
export const BROWSER_UPLOAD_MAX_BYTES = 16 * 1024 * 1024
const IMAGE_MIME_BY_EXTENSION: Record<string, string> = {
@@ -201,6 +203,14 @@ export function createBrowserUploadsBridge({
const extension = normalizedExtension(ext)
const imageMime = IMAGE_MIME_BY_EXTENSION[extension]
+ // /api/chat/image-upload stores images up to 25 MiB, but the send reads the staged image back
+ // with the 16 MiB file-read cap: refuse a larger one here, before it shows as attached.
+ if (imageMime && bytes.byteLength > BROWSER_UPLOAD_MAX_BYTES) {
+ throw new Error(
+ `The image is larger than ${BROWSER_UPLOAD_MAX_BYTES / (1024 * 1024)} MiB, the largest image the browser can send.`
+ )
+ }
+
if (imageMime) {
// Route by the profile active at the paste, not after the encode.
const profile = currentProfile()
diff --git a/website/docs/user-guide/desktop.md b/website/docs/user-guide/desktop.md
index 6b3288b0be5..5c0cf3b4028 100644
--- a/website/docs/user-guide/desktop.md
+++ b/website/docs/user-guide/desktop.md
@@ -85,9 +85,10 @@ Webapp's sign-in, so open tabs need `hermes webapp` again. When the Webapp's bun
yet, the status says so instead: `hermes webapp` builds it, and the running gateway serves it without a
restart.
-Browser-selected files are staged under the active profile before they enter the normal attachment
-flow, with a **16 MiB** limit per file. Host files download through the browser's download manager, and
-supported audio and video play inline using the server's file-access rules. **Save Image** for
+Browser-selected files and pasted or dropped images are staged under the active profile before they
+enter the normal attachment flow, with a **16 MiB** limit per file. Host files download through the
+browser's download manager, and supported audio and video play inline using the server's file-access
+rules. **Save Image** for
cross-origin URLs needs CORS, with a **25 MiB** limit and a **30-second** deadline. Website links open
in a separate tab; explicit URL previews use a sandboxed iframe that cannot open popups, so use **Open
in browser** for sites that refuse embedding. Native-only features stay in Electron: global shortcuts,