Skip to content

Fix token/server pairing, piped auth prompts and escaped URL paths - #33

Merged
joshk merged 4 commits into
mainfrom
fix/auth-config-bugs
Nov 24, 2026
Merged

joshk merged 4 commits into
mainfrom
fix/auth-config-bugs

Conversation

@joshk

@joshk joshk commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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 login used to save only the token, so the next nh device list sent it to https://manage.nervescloud.com: a confusing 401, and the token leaked to the wrong host. nh config set uri and nh migrate could split the pair too.

  • settings.json records token_uri next to token (profiles too, so nh config save/load keep the pair).
  • nh user login and nh user auth save the token with --uri as its server and make that server the default (Default server set to … is printed when it changes).
  • nh user logout clears both.
  • nh config set uri / unset uri print a note when the saved token no longer matches the default.
  • nh migrate imports 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. Otherwise cfg.Token stays empty and commands that need auth fail with:

the saved token is for https://hub.corp, not https://other: pass --uri https://hub.corp to use it, or run nh user login --uri https://other to sign in to https://other

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 --token or NERVES_HUB_TOKEN/NERVES_CLOUD_TOKEN skips the check: the user chose that pairing. An explicit --token "" still means "no token", as before.

Older settings files. A token without token_uri is pinned on load to the saved uri, or to https://manage.nervescloud.com when 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 later nh config set uri can't carry an old token to a new host. One behaviour change: someone with an old file who pointed nh at another server through NERVES_HUB_URI (with no saved uri) 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 has uri set to another server, or already has its own token. --force imports 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 --force can't fix: the old CLI had a token but no uri, and nh has a non-default uri. The reason then says to run nh config unset uri first.

2. Piped email and password for nh user auth

promptLine made a new bufio.Reader for each prompt, and that buffer swallowed the rest of stdin. So printf 'me@x\nsecret\n' | nh user auth failed 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 (including promptPassword's non-terminal path) and for anything else that reads stdin later.

3. Escaped slashes in API URLs

resolve/joinPath joined the decoded ref.Path, so /orgs/a%2Fb/products went out as /api/orgs/a/b/products. They now join the escaped paths (EscapedPath()) and set RawPath, so %2F stays 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) checks cfg.Token == "" itself. On a mismatch it never sends the token, but it says "not authenticated" instead of naming the token's server. Making it return cfg.TokenErr is a one-line follow-up once that PR lands.

Testing

go test ./..., go vet ./... and gofmt -l . are clean. New tests cover each fix and fail without it.

🤖 Generated with Claude Code

joshk and others added 4 commits November 24, 2026 13:16
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>
@joshk
joshk merged commit 6636883 into main Nov 24, 2026
1 check passed
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.

1 participant