Skip to content

Commit 7bd0903

Browse files
bladinobviyus
andauthored
fix(cron): ignore inherited normalization fields (#111728)
Cron normalization now reads schedule, payload, and authority data from own-field snapshots. This prevents inherited prototype values from becoming serialized job configuration while keeping current cron modes unchanged. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent d7bbc64 commit 7bd0903

8 files changed

Lines changed: 142 additions & 68 deletions

‎src/cron/normalize-payload.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
TrimmedNonEmptyStringFieldSchema,
1010
parseOptionalField,
1111
} from "./delivery-field-schemas.js";
12+
import { snapshotOwnCronRecord } from "./own-record.js";
1213

1314
type UnknownRecord = Record<string, unknown>;
1415

@@ -66,7 +67,7 @@ function hasAgentTurnOnlyPayloadHint(payload: UnknownRecord): boolean {
6667
}
6768

6869
export function normalizeCronPayload(payload: UnknownRecord): UnknownRecord {
69-
const next: UnknownRecord = { ...payload };
70+
const next = snapshotOwnCronRecord(payload);
7071
const kindRaw = normalizeLowercaseStringOrEmpty(next.kind);
7172
if (kindRaw === "agentturn") {
7273
next.kind = "agentTurn";
@@ -260,5 +261,5 @@ export function normalizeCronPayload(payload: UnknownRecord): UnknownRecord {
260261
delete next.noOutputTimeoutSeconds;
261262
delete next.outputMaxBytes;
262263
}
263-
return next;
264+
return { ...next };
264265
}

‎src/cron/normalize.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,27 @@ function expectAnnounceDeliveryTarget(
9898
expect(delivery.to).toBe(params.to);
9999
}
100100
describe("normalizeCronJobCreate", () => {
101+
it.each(["create", "patch"] as const)(
102+
"does not promote prototype-only schedule fields during %s normalization",
103+
(mode) => {
104+
const schedule = Object.assign(Object.create({ expr: "*/17 * * * *" }) as UnknownRecord, {
105+
kind: "cron",
106+
});
107+
const normalized =
108+
mode === "create" ? createMain({ schedule }) : normalizePatch({ schedule });
109+
110+
expect(Object.hasOwn(schedule, "expr")).toBe(false);
111+
expect(Object.hasOwn(child(normalized, "schedule"), "expr")).toBe(false);
112+
},
113+
);
114+
it("does not promote prototype-only payload fields", () => {
115+
const payload = Object.assign(
116+
Object.create({ model: "openai/gpt-5" }) as UnknownRecord,
117+
AGENT_TURN,
118+
);
119+
120+
expect(child(createAgent({ payload }), "payload")).not.toHaveProperty("model");
121+
});
101122
it("trims cron timezones and drops blank values", () => {
102123
const trimmed = mainSchedule({ ...CRON_SCHEDULE, tz: " Europe/Vienna " });
103124
const blank = mainSchedule({ ...CRON_SCHEDULE, tz: " " });

‎src/cron/normalize.ts‎

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
/** Normalizes cron create/patch payloads before validation and persistence. */
22
import { parseBoolean } from "@openclaw/normalization-core/boolean-coercion";
33
import { timestampMsToIsoString } from "@openclaw/normalization-core/number-coercion";
4+
import { isRecord } from "@openclaw/normalization-core/record-coerce";
45
import {
56
normalizeLowercaseStringOrEmpty,
67
normalizeOptionalLowercaseString,
78
normalizeOptionalString,
89
} from "@openclaw/normalization-core/string-coerce";
910
import { normalizeOptionalAccountId } from "../routing/account-id.js";
1011
import { sanitizeAgentId } from "../routing/session-key.js";
11-
import { isRecord } from "../utils.js";
1212
import { shouldDefaultCronDeliveryToAnnounce } from "./delivery-defaults.js";
1313
import { parseDeliveryInput } from "./delivery-field-schemas.js";
1414
import { normalizeCronCommandArgv, normalizeCronPayload } from "./normalize-payload.js";
15+
import { snapshotOwnCronRecord } from "./own-record.js";
1516
import { parseAbsoluteTimeMs } from "./parse.js";
1617
import { normalizeCronRuntimeAuthority } from "./runtime-authority.js";
1718
import { coerceFiniteScheduleNumber } from "./schedule-number.js";
@@ -43,8 +44,8 @@ const DEFAULT_OPTIONS: NormalizeOptions = {
4344
};
4445

4546
function coerceSchedule(schedule: UnknownRecord) {
46-
const next: UnknownRecord = { ...schedule };
47-
const rawKind = normalizeLowercaseStringOrEmpty(schedule.kind);
47+
const next = snapshotOwnCronRecord(schedule);
48+
const rawKind = normalizeLowercaseStringOrEmpty(next.kind);
4849
const kind =
4950
rawKind === "at" ||
5051
rawKind === "every" ||
@@ -53,16 +54,16 @@ function coerceSchedule(schedule: UnknownRecord) {
5354
rawKind === "stream"
5455
? rawKind
5556
: undefined;
56-
const exprRaw = normalizeOptionalString(schedule.expr) ?? "";
57-
const timezone = normalizeOptionalString(schedule.tz);
58-
const commandRaw = normalizeOptionalString(schedule.command) ?? "";
59-
const streamCommand = normalizeCronCommandArgv(schedule.command);
60-
const cwdRaw = normalizeOptionalString(schedule.cwd) ?? "";
61-
const streamMode = normalizeOptionalLowercaseString(schedule.mode);
62-
const streamMatch = typeof schedule.match === "string" ? schedule.match : undefined;
63-
const everyMs = coerceFiniteScheduleNumber(schedule.everyMs);
64-
const anchorMs = coerceFiniteScheduleNumber(schedule.anchorMs);
65-
const atString = normalizeOptionalString(schedule.at) ?? "";
57+
const exprRaw = normalizeOptionalString(next.expr) ?? "";
58+
const timezone = normalizeOptionalString(next.tz);
59+
const commandRaw = normalizeOptionalString(next.command) ?? "";
60+
const streamCommand = normalizeCronCommandArgv(next.command);
61+
const cwdRaw = normalizeOptionalString(next.cwd) ?? "";
62+
const streamMode = normalizeOptionalLowercaseString(next.mode);
63+
const streamMatch = typeof next.match === "string" ? next.match : undefined;
64+
const everyMs = coerceFiniteScheduleNumber(next.everyMs);
65+
const anchorMs = coerceFiniteScheduleNumber(next.anchorMs);
66+
const atString = normalizeOptionalString(next.at) ?? "";
6667
const parsedAtMs = atString ? parseAbsoluteTimeMs(atString) : null;
6768

6869
if (kind) {
@@ -118,7 +119,7 @@ function coerceSchedule(schedule: UnknownRecord) {
118119
}
119120
normalizeCronStreamBatching(next);
120121
}
121-
const staggerMs = normalizeCronStaggerMs(schedule.staggerMs);
122+
const staggerMs = normalizeCronStaggerMs(next.staggerMs);
122123
if (staggerMs !== undefined) {
123124
next.staggerMs = staggerMs;
124125
} else if ("staggerMs" in next) {
@@ -175,48 +176,49 @@ function coerceSchedule(schedule: UnknownRecord) {
175176
delete next.maxBatchBytes;
176177
}
177178

178-
return next;
179+
return { ...next };
179180
}
180181

181182
function coerceTrigger(trigger: UnknownRecord): UnknownRecord {
182-
const script = typeof trigger.script === "string" ? trigger.script.trim() : "";
183-
const once = parseBoolean(trigger.once);
183+
const input = snapshotOwnCronRecord(trigger);
184+
const script = typeof input.script === "string" ? input.script.trim() : "";
185+
const once = parseBoolean(input.once);
184186
return {
185187
script,
186188
...(once !== undefined ? { once } : {}),
187189
};
188190
}
189191

190192
function coerceDelivery(delivery: UnknownRecord) {
191-
const next: UnknownRecord = { ...delivery };
192-
const parsed = parseDeliveryInput(delivery);
193+
const next = snapshotOwnCronRecord(delivery);
194+
const parsed = parseDeliveryInput(next);
193195
if (parsed.mode !== undefined) {
194196
next.mode = parsed.mode;
195197
} else if ("mode" in next) {
196198
delete next.mode;
197199
}
198-
if ("channel" in delivery && delivery.channel === null) {
200+
if ("channel" in next && next.channel === null) {
199201
next.channel = null;
200202
} else if (parsed.channel !== undefined) {
201203
next.channel = parsed.channel;
202204
} else if ("channel" in next) {
203205
delete next.channel;
204206
}
205-
if ("to" in delivery && delivery.to === null) {
207+
if ("to" in next && next.to === null) {
206208
next.to = null;
207209
} else if (parsed.to !== undefined) {
208210
next.to = parsed.to;
209211
} else if ("to" in next) {
210212
delete next.to;
211213
}
212-
if ("threadId" in delivery && delivery.threadId === null) {
214+
if ("threadId" in next && next.threadId === null) {
213215
next.threadId = null;
214216
} else if (parsed.threadId !== undefined) {
215217
next.threadId = parsed.threadId;
216218
} else if ("threadId" in next) {
217219
delete next.threadId;
218220
}
219-
if ("accountId" in delivery && delivery.accountId === null) {
221+
if ("accountId" in next && next.accountId === null) {
220222
next.accountId = null;
221223
} else if (parsed.accountId !== undefined) {
222224
next.accountId = parsed.accountId;
@@ -249,12 +251,13 @@ function coerceDelivery(delivery: UnknownRecord) {
249251
}
250252
}
251253
}
252-
return next;
254+
return { ...next };
253255
}
254256

255257
function coerceCompletionDestination(value: UnknownRecord) {
256-
const mode = normalizeOptionalLowercaseString(value.mode);
257-
const to = normalizeOptionalString(value.to);
258+
const input = snapshotOwnCronRecord(value);
259+
const mode = normalizeOptionalLowercaseString(input.mode);
260+
const to = normalizeOptionalString(input.to);
258261
if (mode !== "webhook") {
259262
return null;
260263
}
@@ -265,7 +268,7 @@ function coerceCompletionDestination(value: UnknownRecord) {
265268
}
266269

267270
function coerceFailureDestination(value: UnknownRecord) {
268-
const next: UnknownRecord = { ...value };
271+
const next = snapshotOwnCronRecord(value);
269272
if ("channel" in next) {
270273
if (next.channel === null) {
271274
next.channel = null;
@@ -322,7 +325,7 @@ function coerceFailureDestination(value: UnknownRecord) {
322325
}
323326
}
324327
}
325-
return next;
328+
return { ...next };
326329
}
327330

328331
function normalizeSessionTarget(raw: unknown) {
@@ -361,8 +364,8 @@ export function normalizeCronJobInput(
361364
if (!isRecord(raw)) {
362365
return null;
363366
}
364-
const base = raw;
365-
const next: UnknownRecord = { ...base };
367+
const base = snapshotOwnCronRecord(raw);
368+
const next = snapshotOwnCronRecord(base);
366369

367370
for (const field of ["declarationKey", "displayName"] as const) {
368371
if (field in base && typeof base[field] === "string") {
@@ -376,10 +379,11 @@ export function normalizeCronJobInput(
376379
}
377380

378381
if (isRecord(base.owner)) {
379-
const agentId = normalizeOptionalString(base.owner.agentId);
380-
const sessionKey = normalizeOptionalString(base.owner.sessionKey);
382+
const owner = snapshotOwnCronRecord(base.owner);
383+
const agentId = normalizeOptionalString(owner.agentId);
384+
const sessionKey = normalizeOptionalString(owner.sessionKey);
381385
const accountId = normalizeOptionalAccountId(
382-
typeof base.owner.accountId === "string" ? base.owner.accountId : undefined,
386+
typeof owner.accountId === "string" ? owner.accountId : undefined,
383387
);
384388
if (agentId || sessionKey || accountId) {
385389
next.owner = {
@@ -402,7 +406,9 @@ export function normalizeCronJobInput(
402406
}
403407

404408
if ("toolsAllowProvenance" in base) {
405-
const provenance = base.toolsAllowProvenance;
409+
const provenance = isRecord(base.toolsAllowProvenance)
410+
? snapshotOwnCronRecord(base.toolsAllowProvenance)
411+
: undefined;
406412
if (
407413
isRecord(provenance) &&
408414
provenance.version === 1 &&
@@ -623,7 +629,7 @@ export function normalizeCronJobInput(
623629
}
624630
}
625631

626-
return next;
632+
return { ...next };
627633
}
628634

629635
/** Normalizes a raw cron create request and applies create-time defaults. */

‎src/cron/own-record.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Copies authored fields into a record with no inherited properties.
3+
* Direct normalization would otherwise persist prototype-only input.
4+
*/
5+
export function snapshotOwnCronRecord(record: Record<string, unknown>): Record<string, unknown> {
6+
const snapshot: Record<string, unknown> = Object.create(null);
7+
return Object.assign(snapshot, record);
8+
}

‎src/cron/runtime-authority.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ describe("normalizeCronRuntimeAuthority", () => {
6363
expect(({} as { polluted?: boolean }).polluted).toBeUndefined();
6464
});
6565

66+
it("does not treat an inherited envelope version as authored", () => {
67+
const input = Object.assign(Object.create({ version: 1 }) as Record<string, unknown>, {
68+
runtimeId: "codex",
69+
namespace: "codex.apps",
70+
payload: {},
71+
});
72+
73+
expect(normalizeCronRuntimeAuthority(input)).toBeUndefined();
74+
});
75+
6676
it("rejects the complete envelope above 64 KiB", () => {
6777
expect(
6878
normalizeCronRuntimeAuthority(authority({ data: "x".repeat(64 * 1024) })),

‎src/cron/runtime-authority.ts‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { isRecord } from "@openclaw/normalization-core/record-coerce";
12
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
2-
import { isRecord } from "../utils.js";
3+
import { snapshotOwnCronRecord } from "./own-record.js";
34

45
const CRON_RUNTIME_AUTHORITY_MAX_BYTES = 64 * 1024;
56
const CRON_RUNTIME_AUTHORITY_MAX_ID_LENGTH = 128;
@@ -101,19 +102,20 @@ function deepFreezeJson(value: JsonValue): JsonValue {
101102

102103
/** Validates the private persisted transport without learning runtime-owned payload semantics. */
103104
export function normalizeCronRuntimeAuthority(value: unknown): CronRuntimeAuthority | undefined {
105+
const input = isRecord(value) ? snapshotOwnCronRecord(value) : undefined;
104106
if (
105-
!isRecord(value) ||
106-
value.version !== 1 ||
107-
Object.keys(value).some((key) => !CRON_RUNTIME_AUTHORITY_KEYS.has(key)) ||
108-
!Object.hasOwn(value, "runtimeId") ||
109-
!Object.hasOwn(value, "namespace") ||
110-
!Object.hasOwn(value, "payload")
107+
!input ||
108+
input.version !== 1 ||
109+
Object.keys(input).some((key) => !CRON_RUNTIME_AUTHORITY_KEYS.has(key)) ||
110+
!("runtimeId" in input) ||
111+
!("namespace" in input) ||
112+
!("payload" in input)
111113
) {
112114
return undefined;
113115
}
114-
const runtimeId = normalizeAuthorityId(value.runtimeId);
115-
const namespace = normalizeAuthorityId(value.namespace);
116-
const payload = cloneJsonObject(value.payload);
116+
const runtimeId = normalizeAuthorityId(input.runtimeId);
117+
const namespace = normalizeAuthorityId(input.namespace);
118+
const payload = cloneJsonObject(input.payload);
117119
if (!runtimeId || !namespace || !payload) {
118120
return undefined;
119121
}

‎src/cron/scheduled-tool-policy.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { describe, expect, it } from "vitest";
22
import {
33
createAccountCronScheduledToolPolicy,
4+
normalizeCronScheduledToolCallerOrigin,
45
normalizeCronScheduledToolPolicy,
6+
normalizeCronToolsAllowExecTarget,
7+
normalizeCronToolsAllowExecTargetRequirement,
58
resolveCronScheduledToolPolicy,
69
} from "./scheduled-tool-policy.js";
710

@@ -17,6 +20,27 @@ describe("cron scheduled tool policy", () => {
1720
).toBeUndefined();
1821
});
1922

23+
it("does not treat inherited authority fields as authored", () => {
24+
expect(normalizeCronScheduledToolCallerOrigin(Object.create({ kind: "local" }))).toEqual({
25+
kind: "unknown",
26+
});
27+
expect(
28+
normalizeCronScheduledToolPolicy(Object.create({ version: 1, mode: "trusted" })),
29+
).toBeUndefined();
30+
expect(
31+
normalizeCronToolsAllowExecTarget(Object.create({ version: 1, host: "gateway" })),
32+
).toBeUndefined();
33+
expect(
34+
normalizeCronToolsAllowExecTargetRequirement(
35+
Object.create({
36+
version: 1,
37+
target: { version: 1, host: "gateway" },
38+
grantIndex: 0,
39+
}),
40+
),
41+
).toEqual({ version: 1, recoveryRequired: true });
42+
});
43+
2044
it("requires account provenance to match the persisted owner", () => {
2145
const policy = createAccountCronScheduledToolPolicy({
2246
ownerSessionKey: "agent:main:discord:group:ops",

0 commit comments

Comments
 (0)