Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): skip role-gated reads a key cannot make on the home page
Yudhi Armyndharis94474e5fa8f7
diff --git a/dashboard/src/hooks/queries.ts b/dashboard/src/hooks/queries.tsindex 6e6ba7687..c591d1d4e 100644--- a/dashboard/src/hooks/queries.ts+++ b/dashboard/src/hooks/queries.ts@@ -86,11 +86,12 @@ export function useStopSessionMutation() {// ── Webhook Queries ───────────────────────────────────────────────────-export function useWebhooksQuery() {+export function useWebhooksQuery(enabled = true) {return useQuery({queryKey: queryKeys.webhooks,queryFn: webhookApi.listAll,staleTime: 30_000,+ enabled,// Normalize `events` to an array at the data boundary so every consumer (list render + edit// modal) can trust the declared string[] shape. A malformed payload then renders as no tags// instead of taking down the whole SPA via events.map() in the ErrorBoundary.@@ -355,12 +356,13 @@ export function useCurrentEngineQuery() {// ── Stats Queries ─────────────────────────────────────────────────────// /stats/* is ADMIN-only; a non-admin key gets 403 → don't retry, let the UI fall back gracefully.-export function useStatsOverviewQuery() {+export function useStatsOverviewQuery(enabled = true) {return useQuery({queryKey: queryKeys.statsOverview,queryFn: statsApi.getOverview,staleTime: 30_000,retry: false,+ enabled,});}diff --git a/dashboard/src/pages/Dashboard.test.ts b/dashboard/src/pages/Dashboard.test.tsindex ea9fc14cd..cf0270126 100644--- a/dashboard/src/pages/Dashboard.test.ts+++ b/dashboard/src/pages/Dashboard.test.ts@@ -1,6 +1,7 @@// Render test for the Dashboard stat cards under the bare `node --test` runner, on the Sessions.test.ts-// harness. GET /webhooks and GET /stats/overview both reject a viewer key; each card must then show-// the unavailable placeholder rather than a count the gateway never returned. POST /sessions/:id/stop+// harness. GET /webhooks is OPERATOR-only and GET /stats/overview ADMIN-only: a key without the role+// is not sent them, and a refused or unsent read shows the unavailable placeholder rather than a count+// the gateway never returned. POST /sessions/:id/stop// is OPERATOR-only: a viewer is offered no Disconnect, and a failed stop is reported, not swallowed.import '../test-helpers/register-hooks.ts';import { test, before, afterEach } from 'node:test';@@ -117,9 +118,10 @@ function statValue(label: string): string {test('a refused webhook read shows the unavailable placeholder, not zero webhooks', async () => {webhooksStatus = 403;+ window.sessionStorage.setItem('openwa_user_role', 'operator');renderDashboard();await rtl.screen.findByText('Webhooks Configured');- // The overview card is refused too, so its placeholder is the one the webhook card must match.+ // The overview card is not read for an operator, so its placeholder is the one the webhook card must match.await rtl.waitFor(() => assert.equal(statValue('Webhooks Configured'), statValue('Messages Today')));assert.notEqual(statValue('Webhooks Configured'), '0');});@@ -127,6 +129,7 @@ test('a refused webhook read shows the unavailable placeholder, not zero webhooktest('a failed background refetch keeps counting the cached webhooks', async () => {webhooksStatus = 200;webhookList = [{ id: 'w1', url: 'https://example.test/hook', events: [] }];+ window.sessionStorage.setItem('openwa_user_role', 'operator');renderDashboard();await rtl.screen.findByText('Webhooks Configured');await rtl.waitFor(() => assert.equal(statValue('Webhooks Configured'), '1'));@@ -139,6 +142,7 @@ test('a failed background refetch keeps counting the cached webhooks', async ()test('a successful empty webhook read still counts zero', async () => {webhooksStatus = 200;+ window.sessionStorage.setItem('openwa_user_role', 'operator');renderDashboard();await rtl.screen.findByText('Webhooks Configured');await rtl.waitFor(() => assert.equal(statValue('Webhooks Configured'), '0'));@@ -211,8 +215,11 @@ test('a non-admin key never loads the chart section', async () => {for (const role of ['viewer', 'operator']) {window.sessionStorage.setItem('openwa_user_role', role);renderDashboard();- await rtl.waitFor(() => assert.ok(requested.includes('/api/stats/overview')));+ await rtl.waitFor(() => assert.ok(requested.includes('/api/sessions')));await new Promise(resolve => setTimeout(resolve, 50));+ assert.ok(!requested.includes('/api/stats/overview'), `${role}: the admin-only overview was requested`);+ if (role === 'viewer')+ assert.ok(!requested.includes('/api/webhooks'), 'viewer: the operator-only webhook list was requested');assert.ok(!requested.some(p => p.startsWith('/api/stats/messages')),`${role}: the charts asked for /stats/messages`,@@ -222,6 +229,16 @@ test('a non-admin key never loads the chart section', async () => {}});+test('a viewer is not sent the webhook read and its card shows the placeholder', async () => {+ webhooksStatus = 200;+ renderDashboard();+ await rtl.screen.findByText('Webhooks Configured');+ await new Promise(resolve => setTimeout(resolve, 50));+ assert.ok(!requested.includes('/api/webhooks'));+ assert.equal(statValue('Webhooks Configured'), statValue('Messages Today'));+ assert.notEqual(statValue('Webhooks Configured'), '0');+});+test('an admin key loads the chart section', async () => {window.sessionStorage.setItem('openwa_user_role', 'admin');renderDashboard();diff --git a/dashboard/src/pages/Dashboard.tsx b/dashboard/src/pages/Dashboard.tsxindex 5ea5e3003..8e99cf275 100644--- a/dashboard/src/pages/Dashboard.tsx+++ b/dashboard/src/pages/Dashboard.tsx@@ -34,10 +34,11 @@ export function Dashboard() {isLoadingError: sessionsNeverLoaded,} = useSessionsQuery();const { data: stats } = useSessionStatsQuery();- const { data: webhooks, isError: webhooksFailed } = useWebhooksQuery();- // /stats/overview is ADMIN-only; for a non-admin key it 403s → overview stays undefined and the- // message cards fall back to '—' without breaking the (un-gated) session cards.- const { data: overview } = useStatsOverviewQuery();+ // GET /webhooks is OPERATOR-only and /stats/overview ADMIN-only. A key without the role is not sent+ // them, since the gateway audits every refusal as a failed authentication; their cards show the+ // unavailable placeholder.+ const { data: webhooks, isError: webhooksFailed } = useWebhooksQuery(canWrite);+ const { data: overview } = useStatsOverviewQuery(isAdmin);const stopMutation = useStopSessionMutation();const unavailable = '—';const messagesToday = overview ? overview.messages.today.sent + overview.messages.today.received : unavailable;@@ -50,9 +51,9 @@ export function Dashboard() {? sessionsError.message: t('dashboard.loadError'): null;- // GET /webhooks is OPERATOR-only, so a viewer key always fails it: a failed read is not zero webhooks.+ // A viewer is not sent the webhook read, and a failed read is not zero webhooks either.// A failed background refetch keeps the cached list, which still counts.- const webhookCount = webhooksFailed && !webhooks ? unavailable : (webhooks ?? []).length;+ const webhookCount = !canWrite || (webhooksFailed && !webhooks) ? unavailable : (webhooks ?? []).length;const handleDisconnect = async (id: string) => {try {