fix: reject overflowing wiki timeout values

Agent-Logs-Url: https://github.com/abhigyanpatwari/GitNexus/sessions/2f7def72-828c-419c-a5db-1bf1e2f10203
This commit is contained in:
copilot-swe-agent[bot] 2026-05-17 07:28:14 +00:00 committed by GitHub
parent e85de16bd6
commit d74c5dd25b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 5 deletions

View file

@ -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;

View file

@ -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);
},
);
});