Skip to content

feat(cli): Treat SENTRY_AUTH_TOKEN as an account next to stored logins - #1558

Merged
BYK merged 7 commits into
mainfrom
feat/cli-env-token-account
Oct 21, 2026
Merged

BYK merged 7 commits into
mainfrom
feat/cli-env-token-account

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Closes #1553.

Until now, if you had both a stored login and SENTRY_AUTH_TOKEN/SENTRY_TOKEN, every command ignored one of them. With this change the env token acts as one more account: read-only, never stored, and only sent to its own server. That only happens when its server comes from a trusted source: the token's sntrys_ claim, SENTRY_HOST/SENTRY_URL from the boot env, or SaaS when neither is set. Everything else keeps today's behavior.

Behavior (follows the answers on the issue: env token wins, switching stays implicit, show the /auth/ user)

  • Org routing: when the org cache shows the env token can access the target org, the command uses the env token, even if the active stored account can access it too. Orgs it can't access stay with the stored accounts. Without a target org, the stored login still wins over the env token, as fixed in update_issue returns API errors but operations succeed #646.
  • Server selection: with SENTRY_HOST/SENTRY_URL set, the env token is used for that server when no stored account is on it.
  • sentry org list lists the env token first, as env:SENTRY_AUTH_TOKEN. An org it shares with a login is listed under it, which matches what routing will pick. The label includes the token's user from /auth/ when the server returns one (skipped for org auth tokens). --account env:SENTRY_AUTH_TOKEN lists only the env token's orgs. With only an env token, the output doesn't change.
  • sentry auth list / sentry auth status show the env token with its server and user. sentry auth switch can't select it.
  • SENTRY_FORCE_ENV_TOKEN=1 still means "use only the env token".

Trust model. The env-token host snapshot now records which token was present at boot. A token added later, like a .sentryclirc [auth] token copied in by the shim, isn't scoped by that snapshot, so it doesn't become an account and keeps its fallback-only role. withEnvAccount() (the counterpart of withStoredAccount()) runs work under an env copy with the token forced and SENTRY_URL pinned to its trusted host. That way a .sentryclirc or pasted URL can't move it to another server.

Tradeoff for review. Org routing only learns which orgs the env token can access from the org cache, mostly filled by sentry org list. Because the env token wins for those orgs, an org auth token (sntrys_) whose org is cached will also be used for that org's non-build commands, where its scopes may be too narrow. That follows from the "env token wins" decision. If it's a problem in practice, we can restrict the precedence to user tokens.

via byk.

--

View Junior Session in Sentry

Closes #1553.

An env token whose server is known from a trusted source (its sntrys_
claim, SENTRY_HOST/SENTRY_URL from the boot env, or SaaS) now acts as one
more, never stored, read-only account instead of being all-or-nothing:

- Org routing uses it for organizations it knows, ahead of stored
  accounts (the active one included), and server-based selection uses it
  when no stored account is on the configured server.
- `sentry org list` lists its orgs under `env:SENTRY_AUTH_TOKEN`, labeled
  with its `/auth/` user when available; `--account env:SENTRY_AUTH_TOKEN`
  selects it.
- `sentry auth list` and `sentry auth status` show it with its server and
  user. `sentry auth switch` can't select it.

Tokens set after boot (a `.sentryclirc` token) keep today's fallback-only
behavior, and SENTRY_FORCE_ENV_TOKEN still means "use only the env token".

Co-Authored-By: byk <[email protected]>
@vercel

vercel Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
sentry-local Skipped Skipped Oct 21, 2026 3:31pm UTC

Request Review

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Oct 20, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 1eaacd7. Configure here.

Comment thread packages/cli/src/lib/db/accounts.ts Resolved
The env token's credential fingerprint doesn't depend on its server, so
orgs it cached while scoped to another SENTRY_URL could select it for a
same-slug org on its current server. Match cached orgs against the
token's trusted host (SaaS regions count as SaaS).
Comment thread packages/cli/src/lib/db/accounts.ts Resolved
Comment thread packages/cli/src/lib/db/auth.ts Resolved
…en orgs by server in SQL

Read SENTRY_FORCE_ENV_TOKEN through parseBoolValue (adapted from the SDK's
envToBool) in one helper, so 0/false/no/off no longer force the env token.

Move the env token's same-server org cache check into the query: SaaS
origins match as one server, others by exact normalized origin, with
LIMIT 1 instead of fetching rows to filter in TypeScript.
Comment thread packages/cli/src/lib/db/accounts.ts Resolved
sentry-junior Bot and others added 2 commits October 21, 2026 14:28
… test

Back-to-back logins can share a millisecond, tying last_used_at so the
older account won the tie-break and the test failed intermittently.

Co-Authored-By: byk <[email protected]>
Comment thread packages/cli/src/lib/env-token-host.ts Resolved
Use a plain SELECT 1 ... LIMIT 1 (same short-circuit as EXISTS) and
inline the single-use same-server SQL helper.

Co-Authored-By: byk <[email protected]>
A self-hosted SENTRY_HOST/SENTRY_URL can be injected in layered CI
without access to the token, so it alone must not make a plain env
token an account next to stored logins: that would send the token to
the injected server. The env token is now an account only on its
sntrys_ claim's server or on SaaS; other tokens keep the
stored-login-first behavior, and SENTRY_FORCE_ENV_TOKEN opts in.

Co-Authored-By: byk <[email protected]>
@BYK
BYK enabled auto-merge (squash) October 21, 2026 15:24
@BYK
BYK merged commit 2d953e6 into main Oct 21, 2026
22 checks passed
@BYK
BYK deleted the feat/cli-env-token-account branch October 21, 2026 15:31

This branch was previously deployed

1 inactive deployment
Preview — a6e1314c Deployed Oct 21, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI: treat SENTRY_AUTH_TOKEN/SENTRY_TOKEN as another account alongside stored logins

1 participant