Skip to content

Commit d2e4c65

Browse files
author
Peter Steinberger
committed
fix(feishu): restore custom HTTPS API domains
1 parent ad3e36e commit d2e4c65

2 files changed

Lines changed: 196 additions & 26 deletions

File tree

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

Lines changed: 149 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ function isRecord(value: unknown): value is Record<string, unknown> {
148148
type HttpInstanceLike = {
149149
request: (options?: Record<string, unknown>) => Promise<unknown>;
150150
get: (url: string, options?: Record<string, unknown>) => Promise<unknown>;
151+
delete: (url: string, options?: Record<string, unknown>) => Promise<unknown>;
152+
head: (url: string, options?: Record<string, unknown>) => Promise<unknown>;
153+
options: (url: string, options?: Record<string, unknown>) => Promise<unknown>;
151154
post: (url: string, body?: unknown, options?: Record<string, unknown>) => Promise<unknown>;
155+
put: (url: string, body?: unknown, options?: Record<string, unknown>) => Promise<unknown>;
156+
patch: (url: string, body?: unknown, options?: Record<string, unknown>) => Promise<unknown>;
152157
};
153158

154159
function requireHttpInstance(value: unknown): HttpInstanceLike {
@@ -158,11 +163,7 @@ function requireHttpInstance(value: unknown): HttpInstanceLike {
158163
typeof value.get === "function" &&
159164
typeof value.post === "function"
160165
) {
161-
return {
162-
request: value.request as HttpInstanceLike["request"],
163-
get: value.get as HttpInstanceLike["get"],
164-
post: value.post as HttpInstanceLike["post"],
165-
};
166+
return value as HttpInstanceLike;
166167
}
167168
throw new Error("expected Feishu HTTP instance");
168169
}
@@ -199,7 +200,7 @@ function firstWsClientOptions(): {
199200
beforeAll(async () => {
200201
vi.doMock("@larksuiteoapi/node-sdk", () => ({
201202
AppType: { SelfBuild: "self" },
202-
Domain: { Feishu: "https://open.feishu.cn", Lark: "https://open.larksuite.com" },
203+
Domain: { Feishu: 0, Lark: 1 },
203204
LoggerLevel: { info: "info" },
204205
Client: clientCtorMock,
205206
WSClient: wsClientCtorMock,
@@ -277,6 +278,148 @@ describe("Feishu default User-Agent interceptor", () => {
277278
});
278279
});
279280

281+
describe("Feishu custom HTTPS API domains", () => {
282+
const sdkOrigin = "https://open.feishu.cn";
283+
const customDomain = "https://private.feishu.test:8443/reverse-proxy/";
284+
285+
function createCustomDomainHttpInstance(accountId: string): HttpInstanceLike {
286+
createFeishuClient({
287+
appId: `app_${accountId}`,
288+
appSecret: "local-test-placeholder", // pragma: allowlist secret
289+
accountId,
290+
domain: customDomain,
291+
});
292+
expect(readCallOptions(clientCtorMock).domain).toBe(0);
293+
return requireHttpInstance(readCallOptions(clientCtorMock).httpInstance);
294+
}
295+
296+
it.each(["get", "delete", "head", "options"] as const)(
297+
"routes SDK %s requests through the configured HTTPS origin, port, and path",
298+
async (method) => {
299+
const httpInstance = createCustomDomainHttpInstance(`custom-domain-${method}`);
300+
const requestUrl = `${sdkOrigin}/open-apis/im/v1/messages/om_proof?locale=en`;
301+
302+
await httpInstance[method](requestUrl, { headers: { "X-Proof": "true" } });
303+
304+
expect(mockBaseHttpInstance[method]).toHaveBeenCalledWith(
305+
"https://private.feishu.test:8443/reverse-proxy/open-apis/im/v1/messages/om_proof?locale=en",
306+
{ timeout: FEISHU_HTTP_TIMEOUT_MS, headers: { "X-Proof": "true" } },
307+
);
308+
},
309+
);
310+
311+
it.each(["post", "put", "patch"] as const)(
312+
"routes SDK %s requests without changing their body or request options",
313+
async (method) => {
314+
const httpInstance = createCustomDomainHttpInstance(`custom-domain-${method}`);
315+
const body = { content: "preserve this body" };
316+
317+
await httpInstance[method](`${sdkOrigin}/open-apis/im/v1/messages`, body, {
318+
headers: { "X-Proof": "true" },
319+
});
320+
321+
expect(mockBaseHttpInstance[method]).toHaveBeenCalledWith(
322+
"https://private.feishu.test:8443/reverse-proxy/open-apis/im/v1/messages",
323+
body,
324+
{ timeout: FEISHU_HTTP_TIMEOUT_MS, headers: { "X-Proof": "true" } },
325+
);
326+
},
327+
);
328+
329+
it("routes request options after preserving Feishu SDK multipart normalization", async () => {
330+
const httpInstance = createCustomDomainHttpInstance("custom-domain-upload");
331+
332+
await httpInstance.request({
333+
url: `${sdkOrigin}/open-apis/im/v1/files?folder=root`,
334+
method: "POST",
335+
headers: { "Content-Type": "multipart/form-data" },
336+
data: { file_type: "stream", file_name: "proof.txt", file: Buffer.from("proof") },
337+
});
338+
339+
const delegated = readCallOptions(mockBaseHttpInstance.request);
340+
expect(delegated.url).toBe(
341+
"https://private.feishu.test:8443/reverse-proxy/open-apis/im/v1/files?folder=root",
342+
);
343+
expect(delegated.timeout).toBe(FEISHU_HTTP_TIMEOUT_MS);
344+
expect(delegated.data).toBeInstanceOf(FormData);
345+
expect((delegated.data as FormData).get("file_type")).toBe("stream");
346+
});
347+
348+
it.each([
349+
"https://open.feishu.cn.evil.test/open-apis/im/v1/messages",
350+
"https://open.feishu.cn:444/open-apis/im/v1/messages",
351+
"https://unrelated.example/open-apis/im/v1/messages",
352+
])("does not rewrite an unrelated request origin: %s", async (url) => {
353+
const httpInstance = createCustomDomainHttpInstance(`custom-domain-external-${url.length}`);
354+
355+
await httpInstance.get(url);
356+
357+
expect(mockBaseHttpInstance.get).toHaveBeenCalledWith(url, {
358+
timeout: FEISHU_HTTP_TIMEOUT_MS,
359+
});
360+
});
361+
362+
it("keeps independent account transport origins isolated", async () => {
363+
const first = createCustomDomainHttpInstance("custom-domain-first-account");
364+
createFeishuClient({
365+
appId: "app_second_account",
366+
appSecret: "local-test-placeholder", // pragma: allowlist secret
367+
accountId: "custom-domain-second-account",
368+
domain: "https://another.feishu.test:9443/tenant",
369+
});
370+
const second = requireHttpInstance(readCallOptions(clientCtorMock).httpInstance);
371+
372+
await first.get(`${sdkOrigin}/open-apis/im/v1/chats/oc_first`);
373+
await second.get(`${sdkOrigin}/open-apis/im/v1/chats/oc_second`);
374+
375+
expect(mockBaseHttpInstance.get.mock.calls.map((call) => call[0])).toEqual([
376+
"https://private.feishu.test:8443/reverse-proxy/open-apis/im/v1/chats/oc_first",
377+
"https://another.feishu.test:9443/tenant/open-apis/im/v1/chats/oc_second",
378+
]);
379+
});
380+
381+
it("routes WebSocket endpoint discovery through the same configured origin", async () => {
382+
await createFeishuWSClient({
383+
...baseAccount,
384+
accountId: "custom-domain-websocket",
385+
domain: customDomain,
386+
});
387+
388+
const options = readCallOptions(wsClientCtorMock);
389+
expect(options.domain).toBe(0);
390+
const httpInstance = requireHttpInstance(options.httpInstance);
391+
await httpInstance.request({ url: `${sdkOrigin}/callback/ws/endpoint`, method: "post" });
392+
393+
expect(mockBaseHttpInstance.request).toHaveBeenCalledWith({
394+
url: "https://private.feishu.test:8443/reverse-proxy/callback/ws/endpoint",
395+
method: "post",
396+
timeout: FEISHU_HTTP_TIMEOUT_MS,
397+
});
398+
});
399+
400+
it.each([
401+
["feishu", 0, "https://open.feishu.cn"],
402+
["lark", 1, "https://open.larksuite.com"],
403+
] as const)(
404+
"preserves the existing %s SDK domain and direct requests",
405+
async (domain, sdkDomain, url) => {
406+
createFeishuClient({
407+
appId: `app_${domain}`,
408+
appSecret: "local-test-placeholder", // pragma: allowlist secret
409+
accountId: `official-domain-${domain}`,
410+
domain,
411+
});
412+
413+
const options = readCallOptions(clientCtorMock);
414+
expect(options.domain).toBe(sdkDomain);
415+
await requireHttpInstance(options.httpInstance).get(`${url}/open-apis/im/v1/messages`);
416+
expect(mockBaseHttpInstance.get).toHaveBeenCalledWith(`${url}/open-apis/im/v1/messages`, {
417+
timeout: FEISHU_HTTP_TIMEOUT_MS,
418+
});
419+
},
420+
);
421+
});
422+
280423
describe("createFeishuClient HTTP timeout", () => {
281424
const readLastClientHttpInstance = (): HttpInstanceLike =>
282425
requireHttpInstance(readCallOptions(clientCtorMock).httpInstance);

‎extensions/feishu/src/client.ts‎

Lines changed: 47 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const require = createRequire(import.meta.url);
1414
const pluginVersion = readPluginPackageVersion({ require });
1515

1616
const FEISHU_USER_AGENT = `openclaw-feishu-builtin/${pluginVersion}/${process.platform}`;
17+
const FEISHU_SDK_ORIGIN = "https://open.feishu.cn";
1718

1819
const FEISHU_WS_CONFIG = {
1920
pingTimeout: 3,
@@ -260,14 +261,10 @@ const clientCache = new Map<
260261
}
261262
>();
262263

263-
function resolveDomain(domain: FeishuDomain | undefined): Lark.Domain | string {
264-
if (domain === "lark") {
265-
return feishuClientSdk.Domain.Lark;
266-
}
267-
if (domain === "feishu" || !domain) {
268-
return feishuClientSdk.Domain.Feishu;
269-
}
270-
return domain.replace(/\/+$/, ""); // Custom URL for private deployment
264+
function resolveSdkDomain(domain: FeishuDomain | undefined): Lark.Domain {
265+
// The SDK parses :port in its domain as an API route parameter; custom origins
266+
// must stay in their account-owned HTTP transport until path expansion ends.
267+
return domain === "lark" ? feishuClientSdk.Domain.Lark : feishuClientSdk.Domain.Feishu;
271268
}
272269

273270
/**
@@ -276,13 +273,38 @@ function resolveDomain(domain: FeishuDomain | undefined): Lark.Domain | string {
276273
* indefinite hangs, set a standardized User-Agent per OAPI best practices, and
277274
* keep axios from taking a separate ambient proxy path for HTTPS requests.
278275
*/
279-
function createFeishuHttpInstance(defaultTimeoutMs: number): Lark.HttpInstance {
276+
function createFeishuHttpInstance(
277+
defaultTimeoutMs: number,
278+
configuredDomain?: FeishuDomain,
279+
): Lark.HttpInstance {
280280
const base: FeishuHttpInstanceLike = feishuClientSdk.defaultHttpInstance;
281+
const customDomain =
282+
configuredDomain && configuredDomain !== "feishu" && configuredDomain !== "lark"
283+
? new URL(configuredDomain)
284+
: undefined;
285+
286+
function resolveRequestUrl(url: string): string {
287+
if (!customDomain) {
288+
return url;
289+
}
290+
const requestUrl = new URL(url);
291+
if (requestUrl.origin !== FEISHU_SDK_ORIGIN) {
292+
return url;
293+
}
294+
const destination = new URL(customDomain);
295+
destination.pathname = `${destination.pathname.replace(/\/+$/, "")}${requestUrl.pathname}`;
296+
destination.search = requestUrl.search;
297+
destination.hash = requestUrl.hash;
298+
return destination.toString();
299+
}
281300

282301
async function injectRequestOptions<D>(
283302
opts?: Lark.HttpRequestOptions<D>,
284303
): Promise<FeishuProxyAwareHttpRequestOptions<D>> {
285304
const next: FeishuProxyAwareHttpRequestOptions<D> = { timeout: defaultTimeoutMs, ...opts };
305+
if (typeof next.url === "string") {
306+
next.url = resolveRequestUrl(next.url);
307+
}
286308
const agent = await getFeishuProxyAgent();
287309
if (agent) {
288310
if (isManagedProxyActive()) {
@@ -300,13 +322,18 @@ function createFeishuHttpInstance(defaultTimeoutMs: number): Lark.HttpInstance {
300322
return {
301323
request: async (opts) =>
302324
base.request(await injectRequestOptions(normalizeMultipartUploadData(opts))),
303-
get: async (url, opts) => base.get(url, await injectRequestOptions(opts)),
304-
post: async (url, data, opts) => base.post(url, data, await injectRequestOptions(opts)),
305-
put: async (url, data, opts) => base.put(url, data, await injectRequestOptions(opts)),
306-
patch: async (url, data, opts) => base.patch(url, data, await injectRequestOptions(opts)),
307-
delete: async (url, opts) => base.delete(url, await injectRequestOptions(opts)),
308-
head: async (url, opts) => base.head(url, await injectRequestOptions(opts)),
309-
options: async (url, opts) => base.options(url, await injectRequestOptions(opts)),
325+
get: async (url, opts) => base.get(resolveRequestUrl(url), await injectRequestOptions(opts)),
326+
post: async (url, data, opts) =>
327+
base.post(resolveRequestUrl(url), data, await injectRequestOptions(opts)),
328+
put: async (url, data, opts) =>
329+
base.put(resolveRequestUrl(url), data, await injectRequestOptions(opts)),
330+
patch: async (url, data, opts) =>
331+
base.patch(resolveRequestUrl(url), data, await injectRequestOptions(opts)),
332+
delete: async (url, opts) =>
333+
base.delete(resolveRequestUrl(url), await injectRequestOptions(opts)),
334+
head: async (url, opts) => base.head(resolveRequestUrl(url), await injectRequestOptions(opts)),
335+
options: async (url, opts) =>
336+
base.options(resolveRequestUrl(url), await injectRequestOptions(opts)),
310337
};
311338
}
312339

@@ -352,8 +379,8 @@ export function createFeishuClient(creds: FeishuClientCredentials): Lark.Client
352379
appId,
353380
appSecret,
354381
appType: feishuClientSdk.AppType.SelfBuild,
355-
domain: resolveDomain(domain),
356-
httpInstance: createFeishuHttpInstance(defaultHttpTimeoutMs),
382+
domain: resolveSdkDomain(domain),
383+
httpInstance: createFeishuHttpInstance(defaultHttpTimeoutMs, domain),
357384
});
358385

359386
// Cache it
@@ -389,8 +416,8 @@ export async function createFeishuWSClient(
389416
return new feishuClientSdk.WSClient({
390417
appId,
391418
appSecret,
392-
domain: resolveDomain(domain),
393-
httpInstance: createFeishuHttpInstance(defaultHttpTimeoutMs),
419+
domain: resolveSdkDomain(domain),
420+
httpInstance: createFeishuHttpInstance(defaultHttpTimeoutMs, domain),
394421
...callbacks,
395422
loggerLevel: feishuClientSdk.LoggerLevel.info,
396423
wsConfig: FEISHU_WS_CONFIG,

0 commit comments

Comments
 (0)