forked from EduCraft/curriculum-project-hub
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:
@@ -11,11 +11,14 @@ import {
|
||||
seedProject,
|
||||
seedTestOrganization,
|
||||
silentLogger,
|
||||
testSecretEnvelope,
|
||||
} from "./helpers.js";
|
||||
import { InMemoryModelRegistry } from "../../src/agent/models.js";
|
||||
import { makeTriggerHandler as makeProductionTriggerHandler, extractPrompt } from "../../src/feishu/trigger.js";
|
||||
import { TriggerQueue } from "../../src/feishu/triggerQueue.js";
|
||||
import type { MessageReceiveEvent, CardActionEvent } from "../../src/feishu/client.js";
|
||||
import type { FeishuBotCli } from "../../src/feishu/botCli.js";
|
||||
import { writeNewWorkspaceFileNoFollow } from "../../src/security/workspaceFiles.js";
|
||||
import type { RunRequest, RunResult } from "../../src/agent/runner.js";
|
||||
import type { RuntimeSettings } from "../../src/settings/runtime.js";
|
||||
|
||||
@@ -34,6 +37,7 @@ function makeTriggerHandler(deps: TestTriggerDeps): ReturnType<typeof makeProduc
|
||||
publicBaseUrl: "https://educraft.example.test",
|
||||
siloOrganizationId: DEFAULT_ORG_ID,
|
||||
allowLegacyFeishuIdentity: true,
|
||||
secretEnvelope: testSecretEnvelope,
|
||||
...deps,
|
||||
});
|
||||
}
|
||||
@@ -190,13 +194,14 @@ describe("trigger full lifecycle (integration)", () => {
|
||||
where: { id: "proj-post-image" },
|
||||
data: { workspaceDir },
|
||||
});
|
||||
const messageResourceGet = vi.fn(async () => ({
|
||||
getReadableStream: () => Readable.from([Buffer.from("image bytes")]),
|
||||
}));
|
||||
const imV1 = (rt.client as unknown as {
|
||||
im: { v1: { messageResource?: { get: typeof messageResourceGet } } };
|
||||
}).im.v1;
|
||||
imV1.messageResource = { get: messageResourceGet };
|
||||
const downloadResource = vi.fn(async (request) => writeNewWorkspaceFileNoFollow(
|
||||
request.workspaceRoot,
|
||||
request.workspaceDir,
|
||||
request.workspaceRelativePath,
|
||||
Readable.from([Buffer.from("image bytes")]),
|
||||
request.maxBytes,
|
||||
));
|
||||
const feishuBotCli: FeishuBotCli = { downloadResource };
|
||||
const baseEvent = makeEvent("chat-post-image", "@_user_1 看看这张图");
|
||||
const event: MessageReceiveEvent = {
|
||||
...baseEvent,
|
||||
@@ -220,6 +225,7 @@ describe("trigger full lifecycle (integration)", () => {
|
||||
runAgent,
|
||||
projectWorkspaceRoot: workspaceRoot,
|
||||
messageBatcherOptions: { maxMessages: 1 },
|
||||
feishuBotCli,
|
||||
});
|
||||
|
||||
await trigger(event, rt);
|
||||
@@ -228,10 +234,11 @@ describe("trigger full lifecycle (integration)", () => {
|
||||
expect(runAgentCalls).toHaveLength(1);
|
||||
});
|
||||
expect(runAgentCalls[0]?.prompt).toContain(join(await realpath(workspaceDir), ".cph", "inbox"));
|
||||
expect(messageResourceGet).toHaveBeenCalledWith({
|
||||
params: { type: "image" },
|
||||
path: { message_id: event.message.message_id, file_key: "img-key-1" },
|
||||
});
|
||||
expect(downloadResource).toHaveBeenCalledWith(expect.objectContaining({
|
||||
messageId: event.message.message_id,
|
||||
fileKey: "img-key-1",
|
||||
resourceType: "image",
|
||||
}));
|
||||
const inboxFiles = await readdir(join(workspaceDir, ".cph", "inbox"));
|
||||
expect(inboxFiles).toHaveLength(1);
|
||||
await expect(readFile(join(workspaceDir, ".cph", "inbox", inboxFiles[0]!))).resolves.toEqual(Buffer.from("image bytes"));
|
||||
@@ -1109,15 +1116,18 @@ describe("trigger full lifecycle (integration)", () => {
|
||||
});
|
||||
const resourceEntered = deferred<void>();
|
||||
const releaseResource = deferred<void>();
|
||||
const messageResourceGet = vi.fn(async () => {
|
||||
const downloadResource = vi.fn(async (request) => {
|
||||
resourceEntered.resolve();
|
||||
await releaseResource.promise;
|
||||
return { getReadableStream: () => Readable.from([Buffer.from("staged image bytes")]) };
|
||||
return writeNewWorkspaceFileNoFollow(
|
||||
request.workspaceRoot,
|
||||
request.workspaceDir,
|
||||
request.workspaceRelativePath,
|
||||
Readable.from([Buffer.from("staged image bytes")]),
|
||||
request.maxBytes,
|
||||
);
|
||||
});
|
||||
const imV1 = (rt.client as unknown as {
|
||||
im: { v1: { messageResource?: { get: typeof messageResourceGet } } };
|
||||
}).im.v1;
|
||||
imV1.messageResource = { get: messageResourceGet };
|
||||
const feishuBotCli: FeishuBotCli = { downloadResource };
|
||||
const baseEvent = makeEvent("chat-attachment-race", "@_user_1 附件竞态");
|
||||
const event: MessageReceiveEvent = {
|
||||
...baseEvent,
|
||||
@@ -1141,6 +1151,7 @@ describe("trigger full lifecycle (integration)", () => {
|
||||
runAgent,
|
||||
projectWorkspaceRoot: workspaceRoot,
|
||||
messageBatcherOptions: { maxMessages: 1 },
|
||||
feishuBotCli,
|
||||
});
|
||||
|
||||
const pendingTrigger = trigger(event, rt);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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, "\\$&");
|
||||
}
|
||||
|
||||
@@ -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" },
|
||||
|
||||
Reference in New Issue
Block a user