Skip to content

Commit 3ee6c6a

Browse files
fix(zalo): treat a non-positive mediaMaxMb as unset, not a 0-byte cap (#120988)
`channels.zalo.mediaMaxMb: 0` (or a negative value) passes config validation and then makes every inbound image download and every hosted outbound media send fail, with no error naming the setting. The monitor resolved the cap with `??`, which only replaces `null`/`undefined`, so `0` and negatives survived into `mediaMaxMb * 1024 * 1024` and reached the media core as a byte budget no payload can satisfy. Nothing upstream normalizes the value: the channel schema declares `mediaMaxMb` with no range, and `mergeAccountConfig` is a plain spread that keeps `0`. Everywhere else a non-positive `mediaMaxMb` means unset, never reject-all: the canonical schema is `z.number().positive()`, most bundled channels publish `exclusiveMinimum: 0`, and the core resolvers fall back to their default. Guard the resolver so a non-positive value falls back to `DEFAULT_MEDIA_MAX_MB`, which is what unset already means for Zalo. Co-authored-by: Tak Hoffman <[email protected]>
1 parent e9250be commit 3ee6c6a

3 files changed

Lines changed: 74 additions & 40 deletions

File tree

‎extensions/zalo/src/monitor.image.polling.test-support.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,44 @@ describe("Zalo polling image handling", () => {
320320
server.close(() => resolve());
321321
});
322322
});
323+
324+
it.each([
325+
{ mediaMaxMb: 0, expectedMaxBytes: 5 * 1024 * 1024 },
326+
{ mediaMaxMb: -5, expectedMaxBytes: 5 * 1024 * 1024 },
327+
{ mediaMaxMb: 2, expectedMaxBytes: 2 * 1024 * 1024 },
328+
])(
329+
"caps inbound image downloads at $expectedMaxBytes bytes when mediaMaxMb is $mediaMaxMb",
330+
async ({ mediaMaxMb, expectedMaxBytes }) => {
331+
getUpdatesMock
332+
.mockResolvedValueOnce({
333+
ok: true,
334+
result: createImageUpdate({ messageId: `zalo-image-cap-${mediaMaxMb}` }),
335+
})
336+
.mockImplementation(() => new Promise(() => {}));
337+
338+
const { monitorZaloProvider } = await loadCachedLifecycleMonitorModule("zalo-image-polling");
339+
const abort = new AbortController();
340+
const runtime = createRuntimeEnv();
341+
const { account, config } = createLifecycleMonitorSetup({
342+
accountId: "default",
343+
dmPolicy: "open",
344+
mediaMaxMb,
345+
});
346+
const run = monitorZaloProvider({
347+
token: "zalo-token", // pragma: allowlist secret
348+
account,
349+
config,
350+
runtime,
351+
abortSignal: abort.signal,
352+
});
353+
354+
await settleAsyncWork();
355+
expect(saveRemoteMediaMock).toHaveBeenCalledWith(
356+
expect.objectContaining({ maxBytes: expectedMaxBytes }),
357+
);
358+
359+
abort.abort();
360+
await run;
361+
},
362+
);
323363
});

‎extensions/zalo/src/monitor.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,14 @@ export async function monitorZaloProvider(options: ZaloMonitorOptions): Promise<
869869
} = options;
870870

871871
const core = getZaloRuntime();
872-
const effectiveMediaMaxMb = account.config.mediaMaxMb ?? DEFAULT_MEDIA_MAX_MB;
872+
// A non-positive cap cannot bound a transfer, so treat it as unset: `??` alone
873+
// keeps 0/negatives and turns every inbound download and hosted outbound send
874+
// into a 0-byte limit the media core rejects.
875+
const configuredMediaMaxMb = account.config.mediaMaxMb;
876+
const effectiveMediaMaxMb =
877+
typeof configuredMediaMaxMb === "number" && configuredMediaMaxMb > 0
878+
? configuredMediaMaxMb
879+
: DEFAULT_MEDIA_MAX_MB;
873880
const fetcher = fetcherOverride ?? resolveZaloProxyFetch(account.config.proxy);
874881
const mode = useWebhook ? "webhook" : "polling";
875882
const effectiveWebhookUrl = normalizeWebhookUrl(webhookUrl ?? account.config.webhookUrl);

