Skip to content

Commit 85ac519

Browse files
authored
fix(zalo): reject failed probe responses (#124593)
1 parent 81d4436 commit 85ac519

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

‎extensions/zalo/src/api.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
*/
55

66
import { resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
7-
import { readProviderJsonResponse } from "openclaw/plugin-sdk/provider-http";
7+
import {
8+
assertOkOrThrowProviderError,
9+
readProviderJsonResponse,
10+
} from "openclaw/plugin-sdk/provider-http";
811
import { resolvePinnedHostnameWithPolicy, type SsrFPolicy } from "openclaw/plugin-sdk/ssrf-runtime";
912
import { ZALO_DEFAULT_REQUEST_TIMEOUT_MS, ZALO_SEND_PHOTO_REQUEST_TIMEOUT_MS } from "./timeouts.js";
1013

@@ -155,6 +158,7 @@ export async function callZaloApi<T = unknown>(
155158
signal: controller.signal,
156159
});
157160

161+
await assertOkOrThrowProviderError(response, `zalo.${method}`);
158162
const data = await readProviderJsonResponse<ZaloApiResponse<T>>(response, `zalo.${method}`);
159163

160164
if (!data.ok) {

‎extensions/zalo/src/probe.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createServer } from "node:http";
12
import { describe, expect, it, vi } from "vitest";
23
import type { ZaloFetch } from "./api.js";
34
import { probeZalo } from "./probe.js";
@@ -31,6 +32,53 @@ describe("probeZalo", () => {
3132
});
3233
});
3334

35+
it("rejects a non-ok HTTP response with a success-shaped body", async () => {
36+
const server = createServer((_request, response) => {
37+
response.writeHead(401, { "content-type": "application/json" });
38+
response.end(
39+
JSON.stringify({
40+
ok: true,
41+
result: {
42+
id: "controlled-bot",
43+
account_name: "Controlled Bot",
44+
account_type: "BOT",
45+
can_join_groups: false,
46+
},
47+
}),
48+
);
49+
});
50+
await new Promise<void>((resolve, reject) => {
51+
server.once("error", reject);
52+
server.listen(0, "127.0.0.1", () => {
53+
server.off("error", reject);
54+
resolve();
55+
});
56+
});
57+
const address = server.address();
58+
if (!address || typeof address === "string") {
59+
throw new Error("expected loopback Zalo API address");
60+
}
61+
const originalApiUrl = process.env.ZALO_API_URL;
62+
process.env.ZALO_API_URL = `http://127.0.0.1:${String(address.port)}`;
63+
64+
try {
65+
await expect(probeZalo("controlled-token", 1000)).resolves.toMatchObject({
66+
ok: false,
67+
error: expect.stringContaining("zalo.getMe (401)"),
68+
});
69+
} finally {
70+
if (originalApiUrl === undefined) {
71+
delete process.env.ZALO_API_URL;
72+
} else {
73+
process.env.ZALO_API_URL = originalApiUrl;
74+
}
75+
server.closeAllConnections();
76+
await new Promise<void>((resolve, reject) => {
77+
server.close((error) => (error ? reject(error) : resolve()));
78+
});
79+
}
80+
});
81+
3482
it("preserves timeout errors", async () => {
3583
const fetcher = vi.fn<ZaloFetch>(async () => {
3684
throw new DOMException("aborted", "AbortError");

0 commit comments

Comments
 (0)