Skip to content

fix(auth): say when a token was encrypted for a different machine - #352

Merged
hogan-yuan merged 2 commits into
mainfrom
fix/auth-wrong-machine-message
Nov 19, 2026
Merged

hogan-yuan merged 2 commits into
mainfrom
fix/auth-wrong-machine-message

Conversation

@hogan-yuan

@hogan-yuan hogan-yuan commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Refs longbridge/developers#1290.

Problem

On a host that gets replaced regularly — the reporter's cloud VM was swapped four times in one day — the home directory survives but the machine identity does not. The stored token can no longer be decrypted, and every command fails with:

Authentication failed: Failed to decrypt auth token.

That message names neither the cause nor anything actionable beyond logging in again. The reporter had to work out the machine-id binding themselves to understand why re-authenticating several times a day was their only option.

The cause was already known internally — read_payload logs "Token decryption failed (machine-id changed?)" — but at warn level, where a user never sees it.

Change

Classify the failure and surface it. A file carrying our magic header that this machine's key cannot open is reported as encrypted for a different machine, with the token path and advice that matches the platform:

Authentication failed: The stored auth token was encrypted for a different machine
and cannot be decrypted here.
The token is bound to this machine's identity, so replacing the host (a new cloud
VM, a reinstall, or a regenerated machine ID) invalidates it even though the file
itself was preserved.
Run `longbridge auth login` to re-authenticate.
On macOS the identity is the hardware UUID, which cannot be pinned, so a token
cannot be carried to different hardware. Re-authenticating on each machine is the
only option here.
Token file: /Users/<user>/.longbridge/openapi/cli-auth

Anything else — a corrupt legacy plaintext file, a file our own key opens but whose contents will not parse — keeps the generic wording, because those are not machine-binding problems.

The hint has to be per-platform

The first version of this told everyone to persist /etc/machine-id. That is Linux-only, and machine-uid reads a different source on each platform:

platform source pinnable
Linux /etc/machine-id, falling back to /var/lib/dbus/machine-id yes
Windows MachineGuid under HKLM\SOFTWARE\Microsoft\Cryptography yes, but nothing like the Linux recipe
macOS hardware UUID via gethostuuid(3) no

So two of three readers would have been pointed at a file they do not have. The hint is now selected with cfg, and macOS gets the honest answer rather than a workaround that cannot work.

classify is split out from diagnose so it can be tested on bytes: token_path is a fixed location in the user's home directory, so a test that went through it would clobber a real login.

What this does not do

It does not make tokens portable across hosts. That needs a product decision about whether a token should be storable in a form another machine can read, which trades away the property that a stolen token file is useless elsewhere. This PR only makes the existing behaviour diagnosable; the feature request in #1290 stays open.

Verification

Two unit tests: a payload encrypted with a key derived from a different machine ID classifies as WrongMachine; malformed plaintext, empty input, and a file our own key opens all classify as Unreadable.

End to end on macOS: a token file whose ciphertext was altered so this machine's key no longer opens it produced the message above from a real CLI run. The real token file was backed up first and restored byte-identical afterwards (md5 verified), and longbridge quote 700.HK confirmed the login still works.

The Linux and Windows hints are string literals in cfg'd-out blocks, which the compiler still parses, so cargo check covers their syntax; their wording has not been seen on those platforms.

cargo clippy --all-targets: diffed against main, zero new findings against the repo's existing baseline.

🤖 Generated with Claude Code

On a host that gets replaced regularly the home directory survives but the
machine identity does not, so the stored token can no longer be decrypted
and every command failed with "Failed to decrypt auth token." That message
names neither the cause nor anything the user can do about it beyond
logging in again, and a reporter had to work out the machine-id binding
themselves to understand why re-authenticating several times a day was the
only option.

The cause was already known internally — `read_payload` logs "machine-id
changed?" — but only at warn level, where a user never sees it. Classify
the failure and surface it: a file with our magic header that our key
cannot open is reported as encrypted for a different machine, with the
machine-id pinning workaround and the token path; anything else keeps the
generic wording.

`classify` is split out from `diagnose` so it can be tested on bytes.
`token_path` is a fixed location in the user's home directory, so a test
that went through it would clobber a real login.

Verified end to end against a token file whose ciphertext was altered so
this machine's key no longer opens it: the CLI prints the new message, and
the real token file was restored byte-identical afterwards.

This does not make tokens portable across hosts; that needs a decision
about whether a token should be storable in a form another machine can
read. Refs longbridge/developers#1290
The new message told every user to persist `/etc/machine-id`, which is
Linux-only. `machine-uid` reads a different source on each platform, so on
two of the three that advice points at a file the reader does not have:

  Linux    /etc/machine-id, falling back to /var/lib/dbus/machine-id
  Windows  the MachineGuid value under HKLM\SOFTWARE\Microsoft\Cryptography
  macOS    the hardware UUID, via gethostuuid(3)

Select the hint with cfg. macOS gets the honest answer rather than a
workaround that cannot work: the hardware UUID is not something a user can
pin, so a token cannot be carried to different hardware there at all.
Platforms outside the three get a wording that makes no claim about where
the identifier lives.

Verified on macOS against a token file this machine's key no longer opens;
the real token file was restored byte-identical afterwards. The other
branches are string literals in cfg'd-out blocks, which the compiler still
parses, so `cargo check` covers their syntax.
@hogan-yuan
hogan-yuan force-pushed the fix/auth-wrong-machine-message branch from 0db398d to 32189a9 Compare November 19, 2026 07:28
@hogan-yuan
hogan-yuan merged commit 753cfef into main Nov 19, 2026
1 check passed
@hogan-yuan
hogan-yuan deleted the fix/auth-wrong-machine-message branch November 19, 2026 07:43
hogan-yuan added a commit that referenced this pull request Nov 23, 2026
Minor bump: this release adds a command. The repo's precedent puts a new
command in the minor slot -- v0.31.0 for routing asset commands to the US
APIs, v0.30.0 for serving quote commands over HTTP REST.

