Skip to content

fix(cli): validate URL and --timeout before fetching#82

Open
dmchaledev wants to merge 1 commit into
mainfrom
claude/nice-mendel-pjru0p
Open

fix(cli): validate URL and --timeout before fetching#82
dmchaledev wants to merge 1 commit into
mainfrom
claude/nice-mendel-pjru0p

Conversation

@dmchaledev

Copy link
Copy Markdown
Contributor

Problem

Two inputs in the CLI silently misbehave today:

  1. Invalid URLs — running security-headers example.com (no scheme) or any non-URL string produces a cryptic runtime error:

    Error: fetch failed
    

    The root cause (malformed URL) is completely hidden from the user.

  2. Non-numeric --timeout — running security-headers https://example.com --timeout abc causes parseInt to return NaN, 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.ts lines 100-105):
Call new URL(url) before reaching the network. On failure, print a clear actionable message and exit 1:

Error: Invalid URL "example.com". URLs must include a scheme, e.g. https://example.com

Timeout validation (src/cli.ts lines 83-93):
After parseInt, check Number.isFinite(parsed) && parsed > 0. On failure, print:

Error: --timeout must be a positive integer (got "abc")

Also refactors the timeout-value extraction slightly to avoid the fragile String(timeoutMs) filter used when finding the URL positional argument.

Test plan

  • All 85 existing unit tests pass (npm test)
  • TypeScript compiles with no regressions (npm run typecheck error count unchanged — pre-existing node-types issue in library tsconfig, unrelated to this PR)
  • Manual smoke test: security-headers example.com → clear error message
  • Manual smoke test: security-headers https://example.com --timeout abc → clear error message
  • Manual smoke test: security-headers https://example.com --timeout 5000 → works normally

🤖 Generated with Claude Code

https://claude.ai/code/session_01MUCfb1ic1KQVrSbgUpTpP1


Generated by Claude Code

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants