Skip to content

Commit 45b5f87

Browse files
committed
fix: reject blank cron payloads
1 parent 76fa1b9 commit 45b5f87

3 files changed

Lines changed: 62 additions & 0 deletions

File tree

‎src/cron/normalize.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,42 @@ describe("normalizeCronJobCreate", () => {
195195
expectAnnounceDeliveryTarget(delivery, { channel: "telegram", to: "7200373102" });
196196
});
197197

198+
it("normalizes whitespace-only payload text to empty strings so validation rejects it", () => {
199+
const agentTurn = normalizeCronJobCreate({
200+
name: "blank agent turn",
201+
enabled: true,
202+
schedule: { kind: "every", everyMs: 60_000 },
203+
sessionTarget: "isolated",
204+
wakeMode: "now",
205+
payload: {
206+
kind: "agentTurn",
207+
message: " ",
208+
},
209+
}) as unknown as Record<string, unknown>;
210+
expect(agentTurn.payload).toEqual({ kind: "agentTurn", message: "" });
211+
expect(validateCronAddParams(agentTurn)).toBe(false);
212+
213+
const systemEvent = normalizeCronJobCreate({
214+
name: "blank system event",
215+
enabled: true,
216+
schedule: { kind: "every", everyMs: 60_000 },
217+
sessionTarget: "main",
218+
wakeMode: "now",
219+
payload: {
220+
kind: "systemEvent",
221+
text: " ",
222+
},
223+
}) as unknown as Record<string, unknown>;
224+
expect(systemEvent.payload).toEqual({ kind: "systemEvent", text: "" });
225+
expect(validateCronAddParams(systemEvent)).toBe(false);
226+
227+
const update = normalizeCronJobPatch({
228+
payload: { kind: "agentTurn", message: " " },
229+
}) as unknown as Record<string, unknown>;
230+
expect(update.payload).toEqual({ kind: "agentTurn", message: "" });
231+
expect(validateCronUpdateParams({ id: "job-1", patch: update })).toBe(false);
232+
});
233+
198234
it("normalizes delivery accountId and strips blanks", () => {
199235
const normalized = normalizeIsolatedAgentTurnCreateJob({
200236
name: "delivery account",

‎src/cron/normalize.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,16 @@ function coercePayload(payload: UnknownRecord) {
128128
const trimmed = normalizeOptionalString(next.message) ?? "";
129129
if (trimmed) {
130130
next.message = trimmed;
131+
} else {
132+
next.message = "";
131133
}
132134
}
133135
if (typeof next.text === "string") {
134136
const trimmed = normalizeOptionalString(next.text) ?? "";
135137
if (trimmed) {
136138
next.text = trimmed;
139+
} else {
140+
next.text = "";
137141
}
138142
}
139143
if ("model" in next) {

‎src/gateway/server-methods/cron.validation.test.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,28 @@ describe("cron method validation", () => {
404404
expectResponseError(respond, { code: "INVALID_REQUEST" });
405405
});
406406

407+
it("rejects whitespace-only cron payloads before calling add", async () => {
408+
const agentTurn = await invokeCronAdd(
409+
agentTurnCronParams({
410+
name: "blank agent turn",
411+
payload: { kind: "agentTurn", message: " " },
412+
}),
413+
);
414+
expect(agentTurn.context.cron.add).not.toHaveBeenCalled();
415+
expectResponseError(agentTurn.respond, { code: "INVALID_REQUEST", messageIncludes: "message" });
416+
417+
const systemEvent = await invokeCronAdd({
418+
name: "blank system event",
419+
enabled: true,
420+
schedule: { kind: "every", everyMs: 60_000 },
421+
sessionTarget: "main",
422+
wakeMode: "next-heartbeat",
423+
payload: { kind: "systemEvent", text: " " },
424+
});
425+
expect(systemEvent.context.cron.add).not.toHaveBeenCalled();
426+
expectResponseError(systemEvent.respond, { code: "INVALID_REQUEST", messageIncludes: "text" });
427+
});
428+
407429
it("rejects ambiguous announce delivery on add when multiple channels are configured", async () => {
408430
setRuntimeConfig(telegramSlackConfig({ includeMainSession: true }));
409431

0 commit comments

Comments
 (0)