fix(cli): reject a malformed --endpoint-url with a clear VALIDATION_ERROR#19
Open
Davidson3556 wants to merge 1 commit into
Open
fix(cli): reject a malformed --endpoint-url with a clear VALIDATION_ERROR#19Davidson3556 wants to merge 1 commit into
Davidson3556 wants to merge 1 commit into
Conversation
…RROR
A malformed API endpoint produced an opaque or misleading failure instead
of a clear config error:
--endpoint-url "not a url" -> `Error: Invalid URL` (exit 1, a raw
`new URL()` throw with no guidance)
--endpoint-url "localhost:3000" (missing scheme, parses as scheme
"localhost:") and "ftp://x" (wrong scheme)
-> `fetch failed` / Service unavailable,
emitted only after a full retry+backoff
cycle — looks like a network outage, not a
config typo.
Add `assertValidEndpointUrl` in client-factory.ts and run it in both the
real and dry-run paths of `makeHttpClient`, on the resolved endpoint (so it
covers --endpoint-url, TESTSPRITE_API_URL, and the credentials file). A
malformed value now throws a typed VALIDATION_ERROR (exit 5) with an
actionable message.
Crucially, and unlike the `--target-url` SSRF guard, this does NOT reject
localhost or private hosts — the API endpoint legitimately points at a
self-hosted, local-dev, or mock backend. Only syntactically invalid values
(unparseable, or a non-http(s) scheme) are rejected, so existing
self-hosted/CI configs and the test suite's localhost mock backend are
unaffected.
Adds unit coverage for assertValidEndpointUrl and the two makeHttpClient
paths, plus subprocess regressions.
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.
Fixes #18
Describe the changes you have made in this PR -
A malformed API endpoint was not validated, so it failed in a way that didn't point at the real (config) problem:
--endpoint-url "not a url"→Error: Invalid URL(exit 1) — a rawnew URL()throw with no guidance.--endpoint-url "localhost:3000"(missing scheme —new URL()parses it as schemelocalhost:) and--endpoint-url "ftp://x.com"(wrong scheme) →fetch failed/Service temporarily unavailable, emitted only after a full retry-and-backoff cycle — it looks like a network outage, not a typo.The endpoint can come from
--endpoint-url,TESTSPRITE_API_URL, or the credentials fileapi_url.Changes:
assertValidEndpointUrl(rawUrl)inclient-factory.tsand run it in both the real and dry-run paths ofmakeHttpClient, on the resolved endpoint — so all three sources are covered. A malformed value now throws a typedVALIDATION_ERROR(exit 5) with an actionable message, before any fetch/retry.--target-urlcheck intarget-url.ts, this does not reject localhost or private hosts: the API endpoint legitimately points at self-hosted, local-dev, or mock backends (the test suite itself targetshttp://localhost). Only unparseable values or non-http(s) schemes are rejected, so existing self-hosted/CI configs are unaffected.assertValidEndpointUrland bothmakeHttpClientpaths, plus subprocess regressions.Demo/Screenshot for feature changes and bug fixes -
Before (main) — opaque / misleading:
After (this branch) — fast, actionable config error:
Tests:
Code Understanding and AI Usage
Did you use AI assistance (ChatGPT, Claude, Copilot, etc.) to write any part of this code?
If you used AI assistance:
Explain your implementation approach:
Problem: a typo in the API endpoint is a config error, but the CLI surfaced it as either a bare
Invalid URL(an unhandlednew URL()throw → generic exit 1) or, for a missing/wrong scheme, as afetch failedUNAVAILABLE after burning a retry cycle. Neither tells the user their endpoint is malformed.Alternatives considered:
assertNotLocalfromtarget-url.ts. Rejected: that guard rejects localhost / RFC1918 hosts (it's an SSRF guard for the test target), but the API endpoint legitimately points at private/self-hosted/mock backends — reusing it would break self-hosted users and the project's own localhost test backend.--endpoint-urlflag in each command'sresolveCommonOptions. Rejected: it would missTESTSPRITE_API_URLand the credentials-fileapi_url, and duplicate the check across files.assertValidEndpointUrlat the single chokepoint where the endpoint becomes a base URL (makeHttpClient), validating the resolved value so all three sources are covered in both the real and dry-run paths.Key function:
assertValidEndpointUrl(rawUrl)— parses withnew URL(); an unparseable value or a non-http:/https:scheme throwslocalValidationError('endpoint-url', …, 'field')(the typed VALIDATION_ERROR used elsewhere, exit 5). It is intentionally syntax-only (no host-class checks). InmakeHttpClientit runs before the auth check (so a config typo is reported even without a key) and, in dry-run, before the banner.Edge cases handled/tested: valid http/https including
localhost,127.0.0.1, and internal hostnames pass;"not a url"(unparseable) throws;"localhost:3000"(parses as a bogus scheme) andftp:///file://throw; validation happens before any network call, so no retry cycle is wasted.Checklist before requesting a review
Note: Please check Allow edits from maintainers if you would like us to assist in the PR.