Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): do not request webhooks or templates as a viewer

Yudhi Armyndharis343d92dd8315
diff --git a/dashboard/src/pages/Templates.test.ts b/dashboard/src/pages/Templates.test.ts
index 35130ffc8..823a71b3c 100644
--- a/dashboard/src/pages/Templates.test.ts
+++ b/dashboard/src/pages/Templates.test.ts
@@ -1,6 +1,8 @@
// Render test for the Templates page under the bare `node --test` runner, on the Sessions.test.ts
-// harness. The template list route is OPERATOR-only, so a viewer key always gets 403 there; a failed
-// read must say so instead of rendering the "no templates saved" empty state.
+// harness. The template list route is OPERATOR-only: a viewer key is not sent it (the gateway records every
+// refusal as a failed authentication) and is shown the permission state, and a 403 or any other failed read
+// must say so instead of rendering the "no templates saved" empty state. The role defaults to operator, the
+// lowest one that can read the list.
import '../test-helpers/register-hooks.ts';
import { test, before, afterEach } from 'node:test';
import assert from 'node:assert/strict';
@@ -9,6 +11,7 @@ import { createElement } from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
let templatesStatus = 200;
+let templateReads = 0;
let sessionsStatus = 200;
// When set, GET /api/sessions answers only once this settles, holding the page on its first load.
let sessionsGate: Promise<void> | null = null;
@@ -63,6 +66,7 @@ function installFetchStub(): void {
return Promise.resolve(jsonResponse([{ id: 'tpl-2', name: 'support-greeting', body: 'Hello' }]));
}
if (path === '/api/sessions/sess-1/templates') {
+ templateReads++;
if (templatesStatus === 403) {
return Promise.resolve(jsonResponse({ message: 'Insufficient permissions. Required: operator' }, 403));
}
@@ -95,7 +99,7 @@ before(async () => {
configurable: true,
value: { writeText: (text: string) => (copied.push(text), Promise.resolve()) },
});
- window.sessionStorage.setItem('openwa_user_role', 'viewer');
+ window.sessionStorage.setItem('openwa_user_role', 'operator');
const { i18nReady } = await import('../i18n/index.ts');
await i18nReady;
rtl = await import('@testing-library/react');
@@ -117,7 +121,8 @@ afterEach(() => {
updated.length = 0;
copied.length = 0;
saveGate = null;
- window.sessionStorage.setItem('openwa_user_role', 'viewer');
+ templateReads = 0;
+ window.sessionStorage.setItem('openwa_user_role', 'operator');
});
function renderTemplates(): void {
@@ -131,15 +136,13 @@ function renderTemplates(): void {
);
}
-// The row button exists so a template can be deleted without opening it in the editor first, and it
-// is gated on the same write permission as the editor's own delete. Both halves are pinned here.
-test('a write key can delete a template from its row, and a read-only key cannot', async () => {
+// The row button exists so a template can be deleted without opening it in the editor first.
+test('a write key can delete a template from its row', async () => {
const { screen, fireEvent, within, waitFor } = rtl;
templatesStatus = 200;
templates = [{ id: 'tpl-1', name: 'invoice-reminder', body: 'Hi {{name}}' }];
deleted.length = 0;
- window.sessionStorage.setItem('openwa_user_role', 'operator');
renderTemplates();
const row = (await screen.findByText('invoice-reminder')).closest('.template-list-row') as HTMLElement;
@@ -150,20 +153,25 @@ test('a write key can delete a template from its row, and a read-only key cannot
assert.ok(within(dialog).getByText(/invoice-reminder/), 'the confirm did not name the template');
fireEvent.click(within(dialog).getByRole('button', { name: 'Delete' }));
await waitFor(() => assert.deepEqual(deleted, ['tpl-1'], 'the delete never reached the API'));
+});
- rtl.cleanup();
- window.sessionStorage.setItem('openwa_user_role', 'viewer');
+// A read-only key is never sent the list, so it has no row to delete from.
+test('a read-only key is shown the permission state without a list request the gateway would refuse', async () => {
templates = [{ id: 'tpl-1', name: 'invoice-reminder', body: 'Hi {{name}}' }];
+ window.sessionStorage.setItem('openwa_user_role', 'viewer');
renderTemplates();
- const readOnlyRow = (await screen.findByText('invoice-reminder')).closest('.template-list-row') as HTMLElement;
- assert.equal(
- within(readOnlyRow).queryByRole('button', { name: 'Delete' }) === null,
- true,
- 'a read-only key was offered the row delete',
- );
+ await rtl.screen.findByText('No access to templates');
+ assert.equal(rtl.screen.queryByText('No templates saved') === null, true);
+ assert.equal(rtl.screen.queryByText('invoice-reminder') === null, true);
+
+ // A retry or a window focus would send it again; give either the time to.
+ window.dispatchEvent(new window.Event('focus'));
+ await new Promise(resolve => setTimeout(resolve, 100));
+ assert.equal(templateReads, 0, 'the viewer page asked the gateway for the template list');
});
+// The key's role is what the page learned at sign-in; a 403 for a key it took for a writer is still a refusal.
test('a 403 on the template list shows a permission state, not an empty library', async () => {
templatesStatus = 403;
renderTemplates();
diff --git a/dashboard/src/pages/Templates.tsx b/dashboard/src/pages/Templates.tsx
index 7935de11a..b30670c11 100644
--- a/dashboard/src/pages/Templates.tsx
+++ b/dashboard/src/pages/Templates.tsx
@@ -75,11 +75,13 @@ export function Templates() {
const [previewValues, setPreviewValues] = useState<Record<string, string>>({});
const [searchTerm, setSearchTerm] = useState('');
+ // The template list route is OPERATOR-only and the gateway records every refusal as a failed authentication,
+ // so a key that cannot write is not sent it.
const {
data: templates = [],
isLoading: loadingTemplates,
error: templatesError,
- } = useTemplatesQuery(selectedSessionId, !!selectedSessionId);
+ } = useTemplatesQuery(selectedSessionId, canWrite && !!selectedSessionId);
const createMutation = useCreateTemplateMutation();
const updateMutation = useUpdateTemplateMutation();
const deleteMutation = useDeleteTemplateMutation();
@@ -278,12 +280,13 @@ export function Templates() {
<div className="templates-loading-inline">
<Loader2 className="animate-spin" size={24} />
</div>
- ) : templatesError && templates.length === 0 ? (
- // A failed read is not an empty library: a viewer key always gets 403 here (the route is
- // OPERATOR-only), and a gateway error would otherwise read as "no templates saved".
+ ) : (templatesError || !canWrite) && templates.length === 0 ? (
+ // A failed read is not an empty library: a key below operator is refused here (the route is
+ // OPERATOR-only, so it is never sent), and a gateway error would otherwise read as "no templates
+ // saved". The 403 branch stays for a key whose role the page took for a writer's.
<div className="templates-empty-list" role="alert">
<AlertCircle size={40} strokeWidth={1} />
- {(templatesError as { status?: number }).status === 403 ? (
+ {!canWrite || (templatesError as { status?: number } | null)?.status === 403 ? (
<>
<h3>{t('templates.empty.forbiddenTitle')}</h3>
<p>{t('templates.empty.forbiddenDesc')}</p>
@@ -291,7 +294,7 @@ export function Templates() {
) : (
<>
<h3>{t('templates.empty.loadErrorTitle')}</h3>
- <p>{templatesError.message}</p>
+ <p>{templatesError?.message}</p>
</>
)}
</div>
diff --git a/dashboard/src/pages/Webhooks.test.ts b/dashboard/src/pages/Webhooks.test.ts
index 202568732..33bd0df30 100644
--- a/dashboard/src/pages/Webhooks.test.ts
+++ b/dashboard/src/pages/Webhooks.test.ts
@@ -1,6 +1,8 @@
// Render test for the Webhooks page under the bare `node --test` runner, on the Templates.test.ts
-// harness. GET /webhooks is OPERATOR-only, so a viewer key always gets 403 there; a failed read must
-// say so instead of rendering the "no webhooks configured" empty state.
+// harness. GET /webhooks is OPERATOR-only: a viewer key is not sent it (the gateway records every refusal as a
+// failed authentication) and is shown the permission state, and a 403 or any other failed read must say so
+// instead of rendering the "no webhooks configured" empty state. The role defaults to operator, the lowest
+// one that can read the list.
import '../test-helpers/register-hooks.ts';
import { test, before, afterEach } from 'node:test';
import assert from 'node:assert/strict';
@@ -8,6 +10,7 @@ import { createElement } from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
let webhooksStatus = 200;
+let webhookReads = 0;
let webhookList: unknown[] = [];
let sessionList: unknown[] = [];
let createCalls = 0;
@@ -56,6 +59,7 @@ function installFetchStub(): void {
);
}
if (path === '/api/webhooks') {
+ webhookReads++;
if (webhooksStatus === 403) {
return Promise.resolve(jsonResponse({ message: 'Insufficient permissions. Required: operator' }, 403));
}
@@ -76,7 +80,7 @@ before(async () => {
const { installJsdomGlobals } = await import('../test-helpers/jsdom.ts');
await installJsdomGlobals();
installFetchStub();
- window.sessionStorage.setItem('openwa_user_role', 'viewer');
+ window.sessionStorage.setItem('openwa_user_role', 'operator');
const { i18nReady } = await import('../i18n/index.ts');
await i18nReady;
rtl = await import('@testing-library/react');
@@ -99,7 +103,8 @@ afterEach(() => {
deleteCalls = [];
releaseRequests();
heldRequests = Promise.resolve();
- window.sessionStorage.setItem('openwa_user_role', 'viewer');
+ webhookReads = 0;
+ window.sessionStorage.setItem('openwa_user_role', 'operator');
});
function renderWebhooks(): void {
@@ -113,6 +118,7 @@ function renderWebhooks(): void {
);
}
+// The key's role is what the page learned at sign-in; a 403 for a key it took for a writer is still a refusal.
test('a 403 on the webhook list shows a permission state, not an empty list', async () => {
webhooksStatus = 403;
renderWebhooks();
@@ -120,6 +126,19 @@ test('a 403 on the webhook list shows a permission state, not an empty list', as
assert.ok(!rtl.screen.queryByText('No webhooks configured'), 'a refused read claimed there are no webhooks');
});
+test('a viewer key is shown the permission state without a list request the gateway would refuse', async () => {
+ webhooksStatus = 200;
+ window.sessionStorage.setItem('openwa_user_role', 'viewer');
+ renderWebhooks();
+ await rtl.screen.findByText('No access to webhooks');
+ assert.ok(!rtl.screen.queryByText('No webhooks configured'), 'a read nobody sent claimed there are no webhooks');
+
+ // A retry or a window focus would send it again; give either the time to.
+ window.dispatchEvent(new window.Event('focus'));
+ await new Promise(resolve => setTimeout(resolve, 100));
+ assert.equal(webhookReads, 0, 'the viewer page asked the gateway for the webhook list');
+});
+
test('any other failed read shows the error, not an empty list', async () => {
webhooksStatus = 500;
renderWebhooks();
diff --git a/dashboard/src/pages/Webhooks.tsx b/dashboard/src/pages/Webhooks.tsx
index 6c6be3200..fde837f7f 100644
--- a/dashboard/src/pages/Webhooks.tsx
+++ b/dashboard/src/pages/Webhooks.tsx
@@ -198,7 +198,9 @@ export function Webhooks() {
const { t } = useTranslation();
useDocumentTitle(t('webhooks.title'));
const { canWrite } = useRole();
- const { data: webhooks = [], isLoading: loadingWebhooks, error: webhooksError } = useWebhooksQuery();
+ // GET /webhooks is OPERATOR-only and the gateway records every refusal as a failed authentication, so a key
+ // that cannot write is not sent it.
+ const { data: webhooks = [], isLoading: loadingWebhooks, error: webhooksError } = useWebhooksQuery(canWrite);
const { data: sessions = [] } = useSessionsQuery();
const loading = loadingWebhooks;
const createMutation = useCreateWebhookMutation();
@@ -690,12 +692,13 @@ export function Webhooks() {
<div className="webhooks-content">
<div className="webhooks-list-container">
- {webhooksError && webhooks.length === 0 ? (
- // A failed read is not an empty list: a viewer key always gets 403 here (the route is
- // OPERATOR-only), and a gateway error would otherwise read as "no webhooks configured".
+ {(webhooksError || !canWrite) && webhooks.length === 0 ? (
+ // A failed read is not an empty list: a key below operator is refused here (the route is
+ // OPERATOR-only, so it is never sent), and a gateway error would otherwise read as "no webhooks
+ // configured". The 403 branch stays for a key whose role the page took for a writer's.
<div className="empty-table-state" role="alert">
<AlertCircle size={48} strokeWidth={1} />
- {(webhooksError as { status?: number }).status === 403 ? (
+ {!canWrite || (webhooksError as { status?: number } | null)?.status === 403 ? (
<>
<h3>{t('webhooks.empty.forbiddenTitle')}</h3>
<p>{t('webhooks.empty.forbiddenDesc')}</p>
@@ -703,7 +706,7 @@ export function Webhooks() {
) : (
<>
<h3>{t('webhooks.empty.loadErrorTitle')}</h3>
- <p>{webhooksError.message}</p>
+ <p>{webhooksError?.message}</p>
</>
)}
</div>