‎extensions/zalo/src/test-support/lifecycle-test-support.ts‎

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,59 @@ import { expect, vi } from "vitest";
88
import type { OpenClawConfig, PluginRuntime } from "../runtime-api.js";
99
import type { ResolvedZaloAccount } from "../types.js";
1010

11+
type LifecycleMonitorSetupParams = {
12+
accountId: string;
13+
dmPolicy: "open" | "pairing";
14+
allowFrom?: string[];
15+
webhookUrl?: string;
16+
webhookSecret?: string;
17+
mediaMaxMb?: number;
18+
};
19+
1120
function resolveLifecycleAllowFrom(params: {
1221
dmPolicy: "open" | "pairing";
1322
allowFrom?: string[];
1423
}): string[] | undefined {
1524
return params.allowFrom ?? (params.dmPolicy === "open" ? ["*"] : undefined);
1625
}
1726

18-
function createLifecycleConfig(params: {
19-
accountId: string;
20-
dmPolicy: "open" | "pairing";
21-
allowFrom?: string[];
22-
webhookUrl?: string;
23-
webhookSecret?: string;
24-
}): OpenClawConfig {
25-
const webhookUrl = params.webhookUrl ?? "https://example.com/hooks/zalo";
26-
const webhookSecret = params.webhookSecret ?? "supersecret";
27+
function createLifecycleAccountConfig(params: LifecycleMonitorSetupParams) {
2728
const allowFrom = resolveLifecycleAllowFrom(params);
29+
return {
30+
webhookUrl: params.webhookUrl ?? "https://example.com/hooks/zalo",
31+
webhookSecret: params.webhookSecret ?? "supersecret", // pragma: allowlist secret
32+
dmPolicy: params.dmPolicy,
33+
...(allowFrom ? { allowFrom } : {}),
34+
// Kept undefined-free so cases can assert on an account config that never
35+
// declared the key, which is what the resolver's unset branch expects.
36+
...(params.mediaMaxMb === undefined ? {} : { mediaMaxMb: params.mediaMaxMb }),
37+
};
38+
}
39+
40+
function createLifecycleConfig(params: LifecycleMonitorSetupParams): OpenClawConfig {
2841
return {
2942
channels: {
3043
zalo: {
3144
enabled: true,
3245
accounts: {
33-
[params.accountId]: {
34-
enabled: true,
35-
webhookUrl,
36-
webhookSecret, // pragma: allowlist secret
37-
dmPolicy: params.dmPolicy,
38-
...(allowFrom ? { allowFrom } : {}),
39-
},
46+
[params.accountId]: { enabled: true, ...createLifecycleAccountConfig(params) },
4047
},
4148
},
4249
},
4350
} as OpenClawConfig;
4451
}
4552

46-
function createLifecycleAccount(params: {
47-
accountId: string;
48-
dmPolicy: "open" | "pairing";
49-
allowFrom?: string[];
50-
webhookUrl?: string;
51-
webhookSecret?: string;
52-
}): ResolvedZaloAccount {
53-
const webhookUrl = params.webhookUrl ?? "https://example.com/hooks/zalo";
54-
const webhookSecret = params.webhookSecret ?? "supersecret";
55-
const allowFrom = resolveLifecycleAllowFrom(params);
53+
function createLifecycleAccount(params: LifecycleMonitorSetupParams): ResolvedZaloAccount {
5654
return {
5755
accountId: params.accountId,
5856
enabled: true,
5957
token: "zalo-token",
6058
tokenSource: "config",
61-
config: {
62-
webhookUrl,
63-
webhookSecret, // pragma: allowlist secret
64-
dmPolicy: params.dmPolicy,
65-
...(allowFrom ? { allowFrom } : {}),
66-
},
59+
config: createLifecycleAccountConfig(params),
6760
} as ResolvedZaloAccount;
6861
}
6962

70-
export function createLifecycleMonitorSetup(params: {
71-
accountId: string;
72-
dmPolicy: "open" | "pairing";
73-
allowFrom?: string[];
74-
webhookUrl?: string;
75-
webhookSecret?: string;
76-
}) {
63+
export function createLifecycleMonitorSetup(params: LifecycleMonitorSetupParams) {
7764
return {
7865
account: createLifecycleAccount(params),
7966
config: createLifecycleConfig(params),

0 commit comments

Comments
 (0)