Skip to content

Describe profiles in --json without their credentials - #36

Merged
ergofobe merged 4 commits into
mainfrom
issue-33-redact-tokens
Sep 15, 2026
Merged

ergofobe merged 4 commits into
mainfrom
issue-33-redact-tokens

Conversation

@ergofobe

@ergofobe ergofobe commented Sep 15, 2026 •

Copy link
Copy Markdown
Owner

Closes #33.

What was wrong

imogen profiles --json serialised the whole Config with out.json(&config), and Profile carries access_token and refresh_token with skip_serializing_if only for None. On any logged-in machine that command put live bearer credentials into whatever consumed the stream — a pipe, a CI log, a shell history, a bug report, an agent transcript.

The table immediately below showed name, server, browser|token and scopes, and no secret. The two audiences disagreed about what was safe to print, and the machine one got strictly more — the extra part being the credential.

What changed

--json now answers with a ProfilesView: the same four things the table shows, plus expiresAt (unix ms) where there is one. The top-level shape — current and a profiles map keyed by name — is unchanged, so anything reading .current or .profiles.home.server still works.

This is a wider break of the profiles --json contract than just the credentials, and worth stating plainly: client_id, expires_in and obtained_at are gone from the output too. Nothing is lost — signedInVia stands in for client_id, and expiresAt for the other two — but anyone scripting against .profiles.*.expires_in has to move.

$ imogen profiles --json
{
  "current": "home",
  "profiles": {
    "family": { "server": "https://family.example.com", "signedInVia": "token", "scope": "" },
    "home":   { "server": "https://photos.example.com", "signedInVia": "browser",
                "scope": "library:read", "expiresAt": 1700003600000 }
  }
}

A view struct, not #[serde(skip_serializing)] on the token fields. Both close today's leak. The attribute has to be remembered by whoever adds the next credential to Config — the same omission that caused this bug, waiting to happen again. A struct that names everything it prints has to be extended on purpose, so it cannot leak a new field by omission. The test greps the serialised bytes for the secret rather than checking fields one by one, for the same reason.

The table's "signed in via" and its notion of which profile is current now come from the same two helpers the view uses, so the two audiences cannot drift apart again. current_profile also fixes a small honesty problem the old default_profile_name() had here: it invents "default" for an empty config, which is right when naming a profile about to be written and wrong when describing what is saved.

Nothing needs the token via --json today. login --json already answers with profile, server, user and the credentials path; logout --json with a name and a flag. Nothing in the repo reads a token back out of a --json stream — auth.rs and context.rs take it from Config in process. If a token is ever wanted on stdout it should be an explicit, separate, deliberate command, not a side effect of listing profiles.

Nothing on the wire changes.

Verify

cargo test --locked && cargo fmt --check && cargo clippy --all-targets --locked -- -D warnings && cargo build --release --locked

test result: ok. 128 passed; 0 failed; 2 ignored — fmt, clippy and the release build clean.

The reproducing test was written first and watched fail, printing the leak verbatim:

the access token reached stdout: {"current":"home","profiles":{"home":{...,"access_token":"at-SECRET-ACCESS","refresh_token":"rt-SECRET-REFRESH",...}}}

Review

code-review at medium, three iterations, scoped by PR URL — main...HEAD in the worktree pointed the tool at an unrelated repo. Two findings fixed, both as their own commits:

  • significant — current_profile skipped a current naming a profile that is no longer saved and fell to the first key, while every other command resolves through Config::default_profile_name, which returns it verbatim. profiles would have reported and painted one login while ls failed on another — the same disagreement between audiences this PR exists to remove. It now gives default_profile_name's answer, departing only for the empty config.
  • significant — the new keys were snake_case while every key this program synthesises is camelCase. Aligned before the shape is public.

The final run reported zero blockers and zero significants.

Found, not fixed

Nothing. session.rs is the only place that serialises a Config, and no other command puts a token on stdout.

🤖 Generated with Claude Code

ergofobe and others added 4 commits September 15, 2026 15:23
`imogen profiles --json` serialised the whole `Config`, so `access_token` and
`refresh_token` went to stdout on any logged-in machine — into a pipe, a CI
log, a shell history, an agent transcript. The table beside it showed name,
server, auth kind and scopes and no secret, so the two audiences disagreed
about what was safe to print, and the machine one got strictly more.

`--json` now answers with a `ProfilesView` naming the same four things the
table does, plus `expires_at` where there is one. A view struct rather than
`#[serde(skip_serializing)]` on the token fields: the attribute has to be
remembered by whoever adds the next credential to `Config`, while a struct
that must be extended on purpose cannot leak one by omission.

The table's "signed in via" and its notion of which profile is current now
come from the same two helpers the view uses, so the audiences cannot drift
apart again.

Closes #33

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Review finding. `current_profile` skipped a `current` naming a profile that
is no longer saved and fell to the first key, while every other command
resolves through `Config::default_profile_name`, which returns it verbatim.
With `current = "gone"`, `profiles` reported and painted `family` while
`ls` failed on "gone" — the two disagreeing about which login is in use,
which is the thing this change set out to stop.

It now gives the same answer as `default_profile_name`, departing only for
the empty config, where that function invents “default”: the right name for
a profile about to be written, and the wrong description of what exists.
The tests assert the two agree rather than asserting the view alone.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Review finding. `signed_in_via` and `expires_at` were snake_case because the
fields they replace wear that casing on disk, but every key this program
synthesises is camelCase — `capturedAt`, `faceIds`, `loggedOut` — as are the
SDK's own models. A script that reads `.loggedOut` from one command and
`.signed_in_via` from this one has no way to predict which it will get.

This PR redefines the shape of `profiles --json` anyway, so aligning costs
nothing now and would be a second break later.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Review findings, both low, both about a claim the change makes being wider
than the code behind it.

The structural half of the credential test grepped for `token":`, which the
camelCase rename two commits ago had already defeated: a field added later
as `access_token` serialises as `accessToken` and would have sailed past the
one assertion advertised as catching it. It now walks the keys of the
serialised view case-insensitively — keys rather than bytes, because “token”
is a legitimate *value* of `signedInVia`. Proven by adding an `access_token`
field with a harmless value and watching it fail.

`current_profile`'s empty-profiles early return also swallowed a recorded
`current`, so a truncated config would have had `profiles` report null while
every other command still resolved and failed on the name. Dropping the
early return leaves exactly the one departure the comment claims.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@ergofobe
ergofobe merged commit 21a4301 into main Sep 15, 2026
2 checks passed
@ergofobe
ergofobe deleted the issue-33-redact-tokens branch September 15, 2026 19:36
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.

imogen profiles --json prints access and refresh tokens to stdout

1 participant