From e33a1bb3966a1b11611b3ba0e500e1c7c457e38d Mon Sep 17 00:00:00 2001 From: "jared-outpost[bot]" Date: Mon, 3 Aug 2026 10:27:30 +0000 Subject: [PATCH 1/2] fix(auth): name .sentryclirc as token source in ignored-token hint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When a token is set in .sentryclirc, the env shim copies it into SENTRY_AUTH_TOKEN. The env-token-ignored hint then claimed the user set a SENTRY_AUTH_TOKEN env var, which is misleading — the source was a config file. Record the injecting .sentryclirc path in the shim and have the hint name it instead. Fixes #1345 --- packages/cli/src/lib/auth-hint.ts | 37 +++++++++++++++++------ packages/cli/src/lib/sentryclirc.ts | 25 +++++++++++++++ packages/cli/test/lib/auth-hint.test.ts | 35 ++++++++++++++++++++- packages/cli/test/lib/sentryclirc.test.ts | 18 +++++++++++ 4 files changed, 104 insertions(+), 11 deletions(-) diff --git a/packages/cli/src/lib/auth-hint.ts b/packages/cli/src/lib/auth-hint.ts index 6ceacd4336..9e263824a1 100644 --- a/packages/cli/src/lib/auth-hint.ts +++ b/packages/cli/src/lib/auth-hint.ts @@ -1,13 +1,14 @@ /** * One-shot env-token-ignored hint. * - * When a user sets SENTRY_AUTH_TOKEN (or SENTRY_TOKEN) but also has a - * stored OAuth login from `sentry auth login`, the CLI silently prefers - * the stored login. Users then wonder why their valid provisioned - * token isn't being used — this was the most painful item in the CLI - * UX feedback issue (getsentry/cli#785 #4). The hint surfaces the - * collision on stderr the first time the CLI reaches for auth in a - * given process. + * When a user sets SENTRY_AUTH_TOKEN (or SENTRY_TOKEN) — or a `token` + * in `.sentryclirc`, which the env shim copies into SENTRY_AUTH_TOKEN — + * but also has a stored OAuth login from `sentry auth login`, the CLI + * silently prefers the stored login. Users then wonder why their valid + * provisioned token isn't being used — this was the most painful item + * in the CLI UX feedback issue (getsentry/cli#785 #4). The hint surfaces + * the collision on stderr the first time the CLI reaches for auth in a + * given process, naming the real source (env var vs `.sentryclirc`). * * Design notes: * - Fires at most once per process (module-local latch). CLI invocations @@ -32,6 +33,7 @@ import { import { getUserInfo } from "./db/user.js"; import { getEnv } from "./env.js"; import { logger } from "./logger.js"; +import { getRcInjectedTokenSource } from "./sentryclirc.js"; const log = logger.withTag("auth"); @@ -79,15 +81,30 @@ export function maybeWarnEnvTokenIgnored(): void { hintEmitted = true; - const envVar = getActiveEnvVarName(); const userLabel = resolveStoredUserLabel(); log.info( - `Detected ${envVar} env var but using stored login for ${userLabel}.\n` + - " Set SENTRY_FORCE_ENV_TOKEN=1 to prefer the env var." + `${describeIgnoredTokenSource()} but using stored login for ${userLabel}.\n` + + " Set SENTRY_FORCE_ENV_TOKEN=1 to prefer it." ); } +/** + * Describe where the ignored token came from, for the hint's first line. + * + * The `.sentryclirc` shim copies a `[auth] token` into `SENTRY_AUTH_TOKEN`, + * so a bare "Detected SENTRY_AUTH_TOKEN env var" message would misattribute + * the source — the user set a config file, not an env var (getsentry/cli#1345). + * When the shim was the source, name the `.sentryclirc` file instead. + */ +function describeIgnoredTokenSource(): string { + const rcSource = getRcInjectedTokenSource(); + if (rcSource) { + return `Detected a token in .sentryclirc (${rcSource})`; + } + return `Detected ${getActiveEnvVarName()} env var`; +} + /** * Resolve a user-friendly label for the stored OAuth user. * diff --git a/packages/cli/src/lib/sentryclirc.ts b/packages/cli/src/lib/sentryclirc.ts index 78b46a2cb3..a16ae28810 100644 --- a/packages/cli/src/lib/sentryclirc.ts +++ b/packages/cli/src/lib/sentryclirc.ts @@ -323,6 +323,29 @@ export function loadSentryCliRc(cwd: string): Promise { return promise; } +/** + * Path of the `.sentryclirc` file whose `token` the shim injected into + * `SENTRY_AUTH_TOKEN`, or `undefined` when the active env token did not + * come from the shim. + * + * Lets downstream diagnostics (e.g. the env-token-ignored hint) name the + * real source instead of claiming the user set an env var they never did. + */ +let rcInjectedTokenSource: string | undefined; + +/** + * The `.sentryclirc` file that supplied the token now living in + * `SENTRY_AUTH_TOKEN`, or `undefined` when the token came from a real + * env var (or no token is set). + * + * Populated by {@link applySentryCliRcEnvShim}. Returns `undefined` if a + * genuine `SENTRY_AUTH_TOKEN`/`SENTRY_TOKEN` was already set, since the + * shim only injects when both are unset. + */ +export function getRcInjectedTokenSource(): string | undefined { + return rcInjectedTokenSource; +} + /** * Apply env shim for `.sentryclirc` token and URL fields. * @@ -350,6 +373,7 @@ export async function applySentryCliRcEnvShim(cwd: string): Promise { `Setting SENTRY_AUTH_TOKEN from ${CONFIG_FILENAME} (${config.sources.token})` ); env.SENTRY_AUTH_TOKEN = config.token; + rcInjectedTokenSource = config.sources.token; } const normalizedRcUrl = config.url ? normalizeUrl(config.url) : undefined; @@ -400,4 +424,5 @@ export function clearSentryCliRcCache(): void { cache.clear(); // Reset global paths — tests change SENTRY_CONFIG_DIR between runs globalPaths = null; + rcInjectedTokenSource = undefined; } diff --git a/packages/cli/test/lib/auth-hint.test.ts b/packages/cli/test/lib/auth-hint.test.ts index 5477620924..b2a5a7b03c 100644 --- a/packages/cli/test/lib/auth-hint.test.ts +++ b/packages/cli/test/lib/auth-hint.test.ts @@ -8,8 +8,11 @@ * or when SENTRY_FORCE_ENV_TOKEN is set. * - Repeat calls in the same process are silent. * - User label preference order (username > email > name > fallback). + * - Names the `.sentryclirc` source when the token came from the rc shim. */ +import { writeFileSync } from "node:fs"; +import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; import { maybeWarnEnvTokenIgnored, @@ -17,9 +20,14 @@ import { } from "../../src/lib/auth-hint.js"; import { setAuthToken } from "../../src/lib/db/auth.js"; import { setUserInfo } from "../../src/lib/db/user.js"; +import { + applySentryCliRcEnvShim, + CONFIG_FILENAME, + clearSentryCliRcCache, +} from "../../src/lib/sentryclirc.js"; import { useTestConfigDir } from "../helpers.js"; -useTestConfigDir("auth-hint-"); +const getConfigDir = useTestConfigDir("auth-hint-"); let savedAuthToken: string | undefined; let savedSentryToken: string | undefined; @@ -32,6 +40,7 @@ beforeEach(() => { delete process.env.SENTRY_AUTH_TOKEN; delete process.env.SENTRY_TOKEN; delete process.env.SENTRY_FORCE_ENV_TOKEN; + clearSentryCliRcCache(); resetAuthHintState(); }); @@ -51,6 +60,7 @@ afterEach(() => { } else { delete process.env.SENTRY_FORCE_ENV_TOKEN; } + clearSentryCliRcCache(); }); /** @@ -137,6 +147,29 @@ describe("maybeWarnEnvTokenIgnored", () => { } }); + test("names the .sentryclirc source when the token came from the rc shim", async () => { + setAuthToken("stored_oauth", 3600, "refresh_a"); + setUserInfo({ userId: "u-1", username: "alice" }); + + // The shim reads $SENTRY_CONFIG_DIR/.sentryclirc as a global fallback + // and copies its token into SENTRY_AUTH_TOKEN, recording the source. + const rcPath = join(getConfigDir(), CONFIG_FILENAME); + writeFileSync(rcPath, "[auth]\ntoken = rc-token\n", "utf-8"); + await applySentryCliRcEnvShim(getConfigDir()); + + const cap = captureStderr(); + try { + maybeWarnEnvTokenIgnored(); + expect(cap.hintCalls()).toBe(1); + const text = cap.text(); + expect(text).toContain(`Detected a token in .sentryclirc (${rcPath})`); + expect(text).not.toContain("env var"); + expect(text).toContain("stored login for alice"); + } finally { + cap.restore(); + } + }); + test("is silent on repeat calls within the same process", () => { setAuthToken("stored_oauth", 3600, "refresh_a"); process.env.SENTRY_AUTH_TOKEN = "env_token"; diff --git a/packages/cli/test/lib/sentryclirc.test.ts b/packages/cli/test/lib/sentryclirc.test.ts index bb3ec12335..220c3ea0d5 100644 --- a/packages/cli/test/lib/sentryclirc.test.ts +++ b/packages/cli/test/lib/sentryclirc.test.ts @@ -18,6 +18,7 @@ import { assertRcUrlTrusted, CONFIG_FILENAME, clearSentryCliRcCache, + getRcInjectedTokenSource, loadSentryCliRc, } from "../../src/lib/sentryclirc.js"; import { cleanupTestDir, createTestConfigDir } from "../helpers.js"; @@ -232,6 +233,23 @@ describe("applySentryCliRcEnvShim", () => { expect(readEnv("SENTRY_AUTH_TOKEN")).toBe("rc-token"); }); + test("records the rc file as the injected token source", async () => { + delete process.env.SENTRY_AUTH_TOKEN; + delete process.env.SENTRY_TOKEN; + writeRcFile(testDir, "[auth]\ntoken = rc-token\n"); + + await applySentryCliRcEnvShim(testDir); + expect(getRcInjectedTokenSource()).toBe(join(testDir, CONFIG_FILENAME)); + }); + + test("does not record an injected source when a real env token is set", async () => { + process.env.SENTRY_AUTH_TOKEN = "existing-token"; + writeRcFile(testDir, "[auth]\ntoken = rc-token\n"); + + await applySentryCliRcEnvShim(testDir); + expect(getRcInjectedTokenSource()).toBeUndefined(); + }); + test("does not override existing SENTRY_AUTH_TOKEN", async () => { process.env.SENTRY_AUTH_TOKEN = "existing-token"; writeRcFile(testDir, "[auth]\ntoken = rc-token\n"); From 913102c29f7b68bfb7e52fb7a7ff7147b00b1626 Mon Sep 17 00:00:00 2001 From: "jared-outpost[bot]" Date: Mon, 3 Aug 2026 11:09:19 +0000 Subject: [PATCH 2/2] docs(auth): tie rc-token provenance to env-token-host precedent Collapse the redundant getter JSDoc into a one-liner and note the module-local + getter mirrors the pinnedHost snapshot pattern already used in env-token-host.ts, per review feedback. --- packages/cli/src/lib/sentryclirc.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/packages/cli/src/lib/sentryclirc.ts b/packages/cli/src/lib/sentryclirc.ts index a16ae28810..23f5d98316 100644 --- a/packages/cli/src/lib/sentryclirc.ts +++ b/packages/cli/src/lib/sentryclirc.ts @@ -324,24 +324,19 @@ export function loadSentryCliRc(cwd: string): Promise { } /** - * Path of the `.sentryclirc` file whose `token` the shim injected into - * `SENTRY_AUTH_TOKEN`, or `undefined` when the active env token did not - * come from the shim. + * Path of the `.sentryclirc` file whose `token` the shim copied into + * `SENTRY_AUTH_TOKEN`, pinned by {@link applySentryCliRcEnvShim} at the + * one point it knows the provenance. `undefined` when the active env + * token was set by the user rather than injected from rc. * - * Lets downstream diagnostics (e.g. the env-token-ignored hint) name the - * real source instead of claiming the user set an env var they never did. + * Mirrors the boot-time snapshot pattern in `env-token-host.ts` + * (`pinnedHost` + getter): lets the synchronous env-token-ignored hint + * name the real source instead of claiming the user set an env var they + * never did, without re-awaiting the async rc loader. */ let rcInjectedTokenSource: string | undefined; -/** - * The `.sentryclirc` file that supplied the token now living in - * `SENTRY_AUTH_TOKEN`, or `undefined` when the token came from a real - * env var (or no token is set). - * - * Populated by {@link applySentryCliRcEnvShim}. Returns `undefined` if a - * genuine `SENTRY_AUTH_TOKEN`/`SENTRY_TOKEN` was already set, since the - * shim only injects when both are unset. - */ +/** Read the pinned rc-injected token source. See {@link rcInjectedTokenSource}. */ export function getRcInjectedTokenSource(): string | undefined { return rcInjectedTokenSource; }