Skip to content

Commit 6218d81

Browse files
committed
fix(cron): use in-place key trimming with recognized-cron-keys-only guard (fixes #95407)
- Replace normalizeCronToolKeys (null-prototype object, regex filter) with repairWhitespacePaddedCronToolKeys (in-place, recognized keys only) - In-place modification follows the existing repairConcatenatedCronToolKeys pattern - Only CRON_RECOVERABLE_OBJECT_KEYS are trimmed; unknown keys pass through for strict schema validation to reject - Preserve genuine duplicates when the canonical key already exists - No prototype pollution risk: operates on a plain spread copy - Add execute-path regression tests in cron-tool.test.ts - Update flat-params tests with leading-space, duplicate-key, and clean-key coverage
1 parent c9622ee commit 6218d81

3 files changed

Lines changed: 141 additions & 22 deletions

File tree

‎src/agents/tools/cron-tool-canonicalize.ts‎

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -243,34 +243,40 @@ function canonicalizeCronToolPayload(value: Record<string, unknown>): void {
243243
}
244244
}
245245

246-
function normalizeCronToolKeys(value: Record<string, unknown>): Record<string, unknown> {
247-
// Some small/local tool-call parsers can produce JSON with trailing spaces in
248-
// property names (e.g. "schedule " instead of "schedule"). Normalize keys by
249-
// trimming whitespace so downstream matching against CRON_RECOVERABLE_OBJECT_KEYS
250-
// and schema validation sees the expected names.
251-
const next = Object.create(null) as Record<string, unknown>;
252-
for (const [key, val] of Object.entries(value)) {
246+
/**
247+
* Trim leading/trailing whitespace from recognized cron job/patch keys in-place.
248+
*
249+
* Some small/local tool-call parsers (observed with qwen35b via llamacpp) emit
250+
* valid JSON where cron keys carry whitespace-padded names (e.g. `"schedule "`
251+
* instead of `"schedule"`). Trim those keys early so downstream repair and
252+
* canonicalization helpers see the expected canonical names. The gateway uses
253+
* strict `additionalProperties: false` schemas, so unpadded keys would be
254+
* rejected before persistence.
255+
*
256+
* Only known cron recoverable keys are trimmed; unrecognized keys pass through
257+
* unchanged so strict validation can surface them. When the canonical key
258+
* already exists the padded key is preserved as a genuine duplicate for
259+
* validation to reject instead of silently merging or overwriting.
260+
*/
261+
function repairWhitespacePaddedCronToolKeys(value: Record<string, unknown>): void {
262+
for (const key of Object.keys(value)) {
253263
const trimmed = key.trim();
254-
if (trimmed.length === 0) continue;
255-
// Only accept keys that are recognized cron fields or look like
256-
// standard identifiers (alphanumeric + underscore/dash) to keep the
257-
// accumulator narrow and prototype-safe.
258-
if (
259-
trimmed !== "__proto__" &&
260-
(CRON_RECOVERABLE_OBJECT_KEYS.has(trimmed) || /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(trimmed))
261-
) {
262-
next[trimmed] = val;
263-
}
264+
if (trimmed === key || trimmed.length === 0) continue;
265+
if (!CRON_RECOVERABLE_OBJECT_KEYS.has(trimmed)) continue;
266+
// Preserve genuine duplicates — let strict validation reject them.
267+
if (value[trimmed] !== undefined) continue;
268+
value[trimmed] = value[key];
269+
delete value[key];
264270
}
265-
return next;
266271
}
267272

268273
/** Converts model-friendly cron tool shorthands into the nested gateway job/patch shape. */
269274
export function canonicalizeCronToolObject(
270275
value: Record<string, unknown>,
271276
): Record<string, unknown> {
272277
const unwrapped = isRecord(value.data) ? value.data : isRecord(value.job) ? value.job : value;
273-
const next = normalizeCronToolKeys({ ...unwrapped });
278+
const next = { ...unwrapped };
279+
repairWhitespacePaddedCronToolKeys(next);
274280
repairConcatenatedCronToolKeys(next);
275281
canonicalizeCronToolSchedule(next);
276282
canonicalizeCronToolPayload(next);

‎src/agents/tools/cron-tool.flat-params.test.ts‎

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,51 @@ describe("cron tool trailing-space key trimming", () => {
206206
expect(result.schedule).toEqual({ kind: "cron", expr: "30 10 * * *" });
207207
});
208208

209-
it("rejects __proto__ key even after trimming", () => {
209+
it("trims leading spaces from recognized cron job keys", () => {
210+
const result = canonicalizeCronToolObject({
211+
job: {
212+
" schedule": { kind: "at", at: "2026-12-25T00:00:00Z" },
213+
" sessionTarget": "main",
214+
" payload": { kind: "agentTurn", message: "hi" },
215+
},
216+
});
217+
expect(result).toHaveProperty("schedule");
218+
expect(result).toHaveProperty("sessionTarget");
219+
expect(result).toHaveProperty("payload");
220+
expect(Object.keys(result)).not.toContain(" schedule");
221+
expect(Object.keys(result)).not.toContain(" sessionTarget");
222+
expect(Object.keys(result)).not.toContain(" payload");
223+
});
224+
225+
it("keeps unknown padded keys unchanged for strict validation to reject", () => {
210226
const result = canonicalizeCronToolObject({
211227
job: {
212228
name: "Test",
213229
"__proto__ ": { polluted: true },
214230
schedule: { kind: "cron", expr: "0 * * * *" },
215231
},
216232
});
233+
// Only recognized cron keys are trimmed. Unknown keys stay as-is so
234+
// strict gateway validation can surface them.
217235
expect(result).toHaveProperty("schedule");
218-
expect(Object.keys(result)).not.toContain("__proto__");
219-
expect(Object.getPrototypeOf(result)).toBeNull();
236+
// Result is a normal object — no prototype pollution risk.
237+
expect(Object.getPrototypeOf(result)).toBe(Object.prototype);
238+
});
239+
240+
it("preserves a padded duplicate when canonical key already exists", () => {
241+
const result = canonicalizeCronToolObject({
242+
job: {
243+
name: "dup-test",
244+
schedule: { kind: "cron", expr: "0 9 * * *" },
245+
"enabled ": true,
246+
enabled: false,
247+
payload: { kind: "agentTurn", message: "dup test" },
248+
},
249+
});
250+
// The canonical key already exists — the padded duplicate is preserved
251+
// so strict validation can reject the conflict instead of silently merging.
252+
expect(result.enabled).toBe(false);
253+
expect(result["enabled "]).toBe(true);
220254
});
221255

222256
it("does not strip trailing spaces from values, only keys", () => {
@@ -226,6 +260,19 @@ describe("cron tool trailing-space key trimming", () => {
226260
schedule: { kind: "cron", expr: "30 10 * * *" },
227261
},
228262
});
263+
// Key is trimmed ("name " → "name"), value is preserved as-is.
229264
expect(result.name).toBe("Test Name ");
230265
});
266+
267+
it("passes through clean keys unchanged", () => {
268+
const input: Record<string, unknown> = {
269+
name: "Holiday Check-in",
270+
schedule: { kind: "cron", expr: "30 10,20 * * *", tz: "Europe/Madrid" },
271+
sessionTarget: "isolated",
272+
payload: { kind: "agentTurn", message: "hello" },
273+
enabled: true,
274+
};
275+
const result = canonicalizeCronToolObject(input);
276+
expect(result).toEqual(input);
277+
});
231278
});

‎src/agents/tools/cron-tool.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,72 @@ describe("cron tool", () => {
10981098
});
10991099
});
11001100

