fix(approval): enforce session expiry for every operation handle

This commit is contained in:
gitea committed 2026-10-20 17:58:33 +08:00
1 parent fac81b08e8
commit b69b78fc30
5 files changed
+69 -38

No files matched your search

+2 -2
View File
@@ -88,9 +88,9 @@ export async function handleAiProLogs(ctx: AiProExecutionContext): Promise<AiPro
let runRecord: ApprovalRunRecord | null = null;
if (runHandle) {
runRecord = repo.getRunByHandle(runHandle);
runRecord = repo.getRunByHandle(runHandle, undefined, { allowExpired: true });
} else if (targetSn) {
const maybeRun = repo.getRunByHandle(targetSn);
const maybeRun = repo.getRunByHandle(targetSn, undefined, { allowExpired: true });
if (maybeRun?.shortId && maybeRun.shortId.toUpperCase() === targetSn.toUpperCase()) {
runRecord = maybeRun;
}
+2
View File
@@ -15,3 +15,5 @@
- 决策必须经 gateway 仲裁:`ApprovalDecision` 为封闭枚举(`APPROVE` / `REJECT` / `PASS_WITH_NOTE` / `REVIEW` / `PENDING` / `ERROR`),写动作由 `validateGatewayAction` 卡点。
- 规则严格度分四级:`硬性` / `弹性` / `建议` / `特殊`。
审批操作按短 ID、UUID 或单号访问会话时统一检查 6 小时有效期;重新注入会话会刷新有效期。历史日志显式使用只读过期访问,不删除历史记录,也不放宽审批入口。
+15 -4
View File
@@ -339,10 +339,11 @@ export class ApprovalRepository {
this.db.run(
`
UPDATE approval_runs
SET flow_id = ?, corp_id = ?, scope = ?, decision = ?, decision_summary = ?, matched_rules_count = ?, failed_rules_count = ?, document_snapshot = ?, updated_at = ?
SET expires_at = ?, flow_id = ?, corp_id = ?, scope = ?, decision = ?, decision_summary = ?, matched_rules_count = ?, failed_rules_count = ?, document_snapshot = ?, updated_at = ?
WHERE id = ?
`,
[
input.expiresAt ?? (now + 6 * 3600 * 1000),
(input.flowId !== undefined ? input.flowId : existing.flowId) ?? null,
(input.corpId !== undefined ? input.corpId : existing.corpId) ?? null,
(input.scope !== undefined ? input.scope : existing.scope) ?? null,
@@ -361,11 +362,21 @@ export class ApprovalRepository {
return this.createApprovalRun(input);
}
public getRunByHandle(handle: string, corpId?: string): ApprovalRunRecord | null {
public getRunByHandle(handle: string, corpId?: string, options: { allowExpired?: boolean } = {}): ApprovalRunRecord | null {
const trimmed = handle.trim();
const now = Date.now();
const requireActive = (run: ApprovalRunRecord | null) => {
if (run?.expiresAt && run.expiresAt <= now && !options.allowExpired) {
throw new BusinessError(`审批会话 [${trimmed}] 已过期(有效时长 6 小时),请运行 hose-aipro init 重新发起审批会话`);
}
return run;
};
// 1. 如果符合 6 位大写短 ID 特征
if (/^[A-Za-z0-9]{6}$/.test(trimmed)) {
if (options.allowExpired) {
const historical = this.db.query("SELECT * FROM approval_runs WHERE UPPER(short_id) = UPPER(?)").get(trimmed) as any;
if (historical) return this.mapRunRow(historical);
}
const row = this.db
.query("SELECT * FROM approval_runs WHERE UPPER(short_id) = UPPER(?) AND (expires_at IS NULL OR expires_at > ?)")
.get(trimmed, now) as any;
@@ -377,9 +388,9 @@ export class ApprovalRepository {
}
// 2. 尝试按长 UUID 查找
const byId = this.getRunById(trimmed);
if (byId) return byId;
if (byId) return requireActive(byId);
// 3. 尝试按单号查找最新未过期的运行记录
return this.getLatestRunBySn(trimmed, corpId);
return requireActive(this.getLatestRunBySn(trimmed, corpId));
}
public getLatestRunBySn(documentSn: string, corpId?: string): ApprovalRunRecord | null {
@@ -0,0 +1,18 @@
import { afterEach, expect, test } from "bun:test";
import { getDatabase, closeDatabase } from "../../core/db/index.js";
import { ApprovalRepository } from "./repository.js";
afterEach(closeDatabase);
test("all operation handles reject expired sessions while history remains readable", async () => {
const repo = new ApprovalRepository(getDatabase(":memory:"));
const run = repo.createApprovalRun({ documentSn: "EXPIRED-SN", corpId: "corp", expiresAt: Date.now() - 1000 });
for (const handle of [run.shortId!, run.id, run.documentSn]) {
expect(() => repo.getRunByHandle(handle)).toThrow("已过期");
expect(repo.getRunByHandle(handle, undefined, { allowExpired: true })?.id).toBe(run.id);
}
repo.logAiProAction({ action: "init", runId: run.shortId, documentSn: run.documentSn });
expect(repo.getRunById(run.id)?.id).toBe(run.id);
const reset = repo.createOrResetApprovalRun({ documentSn: run.documentSn, corpId: "corp" });
expect(reset.expiresAt!).toBeGreaterThan(Date.now());
expect(repo.getRunByHandle(run.shortId!)?.id).toBe(run.id);
});