Skip to content

Commit 2947598

Browse files
MoerAIsteipete
andauthored
fix(cron): keep valid command env vars when a sibling value is non-string (#105433) (#106100)
* fix(cron): keep valid command env vars when a sibling value is non-string (#105433) normalizeTrimmedStringRecord returned undefined whenever any env value was non-string, so payload normalization deleted the entire env map before gateway schema validation, silently dropping valid siblings (e.g. env {DEBUG:true, PATH:'/bin'} lost PATH). It now filters invalid entries and only normalizes the map away when no valid entries remain, matching the sibling normalizeTrimmedStringArray behavior in the same file. * fix(cron): reject malformed command env maps Co-authored-by: MoerAI <[email protected]> --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 2dff17c commit 2947598

2 files changed

Lines changed: 30 additions & 12 deletions

File tree

‎src/cron/normalize.ts‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,17 @@ function normalizeTrimmedStringArray(
5454
return undefined;
5555
}
5656

57-
function normalizeTrimmedStringRecord(value: unknown): Record<string, string> | undefined {
57+
function normalizeCommandEnv(value: unknown): Record<string, string> {
5858
if (!isRecord(value)) {
59-
return undefined;
59+
throw new Error("command env must be an object with non-blank keys and string values");
6060
}
6161
const entries: Array<[string, string]> = [];
6262
for (const [rawKey, rawValue] of Object.entries(value)) {
6363
const key = normalizeOptionalString(rawKey);
64-
const val = typeof rawValue === "string" ? rawValue : undefined;
65-
if (!key || val === undefined) {
66-
return undefined;
64+
if (!key || typeof rawValue !== "string") {
65+
throw new Error("command env must be an object with non-blank keys and string values");
6766
}
68-
entries.push([key, val]);
67+
entries.push([key, rawValue]);
6968
}
7069
return Object.fromEntries(entries);
7170
}
@@ -283,12 +282,7 @@ function coercePayload(payload: UnknownRecord) {
283282
}
284283
}
285284
if ("env" in next) {
286-
const env = normalizeTrimmedStringRecord(next.env);
287-
if (env !== undefined) {
288-
next.env = env;
289-
} else {
290-
delete next.env;
291-
}
285+
next.env = normalizeCommandEnv(next.env);
292286
}
293287
if ("input" in next && typeof next.input !== "string") {
294288
delete next.input;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,30 @@ describe("cron method validation", () => {
11631163
}
11641164
});
11651165

1166+
it.each(["add", "update"] as const)(
1167+
"rejects cron.%s when one command env value is non-string",
1168+
async (method) => {
1169+
const payload = {
1170+
kind: "command",
1171+
argv: ["sh", "-lc", "echo ok"],
1172+
env: { PATH: "/bin", DEBUG: true },
1173+
};
1174+
const result =
1175+
method === "add"
1176+
? await invokeCronAdd(agentTurnCronParams({ payload }))
1177+
: await invokeCronUpdate(
1178+
{ id: "cron-1", patch: { payload } },
1179+
createCronJob({ payload: { kind: "command", argv: ["echo", "before"] } }),
1180+
);
1181+
1182+
expect(result.context.cron[method]).not.toHaveBeenCalled();
1183+
expectResponseError(result.respond, {
1184+
code: "INVALID_REQUEST",
1185+
messageIncludes: "command env must be an object with non-blank keys and string values",
1186+
});
1187+
},
1188+
);
1189+
11661190
it("defaults session-target declarations to announce delivery", async () => {
11671191
const { context, respond } = await invokeCronAdd(
11681192
agentTurnCronParams({

0 commit comments

Comments
 (0)