Skip to content

Commit 4993b54

Browse files
committed
fix(slack): route directory cursor pagination through the shared guard
listSlackDirectoryPeersLive/listSlackDirectoryGroupsLive hand-rolled do/while loops with no repeated-cursor detection and no page bound, while every other users.list/conversations.list consumer goes through collectSlackCursorPages. A Slack API or proxy edge that keeps returning the same non-empty next_cursor made directory queries paginate forever, growing the member/channel arrays without bound. Both loops now go through collectSlackCursorPages, which throws on a repeated cursor and caps total pages.
1 parent 53ae576 commit 4993b54

2 files changed

Lines changed: 84 additions & 30 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Slack tests cover directory live behavior.
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import { listSlackDirectoryGroupsLive, listSlackDirectoryPeersLive } from "./directory-live.js";
4+
5+
const slackClientMocks = vi.hoisted(() => ({
6+
createSlackLookupClient: vi.fn(),
7+
usersList: vi.fn(),
8+
conversationsList: vi.fn(),
9+
}));
10+
11+
vi.mock("./client.js", () => ({
12+
createSlackLookupClient: slackClientMocks.createSlackLookupClient,
13+
}));
14+
15+
const params = { cfg: { channels: { slack: { botToken: "xoxb-test" } } } };
16+
17+
describe("slack directory live cursor pagination", () => {
18+
beforeEach(() => {
19+
slackClientMocks.usersList.mockReset();
20+
slackClientMocks.conversationsList.mockReset();
21+
slackClientMocks.createSlackLookupClient.mockReset().mockReturnValue({
22+
users: { list: slackClientMocks.usersList },
23+
conversations: { list: slackClientMocks.conversationsList },
24+
});
25+
});
26+
27+
it("lists peers across advancing cursors", async () => {
28+
slackClientMocks.usersList
29+
.mockResolvedValueOnce({
30+
members: [{ id: "U1", name: "one" }],
31+
response_metadata: { next_cursor: "cursor-1" },
32+
})
33+
.mockResolvedValueOnce({
34+
members: [{ id: "U2", name: "two" }],
35+
response_metadata: { next_cursor: "" },
36+
});
37+
38+
const rows = await listSlackDirectoryPeersLive(params);
39+
40+
expect(rows.map((row) => row.id)).toEqual(["user:U1", "user:U2"]);
41+
expect(slackClientMocks.usersList).toHaveBeenCalledTimes(2);
42+
});
43+
44+
it("rejects a repeated users.list cursor instead of paginating forever", async () => {
45+
slackClientMocks.usersList.mockResolvedValue({
46+
members: [],
47+
response_metadata: { next_cursor: "cursor-loop" },
48+
});
49+
50+
await expect(listSlackDirectoryPeersLive(params)).rejects.toThrow(
51+
"Slack cursor pagination repeated a cursor",
52+
);
53+
expect(slackClientMocks.usersList).toHaveBeenCalledTimes(2);
54+
});
55+
56+
it("rejects a repeated conversations.list cursor instead of paginating forever", async () => {
57+
slackClientMocks.conversationsList.mockResolvedValue({
58+
channels: [],
59+
response_metadata: { next_cursor: "cursor-loop" },
60+
});
61+
62+
await expect(listSlackDirectoryGroupsLive(params)).rejects.toThrow(
63+
"Slack cursor pagination repeated a cursor",
64+
);
65+
expect(slackClientMocks.conversationsList).toHaveBeenCalledTimes(2);
66+
});
67+
});

‎extensions/slack/src/directory-live.ts‎

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from "openclaw/plugin-sdk/string-coerce-runtime";
1212
import { resolveSlackAccount } from "./accounts.js";
1313
import { createSlackLookupClient } from "./client.js";
14+
import { collectSlackCursorPages } from "./cursor-pages.js";
1415

1516
type SlackUser = NonNullable<UsersListResponse["members"]>[number];
1617
type SlackChannel = NonNullable<ConversationsListResponse["channels"]>[number];
@@ -95,20 +96,12 @@ export async function listSlackDirectoryPeersLive(
9596
return [];
9697
}
9798
const query = normalizeQuery(params.query);
98-
const members: SlackUser[] = [];
99-
let cursor: string | undefined;
100-
101-
do {
102-
const res = await client.users.list({
103-
limit: 200,
104-
cursor,
105-
});
106-
if (Array.isArray(res.members)) {
107-
members.push(...res.members);
108-
}
109-
const next = res.response_metadata?.next_cursor?.trim();
110-
cursor = next ? next : undefined;
111-
} while (cursor);
99+
// Route through the shared cursor guard: a repeated or endless next_cursor
100+
// (buggy proxy or Slack edge case) must fail instead of paginating forever.
101+
const members = await collectSlackCursorPages({
102+
fetchPage: (cursor) => client.users.list({ limit: 200, cursor }),
103+
collectPageItems: (res) => (Array.isArray(res.members) ? res.members : []),
104+
});
112105

113106
const filtered = members.filter((member) => {
114107
const name = member.profile?.display_name || member.profile?.real_name || member.real_name;
@@ -141,22 +134,16 @@ export async function listSlackDirectoryGroupsLive(
141134
return [];
142135
}
143136
const query = normalizeQuery(params.query);
144-
const channels: SlackChannel[] = [];
145-
let cursor: string | undefined;
146-
147-
do {
148-
const res = await client.conversations.list({
149-
types: "public_channel,private_channel",
150-
exclude_archived: false,
151-
limit: 1000,
152-
cursor,
153-
});
154-
if (Array.isArray(res.channels)) {
155-
channels.push(...res.channels);
156-
}
157-
const next = res.response_metadata?.next_cursor?.trim();
158-
cursor = next ? next : undefined;
159-
} while (cursor);
137+
const channels = await collectSlackCursorPages({
138+
fetchPage: (cursor) =>
139+
client.conversations.list({
140+
types: "public_channel,private_channel",
141+
exclude_archived: false,
142+
limit: 1000,
143+
cursor,
144+
}),
145+
collectPageItems: (res) => (Array.isArray(res.channels) ? res.channels : []),
146+
});
160147

161148
const filtered = channels.filter((channel) => {
162149
const name = normalizeOptionalLowercaseString(channel.name);

0 commit comments

Comments
 (0)