fix(cli): validate --timeout value instead of silently coercing to NaN#100
Open
dmchaledev wants to merge 1 commit into
Open
fix(cli): validate --timeout value instead of silently coercing to NaN#100dmchaledev wants to merge 1 commit into
dmchaledev wants to merge 1 commit into
Conversation
An invalid or missing --timeout value (e.g. `--timeout abc`, or `--timeout` misplaced before the URL) parsed to NaN and was passed straight through to fetchHeaders. setTimeout(..., NaN) fires in ~1ms in Node, so the fetch aborted almost immediately with a cryptic "This operation was aborted" instead of a clear usage error — misleading for a tool whose main use case is a CI gate. Extract argument parsing into a pure parseArgs() in src/cli-args.ts so it's unit-testable without invoking main()'s network/process.exit side effects, and reject non-positive/non-numeric --timeout values with an actionable error message.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
cli.tsparsed--timeoutwithparseInt(...)and passed the result straight through with no validation:Any non-numeric or missing value (a typo, or
--timeoutaccidentally placed before the URL) parses toNaN.NaNis notnull/undefined, sofetch.ts'soptions?.timeoutMs ?? 10000passes it straight tosetTimeout. Node treats aNaNdelay as~1ms, so theAbortControllerfires almost instantly:Both reproduced against a real network call before this fix (see commit for verification). For a tool whose flagship use case is a CI gate (
security-headers $URL || echo "Gate failed"), a mistyped flag silently produces a generic abort error rather than a clear message pointing at the actual mistake — easy to misdiagnose as a network or target problem.Fix
parseArgs()insrc/cli-args.ts(previously inlined inmain(), which has network +process.exitside effects and is awkward to unit test).--timeoutis now validated: non-numeric or non-positive values produce a clearError: --timeout requires a positive number of milliseconds (got ...)and exit 1, instead of a silently-broken near-instant timeout.--timeoutvalue by argument position, not by comparing every arg againstString(timeoutMs)— the old approach was a coincidental hack that happened to work only because a URL is unlikely to literally equal"NaN".test/cli-args.test.tscovering valid/invalid/missing--timeout, flag/URL ordering, and--help/--versiondetection.Test plan
npm run typecheckpassesnpm testpasses (94 tests, including 9 new ones)npm run buildpasses--timeout abcand--timeout <url>(flag before URL) both aborted almost instantly with a misleading error--timeout 3000and a plain URL with no flags still work; a realhttps://example.comscan still runs end-to-end and grades correctlyGenerated by Claude Code