fix(approval): validate finding states and fail closed on corrupt data

This commit is contained in:
gitea committed 2026-10-20 17:48:48 +08:00
1 parent fac81b08e8
commit a5abfbe077
7 files changed
+366 -328

No files matched your search

+326 -326
View File
File diff suppressed because it is too large. Load diff
+2
View File
@@ -15,3 +15,5 @@
- 决策必须经 gateway 仲裁:`ApprovalDecision` 为封闭枚举(`APPROVE` / `REJECT` / `PASS_WITH_NOTE` / `REVIEW` / `PENDING` / `ERROR`),写动作由 `validateGatewayAction` 卡点。
- 规则严格度分四级:`硬性` / `弹性` / `建议` / `特殊`。
核验状态在批量核验、存储及网关入口均限定为 `PENDING/PASS/FAIL/SKIP`;未知状态拒绝写入,历史异常状态阻断决策。批量状态预检失败不会部分更新要点。
+2
View File
@@ -3,6 +3,7 @@ import {
ApprovalFindingRecord,
ApprovalGatewayBlockedError,
StrictnessTier,
isValidFindingCheckStatus,
} from "./types.js";
/**
@@ -104,6 +105,7 @@ export function analyzeFindings(findings: ApprovalFindingRecord[]): FindingsAnal
const specialFailed: ApprovalFindingRecord[] = [];
for (const f of findings) {
if (!isValidFindingCheckStatus(f.checkStatus)) throw new ApprovalGatewayBlockedError("审核要点包含非法核验状态,禁止审批决策");
if (f.checkStatus === "PENDING") {
pendingFindings.push(f);
} else if (f.checkStatus === "PASS") {
+7 -1
View File
@@ -1,6 +1,6 @@
import { Database } from "bun:sqlite";
import { getDatabase } from "../../core/db/index.js";
import { BusinessError, NotFoundError } from "../../core/errors.js";
import { BusinessError, NotFoundError, ValidationError } from "../../core/errors.js";
import { parseRuleBody, validatePreferenceText } from "./parser.js";
import {
AiProLogRecord,
@@ -8,6 +8,7 @@ import {
ApprovalFindingRecord,
ApprovalRunRecord,
FindingCheckStatus,
isValidFindingCheckStatus,
PreferenceRecord,
RuleRecord,
ScopeDetail,
@@ -472,6 +473,10 @@ export class ApprovalRepository {
throw new NotFoundError(`审批运行记录不存在: ${runId}`);
}
for (const finding of findings) {
if (!isValidFindingCheckStatus(finding.checkStatus ?? "PENDING")) throw new ValidationError("核验状态必须为 PENDING、PASS、FAIL 或 SKIP");
}
// 重新挂载时清空旧要点
this.db.run("DELETE FROM approval_findings WHERE run_id = ?", [runId]);
@@ -526,6 +531,7 @@ export class ApprovalRepository {
}
const checkStatus = updates.checkStatus ?? row.check_status;
if (!isValidFindingCheckStatus(checkStatus)) throw new ValidationError("核验状态必须为 PENDING、PASS、FAIL 或 SKIP");
const evidence = updates.evidence !== undefined ? updates.evidence : row.evidence;
const reasoning = updates.reasoning !== undefined ? updates.reasoning : row.reasoning;
+5 -1
View File
@@ -1,5 +1,5 @@
import { KyInstance } from "ky";
import { BusinessError, NotFoundError } from "../../core/errors.js";
import { BusinessError, NotFoundError, ValidationError } from "../../core/errors.js";
import { ApprovalRepository } from "./repository.js";
import { parseRuleBody } from "./parser.js";
import {
@@ -8,6 +8,7 @@ import {
ApprovalGatewayBlockedError,
ApprovalRunRecord,
FindingCheckStatus,
isValidFindingCheckStatus,
} from "./types.js";
import {
ApprovalGatewayPolicy,
@@ -184,6 +185,9 @@ export class ApprovalService {
);
}
if (evaluations.some(item => !isValidFindingCheckStatus(item.status))) {
throw new ValidationError("核验状态必须为 PENDING、PASS、FAIL 或 SKIP");
}
for (const item of evaluations) {
let target: ApprovalFindingRecord | undefined;
if (item.id) {
@@ -0,0 +1,20 @@
import { afterEach, expect, test } from "bun:test";
import { getDatabase, closeDatabase } from "../../core/db/index.js";
import { ApprovalRepository } from "./repository.js";
import { ApprovalService } from "./service.js";
import { evaluateGatewayPolicy } from "./gateway.js";
afterEach(closeDatabase);
test("rejects unknown status before updates and blocks corrupt persisted findings", () => {
const db = getDatabase(":memory:");
const repo = new ApprovalRepository(db);
const run = repo.createApprovalRun({ documentSn: "SN", corpId: "corp" });
const findings = repo.injectFindings(run.id, [{ clauseText: "invoice", strictness: "硬性", checkStatus: "PENDING" }, { clauseText: "amount", strictness: "硬性", checkStatus: "PENDING" }]);
expect(() => repo.updateFinding(findings[0].id, { checkStatus: "TYPO" as any })).toThrow("核验状态");
expect(() => new ApprovalService(repo).evaluateAuditPoints("SN", [{ index: 1, status: "PASS" }, { index: 2, status: "TYPO" as any }], "corp")).toThrow("核验状态");
expect(repo.getFindingsByRunId(run.id).map(f => f.checkStatus)).toEqual(["PENDING", "PENDING"]);
expect(() => repo.injectFindings(run.id, [{ clauseText: "bad", strictness: "硬性", checkStatus: "TYPO" as any }])).toThrow("核验状态");
expect(repo.getFindingsByRunId(run.id)).toHaveLength(2);
db.run("UPDATE approval_findings SET check_status = 'TYPO' WHERE run_id = ?", [run.id]);
expect(() => evaluateGatewayPolicy({ documentSn: "SN", findings: repo.getFindingsByRunId(run.id) })).toThrow("非法核验状态");
});
+4
View File
@@ -94,6 +94,10 @@ export interface ApprovalRunRecord {
export type FindingCheckStatus = "PENDING" | "PASS" | "FAIL" | "SKIP";
export function isValidFindingCheckStatus(value: unknown): value is FindingCheckStatus {
return typeof value === "string" && ["PENDING", "PASS", "FAIL", "SKIP"].includes(value);
}
export interface ApprovalFindingRecord {
id: string;
runId: string;