fix(approval-handler): only parse workflow payload for dangerous_command type
P1-22: The opportunistic JSON.parse of payloadJson ran for every approval, hoping to find workflowId/stepId. Gate on the actual approval type that carries workflow metadata (dangerous_command, as emitted by runWorkflowPipeline), and validate payload shape via zod.
This commit is contained in:
1 parent
f1dd9786e5
commit
5403ee9aa0
1 file changed
+17
-9
@@ -1,3 +1,4 @@
|
|||||||
|
import { z } from "zod";
|
||||||
import type { AppDatabase } from "../database";
|
import type { AppDatabase } from "../database";
|
||||||
import type { NotificationOptions } from "../notification-service";
|
import type { NotificationOptions } from "../notification-service";
|
||||||
import { extractApprovalDocumentIds } from "@shared/approval-documents";
|
import { extractApprovalDocumentIds } from "@shared/approval-documents";
|
||||||
@@ -6,6 +7,12 @@ import { autoAssignSkillsToAgent } from "./automation-skills";
|
|||||||
import type { AutomationEventContext } from "./types";
|
import type { AutomationEventContext } from "./types";
|
||||||
import { normalizeWorkflowSteps } from "./automation-workflow";
|
import { normalizeWorkflowSteps } from "./automation-workflow";
|
||||||
|
|
||||||
|
const workflowApprovalPayloadSchema = z.object({
|
||||||
|
workflowId: z.string().optional(),
|
||||||
|
stepId: z.string().optional(),
|
||||||
|
stepName: z.string().optional(),
|
||||||
|
}).passthrough();
|
||||||
|
|
||||||
function localizeNotification(locale: string | null | undefined, en: string, zh: string): string {
|
function localizeNotification(locale: string | null | undefined, en: string, zh: string): string {
|
||||||
return locale === "zh" ? zh : en;
|
return locale === "zh" ? zh : en;
|
||||||
}
|
}
|
||||||
@@ -325,14 +332,18 @@ export function handleApprovalResolved(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (approval.payloadJson) {
|
// Workflow-step approvals are emitted by runWorkflowPipeline with
|
||||||
try {
|
// type "dangerous_command" + workflowId/stepId in payloadJson. Gate the
|
||||||
const payload = JSON.parse(approval.payloadJson) as { workflowId?: string; stepId?: string };
|
// parse explicitly on that type instead of attempting a speculative
|
||||||
if (payload.workflowId && payload.stepId) {
|
// JSON.parse for every approval.
|
||||||
const workflow = db.getWorkflowRecord(payload.workflowId, companyId);
|
if (approval.type === "dangerous_command" && approval.payloadJson) {
|
||||||
|
const payload = workflowApprovalPayloadSchema.safeParse(JSON.parse(approval.payloadJson));
|
||||||
|
if (payload.success && payload.data.workflowId && payload.data.stepId) {
|
||||||
|
const { workflowId, stepId } = payload.data;
|
||||||
|
const workflow = db.getWorkflowRecord(workflowId, companyId);
|
||||||
if (workflow) {
|
if (workflow) {
|
||||||
const steps = normalizeWorkflowSteps(JSON.parse(workflow.stepsJson || "[]"));
|
const steps = normalizeWorkflowSteps(JSON.parse(workflow.stepsJson || "[]"));
|
||||||
const stepIndex = steps.findIndex((step) => step.id === payload.stepId);
|
const stepIndex = steps.findIndex((step) => step.id === stepId);
|
||||||
if (stepIndex >= 0) {
|
if (stepIndex >= 0) {
|
||||||
if (decision === "approved") {
|
if (decision === "approved") {
|
||||||
steps[stepIndex]!.status = "pending";
|
steps[stepIndex]!.status = "pending";
|
||||||
@@ -363,9 +374,6 @@ export function handleApprovalResolved(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
// Non-workflow approvals may use payloadJson for unrelated metadata.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (approval.requestedByAgentId) {
|
if (approval.requestedByAgentId) {
|
||||||
|
|||||||
Reference in new issue
Block a user