diff --git a/gitnexus/src/cli/wiki.ts b/gitnexus/src/cli/wiki.ts index 84aeb87bf..ef968b6fa 100644 --- a/gitnexus/src/cli/wiki.ts +++ b/gitnexus/src/cli/wiki.ts @@ -37,13 +37,21 @@ export interface WikiCommandOptions { retries?: string; } -function parsePositiveIntegerOption(value: string | undefined, flag: string): number | undefined { +function parsePositiveIntegerOption( + value: string | undefined, + flag: string, + multiplier = 1, +): number | undefined { if (value === undefined) return undefined; const trimmed = value.trim(); if (!/^[1-9]\d*$/.test(trimmed)) { throw new Error(`${flag} must be a positive integer`); } - return parseInt(trimmed, 10); + const parsed = parseInt(trimmed, 10); + if (parsed > Math.floor(Number.MAX_SAFE_INTEGER / multiplier)) { + throw new Error(`${flag} is too large`); + } + return parsed; } /** @@ -138,7 +146,7 @@ export const wikiCommand = async (inputPath?: string, options?: WikiCommandOptio let timeoutSeconds: number | undefined; try { - timeoutSeconds = parsePositiveIntegerOption(options?.timeout, '--timeout'); + timeoutSeconds = parsePositiveIntegerOption(options?.timeout, '--timeout', 1000); } catch (error) { console.log(` Error: ${(error as Error).message}\n`); process.exitCode = 1; diff --git a/gitnexus/test/unit/wiki-flags.test.ts b/gitnexus/test/unit/wiki-flags.test.ts index 780b475c0..ff4b5eaa0 100644 --- a/gitnexus/test/unit/wiki-flags.test.ts +++ b/gitnexus/test/unit/wiki-flags.test.ts @@ -266,6 +266,7 @@ describe('WikiGenerator --review mode', () => { describe('wikiCommand --timeout validation', () => { const originalExitCode = process.exitCode; + const tooLargeTimeout = String(Math.floor(Number.MAX_SAFE_INTEGER / 1000) + 1); beforeEach(() => { vi.resetModules(); @@ -282,7 +283,7 @@ describe('wikiCommand --timeout validation', () => { process.exitCode = originalExitCode; }); - it.each(['', ' ', '0', '-1', 'abc', '3.14'])( + it.each(['', ' ', '0', '-1', 'abc', '3.14', tooLargeTimeout])( 'rejects invalid --timeout value %s before starting generation', async (timeout) => { const generatorCtor = vi.fn().mockImplementation(() => ({ @@ -343,7 +344,11 @@ describe('wikiCommand --timeout validation', () => { expect(process.exitCode).toBe(1); expect(generatorCtor).not.toHaveBeenCalled(); - expect(consoleSpy).toHaveBeenCalledWith(' Error: --timeout must be a positive integer\n'); + const expectedMessage = + timeout === tooLargeTimeout + ? ' Error: --timeout is too large\n' + : ' Error: --timeout must be a positive integer\n'; + expect(consoleSpy).toHaveBeenCalledWith(expectedMessage); }, ); });