@openclaw/zalo 2026.5.2 → 2026.5.3-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/accounts-9NLDDlZ8.js +118 -0
- package/dist/actions.runtime-kJ65ZxW7.js +5 -0
- package/dist/api.js +5 -0
- package/dist/channel-VPbtV3Oq.js +343 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-BnTAWQx5.js +106 -0
- package/dist/contract-api.js +3 -0
- package/dist/group-access-DZR43lOR.js +30 -0
- package/dist/index.js +22 -0
- package/dist/monitor-DMysJBWa.js +823 -0
- package/dist/monitor.webhook-DqnuvgjV.js +175 -0
- package/dist/proxy-CY8VuC6H.js +135 -0
- package/dist/runtime-BRFxnYQx.js +8 -0
- package/dist/runtime-api-MOTmRW4F.js +19 -0
- package/dist/runtime-api.js +3 -0
- package/dist/secret-contract-Dw93tGo2.js +87 -0
- package/dist/secret-contract-api.js +2 -0
- package/dist/send-Gv3l5EGI.js +101 -0
- package/dist/setup-api.js +30 -0
- package/dist/setup-core-DigRD3j1.js +166 -0
- package/dist/setup-entry.js +15 -0
- package/dist/setup-surface-2Up3yWov.js +216 -0
- package/dist/test-api.js +2 -0
- package/package.json +15 -6
- package/api.ts +0 -9
- package/channel-plugin-api.ts +0 -1
- package/contract-api.ts +0 -5
- package/index.test.ts +0 -15
- package/index.ts +0 -20
- package/runtime-api.test.ts +0 -17
- package/runtime-api.ts +0 -75
- package/secret-contract-api.ts +0 -5
- package/setup-api.ts +0 -34
- package/setup-entry.ts +0 -13
- package/src/accounts.test.ts +0 -70
- package/src/accounts.ts +0 -60
- package/src/actions.runtime.ts +0 -5
- package/src/actions.test.ts +0 -32
- package/src/actions.ts +0 -62
- package/src/api.test.ts +0 -149
- package/src/api.ts +0 -265
- package/src/approval-auth.test.ts +0 -17
- package/src/approval-auth.ts +0 -25
- package/src/channel.directory.test.ts +0 -59
- package/src/channel.runtime.ts +0 -93
- package/src/channel.startup.test.ts +0 -101
- package/src/channel.ts +0 -275
- package/src/config-schema.test.ts +0 -30
- package/src/config-schema.ts +0 -29
- package/src/group-access.ts +0 -49
- package/src/monitor.group-policy.test.ts +0 -94
- package/src/monitor.image.polling.test.ts +0 -110
- package/src/monitor.lifecycle.test.ts +0 -198
- package/src/monitor.pairing.lifecycle.test.ts +0 -141
- package/src/monitor.polling.media-reply.test.ts +0 -425
- package/src/monitor.reply-once.lifecycle.test.ts +0 -171
- package/src/monitor.ts +0 -1028
- package/src/monitor.types.ts +0 -4
- package/src/monitor.webhook.test.ts +0 -806
- package/src/monitor.webhook.ts +0 -278
- package/src/outbound-media.test.ts +0 -182
- package/src/outbound-media.ts +0 -241
- package/src/outbound-payload.contract.test.ts +0 -45
- package/src/probe.ts +0 -45
- package/src/proxy.ts +0 -24
- package/src/runtime-api.ts +0 -75
- package/src/runtime-support.ts +0 -91
- package/src/runtime.ts +0 -9
- package/src/secret-contract.ts +0 -109
- package/src/secret-input.ts +0 -5
- package/src/send.test.ts +0 -120
- package/src/send.ts +0 -153
- package/src/session-route.ts +0 -32
- package/src/setup-allow-from.ts +0 -94
- package/src/setup-core.ts +0 -149
- package/src/setup-status.test.ts +0 -33
- package/src/setup-surface.test.ts +0 -175
- package/src/setup-surface.ts +0 -291
- package/src/status-issues.test.ts +0 -17
- package/src/status-issues.ts +0 -37
- package/src/test-support/lifecycle-test-support.ts +0 -413
- package/src/test-support/monitor-mocks-test-support.ts +0 -209
- package/src/token.test.ts +0 -92
- package/src/token.ts +0 -79
- package/src/types.ts +0 -50
- package/test-api.ts +0 -1
- package/tsconfig.json +0 -16
package/src/monitor.webhook.ts
DELETED
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
-
import { createClaimableDedupe } from "openclaw/plugin-sdk/persistent-dedupe";
|
|
3
|
-
import { safeEqualSecret } from "openclaw/plugin-sdk/security-runtime";
|
|
4
|
-
import type { ResolvedZaloAccount } from "./accounts.js";
|
|
5
|
-
import type { ZaloFetch, ZaloUpdate } from "./api.js";
|
|
6
|
-
import type { ZaloRuntimeEnv } from "./monitor.types.js";
|
|
7
|
-
import {
|
|
8
|
-
createFixedWindowRateLimiter,
|
|
9
|
-
createWebhookAnomalyTracker,
|
|
10
|
-
readJsonWebhookBodyOrReject,
|
|
11
|
-
applyBasicWebhookRequestGuards,
|
|
12
|
-
registerWebhookTargetWithPluginRoute,
|
|
13
|
-
type RegisterWebhookTargetOptions,
|
|
14
|
-
type RegisterWebhookPluginRouteOptions,
|
|
15
|
-
registerWebhookTarget,
|
|
16
|
-
resolveWebhookTargetWithAuthOrRejectSync,
|
|
17
|
-
withResolvedWebhookRequestPipeline,
|
|
18
|
-
WEBHOOK_ANOMALY_COUNTER_DEFAULTS,
|
|
19
|
-
WEBHOOK_RATE_LIMIT_DEFAULTS,
|
|
20
|
-
resolveClientIp,
|
|
21
|
-
type OpenClawConfig,
|
|
22
|
-
} from "./runtime-api.js";
|
|
23
|
-
|
|
24
|
-
const ZALO_WEBHOOK_REPLAY_WINDOW_MS = 5 * 60_000;
|
|
25
|
-
|
|
26
|
-
export type ZaloWebhookTarget = {
|
|
27
|
-
token: string;
|
|
28
|
-
account: ResolvedZaloAccount;
|
|
29
|
-
config: OpenClawConfig;
|
|
30
|
-
runtime: ZaloRuntimeEnv;
|
|
31
|
-
core: unknown;
|
|
32
|
-
secret: string;
|
|
33
|
-
path: string;
|
|
34
|
-
webhookUrl: string;
|
|
35
|
-
webhookPath: string;
|
|
36
|
-
mediaMaxMb: number;
|
|
37
|
-
canHostMedia: boolean;
|
|
38
|
-
statusSink?: (patch: { lastInboundAt?: number; lastOutboundAt?: number }) => void;
|
|
39
|
-
fetcher?: ZaloFetch;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export type ZaloWebhookProcessUpdate = (params: {
|
|
43
|
-
update: ZaloUpdate;
|
|
44
|
-
target: ZaloWebhookTarget;
|
|
45
|
-
}) => Promise<void>;
|
|
46
|
-
|
|
47
|
-
const webhookTargets = new Map<string, ZaloWebhookTarget[]>();
|
|
48
|
-
const webhookRateLimiter = createFixedWindowRateLimiter({
|
|
49
|
-
windowMs: WEBHOOK_RATE_LIMIT_DEFAULTS.windowMs,
|
|
50
|
-
maxRequests: WEBHOOK_RATE_LIMIT_DEFAULTS.maxRequests,
|
|
51
|
-
maxTrackedKeys: WEBHOOK_RATE_LIMIT_DEFAULTS.maxTrackedKeys,
|
|
52
|
-
});
|
|
53
|
-
const recentWebhookEvents = createClaimableDedupe({
|
|
54
|
-
ttlMs: ZALO_WEBHOOK_REPLAY_WINDOW_MS,
|
|
55
|
-
memoryMaxSize: 5000,
|
|
56
|
-
});
|
|
57
|
-
const webhookAnomalyTracker = createWebhookAnomalyTracker({
|
|
58
|
-
maxTrackedKeys: WEBHOOK_ANOMALY_COUNTER_DEFAULTS.maxTrackedKeys,
|
|
59
|
-
ttlMs: WEBHOOK_ANOMALY_COUNTER_DEFAULTS.ttlMs,
|
|
60
|
-
logEvery: WEBHOOK_ANOMALY_COUNTER_DEFAULTS.logEvery,
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
export function clearZaloWebhookSecurityStateForTest(): void {
|
|
64
|
-
webhookRateLimiter.clear();
|
|
65
|
-
recentWebhookEvents.clearMemory();
|
|
66
|
-
webhookAnomalyTracker.clear();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function getZaloWebhookRateLimitStateSizeForTest(): number {
|
|
70
|
-
return webhookRateLimiter.size();
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export function getZaloWebhookStatusCounterSizeForTest(): number {
|
|
74
|
-
return webhookAnomalyTracker.size();
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function timingSafeEquals(left: string, right: string): boolean {
|
|
78
|
-
return safeEqualSecret(left, right);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function buildReplayEventCacheKey(target: ZaloWebhookTarget, update: ZaloUpdate): string | null {
|
|
82
|
-
const messageId = update.message?.message_id;
|
|
83
|
-
if (!messageId) {
|
|
84
|
-
return null;
|
|
85
|
-
}
|
|
86
|
-
const chatId = update.message?.chat?.id ?? "";
|
|
87
|
-
const senderId = update.message?.from?.id ?? "";
|
|
88
|
-
return JSON.stringify([
|
|
89
|
-
target.path,
|
|
90
|
-
target.account.accountId,
|
|
91
|
-
update.event_name,
|
|
92
|
-
chatId,
|
|
93
|
-
senderId,
|
|
94
|
-
messageId,
|
|
95
|
-
]);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export class ZaloRetryableWebhookError extends Error {
|
|
99
|
-
constructor(message: string, options?: ErrorOptions) {
|
|
100
|
-
super(message, options);
|
|
101
|
-
this.name = "ZaloRetryableWebhookError";
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export async function processZaloReplayGuardedUpdate(params: {
|
|
106
|
-
target: ZaloWebhookTarget;
|
|
107
|
-
update: ZaloUpdate;
|
|
108
|
-
processUpdate: ZaloWebhookProcessUpdate;
|
|
109
|
-
nowMs?: number;
|
|
110
|
-
}): Promise<"processed" | "duplicate"> {
|
|
111
|
-
const replayEventKey = buildReplayEventCacheKey(params.target, params.update);
|
|
112
|
-
if (replayEventKey) {
|
|
113
|
-
const replayClaim = await recentWebhookEvents.claim(replayEventKey, { now: params.nowMs });
|
|
114
|
-
if (replayClaim.kind !== "claimed") {
|
|
115
|
-
return "duplicate";
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
params.target.statusSink?.({ lastInboundAt: Date.now() });
|
|
120
|
-
try {
|
|
121
|
-
await params.processUpdate({ update: params.update, target: params.target });
|
|
122
|
-
if (replayEventKey) {
|
|
123
|
-
await recentWebhookEvents.commit(replayEventKey);
|
|
124
|
-
}
|
|
125
|
-
return "processed";
|
|
126
|
-
} catch (error) {
|
|
127
|
-
if (replayEventKey) {
|
|
128
|
-
if (error instanceof ZaloRetryableWebhookError) {
|
|
129
|
-
recentWebhookEvents.release(replayEventKey, { error });
|
|
130
|
-
} else {
|
|
131
|
-
await recentWebhookEvents.commit(replayEventKey);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
throw error;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function recordWebhookStatus(
|
|
139
|
-
runtime: ZaloRuntimeEnv | undefined,
|
|
140
|
-
path: string,
|
|
141
|
-
statusCode: number,
|
|
142
|
-
): void {
|
|
143
|
-
webhookAnomalyTracker.record({
|
|
144
|
-
key: `${path}:${statusCode}`,
|
|
145
|
-
statusCode,
|
|
146
|
-
log: runtime?.log,
|
|
147
|
-
message: (count) =>
|
|
148
|
-
`[zalo] webhook anomaly path=${path} status=${statusCode} count=${String(count)}`,
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function headerValue(value: string | string[] | undefined): string | undefined {
|
|
153
|
-
return Array.isArray(value) ? value[0] : value;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export function registerZaloWebhookTarget(
|
|
157
|
-
target: ZaloWebhookTarget,
|
|
158
|
-
opts?: {
|
|
159
|
-
route?: RegisterWebhookPluginRouteOptions;
|
|
160
|
-
} & Pick<
|
|
161
|
-
RegisterWebhookTargetOptions<ZaloWebhookTarget>,
|
|
162
|
-
"onFirstPathTarget" | "onLastPathTargetRemoved"
|
|
163
|
-
>,
|
|
164
|
-
): () => void {
|
|
165
|
-
if (opts?.route) {
|
|
166
|
-
return registerWebhookTargetWithPluginRoute({
|
|
167
|
-
targetsByPath: webhookTargets,
|
|
168
|
-
target,
|
|
169
|
-
route: opts.route,
|
|
170
|
-
onLastPathTargetRemoved: opts.onLastPathTargetRemoved,
|
|
171
|
-
}).unregister;
|
|
172
|
-
}
|
|
173
|
-
return registerWebhookTarget(webhookTargets, target, opts).unregister;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export async function handleZaloWebhookRequest(
|
|
177
|
-
req: IncomingMessage,
|
|
178
|
-
res: ServerResponse,
|
|
179
|
-
processUpdate: ZaloWebhookProcessUpdate,
|
|
180
|
-
): Promise<boolean> {
|
|
181
|
-
return await withResolvedWebhookRequestPipeline({
|
|
182
|
-
req,
|
|
183
|
-
res,
|
|
184
|
-
targetsByPath: webhookTargets,
|
|
185
|
-
allowMethods: ["POST"],
|
|
186
|
-
handle: async ({ targets, path }) => {
|
|
187
|
-
const trustedProxies = targets[0]?.config.gateway?.trustedProxies;
|
|
188
|
-
const allowRealIpFallback = targets[0]?.config.gateway?.allowRealIpFallback === true;
|
|
189
|
-
const clientIp =
|
|
190
|
-
resolveClientIp({
|
|
191
|
-
remoteAddr: req.socket.remoteAddress,
|
|
192
|
-
forwardedFor: headerValue(req.headers["x-forwarded-for"]),
|
|
193
|
-
realIp: headerValue(req.headers["x-real-ip"]),
|
|
194
|
-
trustedProxies,
|
|
195
|
-
allowRealIpFallback,
|
|
196
|
-
}) ??
|
|
197
|
-
req.socket.remoteAddress ??
|
|
198
|
-
"unknown";
|
|
199
|
-
const rateLimitKey = `${path}:${clientIp}`;
|
|
200
|
-
const nowMs = Date.now();
|
|
201
|
-
if (
|
|
202
|
-
!applyBasicWebhookRequestGuards({
|
|
203
|
-
req,
|
|
204
|
-
res,
|
|
205
|
-
rateLimiter: webhookRateLimiter,
|
|
206
|
-
rateLimitKey,
|
|
207
|
-
nowMs,
|
|
208
|
-
})
|
|
209
|
-
) {
|
|
210
|
-
recordWebhookStatus(targets[0]?.runtime, path, res.statusCode);
|
|
211
|
-
return true;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
const headerToken = String(req.headers["x-bot-api-secret-token"] ?? "");
|
|
215
|
-
const target = resolveWebhookTargetWithAuthOrRejectSync({
|
|
216
|
-
targets,
|
|
217
|
-
res,
|
|
218
|
-
isMatch: (entry) => timingSafeEquals(entry.secret, headerToken),
|
|
219
|
-
});
|
|
220
|
-
if (!target) {
|
|
221
|
-
recordWebhookStatus(targets[0]?.runtime, path, res.statusCode);
|
|
222
|
-
return true;
|
|
223
|
-
}
|
|
224
|
-
// Preserve the historical 401-before-415 ordering for invalid secrets while still
|
|
225
|
-
// consuming rate-limit budget on unauthenticated guesses.
|
|
226
|
-
if (
|
|
227
|
-
!applyBasicWebhookRequestGuards({
|
|
228
|
-
req,
|
|
229
|
-
res,
|
|
230
|
-
requireJsonContentType: true,
|
|
231
|
-
})
|
|
232
|
-
) {
|
|
233
|
-
recordWebhookStatus(target.runtime, path, res.statusCode);
|
|
234
|
-
return true;
|
|
235
|
-
}
|
|
236
|
-
const body = await readJsonWebhookBodyOrReject({
|
|
237
|
-
req,
|
|
238
|
-
res,
|
|
239
|
-
maxBytes: 1024 * 1024,
|
|
240
|
-
timeoutMs: 30_000,
|
|
241
|
-
emptyObjectOnEmpty: false,
|
|
242
|
-
invalidJsonMessage: "Bad Request",
|
|
243
|
-
});
|
|
244
|
-
if (!body.ok) {
|
|
245
|
-
recordWebhookStatus(target.runtime, path, res.statusCode);
|
|
246
|
-
return true;
|
|
247
|
-
}
|
|
248
|
-
const raw = body.value;
|
|
249
|
-
|
|
250
|
-
// Zalo sends updates directly as { event_name, message, ... }, not wrapped in { ok, result }.
|
|
251
|
-
const record = raw && typeof raw === "object" ? (raw as Record<string, unknown>) : null;
|
|
252
|
-
const update: ZaloUpdate | undefined =
|
|
253
|
-
record && record.ok === true && record.result
|
|
254
|
-
? (record.result as ZaloUpdate)
|
|
255
|
-
: ((record as ZaloUpdate | null) ?? undefined);
|
|
256
|
-
|
|
257
|
-
if (!update?.event_name) {
|
|
258
|
-
res.statusCode = 400;
|
|
259
|
-
res.end("Bad Request");
|
|
260
|
-
recordWebhookStatus(target.runtime, path, res.statusCode);
|
|
261
|
-
return true;
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
void processZaloReplayGuardedUpdate({
|
|
265
|
-
target,
|
|
266
|
-
update,
|
|
267
|
-
processUpdate,
|
|
268
|
-
nowMs,
|
|
269
|
-
}).catch((err) => {
|
|
270
|
-
target.runtime.error?.(`[${target.account.accountId}] Zalo webhook failed: ${String(err)}`);
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
res.statusCode = 200;
|
|
274
|
-
res.end("ok");
|
|
275
|
-
return true;
|
|
276
|
-
},
|
|
277
|
-
});
|
|
278
|
-
}
|
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import { stat } from "node:fs/promises";
|
|
2
|
-
import { join } from "node:path";
|
|
3
|
-
import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/temp-path";
|
|
4
|
-
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
5
|
-
|
|
6
|
-
const loadOutboundMediaFromUrlMock = vi.fn();
|
|
7
|
-
|
|
8
|
-
vi.mock("openclaw/plugin-sdk/outbound-media", () => ({
|
|
9
|
-
loadOutboundMediaFromUrl: (...args: unknown[]) => loadOutboundMediaFromUrlMock(...args),
|
|
10
|
-
}));
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
clearHostedZaloMediaForTest,
|
|
14
|
-
prepareHostedZaloMediaUrl,
|
|
15
|
-
resolveHostedZaloMediaRoutePrefix,
|
|
16
|
-
tryHandleHostedZaloMediaRequest,
|
|
17
|
-
} from "./outbound-media.js";
|
|
18
|
-
|
|
19
|
-
function createMockResponse() {
|
|
20
|
-
const headers = new Map<string, string>();
|
|
21
|
-
return {
|
|
22
|
-
headers,
|
|
23
|
-
res: {
|
|
24
|
-
statusCode: 200,
|
|
25
|
-
setHeader(name: string, value: string) {
|
|
26
|
-
headers.set(name, value);
|
|
27
|
-
},
|
|
28
|
-
end: vi.fn(),
|
|
29
|
-
},
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
describe("zalo outbound hosted media", () => {
|
|
34
|
-
beforeEach(() => {
|
|
35
|
-
clearHostedZaloMediaForTest();
|
|
36
|
-
loadOutboundMediaFromUrlMock.mockReset();
|
|
37
|
-
loadOutboundMediaFromUrlMock.mockResolvedValue({
|
|
38
|
-
buffer: Buffer.from("image-bytes"),
|
|
39
|
-
contentType: "image/png",
|
|
40
|
-
fileName: "photo.png",
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it("loads outbound media under OpenClaw control and returns a hosted URL", async () => {
|
|
45
|
-
const hostedUrl = await prepareHostedZaloMediaUrl({
|
|
46
|
-
mediaUrl: "https://example.com/photo.png",
|
|
47
|
-
webhookUrl: "https://gateway.example.com/zalo-webhook",
|
|
48
|
-
maxBytes: 1024,
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
expect(loadOutboundMediaFromUrlMock).toHaveBeenCalledWith("https://example.com/photo.png", {
|
|
52
|
-
maxBytes: 1024,
|
|
53
|
-
});
|
|
54
|
-
expect(hostedUrl).toMatch(
|
|
55
|
-
/^https:\/\/gateway\.example\.com\/zalo-webhook\/media\/[a-f0-9]+\?token=[a-f0-9]+$/,
|
|
56
|
-
);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it("passes proxy-aware fetch options into hosted media downloads", async () => {
|
|
60
|
-
await prepareHostedZaloMediaUrl({
|
|
61
|
-
mediaUrl: "https://example.com/photo.png",
|
|
62
|
-
webhookUrl: "https://gateway.example.com/zalo-webhook",
|
|
63
|
-
maxBytes: 1024,
|
|
64
|
-
proxyUrl: "http://proxy.example:8080",
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
expect(loadOutboundMediaFromUrlMock).toHaveBeenCalledWith("https://example.com/photo.png", {
|
|
68
|
-
maxBytes: 1024,
|
|
69
|
-
proxyUrl: "http://proxy.example:8080",
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it("creates hosted media storage with private filesystem permissions", async () => {
|
|
74
|
-
const hostedUrl = await prepareHostedZaloMediaUrl({
|
|
75
|
-
mediaUrl: "https://example.com/photo.png",
|
|
76
|
-
webhookUrl: "https://gateway.example.com/zalo-webhook",
|
|
77
|
-
maxBytes: 1024,
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
if (process.platform === "win32") {
|
|
81
|
-
expect(hostedUrl).toContain("/zalo-webhook/media/");
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const { pathname } = new URL(hostedUrl);
|
|
86
|
-
const id = pathname.split("/").pop();
|
|
87
|
-
expect(id).toBeTruthy();
|
|
88
|
-
|
|
89
|
-
const storageDir = join(resolvePreferredOpenClawTmpDir(), "openclaw-zalo-outbound-media");
|
|
90
|
-
const [dirStats, metadataStats, bufferStats] = await Promise.all([
|
|
91
|
-
stat(storageDir),
|
|
92
|
-
stat(join(storageDir, `${id}.json`)),
|
|
93
|
-
stat(join(storageDir, `${id}.bin`)),
|
|
94
|
-
]);
|
|
95
|
-
|
|
96
|
-
expect(dirStats.mode & 0o777).toBe(0o700);
|
|
97
|
-
expect(metadataStats.mode & 0o777).toBe(0o600);
|
|
98
|
-
expect(bufferStats.mode & 0o777).toBe(0o600);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
it("preserves the root webhook path when deriving the hosted media route", () => {
|
|
102
|
-
expect(
|
|
103
|
-
resolveHostedZaloMediaRoutePrefix({
|
|
104
|
-
webhookUrl: "https://gateway.example.com/",
|
|
105
|
-
}),
|
|
106
|
-
).toBe("/media");
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it("serves hosted media once when the route token matches", async () => {
|
|
110
|
-
const hostedUrl = await prepareHostedZaloMediaUrl({
|
|
111
|
-
mediaUrl: "https://example.com/photo.png",
|
|
112
|
-
webhookUrl: "https://gateway.example.com/zalo-webhook",
|
|
113
|
-
maxBytes: 1024,
|
|
114
|
-
});
|
|
115
|
-
const { pathname, search } = new URL(hostedUrl);
|
|
116
|
-
const response = createMockResponse();
|
|
117
|
-
|
|
118
|
-
const handled = await tryHandleHostedZaloMediaRequest(
|
|
119
|
-
{
|
|
120
|
-
method: "GET",
|
|
121
|
-
url: `${pathname}${search}`,
|
|
122
|
-
} as never,
|
|
123
|
-
response.res as never,
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
expect(handled).toBe(true);
|
|
127
|
-
expect(response.res.statusCode).toBe(200);
|
|
128
|
-
expect(response.headers.get("Content-Type")).toBe("image/png");
|
|
129
|
-
expect(response.res.end).toHaveBeenCalledWith(Buffer.from("image-bytes"));
|
|
130
|
-
|
|
131
|
-
const secondResponse = createMockResponse();
|
|
132
|
-
const handledAgain = await tryHandleHostedZaloMediaRequest(
|
|
133
|
-
{
|
|
134
|
-
method: "GET",
|
|
135
|
-
url: `${pathname}${search}`,
|
|
136
|
-
} as never,
|
|
137
|
-
secondResponse.res as never,
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
expect(handledAgain).toBe(true);
|
|
141
|
-
expect(secondResponse.res.statusCode).toBe(404);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it("rejects hosted media requests with the wrong token", async () => {
|
|
145
|
-
const hostedUrl = await prepareHostedZaloMediaUrl({
|
|
146
|
-
mediaUrl: "https://example.com/photo.png",
|
|
147
|
-
webhookUrl: "https://gateway.example.com/custom/zalo",
|
|
148
|
-
webhookPath: "/custom/zalo-hook",
|
|
149
|
-
maxBytes: 1024,
|
|
150
|
-
});
|
|
151
|
-
const pathname = new URL(hostedUrl).pathname;
|
|
152
|
-
const response = createMockResponse();
|
|
153
|
-
|
|
154
|
-
const handled = await tryHandleHostedZaloMediaRequest(
|
|
155
|
-
{
|
|
156
|
-
method: "GET",
|
|
157
|
-
url: `${pathname}?token=wrong`,
|
|
158
|
-
} as never,
|
|
159
|
-
response.res as never,
|
|
160
|
-
);
|
|
161
|
-
|
|
162
|
-
expect(handled).toBe(true);
|
|
163
|
-
expect(response.res.statusCode).toBe(401);
|
|
164
|
-
expect(response.res.end).toHaveBeenCalledWith("Unauthorized");
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
it("rejects malformed hosted media ids before touching disk", async () => {
|
|
168
|
-
const response = createMockResponse();
|
|
169
|
-
|
|
170
|
-
const handled = await tryHandleHostedZaloMediaRequest(
|
|
171
|
-
{
|
|
172
|
-
method: "GET",
|
|
173
|
-
url: "/zalo-webhook/media/not-a-valid-hex-id?token=wrong",
|
|
174
|
-
} as never,
|
|
175
|
-
response.res as never,
|
|
176
|
-
);
|
|
177
|
-
|
|
178
|
-
expect(handled).toBe(true);
|
|
179
|
-
expect(response.res.statusCode).toBe(404);
|
|
180
|
-
expect(response.res.end).toHaveBeenCalledWith("Not Found");
|
|
181
|
-
});
|
|
182
|
-
});
|
package/src/outbound-media.ts
DELETED
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
import { randomBytes } from "node:crypto";
|
|
2
|
-
import { rmSync } from "node:fs";
|
|
3
|
-
import { chmod, mkdir, readdir, readFile, stat, unlink, writeFile } from "node:fs/promises";
|
|
4
|
-
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
5
|
-
import { join } from "node:path";
|
|
6
|
-
import { loadOutboundMediaFromUrl } from "openclaw/plugin-sdk/outbound-media";
|
|
7
|
-
import { resolvePreferredOpenClawTmpDir } from "openclaw/plugin-sdk/temp-path";
|
|
8
|
-
import { resolveWebhookPath } from "openclaw/plugin-sdk/webhook-ingress";
|
|
9
|
-
|
|
10
|
-
const ZALO_OUTBOUND_MEDIA_TTL_MS = 2 * 60_000;
|
|
11
|
-
const ZALO_OUTBOUND_MEDIA_SEGMENT = "media";
|
|
12
|
-
const ZALO_OUTBOUND_MEDIA_PREFIX = `/${ZALO_OUTBOUND_MEDIA_SEGMENT}/`;
|
|
13
|
-
const ZALO_OUTBOUND_MEDIA_DIR = join(
|
|
14
|
-
resolvePreferredOpenClawTmpDir(),
|
|
15
|
-
"openclaw-zalo-outbound-media",
|
|
16
|
-
);
|
|
17
|
-
const ZALO_OUTBOUND_MEDIA_ID_RE = /^[a-f0-9]{24}$/;
|
|
18
|
-
|
|
19
|
-
type HostedZaloMediaMetadata = {
|
|
20
|
-
routePath: string;
|
|
21
|
-
token: string;
|
|
22
|
-
contentType?: string;
|
|
23
|
-
expiresAt: number;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
function resolveHostedZaloMediaMetadataPath(id: string): string {
|
|
27
|
-
return join(ZALO_OUTBOUND_MEDIA_DIR, `${id}.json`);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function resolveHostedZaloMediaBufferPath(id: string): string {
|
|
31
|
-
return join(ZALO_OUTBOUND_MEDIA_DIR, `${id}.bin`);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function createHostedZaloMediaId(): string {
|
|
35
|
-
return randomBytes(12).toString("hex");
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function createHostedZaloMediaToken(): string {
|
|
39
|
-
return randomBytes(24).toString("hex");
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
async function ensureHostedZaloMediaDir(): Promise<void> {
|
|
43
|
-
await mkdir(ZALO_OUTBOUND_MEDIA_DIR, { recursive: true, mode: 0o700 });
|
|
44
|
-
await chmod(ZALO_OUTBOUND_MEDIA_DIR, 0o700).catch(() => undefined);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
async function deleteHostedZaloMediaEntry(id: string): Promise<void> {
|
|
48
|
-
await Promise.all([
|
|
49
|
-
unlink(resolveHostedZaloMediaMetadataPath(id)).catch(() => undefined),
|
|
50
|
-
unlink(resolveHostedZaloMediaBufferPath(id)).catch(() => undefined),
|
|
51
|
-
]);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
async function cleanupExpiredHostedZaloMedia(nowMs = Date.now()): Promise<void> {
|
|
55
|
-
let fileNames: string[];
|
|
56
|
-
try {
|
|
57
|
-
fileNames = await readdir(ZALO_OUTBOUND_MEDIA_DIR);
|
|
58
|
-
} catch {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
await Promise.all(
|
|
63
|
-
fileNames
|
|
64
|
-
.filter((fileName) => fileName.endsWith(".json"))
|
|
65
|
-
.map(async (fileName) => {
|
|
66
|
-
const id = fileName.slice(0, -5);
|
|
67
|
-
try {
|
|
68
|
-
const metadataRaw = await readFile(resolveHostedZaloMediaMetadataPath(id), "utf8");
|
|
69
|
-
const metadata = JSON.parse(metadataRaw) as HostedZaloMediaMetadata;
|
|
70
|
-
if (metadata.expiresAt <= nowMs) {
|
|
71
|
-
await deleteHostedZaloMediaEntry(id);
|
|
72
|
-
}
|
|
73
|
-
} catch {
|
|
74
|
-
await deleteHostedZaloMediaEntry(id);
|
|
75
|
-
}
|
|
76
|
-
}),
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
async function readHostedZaloMediaEntry(id: string): Promise<{
|
|
81
|
-
metadata: HostedZaloMediaMetadata;
|
|
82
|
-
buffer: Buffer;
|
|
83
|
-
} | null> {
|
|
84
|
-
try {
|
|
85
|
-
const [metadataRaw, buffer] = await Promise.all([
|
|
86
|
-
readFile(resolveHostedZaloMediaMetadataPath(id), "utf8"),
|
|
87
|
-
readFile(resolveHostedZaloMediaBufferPath(id)),
|
|
88
|
-
]);
|
|
89
|
-
return {
|
|
90
|
-
metadata: JSON.parse(metadataRaw) as HostedZaloMediaMetadata,
|
|
91
|
-
buffer,
|
|
92
|
-
};
|
|
93
|
-
} catch {
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function resolveHostedZaloMediaRoutePrefix(params: {
|
|
99
|
-
webhookUrl: string;
|
|
100
|
-
webhookPath?: string;
|
|
101
|
-
}): string {
|
|
102
|
-
const webhookRoutePath = resolveWebhookPath({
|
|
103
|
-
webhookPath: params.webhookPath,
|
|
104
|
-
webhookUrl: params.webhookUrl,
|
|
105
|
-
defaultPath: null,
|
|
106
|
-
});
|
|
107
|
-
if (!webhookRoutePath) {
|
|
108
|
-
throw new Error("Zalo webhookPath could not be derived for outbound media hosting");
|
|
109
|
-
}
|
|
110
|
-
return webhookRoutePath === "/"
|
|
111
|
-
? `/${ZALO_OUTBOUND_MEDIA_SEGMENT}`
|
|
112
|
-
: `${webhookRoutePath}/${ZALO_OUTBOUND_MEDIA_SEGMENT}`;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function resolveHostedZaloMediaRoutePath(params: {
|
|
116
|
-
webhookUrl: string;
|
|
117
|
-
webhookPath?: string;
|
|
118
|
-
}): string {
|
|
119
|
-
return `${resolveHostedZaloMediaRoutePrefix(params)}/`;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
export async function prepareHostedZaloMediaUrl(params: {
|
|
123
|
-
mediaUrl: string;
|
|
124
|
-
webhookUrl: string;
|
|
125
|
-
webhookPath?: string;
|
|
126
|
-
maxBytes: number;
|
|
127
|
-
proxyUrl?: string;
|
|
128
|
-
}): Promise<string> {
|
|
129
|
-
await ensureHostedZaloMediaDir();
|
|
130
|
-
await cleanupExpiredHostedZaloMedia();
|
|
131
|
-
|
|
132
|
-
const media = await loadOutboundMediaFromUrl(params.mediaUrl, {
|
|
133
|
-
maxBytes: params.maxBytes,
|
|
134
|
-
...(params.proxyUrl ? { proxyUrl: params.proxyUrl } : {}),
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
const routePath = resolveHostedZaloMediaRoutePath({
|
|
138
|
-
webhookUrl: params.webhookUrl,
|
|
139
|
-
webhookPath: params.webhookPath,
|
|
140
|
-
});
|
|
141
|
-
const id = createHostedZaloMediaId();
|
|
142
|
-
const token = createHostedZaloMediaToken();
|
|
143
|
-
const publicBaseUrl = new URL(params.webhookUrl).origin;
|
|
144
|
-
|
|
145
|
-
await writeFile(resolveHostedZaloMediaBufferPath(id), media.buffer, { mode: 0o600 });
|
|
146
|
-
try {
|
|
147
|
-
await writeFile(
|
|
148
|
-
resolveHostedZaloMediaMetadataPath(id),
|
|
149
|
-
JSON.stringify({
|
|
150
|
-
routePath,
|
|
151
|
-
token,
|
|
152
|
-
contentType: media.contentType,
|
|
153
|
-
expiresAt: Date.now() + ZALO_OUTBOUND_MEDIA_TTL_MS,
|
|
154
|
-
} satisfies HostedZaloMediaMetadata),
|
|
155
|
-
{ encoding: "utf8", mode: 0o600 },
|
|
156
|
-
);
|
|
157
|
-
} catch (error) {
|
|
158
|
-
await deleteHostedZaloMediaEntry(id);
|
|
159
|
-
throw error;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return `${publicBaseUrl}${routePath}${id}?token=${token}`;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export async function tryHandleHostedZaloMediaRequest(
|
|
166
|
-
req: IncomingMessage,
|
|
167
|
-
res: ServerResponse,
|
|
168
|
-
): Promise<boolean> {
|
|
169
|
-
await cleanupExpiredHostedZaloMedia();
|
|
170
|
-
|
|
171
|
-
const method = req.method ?? "GET";
|
|
172
|
-
if (method !== "GET" && method !== "HEAD") {
|
|
173
|
-
return false;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
let url: URL;
|
|
177
|
-
try {
|
|
178
|
-
url = new URL(req.url ?? "/", "http://localhost");
|
|
179
|
-
} catch {
|
|
180
|
-
return false;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
const mediaPath = url.pathname;
|
|
184
|
-
const prefixIndex = mediaPath.lastIndexOf(ZALO_OUTBOUND_MEDIA_PREFIX);
|
|
185
|
-
if (prefixIndex < 0) {
|
|
186
|
-
return false;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const routePath = mediaPath.slice(0, prefixIndex + ZALO_OUTBOUND_MEDIA_PREFIX.length);
|
|
190
|
-
const id = mediaPath.slice(prefixIndex + ZALO_OUTBOUND_MEDIA_PREFIX.length);
|
|
191
|
-
if (!id || !ZALO_OUTBOUND_MEDIA_ID_RE.test(id)) {
|
|
192
|
-
res.statusCode = 404;
|
|
193
|
-
res.end("Not Found");
|
|
194
|
-
return true;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
const entry = await readHostedZaloMediaEntry(id);
|
|
198
|
-
if (!entry || entry.metadata.routePath !== routePath) {
|
|
199
|
-
res.statusCode = 404;
|
|
200
|
-
res.end("Not Found");
|
|
201
|
-
return true;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (entry.metadata.expiresAt <= Date.now()) {
|
|
205
|
-
await deleteHostedZaloMediaEntry(id);
|
|
206
|
-
res.statusCode = 410;
|
|
207
|
-
res.end("Expired");
|
|
208
|
-
return true;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
if (url.searchParams.get("token") !== entry.metadata.token) {
|
|
212
|
-
res.statusCode = 401;
|
|
213
|
-
res.end("Unauthorized");
|
|
214
|
-
return true;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (entry.metadata.contentType) {
|
|
218
|
-
res.setHeader("Content-Type", entry.metadata.contentType);
|
|
219
|
-
}
|
|
220
|
-
res.setHeader("Cache-Control", "no-store");
|
|
221
|
-
res.setHeader("X-Content-Type-Options", "nosniff");
|
|
222
|
-
const bufferStats = await stat(resolveHostedZaloMediaBufferPath(id)).catch(() => null);
|
|
223
|
-
if (bufferStats) {
|
|
224
|
-
res.setHeader("Content-Length", String(bufferStats.size));
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
if (method === "HEAD") {
|
|
228
|
-
res.statusCode = 200;
|
|
229
|
-
res.end();
|
|
230
|
-
return true;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
res.statusCode = 200;
|
|
234
|
-
res.end(entry.buffer);
|
|
235
|
-
await deleteHostedZaloMediaEntry(id);
|
|
236
|
-
return true;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
export function clearHostedZaloMediaForTest(): void {
|
|
240
|
-
rmSync(ZALO_OUTBOUND_MEDIA_DIR, { recursive: true, force: true });
|
|
241
|
-
}
|