Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): stop a throttled page walk once the key is signed out (#1852)

Yudhi Armyndharisfff7245ad9d9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 39e0d2add..d199b3731 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- Stop a dashboard Logs export from downloading rows after sign-out when its last page was throttled.
- Stop reopening a just-saved dashboard template from loading its old values, which a second save wrote back.
- Stop an operator webhook redrive at its next row on shutdown.
- Run a plugin's `onDisable` once when shutdown, uninstall or an update overlaps a disable already in progress.
diff --git a/dashboard/src/pages/Chats.test.ts b/dashboard/src/pages/Chats.test.ts
index 508305712..8b62f3044 100644
--- a/dashboard/src/pages/Chats.test.ts
+++ b/dashboard/src/pages/Chats.test.ts
@@ -1076,12 +1076,16 @@ test('a reconnect refetch that settles after the page was left sends no mark-as-
async function leaveWithPendingRead(loggedOut: boolean): Promise<number> {
const { screen, fireEvent, within } = rtl;
+ resetFetchCalls();
const { container, unmount } = renderChats();
await screen.findByText('Main (15551234567)');
fireEvent.click(await screen.findByText('Alice'));
await within(container.querySelector('.room-messages') as HTMLElement).findByText('hello from alice');
// Opening the chat queued its read behind the quiet window; the page goes away inside it.
- resetFetchCalls();
+ assert.ok(
+ !findFetchCall('POST', `/api/sessions/${SESSION.id}/chats/read`),
+ 'the read went out before the page was left',
+ );
// Logout removes the key before the authenticated tree unmounts.
if (loggedOut) window.sessionStorage.removeItem('openwa_api_key');
try {
@@ -1212,9 +1216,12 @@ test('switching session with a chat open marks only the chats opened on the new
Promise.resolve(
jsonResponse(sessionId === SESSION.id ? [CHAT] : [{ ...CHAT, lastMessage: 'alice on two' }, CHAT_2]),
);
+ const readsOnOne: unknown[] = [];
const readsOnTwo: unknown[] = [];
const stub = globalThis.fetch;
globalThis.fetch = ((input: RequestInfo | URL, init?: RequestInit) => {
+ if (String(input).endsWith(`/api/sessions/${SESSION.id}/chats/read`))
+ readsOnOne.push(JSON.parse(String(init?.body)));
if (String(input).endsWith(`/api/sessions/${SESSION_2.id}/chats/read`))
readsOnTwo.push(JSON.parse(String(init?.body)));
return stub(input, init);
@@ -1237,6 +1244,8 @@ test('switching session with a chat open marks only the chats opened on the new
await flush();
await flush();
assert.deepEqual(readsOnTwo, [{ chatId: CHAT_2.id }]);
+ // The read queued for the session that was left still reaches that session.
+ assert.deepEqual(readsOnOne, [{ chatId: CHAT.id }]);
} finally {
globalThis.fetch = stub;
twoSessions = false;
diff --git a/dashboard/src/utils/fetchAllPages.test.ts b/dashboard/src/utils/fetchAllPages.test.ts
index 55e36b245..d5f8deacd 100644
--- a/dashboard/src/utils/fetchAllPages.test.ts
+++ b/dashboard/src/utils/fetchAllPages.test.ts
@@ -184,6 +184,7 @@ test('a walk stops once the key it started with is signed out or replaced', asyn
},
{ retryDelayMs: 0 },
),
+ /Signed out/,
);
assert.deepEqual(sentWith, ['key-a', 'key-a']);
@@ -196,6 +197,7 @@ test('a walk stops once the key it started with is signed out or replaced', asyn
if (offset === 200) sessionStorage.setItem('openwa_api_key', 'key-b');
return page;
}),
+ /Signed out/,
);
assert.deepEqual(sentWith, ['key-a', 'key-a']);
@@ -207,5 +209,23 @@ test('a walk stops once the key it started with is signed out or replaced', asyn
if (offset === 800) sessionStorage.removeItem('openwa_api_key');
return page;
}),
+ /Signed out/,
);
+
+ // Signed out while the last retry of a throttled page was out: the rows in hand do not come back
+ // as a throttled partial walk.
+ sessionStorage.setItem('openwa_api_key', 'key-a');
+ let refused = 0;
+ await assert.rejects(
+ fetchAllPages(
+ async (limit, offset) => {
+ if (offset === 0) return fetchPage(limit, offset);
+ if (++refused === 3) sessionStorage.removeItem('openwa_api_key');
+ throw httpError(429);
+ },
+ { retryDelayMs: 0 },
+ ),
+ /Signed out/,
+ );
+ assert.equal(refused, 3, 'the walk did not reach the last retry');
});
diff --git a/dashboard/src/utils/fetchAllPages.ts b/dashboard/src/utils/fetchAllPages.ts
index 3d575069b..7d9d888a3 100644
--- a/dashboard/src/utils/fetchAllPages.ts
+++ b/dashboard/src/utils/fetchAllPages.ts
@@ -49,13 +49,19 @@ export async function fetchAllPages<T>(
let offset = 0;
// Logout cancels nothing in flight. A walk that outlived its key would send the rest of its pages
// keyless (a 401 that reloads the login form) or as the next sign-in's key, mixing two actors'
- // rows, and a page answered after logout would still finish it. Stop once the key has changed.
+ // rows, and a page answered after logout would still finish it, even one refused with a 429 that
+ // ends the walk early with the rows in hand. Stop once the key has changed, however the page ended.
const key = sessionStorage.getItem('openwa_api_key');
- const fetchOnce = async () => {
- if (sessionStorage.getItem('openwa_api_key') !== key) throw new Error('Signed out');
- const page = await fetchPage(pageSize, offset);
+ const assertSameKey = () => {
if (sessionStorage.getItem('openwa_api_key') !== key) throw new Error('Signed out');
- return page;
+ };
+ const fetchOnce = async () => {
+ assertSameKey();
+ try {
+ return await fetchPage(pageSize, offset);
+ } finally {
+ assertSameKey();
+ }
};
for (;;) {
let page: Page<T>;