fix(cli): validate URL and --timeout before fetching#82
Open
dmchaledev wants to merge 1 commit into
Open
Conversation
Passing an invalid URL (e.g. `example.com` without a scheme) previously produced a cryptic `TypeError: fetch failed` from the underlying HTTP stack. Now the URL is validated with `new URL()` before the network call and a clear message is printed. Passing `--timeout abc` previously parsed to NaN and was silently ignored. Now the parsed value is validated to be a positive finite integer, and the CLI exits with an explicit error if it isn't. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MUCfb1ic1KQVrSbgUpTpP1
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
Two inputs in the CLI silently misbehave today:
Invalid URLs — running
security-headers example.com(no scheme) or any non-URL string produces a cryptic runtime error:The root cause (malformed URL) is completely hidden from the user.
Non-numeric
--timeout— runningsecurity-headers https://example.com --timeout abccausesparseIntto returnNaN, which is then silently ignored and the default timeout is used instead. The user gets no feedback that their flag was discarded.Fix
URL validation (
src/cli.tslines 100-105):Call
new URL(url)before reaching the network. On failure, print a clear actionable message and exit 1:Timeout validation (
src/cli.tslines 83-93):After
parseInt, checkNumber.isFinite(parsed) && parsed > 0. On failure, print:Also refactors the timeout-value extraction slightly to avoid the fragile
String(timeoutMs)filter used when finding the URL positional argument.Test plan
npm test)npm run typecheckerror count unchanged — pre-existing node-types issue in library tsconfig, unrelated to this PR)security-headers example.com→ clear error messagesecurity-headers https://example.com --timeout abc→ clear error messagesecurity-headers https://example.com --timeout 5000→ works normally🤖 Generated with Claude Code
https://claude.ai/code/session_01MUCfb1ic1KQVrSbgUpTpP1
Generated by Claude Code