Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): cap the status image upload like the chat composer

Yudhi Armyndharis60e1555ec5bd
diff --git a/dashboard/src/components/chats/ChatComposer.tsx b/dashboard/src/components/chats/ChatComposer.tsx
index 999fc19d1..3938fef1a 100644
--- a/dashboard/src/components/chats/ChatComposer.tsx
+++ b/dashboard/src/components/chats/ChatComposer.tsx
@@ -22,7 +22,7 @@ const messageTypeFromMime = (mimetype: string): MessageType => {
// Client pre-check before base64-encoding an upload, same cap as the message tester: base64
// inflates ~1.33x, so ~18 MiB raw stays under the backend's default 25 MiB body limit and the
// pick fails here with a toast instead of OOMing the tab on the FileReader.
-const MEDIA_UPLOAD_MAX_BYTES = 18 * 1024 * 1024;
+export const MEDIA_UPLOAD_MAX_BYTES = 18 * 1024 * 1024;
/** A picked-but-unsent file, staged until send, removal, or a move to another chat. */
export interface StagedAttachment {
diff --git a/dashboard/src/components/chats/StatusComposeModal.test.ts b/dashboard/src/components/chats/StatusComposeModal.test.ts
new file mode 100644
index 000000000..21adcae41
--- /dev/null
+++ b/dashboard/src/components/chats/StatusComposeModal.test.ts
@@ -0,0 +1,76 @@
+// An image status is posted as base64 JSON, so a file past the upload cap is refused only after the whole
+// inflated body went up. The picker rejects it before reading, like the chat composer does.
+import '../../test-helpers/register-hooks.ts';
+import { test, before, after } from 'node:test';
+import assert from 'node:assert/strict';
+import { createElement } from 'react';
+
+let rtl: typeof import('@testing-library/react');
+let render: () => ReturnType<typeof import('@testing-library/react').render>;
+let queryClient: import('@tanstack/react-query').QueryClient;
+
+let reads = 0;
+class CountingFileReader {
+ onload: (() => void) | null = null;
+ readAsDataURL(): void {
+ reads += 1;
+ }
+}
+
+before(async () => {
+ const { installJsdomGlobals } = await import('../../test-helpers/jsdom.ts');
+ await installJsdomGlobals();
+ (globalThis as Record<string, unknown>).FileReader = CountingFileReader;
+ const { i18nReady } = await import('../../i18n/index.ts');
+ await i18nReady;
+ rtl = await import('@testing-library/react');
+ const { QueryClient, QueryClientProvider } = await import('@tanstack/react-query');
+ const { ToastProvider } = await import('../Toast.tsx');
+ const { RoleContext } = await import('../../hooks/useRole.tsx');
+ const { default: StatusComposeModal } = await import('./StatusComposeModal.tsx');
+ queryClient = new QueryClient();
+ const role = {
+ role: 'operator' as const,
+ setRole: () => undefined,
+ isAdmin: false,
+ isOperator: true,
+ isViewer: false,
+ canWrite: true,
+ engineType: 'whatsapp-web.js',
+ setEngineType: () => undefined,
+ };
+ render = () =>
+ rtl.render(
+ createElement(
+ QueryClientProvider,
+ { client: queryClient },
+ createElement(
+ RoleContext.Provider,
+ { value: role },
+ createElement(
+ ToastProvider,
+ null,
+ createElement(StatusComposeModal, { sessionId: 's1', onClose: () => undefined, onPosted: () => undefined }),
+ ),
+ ),
+ ),
+ );
+});
+
+after(() => {
+ rtl.cleanup();
+ // Drops the unmounted query's five-minute garbage-collection timer, which would hold the runner open.
+ queryClient.clear();
+});
+
+test('an image past the upload cap is refused before it is read', async () => {
+ const { screen, fireEvent } = rtl;
+ render();
+ fireEvent.click(screen.getByRole('button', { name: 'Image' }));
+ const file = new window.File(['x'], 'huge.jpg', { type: 'image/jpeg' });
+ Object.defineProperty(file, 'size', { value: 18 * 1024 * 1024 + 1 });
+ fireEvent.change(screen.getByLabelText('Status image'), { target: { files: [file] } });
+
+ await screen.findByText('File is too large (max 18 MB)');
+ assert.equal(reads, 0, 'the oversized file was read');
+});
diff --git a/dashboard/src/components/chats/StatusComposeModal.tsx b/dashboard/src/components/chats/StatusComposeModal.tsx
index 8fd2d5598..c0c652d76 100644
--- a/dashboard/src/components/chats/StatusComposeModal.tsx
+++ b/dashboard/src/components/chats/StatusComposeModal.tsx
@@ -6,6 +6,7 @@ import { contactApi, sessionApi } from '../../services/api';
import { useRole } from '../../hooks/useRole';
import { useToast } from '../../hooks/useToast';
import { Modal } from '../Modal';
+import { MEDIA_UPLOAD_MAX_BYTES } from './ChatComposer';
// Mirrors @ArrayMaxSize(256) on the send-status DTOs — the picker caps selection client-side so the
// user can't build a list the backend is guaranteed to reject.
@@ -103,6 +104,13 @@ function StatusComposeModal({ sessionId, onClose, onPosted }: Props) {
const handleComposeImageFile = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (!file) return;
+ // Posted as base64 JSON, so an oversized image would only be refused after the whole inflated
+ // body went up. Rejected before anything is cleared, so an image URL or earlier pick is kept.
+ if (file.size > MEDIA_UPLOAD_MAX_BYTES) {
+ showErrorToast(t('chats.errors.fileTooLarge'));
+ e.target.value = '';
+ return;
+ }
setComposeImageUrl('');
const myRead = ++composeImageReadSeq.current;
const reader = new FileReader();