Repository navigation
Fix token/server pairing, piped auth prompts and escaped URL paths - #33
Merged
Merged
Conversation
The client joined the decoded request path onto the base URL, so a
segment built with url.PathEscape("a/b") went out as /api/orgs/a/b and
hit a different route. Join the escaped paths instead and carry them in
RawPath, so %2F stays one segment.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
promptLine made a new bufio.Reader for every prompt, and that buffer swallowed the lines after the first. Piping an email and a password to `nh user auth` lost the password. Read up to the newline a byte at a time so each prompt leaves the rest of stdin for the next. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`nh --uri https://hub.corp user login` saved just the token, so the next command sent it to the default server: a confusing 401, and the token leaked to the wrong host. `nh config set uri` and `nh migrate` could split the pair the same way. The settings file now records token_uri next to the token. Login and auth save it and make that server the default. A saved token is only used when the effective URI is the same server; otherwise commands fail with an error naming the token's server and how to fix it. A token from --token or the environment is used as is. Files from older versions are pinned on load to the saved uri, or the default server. Profiles carry token_uri too, `nh config set/unset uri` warns when the token no longer matches, and migrate imports the uri and token as a pair. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- migrate: check the uri/token pairing whenever a uri is imported, so an old config with only a uri can't move the default off the server nh's own token is for. - An explicit empty --token means no token again, rather than falling back to the saved one. - readLine gives up with io.ErrNoProgress on a reader that keeps returning no data and no error. - Rewrap the migrate help text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 three bugs in auth, config and the HTTP client's URL building.
1. A saved token is now tied to the server it came from
nh --uri https://hub.corp user loginused to save only the token, so the nextnh device listsent it tohttps://manage.nervescloud.com: a confusing 401, and the token leaked to the wrong host.nh config set uriandnh migratecould split the pair too.settings.jsonrecordstoken_urinext totoken(profiles too, sonh config save/loadkeep the pair).nh user loginandnh user authsave the token with--urias its server and make that server the default (Default server set to …is printed when it changes).nh user logoutclears both.nh config set uri/unset uriprint a note when the saved token no longer matches the default.nh migrateimports uri and token as a pair (see below).Refusal rule. A saved token is used only when the effective URI (flag > env > settings > default) is the same server as
token_uri. Otherwisecfg.Tokenstays empty and commands that need auth fail with:Commands that don't need a token (
status,user login,config …) are unaffected. "Same server" compares scheme and host case-insensitively, treats a default port (443/80) as no port, and ignores trailing slashes and a trailing/api, since the client treats those as the same base. A token from--tokenorNERVES_HUB_TOKEN/NERVES_CLOUD_TOKENskips the check: the user chose that pairing. An explicit--token ""still means "no token", as before.Older settings files. A token without
token_uriis pinned on load to the saveduri, or tohttps://manage.nervescloud.comwhen none is set. That is the server it was being sent to, so existing setups keep working. It's pinned rather than computed each time, so a laternh config set urican't carry an old token to a new host. One behaviour change: someone with an old file who pointed nh at another server throughNERVES_HUB_URI(with no saveduri) now gets the refusal above and needs to sign in again once.Migrate. The old CLI's token is taken to belong to its
uri, or the default server when it had none. If importing only one of the pair would leave the token on a different server from the default URI, that field is skipped with a reason. For example, nh already hasuriset to another server, or already has its own token.--forceimports both. The same check applies when the old config has only a uri, so it can't move the default off the server nh's own token is for. One case--forcecan't fix: the old CLI had a token but no uri, and nh has a non-default uri. The reason then says to runnh config unset urifirst.2. Piped email and password for
nh user authpromptLinemade a newbufio.Readerfor each prompt, and that buffer swallowed the rest of stdin. Soprintf 'me@x\nsecret\n' | nh user authfailed with "password is required". It now reads up to the newline one byte at a time, which leaves the rest of stdin for the next prompt (includingpromptPassword's non-terminal path) and for anything else that reads stdin later.3. Escaped slashes in API URLs
resolve/joinPathjoined the decodedref.Path, so/orgs/a%2Fb/productswent out as/api/orgs/a/b/products. They now join the escaped paths (EscapedPath()) and setRawPath, so%2Fstays one segment. Table tests cover plain paths,%2F/space/%escapes, base URLs with a path prefix or an existing/api, query merging, and a wire-level check through httptest.Not changed
cmd/device_console.go(another PR's area) checkscfg.Token == ""itself. On a mismatch it never sends the token, but it says "not authenticated" instead of naming the token's server. Making it returncfg.TokenErris a one-line follow-up once that PR lands.Testing
go test ./...,go vet ./...andgofmt -l .are clean. New tests cover each fix and fail without it.🤖 Generated with Claude Code