From 8c3d98fd980dc4915120e0ef43bceaec82869cfc Mon Sep 17 00:00:00 2001 From: yunyingk Date: Sun, 11 Oct 2026 19:51:29 +0800 Subject: [PATCH] Fix absent-SN record match and document --sign-key on replay MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two findings from reviewing the outbound-push extraction: The verify poll compared `record.sourceDocumentSN === sn` with `sn` optional, so a caller that only knows the flowId compared against `undefined` and matched any record lacking an SN — reporting an unrelated run as the one this push created, with its id and taskMode in the response. Guard the needle: an absent SN matches nothing. This class did not exist on the deploy path before (it compared against flowId only), so it was introduced by the merge. The replay help text also never listed --sign-key even though the channel resolves the target integration through it, and selectHoseIntegration now errors on an ambiguous list with "请通过 --sign-key 指定" — pointing at a flag the user could not discover from `help approval-runs`. Adds direct tests for replay-safety.ts (the exit-14 wording, the next action, and that --force is the only override), which previously had none. --- src/cli/help.ts | 5 +- src/domains/seal/outbound-push.test.ts | 30 ++++++++++++ src/domains/seal/outbound-push.ts | 9 +++- src/domains/seal/replay-safety.test.ts | 64 ++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 src/domains/seal/replay-safety.test.ts diff --git a/src/cli/help.ts b/src/cli/help.ts index 2b18735..9e6c277 100644 --- a/src/cli/help.ts +++ b/src/cli/help.ts @@ -584,14 +584,15 @@ export const HELP_REGISTRY: AreaHelp[] = [ usage: [ "seal-use approval-runs replay [--webhook ] [--sn-suffix ] [--dry-run] [--corp ]", "seal-use approval-runs replay --sn [--webhook ] [--source-corp ] [--target-corp ]", - "seal-use approval-runs replay --sn --channel hose-outbound [--target-corp ] [--dry-run]", - "seal-use approval-runs replay --channel hose-outbound [--target-corp ] [--dry-run]", + "seal-use approval-runs replay --sn --channel hose-outbound [--sign-key ] [--target-corp ] [--dry-run]", + "seal-use approval-runs replay --channel hose-outbound [--sign-key ] [--target-corp ] [--dry-run]", "seal-use approval-runs replay --limit [--status ] [--source-corp ] [--target-corp ]" ], options: [ { flag: "", description: "Approval run record ID" }, { flag: "--sn ", description: "Lookup approval record by source document SN" }, { flag: "--channel ", description: "Replay channel: webhook (default) or hose-outbound (Hosecloud official outbound)" }, + { flag: "--sign-key ", description: "Target Hose integration signKey for hose-outbound; required when the target has several integrations" }, { flag: "--message-id ", description: "Explicit Hose outbound messageId override" }, { flag: "--rejection-num ", description: "Target specific rejection iteration (0, 1, 2, ...)" }, { flag: "--limit ", description: "Batch replay latest N records" }, diff --git a/src/domains/seal/outbound-push.test.ts b/src/domains/seal/outbound-push.test.ts index d92d01e..cf61b86 100644 --- a/src/domains/seal/outbound-push.test.ts +++ b/src/domains/seal/outbound-push.test.ts @@ -300,6 +300,36 @@ describe("pushOutboundAndVerify", () => { } }); + test("an absent sn never matches a record that has no SN", async () => { + // Regression: comparing `record.sourceDocumentSN === sn` with `sn` + // undefined matched any record lacking an SN, so an unrelated run was + // reported as the one this push created. + const lines: string[] = []; + const originalError = console.error; + console.error = (line: string) => lines.push(line); + try { + const { createdRecord } = await pushOutboundAndVerify({ + client: fakeClient([{ id: "unrelated-old-run" }]), + signKey: "key-1", + payload: buildOutboundPayload({ + flowId: "ID01abc", + messageId: "msg-1", + corporationId: "c" + }), + flowId: "ID01abc", + corpName: "目标企业", + corpId: "target-corp", + verifyWaitMs: 0 + }); + + expect(createdRecord).toBeUndefined(); + expect(lines.join("\n")).toContain("消息已入队正在异步消费"); + expect(lines.join("\n")).not.toContain("unrelated-old-run"); + } finally { + console.error = originalError; + } + }); + test("a failing poll never turns a successful push into an error", async () => { const originalError = console.error; console.error = () => {}; diff --git a/src/domains/seal/outbound-push.ts b/src/domains/seal/outbound-push.ts index 849b686..af2900c 100644 --- a/src/domains/seal/outbound-push.ts +++ b/src/domains/seal/outbound-push.ts @@ -176,14 +176,19 @@ export async function pushOutboundAndVerify(options: { await new Promise((resolve) => setTimeout(resolve, options.verifyWaitMs ?? OUTBOUND_VERIFY_WAIT_MS)); + // `sn` is optional: callers that only know the flowId leave it undefined, and + // an undefined needle must never match a record whose SN is absent — that + // would report an unrelated run as the one this push just created. + const snNeedle = sn?.trim() || undefined; + let createdRecord: ApprovalRun | undefined; try { const runs = await api.listApprovalRuns(client, { limit: 5, search: flowId }); createdRecord = (runs.records || []).find( (record) => record.sourceDocumentId === flowId || - record.sourceDocumentSN === sn || - record.sourceDocumentSN === flowId + record.sourceDocumentSN === flowId || + (snNeedle !== undefined && record.sourceDocumentSN === snNeedle) ); } catch { // Non-blocking: the run may not exist yet even when the push succeeded. diff --git a/src/domains/seal/replay-safety.test.ts b/src/domains/seal/replay-safety.test.ts new file mode 100644 index 0000000..359e00b --- /dev/null +++ b/src/domains/seal/replay-safety.test.ts @@ -0,0 +1,64 @@ +import { describe, expect, test } from "bun:test"; +import type { CorpConfig } from "../../core/config/types.js"; +import { assertNonProductionReplayTarget } from "./replay-safety.js"; + +function corp(overrides: Partial = {}): CorpConfig { + return { + id: "corp-test", + name: "测试企业", + environment: "test", + seal: { url: "https://test.sealai.cc" }, + source: { type: "seal", token: "t", sealUrl: "https://test.sealai.cc" }, + auth: { refreshTtl: 300 }, + ...overrides + } as CorpConfig; +} + +const baseOptions = { + action: "重放单据", + nextAction: "seal-use webhook replay --target-corp " +}; + +describe("assertNonProductionReplayTarget", () => { + test("lets a test-environment target through", () => { + expect(() => assertNonProductionReplayTarget(corp(), baseOptions)).not.toThrow(); + }); + + test("blocks a production target with an exit-14 safety error", () => { + let thrown: unknown; + try { + assertNonProductionReplayTarget(corp({ environment: "production" }), baseOptions); + } catch (error) { + thrown = error; + } + expect(thrown).toBeInstanceOf(Error); + expect((thrown as { exitCode?: number }).exitCode).toBe(14); + // The wording must stay regex-classifiable by core/errors SAFETY_GUARD so + // an error crossing a process boundary still lands on exit 14. + expect((thrown as Error).message).toContain("标记为生产正式环境"); + }); + + test("carries the caller's next action and action verb verbatim", () => { + try { + assertNonProductionReplayTarget(corp({ environment: "production" }), { + action: "重放出站报文", + nextAction: "seal-use webhook replay --channel hose-outbound --target-corp " + }); + throw new Error("expected a block"); + } catch (error) { + expect((error as Error).message).toContain("禁止向生产环境重放出站报文"); + expect((error as { nextAction?: string }).nextAction).toContain("--channel hose-outbound"); + } + }); + + test("--force is the only override, and it must be explicit", () => { + const production = corp({ environment: "production" }); + expect(() => assertNonProductionReplayTarget(production, baseOptions)).toThrow(); + expect(() => + assertNonProductionReplayTarget(production, { ...baseOptions, force: true }) + ).not.toThrow(); + expect(() => + assertNonProductionReplayTarget(production, { ...baseOptions, force: false }) + ).toThrow(); + }); +});