Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): correct two defects in the API key scope editor
Yudhi Armyndharis9df65bd88cc1
diff --git a/dashboard/src/components/SessionScopePicker.tsx b/dashboard/src/components/SessionScopePicker.tsxindex 3e4dd8829..44ae2c8fd 100644--- a/dashboard/src/components/SessionScopePicker.tsx+++ b/dashboard/src/components/SessionScopePicker.tsx@@ -1,7 +1,7 @@import { useState } from 'react';import { useTranslation } from 'react-i18next';import type { Session } from '../services/api';-import { sessionPickerStartsExpanded } from '../utils/sessionScope';+import { sessionPickerStartsExpanded, sessionScopeRows } from '../utils/sessionScope';interface SessionScopePickerProps {sessions: Session[];@@ -13,6 +13,9 @@ interface SessionScopePickerProps {export function SessionScopePicker({ sessions, selectedIds, onChange, disabled }: SessionScopePickerProps) {const { t } = useTranslation();const [expanded, setExpanded] = useState(() => sessionPickerStartsExpanded(selectedIds));+ // Live sessions plus any selected id that no longer resolves to one, so a deleted session's id+ // stays visible and can be unticked instead of riding along on every save.+ const rows = sessionScopeRows(sessions, selectedIds);const toggle = (id: string) => {onChange(selectedIds.includes(id) ? selectedIds.filter(current => current !== id) : [...selectedIds, id]);@@ -25,7 +28,7 @@ export function SessionScopePicker({ sessions, selectedIds, onChange, disabled }};return (- <div className="session-scope-picker" role="group">+ <div className="session-scope-picker" role="group" aria-label={t('apiKeys.sessions.label')}>{expanded ? (<><button@@ -38,22 +41,24 @@ export function SessionScopePicker({ sessions, selectedIds, onChange, disabled }{t('apiKeys.sessions.leaveAll')}</button><p className="session-scope-hint">{t('apiKeys.sessions.hint')}</p>- {sessions.length === 0 ? (+ {rows.length === 0 ? (<p className="session-scope-empty">{t('apiKeys.sessions.empty')}</p>) : (<ul className="session-scope-list">- {sessions.map(session => (- <li key={session.id}>+ {rows.map(({ id, session }) => (+ <li key={id}><label className="session-scope-option"><inputtype="checkbox"- checked={selectedIds.includes(session.id)}- onChange={() => toggle(session.id)}+ checked={selectedIds.includes(id)}+ onChange={() => toggle(id)}disabled={disabled}/><span className="session-scope-meta">- <span className="session-scope-name">{session.name}</span>- {session.phone ? <span className="session-scope-phone">{session.phone}</span> : null}+ {/* No live session behind the id: show it raw rather than hide it, which is+ the only way an operator can drop a deleted session from the allowlist. */}+ <span className="session-scope-name">{session ? session.name : id}</span>+ {session?.phone ? <span className="session-scope-phone">{session.phone}</span> : null}</span></label></li>diff --git a/dashboard/src/pages/ApiKeys.tsx b/dashboard/src/pages/ApiKeys.tsxindex 07fc88787..bcc2ef65a 100644--- a/dashboard/src/pages/ApiKeys.tsx+++ b/dashboard/src/pages/ApiKeys.tsx@@ -38,7 +38,7 @@ import { Modal } from '../components/Modal';import { SessionScopePicker } from '../components/SessionScopePicker';import { useToast } from '../hooks/useToast';import { copyToClipboard } from '../utils/clipboard';-import { canScopeSessions, sessionScopeNames } from '../utils/sessionScope';+import { canScopeSessions, sameSessionScope, sessionScopeNames } from '../utils/sessionScope';import './ApiKeys.css';const roleNames = ['admin', 'operator', 'viewer'] as const;@@ -121,6 +121,14 @@ export function ApiKeys() {const handleSaveSessions = async () => {if (!editingKey) return;+ // An unchanged Save must not be sent. The server writes `allowedSessions` whenever the field is+ // present, and storing [] over a key that was never scoped reads back as an authorization+ // change: it drops every live /events socket holding that key and writes an audit row saying+ // the scope moved when it did not.+ if (sameSessionScope(editSessions, editingKey.allowedSessions ?? [])) {+ setEditingKey(null);+ return;+ }try {await updateMutation.mutateAsync({id: editingKey.id,diff --git a/dashboard/src/utils/sessionScope.test.ts b/dashboard/src/utils/sessionScope.test.tsindex f69eb28ea..b1b009f25 100644--- a/dashboard/src/utils/sessionScope.test.ts+++ b/dashboard/src/utils/sessionScope.test.ts@@ -1,6 +1,12 @@import { test } from 'node:test';import assert from 'node:assert/strict';-import { canScopeSessions, sessionPickerStartsExpanded, sessionScopeNames } from './sessionScope.ts';+import {+ canScopeSessions,+ sameSessionScope,+ sessionPickerStartsExpanded,+ sessionScopeNames,+ sessionScopeRows,+} from './sessionScope.ts';test('only operator and viewer keys can be session-scoped in the dashboard', () => {assert.equal(canScopeSessions('operator'), true);@@ -31,3 +37,30 @@ test('a selected allowlist resolves to session names, falling back to the id', (];assert.deepEqual(sessionScopeNames(['b', 'missing'], sessions), ['Support', 'missing']);});++test('an unchanged selection is recognised so the save is skipped, and a reorder is not a change', () => {+ assert.equal(sameSessionScope([], []), true);+ assert.equal(sameSessionScope(['a', 'b'], ['b', 'a']), true);+ assert.equal(sameSessionScope(['a'], []), false);+ assert.equal(sameSessionScope([], ['a']), false);+ assert.equal(sameSessionScope(['a', 'b'], ['a', 'c']), false);+});++test('the picker lists a selected id whose session is gone, so it can be unticked', () => {+ const sessions = [+ { id: 'a', name: 'Sales' },+ { id: 'b', name: 'Support' },+ ];+ assert.deepEqual(sessionScopeRows(sessions, []), [+ { id: 'a', session: sessions[0] },+ { id: 'b', session: sessions[1] },+ ]);+ assert.deepEqual(sessionScopeRows(sessions, ['b', 'deleted', 'deleted']), [+ { id: 'a', session: sessions[0] },+ { id: 'b', session: sessions[1] },+ { id: 'deleted' },+ ]);+ // Every session deleted but the allowlist still names one: the row survives, so the empty state+ // does not swallow the only control that can clear it.+ assert.deepEqual(sessionScopeRows([], ['deleted']), [{ id: 'deleted' }]);+});diff --git a/dashboard/src/utils/sessionScope.ts b/dashboard/src/utils/sessionScope.tsindex 68bb54b04..8275d97e7 100644--- a/dashboard/src/utils/sessionScope.ts+++ b/dashboard/src/utils/sessionScope.ts@@ -12,6 +12,35 @@ export function sessionPickerStartsExpanded(selectedIds: readonly string[]): booreturn selectedIds.length > 0;}+/**+ * Membership comparison, so an unchanged Save is not sent at all. `PUT /auth/api-keys/:id` writes+ * `allowedSessions` whenever the field is present, and an empty array is not the same stored value+ * as "never scoped": the server reads the write back as an authorization change and disconnects+ * every live `/events` socket holding that key.+ */+export function sameSessionScope(a: readonly string[], b: readonly string[]): boolean {+ if (a.length !== b.length) return false;+ const left = [...a].sort();+ const right = [...b].sort();+ return left.every((id, index) => id === right[index]);+}++/**+ * The picker's rows: every live session, then any selected id that no longer resolves to one.+ *+ * Nothing prunes a key's allowlist when its session is deleted, so an orphaned id is a steady state,+ * not an edge case. Listing only live sessions would leave it invisible and therefore impossible to+ * untick, and it would be re-persisted on every save.+ */+export function sessionScopeRows<T extends { id: string }>(+ sessions: readonly T[],+ selectedIds: readonly string[],+): Array<{ id: string; session?: T }> {+ const live = new Set(sessions.map(session => session.id));+ const orphans = [...new Set(selectedIds)].filter(id => !live.has(id));+ return [...sessions.map(session => ({ id: session.id, session })), ...orphans.map(id => ({ id }))];+}+export function sessionScopeNames(allowedSessions: string[] | undefined | null,sessions: ReadonlyArray<{ id: string; name: string }>,