Since v0.31.2:

- feat(trade): add risk-assessment command (#350)
- fix(tls): trust the OS certificate store in addition to the bundled roots (#351)
- fix(auth): say when a token was encrypted for a different machine (#352)
- chore: build x86_64 Linux releases with cross to keep a baseline-CPU binary (#348)
- ai: start a new conversation after signing in (#346)
- docs(valuation): explain high/low are fair-value band bounds (#354)
- ci: cancel a pull request's superseded runs in test.yml (#353)

Only Cargo.toml and the lockfile change, as in the 0.31.2 bump. The scoop
manifest is updated by update-scoop.yml after the tag is published, not
here.

The `[patch.crates-io]` pin on the SDK stays for now. longbridge 5.5.0 was
tagged and released today but is not yet on crates.io, and dropping the
patch before it lands would resolve to 5.4.0, which has no
`risk_assessment`. Moving to `longbridge = "5.5"` is the next change, and it
is what carries the SDK-side TLS fix into the quote and trade paths.
hogan-yuan added a commit that referenced this pull request Nov 23, 2026
Minor bump: this release adds a command. The repo's precedent puts a new
command in the minor slot -- v0.31.0 for routing asset commands to the US
APIs, v0.30.0 for serving quote commands over HTTP REST.

Since v0.31.2:

- feat(trade): add risk-assessment command (#350)
- fix(tls): trust the OS certificate store in addition to the bundled roots (#351)
- fix(auth): say when a token was encrypted for a different machine (#352)
- chore: build x86_64 Linux releases with cross to keep a baseline-CPU binary (#348)
- ai: start a new conversation after signing in (#346)
- docs(valuation): explain high/low are fair-value band bounds (#354)
- ci: cancel a pull request's superseded runs in test.yml (#353)

Only Cargo.toml and the lockfile change, as in the 0.31.2 bump. The scoop
manifest is updated by update-scoop.yml after the tag is published, not
here.

The `[patch.crates-io]` pin on the SDK stays for now. longbridge 5.5.0 was
tagged and released today but is not yet on crates.io, and dropping the
patch before it lands would resolve to 5.4.0, which has no
`risk_assessment`. Moving to `longbridge = "5.5"` is the next change, and it
is what carries the SDK-side TLS fix into the quote and trade paths.
hogan-yuan added a commit that referenced this pull request Nov 23, 2026
Bumps `version` to `0.32.0`. One line in `Cargo.toml` plus the lockfile;
no code changes.

## Why 0.32.0 and not 0.31.3

This release adds a command, and the repo's precedent puts a new command
in the minor slot:

| version | what earned the bump |
|---|---|
| v0.31.0 | `feat(trade): route asset commands to US APIs for US
accounts` |
| v0.30.0 | `feat(quote): serve one-shot quote commands over HTTP REST`
|

`risk-assessment` is the same kind of change.

## Contents since v0.31.2

```
75f3b68 docs(valuation): explain high/low are fair-value band bounds (#354)
4954d57 feat(trade): add risk-assessment command (#350)
753cfef fix(auth): say when a token was encrypted for a different machine (#352)
e77245e fix(tls): trust the OS certificate store in addition to the bundled roots (#351)
ac6cf86 ci: cancel a pull request's superseded runs in test.yml (#353)
bddd70b chore: Build x86_64 Linux releases with cross to keep a baseline-CPU binary (#348)
52eb7a6 ai: Start a new conversation after signing in (#346)
```

Worth calling out:

- **TLS (#351)** — fixes `longbridge auth login` failing with `invalid
peer certificate: UnknownIssuer` behind a corporate TLS-inspecting proxy
(longbridge/developers#1153). **This widens what the CLI trusts**: a
certificate authority installed on the machine can now intercept its
traffic, which is what makes such a proxy work. Reasoning in
longbridge/openapi#628.
- **Auth diagnostics (#352)** — a token that cannot be decrypted now
says it was encrypted for a different machine, with the token path and
advice selected for the platform, instead of a bare "Failed to decrypt
auth token."

## Scope: two files only

`Cargo.toml` and `Cargo.lock`, matching the 0.31.2 bump (`263d9a5`,
which touched exactly those two). `.scoop/longbridge.json` also carries
a version, but `update-scoop.yml` rewrites it after the tag is published
— it is not part of the release commit.

## The SDK pin stays, deliberately

`[patch.crates-io]` still points `longbridge` at a git rev. It has to
for now: `longbridge 5.5.0` was tagged and released today but **is not
yet on crates.io**, and dropping the patch before it lands resolves to
5.4.0, which has no `risk_assessment` — `risk-assessment` (#350) would
not build.

**The follow-up matters more than this release.** Once 5.5.0 is on
crates.io: drop the patch and move to `longbridge = "5.5"`. That is what
carries the SDK-side TLS fix into the quote and trade paths. Until then
the CLI's **own** clients are fixed — which covers the
`/oauth2/register` failure actually reported — but
`OAuthBuilder::build()`, quotes and trades still go through the SDK on
the old root set, so longbridge/developers#1153 is not yet closed for
CLI users.

## Verified

`cargo test --bin longbridge`: 981 pass. `cargo check` clean.

## After merge

Tag `v0.32.0`; `update-scoop.yml` and `update-homebrew.yml` pick it up
from there.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
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