Skip to content

Commit d43c8ec

Browse files
itsyujaysteipete
andauthored
fix(feishu): route SDK traffic through ambient proxy (#86386)
* fix(feishu): route sdk http through ambient proxy * fix(feishu): preserve request agents under proxy * fix(feishu): honor managed proxy agent override * fix(feishu): share ambient proxy transport * chore(feishu): move release note to pr --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent c490117 commit d43c8ec

2 files changed

Lines changed: 266 additions & 27 deletions

File tree

‎extensions/feishu/src/client.test.ts‎

Lines changed: 176 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const FEISHU_HTTP_TIMEOUT_MAX_MS = 300_000;
1010
type CreateFeishuClient = typeof import("./client.js").createFeishuClient;
1111
type CreateFeishuWSClient = typeof import("./client.js").createFeishuWSClient;
1212
type GetFeishuUserAgent = typeof import("./client.js").getFeishuUserAgent;
13+
type ResetFeishuProxyAgentForTest = typeof import("./client.js").resetFeishuProxyAgentForTest;
1314

1415
const requestInterceptorState = vi.hoisted(() => {
1516
let registered: ((req: unknown) => unknown) | undefined;
@@ -32,11 +33,8 @@ const wsClientCtorMock = vi.hoisted(() =>
3233
return { connected: true };
3334
}),
3435
);
35-
const proxyAgentCtorMock = vi.hoisted(() =>
36-
vi.fn(function createAmbientNodeProxyAgent() {
37-
return { proxied: true };
38-
}),
39-
);
36+
const proxyAgentInstance = vi.hoisted(() => ({ proxied: true, destroy: vi.fn() }));
37+
const proxyAgentCtorMock = vi.hoisted(() => vi.fn(() => proxyAgentInstance));
4038
const mockBaseHttpInstance = vi.hoisted(() => {
4139
const requestInterceptors = { use: requestInterceptorState.use };
4240
Object.defineProperty(requestInterceptors, "handlers", {
@@ -62,7 +60,13 @@ const mockBaseHttpInstance = vi.hoisted(() => {
6260
},
6361
};
6462
});
65-
const proxyEnvKeys = ["https_proxy", "HTTPS_PROXY", "http_proxy", "HTTP_PROXY"] as const;
63+
const proxyEnvKeys = [
64+
"https_proxy",
65+
"HTTPS_PROXY",
66+
"http_proxy",
67+
"HTTP_PROXY",
68+
"OPENCLAW_PROXY_ACTIVE",
69+
] as const;
6670
type ProxyEnvKey = (typeof proxyEnvKeys)[number];
6771
const registerFeishuDocToolsMock = vi.hoisted(() => vi.fn());
6872
const registerFeishuChatToolsMock = vi.hoisted(() => vi.fn());
@@ -77,6 +81,7 @@ const registerFeishuSubagentHooksMock = vi.hoisted(() => vi.fn());
7781
let createFeishuClient: CreateFeishuClient;
7882
let createFeishuWSClient: CreateFeishuWSClient;
7983
let getFeishuUserAgent: GetFeishuUserAgent;
84+
let resetFeishuProxyAgentForTest: ResetFeishuProxyAgentForTest;
8085

8186
let priorProxyEnv: Partial<Record<ProxyEnvKey, string | undefined>> = {};
8287
let priorFeishuTimeoutEnv: string | undefined;
@@ -141,13 +146,20 @@ function isRecord(value: unknown): value is Record<string, unknown> {
141146
}
142147

143148
type HttpInstanceLike = {
149+
request: (options?: Record<string, unknown>) => Promise<unknown>;
144150
get: (url: string, options?: Record<string, unknown>) => Promise<unknown>;
145151
post: (url: string, body?: unknown, options?: Record<string, unknown>) => Promise<unknown>;
146152
};
147153

148154
function requireHttpInstance(value: unknown): HttpInstanceLike {
149-
if (isRecord(value) && typeof value.get === "function" && typeof value.post === "function") {
155+
if (
156+
isRecord(value) &&
157+
typeof value.request === "function" &&
158+
typeof value.get === "function" &&
159+
typeof value.post === "function"
160+
) {
150161
return {
162+
request: value.request as HttpInstanceLike["request"],
151163
get: value.get as HttpInstanceLike["get"],
152164
post: value.post as HttpInstanceLike["post"],
153165
};
@@ -170,6 +182,7 @@ function firstWsClientOptions(): {
170182
onReady?: unknown;
171183
onReconnected?: unknown;
172184
onReconnecting?: unknown;
185+
httpInstance?: unknown;
173186
} {
174187
const options = readCallOptions(wsClientCtorMock, 0);
175188
return {
@@ -179,6 +192,7 @@ function firstWsClientOptions(): {
179192
onReady: options.onReady,
180193
onReconnected: options.onReconnected,
181194
onReconnecting: options.onReconnecting,
195+
httpInstance: options.httpInstance,
182196
};
183197
}
184198

@@ -204,7 +218,8 @@ beforeAll(async () => {
204218
),
205219
}));
206220

207-
({ createFeishuClient, createFeishuWSClient, getFeishuUserAgent } = await import("./client.js"));
221+
({ createFeishuClient, createFeishuWSClient, getFeishuUserAgent, resetFeishuProxyAgentForTest } =
222+
await import("./client.js"));
208223
});
209224

210225
beforeEach(() => {
@@ -215,6 +230,7 @@ beforeEach(() => {
215230
priorProxyEnv[key] = process.env[key];
216231
setFeishuTestEnvValue(key, undefined);
217232
}
233+
resetFeishuProxyAgentForTest();
218234
vi.clearAllMocks();
219235
});
220236

@@ -226,6 +242,7 @@ afterEach(() => {
226242
});
227243

228244
afterAll(() => {
245+
resetFeishuProxyAgentForTest();
229246
vi.doUnmock("./channel.js");
230247
vi.doUnmock("./docx.js");
231248
vi.doUnmock("./chat.js");
@@ -409,6 +426,134 @@ describe("createFeishuClient HTTP timeout", () => {
409426
timeout: 45_000,
410427
});
411428
});
429+
430+
it("uses OpenClaw's ambient proxy agent for Feishu HTTP API requests", async () => {
431+
process.env.HTTPS_PROXY = "http://upper-https:8002";
432+
433+
createFeishuClient({
434+
appId: "app_11",
435+
appSecret: "secret_11", // pragma: allowlist secret
436+
accountId: "http-proxy-agent",
437+
});
438+
439+
const httpInstance = readLastClientHttpInstance();
440+
await httpInstance.post("https://example.com/api", { data: 1 });
441+
await httpInstance.get("https://example.com/other");
442+
443+
expect(proxyAgentCtorMock).toHaveBeenCalledTimes(1);
444+
expect(mockBaseHttpInstance.post).toHaveBeenCalledWith(
445+
"https://example.com/api",
446+
{ data: 1 },
447+
{
448+
timeout: FEISHU_HTTP_TIMEOUT_MS,
449+
httpAgent: proxyAgentInstance,
450+
httpsAgent: proxyAgentInstance,
451+
proxy: false,
452+
},
453+
);
454+
expect(mockBaseHttpInstance.get).toHaveBeenCalledWith("https://example.com/other", {
455+
timeout: FEISHU_HTTP_TIMEOUT_MS,
456+
httpAgent: proxyAgentInstance,
457+
httpsAgent: proxyAgentInstance,
458+
proxy: false,
459+
});
460+
});
461+
462+
it("preserves request-level agents while disabling axios ambient proxy handling", async () => {
463+
process.env.HTTPS_PROXY = "http://upper-https:8002";
464+
const callerHttpAgent = { caller: "http" };
465+
const callerHttpsAgent = { caller: "https" };
466+
467+
createFeishuClient({
468+
appId: "app_12",
469+
appSecret: "secret_12", // pragma: allowlist secret
470+
accountId: "http-proxy-preserve-agent",
471+
});
472+
473+
const httpInstance = readLastClientHttpInstance();
474+
await httpInstance.request({
475+
url: "https://example.com/api",
476+
httpAgent: callerHttpAgent,
477+
httpsAgent: callerHttpsAgent,
478+
});
479+
480+
expect(proxyAgentCtorMock).toHaveBeenCalledTimes(1);
481+
expect(mockBaseHttpInstance.request).toHaveBeenCalledWith({
482+
url: "https://example.com/api",
483+
timeout: FEISHU_HTTP_TIMEOUT_MS,
484+
httpAgent: callerHttpAgent,
485+
httpsAgent: callerHttpsAgent,
486+
proxy: false,
487+
});
488+
});
489+
490+
it("overrides request-level agents while managed proxy mode is active", async () => {
491+
process.env.HTTPS_PROXY = "http://upper-https:8002";
492+
process.env.OPENCLAW_PROXY_ACTIVE = "1";
493+
const callerHttpAgent = { caller: "http" };
494+
const callerHttpsAgent = { caller: "https" };
495+
496+
createFeishuClient({
497+
appId: "app_13",
498+
appSecret: "secret_13", // pragma: allowlist secret
499+
accountId: "http-proxy-managed-agent",
500+
});
501+
502+
const httpInstance = readLastClientHttpInstance();
503+
await httpInstance.request({
504+
url: "https://example.com/api",
505+
httpAgent: callerHttpAgent,
506+
httpsAgent: callerHttpsAgent,
507+
});
508+
509+
expect(proxyAgentCtorMock).toHaveBeenCalledTimes(1);
510+
expect(mockBaseHttpInstance.request).toHaveBeenCalledWith({
511+
url: "https://example.com/api",
512+
timeout: FEISHU_HTTP_TIMEOUT_MS,
513+
httpAgent: proxyAgentInstance,
514+
httpsAgent: proxyAgentInstance,
515+
proxy: false,
516+
});
517+
});
518+
519+
it("falls back to Axios ambient proxy handling when agent creation fails", async () => {
520+
process.env.HTTPS_PROXY = "http://upper-https:8002";
521+
proxyAgentCtorMock.mockImplementationOnce(() => {
522+
throw new Error("proxy agent failed");
523+
});
524+
525+
createFeishuClient({
526+
...baseAccount,
527+
accountId: "http-proxy-fallback",
528+
});
529+
530+
const httpInstance = readLastClientHttpInstance();
531+
await httpInstance.request({ url: "https://example.com/api" });
532+
533+
expect(mockBaseHttpInstance.request).toHaveBeenCalledWith({
534+
url: "https://example.com/api",
535+
timeout: FEISHU_HTTP_TIMEOUT_MS,
536+
});
537+
});
538+
539+
it("fails closed when managed proxy agent creation fails", async () => {
540+
process.env.HTTPS_PROXY = "http://upper-https:8002";
541+
process.env.OPENCLAW_PROXY_ACTIVE = "1";
542+
proxyAgentCtorMock.mockImplementationOnce(() => {
543+
throw new Error("proxy agent failed");
544+
});
545+
546+
createFeishuClient({
547+
...baseAccount,
548+
accountId: "http-proxy-managed-failure",
549+
});
550+
551+
const httpInstance = readLastClientHttpInstance();
552+
await expect(httpInstance.request({ url: "https://example.com/api" })).rejects.toThrow(
553+
"Feishu managed proxy is active but no proxy agent could be created",
554+
);
555+
expect(mockBaseHttpInstance.request).not.toHaveBeenCalled();
556+
});
412557
});
413558

414559
describe("createFeishuWSClient proxy handling", () => {
@@ -444,6 +589,26 @@ describe("createFeishuWSClient proxy handling", () => {
444589
});
445590
});
446591

592+
it("passes the proxy-aware HTTP instance to Lark.WSClient bootstrap requests", async () => {
593+
process.env.HTTPS_PROXY = "http://upper-https:8002";
594+
595+
await createFeishuWSClient(baseAccount);
596+
597+
const options = firstWsClientOptions();
598+
const httpInstance = requireHttpInstance(options.httpInstance);
599+
await httpInstance.request({ url: "https://open.feishu.cn/open-apis/ws/v1" });
600+
601+
expect(proxyAgentCtorMock).toHaveBeenCalledTimes(1);
602+
expect(options.agent).toBe(proxyAgentInstance);
603+
expect(mockBaseHttpInstance.request).toHaveBeenCalledWith({
604+
url: "https://open.feishu.cn/open-apis/ws/v1",
605+
timeout: FEISHU_HTTP_TIMEOUT_MS,
606+
httpAgent: proxyAgentInstance,
607+
httpsAgent: proxyAgentInstance,
608+
proxy: false,
609+
});
610+
});
611+
447612
it("does not set a ws proxy agent when proxy env is absent", async () => {
448613
await createFeishuWSClient(baseAccount);
449614

@@ -459,7 +624,7 @@ describe("createFeishuWSClient proxy handling", () => {
459624

460625
expect(proxyAgentCtorMock).toHaveBeenCalledTimes(1);
461626
const options = firstWsClientOptions();
462-
expect(options.agent).toEqual({ proxied: true });
627+
expect(options.agent).toBe(proxyAgentInstance);
463628
});
464629

465630
it("creates a ws proxy agent when uppercase HTTPS_PROXY is set", async () => {
@@ -469,7 +634,7 @@ describe("createFeishuWSClient proxy handling", () => {
469634

470635
expect(proxyAgentCtorMock).toHaveBeenCalledTimes(1);
471636
const options = firstWsClientOptions();
472-
expect(options.agent).toEqual({ proxied: true });
637+
expect(options.agent).toBe(proxyAgentInstance);
473638
});
474639

475640
it("falls back to HTTP_PROXY for ws proxy agent creation", async () => {
@@ -479,6 +644,6 @@ describe("createFeishuWSClient proxy handling", () => {
479644

480645
expect(proxyAgentCtorMock).toHaveBeenCalledTimes(1);
481646
const options = firstWsClientOptions();
482-
expect(options.agent).toEqual({ proxied: true });
647+
expect(options.agent).toBe(proxyAgentInstance);
483648
});
484649
});

0 commit comments

Comments
 (0)