fix(approval): reject execution across session corporation boundaries

This commit is contained in:
gitea committed 2026-10-20 17:48:03 +08:00
1 parent fac81b08e8
commit 221b9596b8
4 files changed
+219 -182

No files matched your search

@@ -0,0 +1,30 @@
import { afterEach, expect, mock, spyOn, test } from "bun:test";
import { getDatabase, closeDatabase } from "../../../core/db/index.js";
import { ApprovalRepository } from "../../../domains/approval/repository.js";
import * as bills from "../../../domains/bill/tools.js";
import { handleAiProDecide } from "./session.js";
afterEach(() => { closeDatabase(); });
for (const action of ["approve", "reject"] as const) {
test(`blocks cross-corp ${action} before creating a client or changing the run`, async () => {
const repo = new ApprovalRepository(getDatabase(":memory:"));
const run = repo.createApprovalRun({ documentSn: "SAME-SN", corpId: "corp-A" });
repo.injectFindings(run.id, [{ clauseText: "invoice", strictness: "硬性", checkStatus: action === "approve" ? "PASS" : "FAIL" }]);
const getClient = mock(async () => ({ client: {} as any, corpId: "corp-B" }));
await expect(handleAiProDecide({ repo, corpId: "corp-B", command: action, positional: [action, run.shortId!], parsed: { options: { apply: true, yes: true, opinion: "checked" } }, getClient } as any)).rejects.toThrow("不一致");
expect(getClient).not.toHaveBeenCalled();
expect(repo.getRunById(run.id)?.decision).toBe("PENDING");
});
}
test("uses the checked corp client for same-corp approval", async () => {
const repo = new ApprovalRepository(getDatabase(":memory:"));
const run = repo.createApprovalRun({ documentSn: "SAME-SN", corpId: "corp-A" });
repo.injectFindings(run.id, [{ clauseText: "invoice", strictness: "硬性", checkStatus: "PASS" }]);
const approve = spyOn(bills, "approveBill").mockResolvedValue({} as any);
const client = {} as any;
try {
const result = await handleAiProDecide({ repo, corpId: "corp-A", command: "approve", positional: ["approve", run.shortId!], parsed: { options: { apply: true, yes: true, opinion: "checked" } }, getClient: async () => ({ client, corpId: "corp-A" }), rawArgs: [], operator: "staff" } as any);
expect(result.exitCode).toBe(0);
expect(approve).toHaveBeenCalledWith(client, "corp-A", "SAME-SN", { comment: "checked" });
} finally { approve.mockRestore(); }
});
+5
View File
@@ -1,3 +1,4 @@
import { PermissionError } from "../../../core/errors.js";
import {
type ApprovalRunRecord,
type CreateFindingInput,
@@ -440,6 +441,9 @@ export async function handleAiProDecide(ctx: AiProExecutionContext): Promise<AiP
let executionStatus = "模拟完成(未带 --apply,未真实修改线上单据)";
if (apply) {
if (!runRecord.corpId || ctx.corpId !== runRecord.corpId) {
throw new PermissionError(`审批会话所属企业 [${runRecord.corpId || "未知"}] 与当前企业 [${ctx.corpId || "未选择"}] 不一致,请显式选择会话所属企业。`);
}
const confirmed = booleanOption(parsed.options, ["yes", "y", "confirm"]);
if (!confirmed) {
if (!process.stdin.isTTY || !process.stdout.isTTY) {
@@ -453,6 +457,7 @@ export async function handleAiProDecide(ctx: AiProExecutionContext): Promise<AiP
}
const context = await getClient();
if (context.corpId !== runRecord.corpId) throw new PermissionError("审批客户端企业与会话所属企业不一致");
if (action === "APPROVE") {
await approveBill(context.client, context.corpId, runRecord.documentSn, {
comment: opinion,