fix(hub): download Feishu resources via bot-owned lark-cli

Agent tool downloads and trigger attachment staging both used the SDK
messageResource path, which fails closed for multi-MB teacher files and
did not share the bot-identity transport contract. Route every download
through Hub-owned createFeishuBotCli (secret via stdin, disposable HOME,
HUB_FEISHU_CLI_BIN), keep workspace containment on write, and inject the
adapter in trigger tests.
This commit is contained in:
2026-07-30 11:27:53 +08:00
parent 97c7054529
commit 88386fb943
11 changed files with 456 additions and 56 deletions
+101
View File
@@ -0,0 +1,101 @@
import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { describe, expect, it } from "vitest";
import { createFeishuBotCli } from "../../src/feishu/botCli.js";
import type { PrismaClient } from "@prisma/client";
import type { LocalSecretEnvelope } from "../../src/security/secretEnvelope.js";
const itOnLinux = process.platform === "linux" ? it : it.skip;
const fakeCredential = {
connectionId: "connection-1",
organizationId: "org-1",
appId: "cli-test-app",
appSecret: "cli-test-secret",
botOpenId: "ou-test-bot",
verificationToken: "verification-token",
encryptKey: "encrypt-key",
};
describe("Feishu bot CLI adapter", () => {
itOnLinux("uses bot identity and writes the CLI result into the workspace", async () => {
const root = await mkdtemp(join(tmpdir(), "hub-feishu-bot-cli-test-"));
const workspaceDir = join(root, "workspace");
const binary = join(root, "fake-lark-cli");
await mkdir(workspaceDir);
await writeFakeCli(binary);
try {
const cli = createFeishuBotCli({
organizationId: "org-1",
prisma: {} as PrismaClient,
secretEnvelope: {} as LocalSecretEnvelope,
binary,
resolveCredential: async () => fakeCredential,
});
const result = await cli.downloadResource({
messageId: "message-1",
fileKey: "file-1",
resourceType: "file",
workspaceRoot: root,
workspaceDir,
workspaceRelativePath: "inbox/resource.bin",
maxBytes: 1024,
});
await expect(readFile(result, "utf8")).resolves.toBe("resource bytes");
} finally {
await rm(root, { recursive: true, force: true });
}
});
it("rejects a resource above the configured limit", async () => {
const root = await mkdtemp(join(tmpdir(), "hub-feishu-bot-cli-limit-"));
const workspaceDir = join(root, "workspace");
const binary = join(root, "fake-lark-cli");
await mkdir(workspaceDir);
await writeFakeCli(binary);
try {
const cli = createFeishuBotCli({
organizationId: "org-1",
prisma: {} as PrismaClient,
secretEnvelope: {} as LocalSecretEnvelope,
binary,
resolveCredential: async () => fakeCredential,
});
await expect(cli.downloadResource({
messageId: "message-1",
fileKey: "file-1",
resourceType: "file",
workspaceRoot: root,
workspaceDir,
workspaceRelativePath: "inbox/resource.bin",
maxBytes: 4,
})).rejects.toMatchObject({ reason: "limit" });
} finally {
await rm(root, { recursive: true, force: true });
}
});
});
async function writeFakeCli(path: string): Promise<void> {
await writeFile(path, `#!/usr/bin/env node
import { writeFileSync } from "node:fs";
import { join } from "node:path";
const args = process.argv.slice(2);
if (args[0] === "config" && args[1] === "init") {
process.stdin.resume();
process.stdin.on("end", () => process.exit(0));
} else if (args.includes("+messages-resources-download")) {
const asIndex = args.indexOf("--as");
const outputIndex = args.indexOf("--output");
if (asIndex < 0 || args[asIndex + 1] !== "bot" || outputIndex < 0) process.exit(2);
writeFileSync(join(process.cwd(), args[outputIndex + 1]), "resource bytes");
process.exit(0);
} else {
process.exit(3);
}
`);
await chmod(path, 0o755);
}
+24 -11
View File
@@ -5,6 +5,8 @@ import { Readable } from "node:stream";
import { describe, expect, it, vi } from "vitest";
import { claudeSdkToolConfigForRole, cphHubMcpToolsForRole } from "../../src/agent/roleTools.js";
import type { FeishuRuntime } from "../../src/feishu/client.js";
import type { FeishuBotCli } from "../../src/feishu/botCli.js";
import { writeNewWorkspaceFileNoFollow } from "../../src/security/workspaceFiles.js";
import { downloadFeishuMessageResource } from "../../src/feishu/download.js";
const itOnLinux = process.platform === "linux" ? it : it.skip;
@@ -27,14 +29,12 @@ describe("Feishu message resource download", () => {
await mkdir(workspaceDir);
try {
const messageGet = vi.fn(async () => ({ data: { items: [{ chat_id: "chat-1" }] } }));
const messageResourceGet = vi.fn(async () => ({
getReadableStream: () => Readable.from([Buffer.from("image bytes")]),
}));
const messageResourceGet = vi.fn();
const rt = mockRuntime(messageGet, messageResourceGet);
const botCli = fakeBotCli();
const result = await downloadFeishuMessageResource(
{ messageId: "message-1", fileKey: "img-key-1", resourceType: "image" },
{ boundChatId: "chat-1", workspaceRoot, workspaceDir },
{ boundChatId: "chat-1", workspaceRoot, workspaceDir, botCli },
rt,
);
@@ -44,10 +44,7 @@ describe("Feishu message resource download", () => {
);
expect(result.path).toMatch(/\.png$/);
await expect(readFile(result.path, "utf8")).resolves.toBe("image bytes");
expect(messageResourceGet).toHaveBeenCalledWith({
params: { type: "image" },
path: { message_id: "message-1", file_key: "img-key-1" },
});
expect(messageResourceGet).not.toHaveBeenCalled();
} finally {
await rm(workspaceRoot, { recursive: true, force: true });
}
@@ -60,10 +57,14 @@ describe("Feishu message resource download", () => {
await expect(downloadFeishuMessageResource(
{ messageId: "message-other", fileKey: "img-key-other", resourceType: "image" },
{ boundChatId: "chat-1", workspaceRoot: "/tmp", workspaceDir: "/tmp/project-1" },
{
boundChatId: "chat-1",
workspaceRoot: "/tmp",
workspaceDir: "/tmp/project-1",
botCli: fakeBotCli(),
},
rt,
)).rejects.toThrow("current project's bound chat");
expect(messageResourceGet).not.toHaveBeenCalled();
});
});
@@ -92,6 +93,18 @@ function mockRuntime(
};
}
function fakeBotCli(): FeishuBotCli {
return {
downloadResource: (request) => writeNewWorkspaceFileNoFollow(
request.workspaceRoot,
request.workspaceDir,
request.workspaceRelativePath,
Readable.from([Buffer.from("image bytes")]),
request.maxBytes,
),
};
}
function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
+7 -2
View File
@@ -3,7 +3,7 @@ import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { removeAbandonedMessageResourceStages, stageMessageResources } from "../../src/feishu/resourceStaging.js";
import type { FeishuRuntime } from "../../src/feishu/client.js";
import type { FeishuBotCli } from "../../src/feishu/botCli.js";
const roots: string[] = [];
@@ -34,8 +34,13 @@ describe("Feishu resource staging recovery", () => {
it("rejects too many resources before contacting Feishu", async () => {
const root = await tempRoot();
const botCli: FeishuBotCli = {
downloadResource: async () => {
throw new Error("should not contact Feishu when over limit");
},
};
await expect(stageMessageResources(
{} as FeishuRuntime,
botCli,
"message-1",
[
{ fileKey: "a", resourceType: "file", workspaceRelativePath: "a" },