Skip to content

Commit e4825a0

Browse files
authored
fix(telegram): unify transport fallback chain (#49148)
* fix(telegram): unify transport fallback chain * fix: address telegram fallback review comments * fix: validate pinned SSRF overrides * fix: unify telegram fallback retries (#49148)
1 parent 272d6ed commit e4825a0

10 files changed

Lines changed: 457 additions & 133 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ Docs: https://docs.openclaw.ai
136136
- ACP/gateway startup: use direct Telegram and Discord startup/status helpers instead of routing probes through the plugin runtime, and prepend the selected daemon Node bin dir to service PATH so plugin-local installs can still find `npm` and `pnpm`.
137137
- ACP/configured bindings: reinitialize configured ACP sessions that are stuck in `error` state instead of reusing the failed runtime.
138138
- Mattermost/DM send: retry transient direct-channel creation failures for DM deliveries, with configurable backoff and per-request timeout. (#42398) Thanks @JonathanJing.
139+
- Telegram/network: unify API and media fetches under the same sticky IPv4 and pinned-IP fallback chain, and re-validate pinned override addresses against SSRF policy. (#49148) Thanks @obviyus.
139140

140141
## 2026.3.13
141142

‎extensions/telegram/src/bot/delivery.resolve-media.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { retryAsync } from "openclaw/plugin-sdk/infra-runtime";
44
import { fetchRemoteMedia } from "openclaw/plugin-sdk/media-runtime";
55
import { saveMediaBuffer } from "openclaw/plugin-sdk/media-runtime";
66
import { logVerbose, warn } from "openclaw/plugin-sdk/runtime-env";
7-
import { shouldRetryTelegramIpv4Fallback, type TelegramTransport } from "../fetch.js";
7+
import { shouldRetryTelegramTransportFallback, type TelegramTransport } from "../fetch.js";
88
import { cacheSticker, getCachedSticker } from "../sticker-cache.js";
99
import { resolveTelegramMediaPlaceholder } from "./helpers.js";
1010
import type { StickerMetadata, TelegramContext } from "./types.js";
@@ -129,9 +129,8 @@ async function downloadAndSaveTelegramFile(params: {
129129
const fetched = await fetchRemoteMedia({
130130
url,
131131
fetchImpl: params.transport.sourceFetch,
132-
dispatcherPolicy: params.transport.pinnedDispatcherPolicy,
133-
fallbackDispatcherPolicy: params.transport.fallbackPinnedDispatcherPolicy,
134-
shouldRetryFetchError: shouldRetryTelegramIpv4Fallback,
132+
dispatcherAttempts: params.transport.dispatcherAttempts,
133+
shouldRetryFetchError: shouldRetryTelegramTransportFallback,
135134
filePathHint: params.filePath,
136135
maxBytes: params.maxBytes,
137136
readIdleTimeoutMs: TELEGRAM_DOWNLOAD_IDLE_TIMEOUT_MS,

‎extensions/telegram/src/fetch.test.ts‎

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { afterEach, describe, expect, it, vi } from "vitest";
2-
import { resolveFetch } from "../../../src/infra/fetch.js";
3-
import { resolveTelegramFetch, resolveTelegramTransport } from "./fetch.js";
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
42

53
const setDefaultResultOrder = vi.hoisted(() => vi.fn());
64
const setDefaultAutoSelectFamily = vi.hoisted(() => vi.fn());
@@ -56,6 +54,16 @@ vi.mock("undici", () => ({
5654
setGlobalDispatcher,
5755
}));
5856

57+
let resolveFetch: typeof import("../../../src/infra/fetch.js").resolveFetch;
58+
let resolveTelegramFetch: typeof import("./fetch.js").resolveTelegramFetch;
59+
let resolveTelegramTransport: typeof import("./fetch.js").resolveTelegramTransport;
60+
61+
beforeEach(async () => {
62+
vi.resetModules();
63+
({ resolveFetch } = await import("../../../src/infra/fetch.js"));
64+
({ resolveTelegramFetch, resolveTelegramTransport } = await import("./fetch.js"));
65+
});
66+
5967
function resolveTelegramFetchOrThrow(
6068
proxyFetch?: typeof fetch,
6169
options?: { network?: { autoSelectFamily?: boolean; dnsResultOrder?: "ipv4first" | "verbatim" } },
@@ -152,6 +160,24 @@ function expectPinnedIpv4ConnectDispatcher(args: {
152160
}
153161
}
154162

163+
function expectPinnedFallbackIpDispatcher(callIndex: number) {
164+
const dispatcher = getDispatcherFromUndiciCall(callIndex);
165+
expect(dispatcher?.options?.connect).toEqual(
166+
expect.objectContaining({
167+
family: 4,
168+
autoSelectFamily: false,
169+
lookup: expect.any(Function),
170+
}),
171+
);
172+
const callback = vi.fn();
173+
(
174+
dispatcher?.options?.connect?.lookup as
175+
| ((hostname: string, callback: (err: null, address: string, family: number) => void) => void)
176+
| undefined
177+
)?.("api.telegram.org", callback);
178+
expect(callback).toHaveBeenCalledWith(null, "149.154.167.220", 4);
179+
}
180+
155181
function expectCallerDispatcherPreserved(callIndexes: number[], dispatcher: unknown) {
156182
for (const callIndex of callIndexes) {
157183
const callInit = undiciFetch.mock.calls[callIndex - 1]?.[1] as
@@ -395,7 +421,7 @@ describe("resolveTelegramFetch", () => {
395421
pinnedCall: 2,
396422
followupCall: 3,
397423
});
398-
expect(transport.pinnedDispatcherPolicy).toEqual(
424+
expect(transport.dispatcherAttempts?.[0]?.dispatcherPolicy).toEqual(
399425
expect.objectContaining({
400426
mode: "direct",
401427
}),
@@ -533,6 +559,34 @@ describe("resolveTelegramFetch", () => {
533559
);
534560
});
535561

562+
it("escalates from IPv4 fallback to pinned Telegram IP and keeps it sticky", async () => {
563+
undiciFetch
564+
.mockRejectedValueOnce(buildFetchFallbackError("ETIMEDOUT"))
565+
.mockRejectedValueOnce(buildFetchFallbackError("EHOSTUNREACH"))
566+
.mockResolvedValueOnce({ ok: true } as Response)
567+
.mockResolvedValueOnce({ ok: true } as Response);
568+
569+
const resolved = resolveTelegramFetchOrThrow(undefined, {
570+
network: {
571+
autoSelectFamily: true,
572+
dnsResultOrder: "ipv4first",
573+
},
574+
});
575+
576+
await resolved("https://api.telegram.org/botx/sendMessage");
577+
await resolved("https://api.telegram.org/botx/sendChatAction");
578+
579+
expect(undiciFetch).toHaveBeenCalledTimes(4);
580+
581+
const secondDispatcher = getDispatcherFromUndiciCall(2);
582+
const thirdDispatcher = getDispatcherFromUndiciCall(3);
583+
const fourthDispatcher = getDispatcherFromUndiciCall(4);
584+
585+
expect(secondDispatcher).not.toBe(thirdDispatcher);
586+
expect(thirdDispatcher).toBe(fourthDispatcher);
587+
expectPinnedFallbackIpDispatcher(3);
588+
});
589+
536590
it("preserves caller-provided dispatcher across fallback retry", async () => {
537591
const fetchError = buildFetchFallbackError("EHOSTUNREACH");
538592
undiciFetch.mockRejectedValueOnce(fetchError).mockResolvedValueOnce({ ok: true } as Response);

0 commit comments

Comments
 (0)