Skip to content

Commit 459cee5

Browse files
committed
fix(cli): reject malformed timeout options
1 parent 96959ec commit 459cee5

5 files changed

Lines changed: 50 additions & 12 deletions

File tree

‎src/cli/cron-cli/register.cron-edit.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ export function registerCronEditCommand(cron: Command) {
186186
patch.sessionTarget = sessionTarget;
187187
}
188188
if (typeof opts.wake === "string") {
189-
patch.wakeMode = opts.wake;
189+
const wakeMode = opts.wake.trim();
190+
if (wakeMode !== "now" && wakeMode !== "next-heartbeat") {
191+
throw new Error("--wake must be now or next-heartbeat");
192+
}
193+
patch.wakeMode = wakeMode;
190194
}
191195
if (opts.agent && opts.clearAgent) {
192196
throw new Error("Use --agent or --clear-agent, not both");
@@ -229,10 +233,20 @@ export function registerCronEditCommand(cron: Command) {
229233
const model = normalizeOptionalString(opts.model);
230234
const thinking = normalizeOptionalString(opts.thinking);
231235
const toolsAllow = parseCronToolsAllow(opts.tools);
232-
const timeoutSeconds = opts.timeoutSeconds
233-
? Number.parseInt(String(opts.timeoutSeconds), 10)
234-
: undefined;
235-
const hasTimeoutSeconds = Boolean(timeoutSeconds && Number.isFinite(timeoutSeconds));
236+
const rawTimeoutSeconds =
237+
opts.timeoutSeconds === undefined ? undefined : String(opts.timeoutSeconds).trim();
238+
if (rawTimeoutSeconds !== undefined && !/^\d+$/u.test(rawTimeoutSeconds)) {
239+
throw new Error("Invalid --timeout-seconds (must be a positive integer).");
240+
}
241+
const timeoutSeconds =
242+
rawTimeoutSeconds === undefined ? undefined : Number(rawTimeoutSeconds);
243+
const hasTimeoutSeconds =
244+
typeof timeoutSeconds === "number" &&
245+
Number.isSafeInteger(timeoutSeconds) &&
246+
timeoutSeconds > 0;
247+
if (rawTimeoutSeconds !== undefined && !hasTimeoutSeconds) {
248+
throw new Error("Invalid --timeout-seconds (must be a positive integer).");
249+
}
236250
const hasDeliveryModeFlag = opts.announce || typeof opts.deliver === "boolean";
237251
const threadId = parseCronThreadIdOption(opts.threadId);
238252
const hasDeliveryThreadId = typeof threadId === "number";

‎src/cli/parse-timeout.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ describe("parseTimeoutMs", () => {
1010
expect(parseTimeoutMs(undefined)).toBeUndefined();
1111
expect(parseTimeoutMs("")).toBeUndefined();
1212
expect(parseTimeoutMs("nope")).toBeUndefined();
13+
expect(parseTimeoutMs("10abc")).toBeUndefined();
14+
expect(parseTimeoutMs("1.5")).toBeUndefined();
15+
expect(parseTimeoutMs("0")).toBeUndefined();
1316
});
1417
});
1518

@@ -40,4 +43,12 @@ describe("parseTimeoutMsWithFallback", () => {
4043
expect(() => parseTimeoutMsWithFallback("0", 3000)).toThrow('Received: "0"');
4144
expect(() => parseTimeoutMsWithFallback("-1", 3000)).toThrow('Received: "-1"');
4245
});
46+
47+
it("throws on malformed or unsafe parsed values", () => {
48+
expect(() => parseTimeoutMsWithFallback("10abc", 3000)).toThrow('Received: "10abc"');
49+
expect(() => parseTimeoutMsWithFallback("1.5", 3000)).toThrow('Received: "1.5"');
50+
expect(() => parseTimeoutMsWithFallback(String(Number.MAX_SAFE_INTEGER + 1), 3000)).toThrow(
51+
"Received",
52+
);
53+
});
4354
});

‎src/cli/parse-timeout.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ export function parseTimeoutMs(raw: unknown): number | undefined {
1212
if (!trimmed) {
1313
return undefined;
1414
}
15-
value = Number.parseInt(trimmed, 10);
15+
if (!/^\d+$/u.test(trimmed)) {
16+
return undefined;
17+
}
18+
value = Number(trimmed);
1619
}
17-
return Number.isFinite(value) ? value : undefined;
20+
return Number.isSafeInteger(value) && value > 0 ? value : undefined;
1821
}
1922

2023
function invalidTimeout(value?: string): Error {
@@ -53,8 +56,11 @@ export function parseTimeoutMsWithFallback(
5356
return fallbackMs;
5457
}
5558

56-
const parsed = Number.parseInt(value, 10);
57-
if (!Number.isFinite(parsed) || parsed <= 0) {
59+
if (!/^\d+$/u.test(value)) {
60+
throw invalidTimeout(value);
61+
}
62+
const parsed = Number(value);
63+
if (!Number.isSafeInteger(parsed) || parsed <= 0) {
5864
throw invalidTimeout(value);
5965
}
6066
return parsed;

‎src/cli/update-cli/shared.command-runner.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ describe("createGlobalCommandRunner", () => {
6060
expect(parseTimeoutMsOrExit("0")).toBeNull();
6161
expect(parseTimeoutMsOrExit("-1")).toBeNull();
6262
expect(parseTimeoutMsOrExit(" ")).toBeNull();
63+
expect(parseTimeoutMsOrExit(String(Number.MAX_SAFE_INTEGER))).toBeNull();
6364

64-
expect(error).toHaveBeenCalledTimes(5);
65+
expect(error).toHaveBeenCalledTimes(6);
6566
expect(error).toHaveBeenCalledWith("--timeout must be a positive integer (seconds)");
66-
expect(exit).toHaveBeenCalledTimes(5);
67+
expect(exit).toHaveBeenCalledTimes(6);
6768
expect(exit).toHaveBeenCalledWith(1);
6869
} finally {
6970
error.mockRestore();

‎src/cli/update-cli/shared.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,20 @@ export type UpdateWizardOptions = {
5252
};
5353

5454
const INVALID_TIMEOUT_ERROR = "--timeout must be a positive integer (seconds)";
55+
const MAX_SAFE_TIMEOUT_SECONDS = Math.floor(Number.MAX_SAFE_INTEGER / 1000);
5556

5657
export function parseTimeoutMsOrExit(timeout?: string): number | undefined | null {
5758
if (timeout === undefined) {
5859
return undefined;
5960
}
6061
const trimmed = timeout.trim();
6162
const seconds = Number(trimmed);
62-
if (!/^\d+$/u.test(trimmed) || !Number.isSafeInteger(seconds) || seconds <= 0) {
63+
if (
64+
!/^\d+$/u.test(trimmed) ||
65+
!Number.isSafeInteger(seconds) ||
66+
seconds <= 0 ||
67+
seconds > MAX_SAFE_TIMEOUT_SECONDS
68+
) {
6369
defaultRuntime.error(INVALID_TIMEOUT_ERROR);
6470
defaultRuntime.exit(1);
6571
return null;

0 commit comments

Comments
 (0)