Repository navigation
fix: reject an --api-url that is not an http(s) URL, with exit 2 - #9
Open
eastagiletracker wants to merge 1 commit into
Open
eastagiletracker wants to merge 1 commit into
eastagiletracker wants to merge 1 commit into
Conversation
A base URL without a scheme was only noticed once a request was built. `api.databox.com` failed every command with a bare "TypeError: Invalid URL" (exit 1, the code for an API error), and `localhost:8080`, which parses as a URL with the scheme "localhost:", failed inside fetch as "Could not connect to API" with a request ID for a request that never left. `auth login --api-url` saved such a URL and exited 0, so every later command failed the same way. --api-url and DATABOX_API_URL are now checked in BaseCommand.init and in auth login, before any prompt, request or save, and exit 2. A bad URL already stored in the config exits 1, like an unreadable config, and names the command that replaces it.
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.
This PR proposes rejecting an
--api-url/DATABOX_API_URLthat is not an http(s) URL with exit 2, instead of failing later with a rawTypeErroror a misleading connection error. We include this PR work along with a full history of your repo at https://eastagiletracker.com/projects/1047. You can sign in with your GitHub ID to claim ownership of the project.What was wrong
The API base URL was never checked, so a mistake only showed up once a request was built. If the URL has no scheme (
api.databox.com), every command fails with a bareTypeError: Invalid URLand exits 1, which the README's exit-code table reserves for errors from the API.localhost:8080is worse:new URL()reads it as a URL whose scheme islocalhost:, so it gets as far asfetch()and is reported asCould not connect to API. Check your internet connection.. That message also tells the user to quote a request ID to support, but the request never left the machine. Andauth login --api-url api.databox.comsaves the bad URL, prints onlyWarning: API key could not be validated.and exits 0, so every command after it fails the same way.Reproduced on
mainatbe7d72d:The change
isApiUrl()insrc/lib/api-client.tsaccepts only an absolutehttp:orhttps:URL.BaseCommand.initchecks--api-url/DATABOX_API_URLnext to the--account-idcheck. A bad value exits 2 before any prompt or request, and the message names the value:--api-url must be an http:// or https:// URL, e.g. https://api.databox.com. Got "api.databox.com" (from --api-url or DATABOX_API_URL).databox auth login --api-url https://api.databox.com). Passing--api-urlstill overrides the stored value.auth loginchecks--api-urlbefore it prompts, reads stdin or saves anything, so a bad URL can no longer be stored.Verification
npm test(lint plus unit suite) passes: 628 tests after the change. Before it, the baseline had 612 passing and 1 failure, a 10s cold-start timeout inbase-command.test.tsthat does not happen on a re-run, so the change adds no new failures. There are 16 new tests:isApiUrlcases (no scheme,host:port,ftp:/file:), plus command-level tests for the flag, the env var, a stored URL, override by flag, a good stored URL still being used, a destructive command failing before its prompt, andauth loginsaving nothing. If I keep the helper but revert the checks inbase-command.tsandlogin.ts, 6 of the new tests fail.tsc -bandtsc -p tsconfig.test.json --noEmitboth pass. With the built CLI, the four commands above now each exit 2 with the message shown,auth loginwrites no config file, and a stored bad URL gives the exit-1 message with the replacement command.How this was managed
This work was tracked as https://eastagiletracker.com/projects/1047/stories/1334302 on https://eastagiletracker.com/projects/1047, a board we imported from this repository's pull requests (8 stories) and used to manage this work.
If you'd rather not receive contributions like this, reply
no-more-prson this pull request and we won't open any further ones on your repositories.Lawrence W. Sinclair
CEO / East Agile
linkedin.com/in/lwsinclair/
eastagile.com