1101+
it("recovers whitespace-padded cron add keys from local tool-call parsers", async () => {
1102+
const tool = createTestCronTool();
1103+
await tool.execute("call-padded-add", {
1104+
action: "add",
1105+
job: {
1106+
name: "Holiday Check-in",
1107+
description: "Casual check-in while on holiday, 2x per day",
1108+
"schedule ": { kind: "cron", expr: "30 10,20 * * *", tz: "Europe/Madrid" },
1109+
"sessionTarget ": "isolated",
1110+
"payload ": { kind: "agentTurn", message: "Holiday check-in." },
1111+
"enabled ": true,
1112+
},
1113+
});
1114+
1115+
const params = expectSingleGatewayCallMethod("cron.add") as Record<string, unknown>;
1116+
expect(Object.keys(params).every((key) => key === key.trim())).toBe(true);
1117+
expect(params).toMatchObject({
1118+
name: "Holiday Check-in",
1119+
description: "Casual check-in while on holiday, 2x per day",
1120+
schedule: { kind: "cron", expr: "30 10,20 * * *", tz: "Europe/Madrid" },
1121+
sessionTarget: "isolated",
1122+
payload: { kind: "agentTurn", message: "Holiday check-in." },
1123+
enabled: true,
1124+
});
1125+
});
1126+
1127+
it("recovers leading-space cron add keys from local tool-call parsers", async () => {
1128+
const tool = createTestCronTool();
1129+
await tool.execute("call-leading-space-add", {
1130+
action: "add",
1131+
job: {
1132+
" schedule": { kind: "at", at: "2026-12-25T00:00:00Z" },
1133+
" sessionTarget": "main",
1134+
" payload": { kind: "agentTurn", message: "padded" },
1135+
},
1136+
});
1137+
1138+
const params = expectSingleGatewayCallMethod("cron.add") as Record<string, unknown>;
1139+
expect(params).toHaveProperty("schedule");
1140+
expect(params).toHaveProperty("sessionTarget");
1141+
expect(params).toHaveProperty("payload");
1142+
expect(Object.keys(params)).not.toContain(" schedule");
1143+
expect(Object.keys(params)).not.toContain(" sessionTarget");
1144+
expect(Object.keys(params)).not.toContain(" payload");
1145+
});
1146+
1147+
it("keeps a padded duplicate cron key when the canonical key already exists", async () => {
1148+
const tool = createTestCronTool();
1149+
await tool.execute("call-padded-dup-add", {
1150+
action: "add",
1151+
job: {
1152+
name: "dup",
1153+
schedule: { kind: "cron", expr: "0 9 * * *" },
1154+
"enabled ": true,
1155+
enabled: false,
1156+
payload: { kind: "agentTurn", message: "dup test" },
1157+
},
1158+
});
1159+
1160+
// Genuine duplicates are not silently merged: the padded key survives so
1161+
// strict gateway validation surfaces the conflict instead of guessing.
1162+
const params = expectSingleGatewayCallMethod("cron.add") as Record<string, unknown>;
1163+
expect(params.enabled).toBe(false);
1164+
expect(params["enabled "]).toBe(true);
1165+
});
1166+
11011167
it("stamps cron.add with caller sessionKey when missing", async () => {
11021168
callGatewayMock.mockResolvedValueOnce({ ok: true });
11031169

0 commit comments

Comments
 (0)