fix(cli): reject malformed timeout options

This commit is contained in:
Peter Steinberger
2026-05-24 01:03:58 +01:00
parent 96959ec3d7
commit 459cee5315
5 changed files with 50 additions and 12 deletions

View File

@@ -60,10 +60,11 @@ describe("createGlobalCommandRunner", () => {
expect(parseTimeoutMsOrExit("0")).toBeNull();
expect(parseTimeoutMsOrExit("-1")).toBeNull();
expect(parseTimeoutMsOrExit(" ")).toBeNull();
expect(parseTimeoutMsOrExit(String(Number.MAX_SAFE_INTEGER))).toBeNull();
expect(error).toHaveBeenCalledTimes(5);
expect(error).toHaveBeenCalledTimes(6);
expect(error).toHaveBeenCalledWith("--timeout must be a positive integer (seconds)");
expect(exit).toHaveBeenCalledTimes(5);
expect(exit).toHaveBeenCalledTimes(6);
expect(exit).toHaveBeenCalledWith(1);
} finally {
error.mockRestore();

View File

@@ -52,6 +52,7 @@ export type UpdateWizardOptions = {
};
const INVALID_TIMEOUT_ERROR = "--timeout must be a positive integer (seconds)";
const MAX_SAFE_TIMEOUT_SECONDS = Math.floor(Number.MAX_SAFE_INTEGER / 1000);
export function parseTimeoutMsOrExit(timeout?: string): number | undefined | null {
if (timeout === undefined) {
@@ -59,7 +60,12 @@ export function parseTimeoutMsOrExit(timeout?: string): number | undefined | nul
}
const trimmed = timeout.trim();
const seconds = Number(trimmed);
if (!/^\d+$/u.test(trimmed) || !Number.isSafeInteger(seconds) || seconds <= 0) {
if (
!/^\d+$/u.test(trimmed) ||
!Number.isSafeInteger(seconds) ||
seconds <= 0 ||
seconds > MAX_SAFE_TIMEOUT_SECONDS
) {
defaultRuntime.error(INVALID_TIMEOUT_ERROR);
defaultRuntime.exit(1);
return null;