Skip to content

Commit 56e97da

Browse files
mmapspgondhi987
andauthored
fix(qqbot): limit channel API targets to configured groups [AI] (#101765)
* fix(qqbot): scope channel API targets to configured groups * fix(qqbot): fail closed scoped channel API resources --------- Co-authored-by: Pavan Kumar Gondhi <[email protected]>
1 parent 57f72bc commit 56e97da

3 files changed

Lines changed: 188 additions & 1 deletion

File tree

‎extensions/qqbot/src/bridge/tools/channel.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ export function registerChannelTool(api: OpenClawPluginApi): void {
5252
async execute(_toolCallId, params) {
5353
const { getAccessToken } = await import("../../engine/messaging/sender.js");
5454
const accessToken = await getAccessToken(account.appId, account.clientSecret);
55-
return executeChannelApi(params as ChannelApiParams, { accessToken });
55+
return executeChannelApi(params as ChannelApiParams, {
56+
accessToken,
57+
cfg,
58+
accountId: firstAccountId,
59+
});
5660
},
5761
},
5862
{ name: "qqbot_channel_api" },

‎extensions/qqbot/src/engine/tools/channel-api.test.ts‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Qqbot tests cover channel-api tool behavior.
2+
3+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
24
import { afterEach, describe, expect, it, vi } from "vitest";
35
import { createStreamingResponse } from "../../../../test-support/streaming-error-response.js";
46

@@ -14,6 +16,10 @@ vi.mock("openclaw/plugin-sdk/ssrf-runtime", async (importOriginal) => {
1416

1517
import { executeChannelApi } from "./channel-api.js";
1618

19+
function qqbotCfg(qqbot: Record<string, unknown>): OpenClawConfig {
20+
return { channels: { qqbot } } as OpenClawConfig;
21+
}
22+
1723
function cancelTrackedResponse(
1824
text: string,
1925
init: ResponseInit,
@@ -79,6 +85,110 @@ describe("executeChannelApi", () => {
7985
});
8086
});
8187

88+
it("blocks guild listing when qqbot groups are scoped", async () => {
89+
const result = await executeChannelApi(
90+
{ method: "GET", path: "/users/@me/guilds" },
91+
{
92+
accessToken: "token-1",
93+
cfg: qqbotCfg({ groups: { G1: {} } }),
94+
},
95+
);
96+
97+
expect(result.details).toEqual({
98+
error: "QQ channel API guild listing is unavailable while qqbot groups are scoped.",
99+
path: "/users/@me/guilds",
100+
});
101+
expect(fetchWithSsrFGuardMock).not.toHaveBeenCalled();
102+
});
103+
104+
it("blocks guild paths when qqbot groups are scoped", async () => {
105+
const result = await executeChannelApi(
106+
{ method: "GET", path: "/guilds/G1/channels" },
107+
{
108+
accessToken: "token-1",
109+
cfg: qqbotCfg({ groups: { G1: {} } }),
110+
},
111+
);
112+
113+
expect(result.details).toEqual({
114+
error: "QQ channel API guild paths are unavailable while qqbot groups are scoped.",
115+
path: "/guilds/G1/channels",
116+
});
117+
expect(fetchWithSsrFGuardMock).not.toHaveBeenCalled();
118+
});
119+
120+
it("blocks channel paths when qqbot groups are scoped", async () => {
121+
const result = await executeChannelApi(
122+
{ method: "GET", path: "/channels/C1/threads" },
123+
{
124+
accessToken: "token-1",
125+
cfg: qqbotCfg({ groups: { C1: {} } }),
126+
},
127+
);
128+
129+
expect(result.details).toEqual({
130+
error: "QQ channel API channel paths are unavailable while qqbot groups are scoped.",
131+
path: "/channels/C1/threads",
132+
});
133+
expect(fetchWithSsrFGuardMock).not.toHaveBeenCalled();
134+
});
135+
136+
it("allows guild paths with wildcard qqbot groups", async () => {
137+
const release = vi.fn(async () => {});
138+
fetchWithSsrFGuardMock.mockResolvedValueOnce({
139+
response: new Response(JSON.stringify({ id: "channel-1" }), { status: 200 }),
140+
release,
141+
});
142+
143+
const result = await executeChannelApi(
144+
{ method: "GET", path: "/guilds/G1/channels" },
145+
{
146+
accessToken: "token-1",
147+
cfg: qqbotCfg({ groups: { "*": {} } }),
148+
},
149+
);
150+
151+
expect(result.details).toMatchObject({
152+
success: true,
153+
status: 200,
154+
path: "/guilds/G1/channels",
155+
});
156+
expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith(
157+
expect.objectContaining({
158+
url: "https://api.sgroup.qq.com/guilds/G1/channels",
159+
}),
160+
);
161+
expect(release).toHaveBeenCalledTimes(1);
162+
});
163+
164+
it("allows global guild listing with wildcard qqbot groups", async () => {
165+
const release = vi.fn(async () => {});
166+
fetchWithSsrFGuardMock.mockResolvedValueOnce({
167+
response: new Response(JSON.stringify([{ id: "guild-1" }]), { status: 200 }),
168+
release,
169+
});
170+
171+
const result = await executeChannelApi(
172+
{ method: "GET", path: "/users/@me/guilds" },
173+
{
174+
accessToken: "token-1",
175+
cfg: qqbotCfg({ groups: { "*": {} } }),
176+
},
177+
);
178+
179+
expect(result.details).toMatchObject({
180+
success: true,
181+
status: 200,
182+
path: "/users/@me/guilds",
183+
});
184+
expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith(
185+
expect.objectContaining({
186+
url: "https://api.sgroup.qq.com/users/@me/guilds",
187+
}),
188+
);
189+
expect(release).toHaveBeenCalledTimes(1);
190+
});
191+
82192
it("bounds error bodies without using response.text()", async () => {
83193
const release = vi.fn(async () => {});
84194
const tracked = cancelTrackedResponse(`${"channel api unavailable ".repeat(1024)}tail`, {

‎extensions/qqbot/src/engine/tools/channel-api.ts‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* validation, fetch, and structured response formatting.
99
*/
1010

11+
import { resolveChannelGroupPolicy } from "openclaw/plugin-sdk/channel-policy";
12+
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
1113
import {
1214
readProviderTextResponse,
1315
readResponseTextLimited,
@@ -152,6 +154,70 @@ function decodePathSegments(path: string): string[] | null {
152154
}
153155
}
154156

157+
type ChannelApiPathTarget =
158+
| { kind: "guild-list" }
159+
| { kind: "guild"; id: string }
160+
| { kind: "channel"; id: string }
161+
| { kind: "unverified" };
162+
163+
function resolvePathTarget(path: string): ChannelApiPathTarget {
164+
const segments = decodePathSegments(path);
165+
if (!segments || segments.length === 0) {
166+
return { kind: "unverified" };
167+
}
168+
169+
const [scope, firstId, second] = segments;
170+
if (
171+
scope?.toLowerCase() === "users" &&
172+
firstId?.toLowerCase() === "@me" &&
173+
second?.toLowerCase() === "guilds"
174+
) {
175+
return { kind: "guild-list" };
176+
}
177+
if (scope?.toLowerCase() === "guilds" && firstId) {
178+
return { kind: "guild", id: firstId };
179+
}
180+
if (scope?.toLowerCase() === "channels" && firstId) {
181+
return { kind: "channel", id: firstId };
182+
}
183+
return { kind: "unverified" };
184+
}
185+
186+
function validateConfiguredTargetScope(
187+
path: string,
188+
options: ChannelApiExecuteOptions,
189+
): string | null {
190+
if (!options.cfg) {
191+
return null;
192+
}
193+
194+
const basePolicy = resolveChannelGroupPolicy({
195+
cfg: options.cfg,
196+
channel: "qqbot",
197+
accountId: options.accountId,
198+
groupIdCaseInsensitive: true,
199+
});
200+
if (!basePolicy.allowlistEnabled && basePolicy.allowed) {
201+
return null;
202+
}
203+
204+
const target = resolvePathTarget(path);
205+
if (target.kind === "guild-list") {
206+
return basePolicy.allowed
207+
? null
208+
: "QQ channel API guild listing is unavailable while qqbot groups are scoped.";
209+
}
210+
if (target.kind === "unverified") {
211+
return basePolicy.allowed
212+
? null
213+
: "QQ channel API path target cannot be verified against configured qqbot groups.";
214+
}
215+
216+
return basePolicy.allowed
217+
? null
218+
: `QQ channel API ${target.kind} paths are unavailable while qqbot groups are scoped.`;
219+
}
220+
155221
function isBulkAnnouncementDeletePath(path: string): boolean {
156222
const segments = decodePathSegments(path);
157223
return Boolean(
@@ -182,6 +248,8 @@ function validateDeleteConfirmation(params: ChannelApiParams): string | null {
182248
*/
183249
interface ChannelApiExecuteOptions {
184250
accessToken: string;
251+
cfg?: OpenClawConfig;
252+
accountId?: string | null;
185253
}
186254

187255
/**
@@ -215,6 +283,11 @@ export async function executeChannelApi(
215283
return json({ error: pathError });
216284
}
217285

286+
const scopeError = validateConfiguredTargetScope(params.path, options);
287+
if (scopeError) {
288+
return json({ error: scopeError, path: params.path });
289+
}
290+
218291
const confirmationError = validateDeleteConfirmation({ ...params, method });
219292
if (confirmationError) {
220293
return json({ error: confirmationError, path: params.path });

0 commit comments

Comments
 (0)