Skip to content

fix(auth): only treat 401/403 as an invalid token on login (CLI-19)#1153

Merged
BYK merged 1 commit into
mainfrom
byk/fix/auth-login-token-validation
Jun 27, 2026
Merged

fix(auth): only treat 401/403 as an invalid token on login (CLI-19)#1153
BYK merged 1 commit into
mainfrom
byk/fix/auth-login-token-validation

Conversation

@BYK

@BYK BYK commented Jun 26, 2026

Copy link
Copy Markdown
Member

Summary

sentry auth login --token <t> saves the token and validates it via
getUserRegions(). The validation was wrapped in a blind catch {} that, on
any failure, cleared the token and threw AuthError("invalid", "Invalid API token…").

So a transient network blip, a 5xx server error, or a parse failure was:

  • reported to the user as a bad token (misleading — the token may be fine),
  • discarded (no diagnostics — a silent-catch anti-pattern flagged by
    AGENTS.md), and
  • caused a possibly-valid token to be wiped.

This made CLI-19 a catch-all
bucket (~90 users).

Fix

  • Discriminate the failure (handleTokenValidationError): only a 401/403
    ApiError means the supplied token is invalid/insufficient — clear it and
    raise AuthError("invalid"). Any other error keeps the token and is re-thrown
    unchanged, so the user sees the real failure and it's classified accurately
    (network → silenced per CLI-16W; 5xx → captured as a real server issue).
  • Silence all AuthError reasons in classifySilenced instead of just
    not_authenticated/expired. Now that invalid is precise (a genuine
    bad-token user error), all three are expected auth states the user must act
    on — not CLI bugs. Volume stays visible via the cli.error.silenced metric.

Extracted the discrimination into a helper to keep func under the cognitive
complexity limit.

Test plan

  • login: 401 → clears auth + throws AuthError; non-401 (503) → keeps token
    and re-throws the original cause
    (new test).
  • classifySilenced / reportCliError: AuthError("invalid") → silenced
    (auth_expected, auth_reason: invalid) instead of captured.
  • vitest run test/lib/{errors,error-reporting,telemetry}.test.ts test/commands/auth/
    → 326 passed; biome check clean.

Fixes CLI-19

`sentry auth login --token` validated the token via getUserRegions() and
wrapped ANY failure in a blind `catch {}` that cleared the token and threw
"Invalid API token". So a transient network blip or a 5xx server error was
reported to the user as a bad token (misleading), discarded the real cause
(no diagnostics — a silent-catch anti-pattern), and wiped a possibly-valid
token. This made CLI-19 a catch-all bucket (~90 users).

- Discriminate the failure: only a 401/403 ApiError means the supplied
  token is invalid/insufficient — clear it and raise AuthError("invalid").
  Any other error keeps the token and is re-thrown unchanged, so the user
  sees the real failure and it is classified accurately.
- Now that "invalid" is precise (a genuine bad-token user error, like
  not_authenticated/expired), silence all AuthError reasons in
  classifySilenced instead of just two — these are expected auth states,
  not CLI bugs. Volume stays visible via the cli.error.silenced metric.
@linear-code

linear-code Bot commented Jun 26, 2026

Copy link
Copy Markdown

CLI-19

@github-actions github-actions Bot added the risk: medium PR risk score: medium label Jun 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor
PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://cli.sentry.dev/_preview/pr-1153/

Built to branch gh-pages at 2026-06-27 00:00 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@github-actions

Copy link
Copy Markdown
Contributor

Codecov Results 📊

✅ Patch coverage is 100.00%. Project has 5132 uncovered lines.
✅ Project coverage is 81.47%. Comparing base (base) to head (head).

Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    81.47%    81.47%        —%
==========================================
  Files          397       397         —
  Lines        27699     27702        +3
  Branches     17989     17991        +2
==========================================
+ Hits         22568     22570        +2
- Misses        5131      5132        +1
- Partials      1861      1862        +1

Generated by Codecov Action

@BYK BYK merged commit 19ba49c into main Jun 27, 2026
28 checks passed
@BYK BYK deleted the byk/fix/auth-login-token-validation branch June 27, 2026 00:09
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.

1 participant