Skip to content
This repository was archived by the owner on Oct 23, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions packages/cli/src/lib/auth-hint.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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");

Expand Down Expand Up @@ -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.
*
Expand Down
20 changes: 20 additions & 0 deletions packages/cli/src/lib/sentryclirc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,24 @@ export function loadSentryCliRc(cwd: string): Promise<SentryCliRcConfig> {
return promise;
}

/**
* 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.
*
* 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;

/** Read the pinned rc-injected token source. See {@link rcInjectedTokenSource}. */
export function getRcInjectedTokenSource(): string | undefined {
Comment thread
jared-outpost[bot] marked this conversation as resolved.
Resolved
return rcInjectedTokenSource;
}

/**
* Apply env shim for `.sentryclirc` token and URL fields.
*
Expand Down Expand Up @@ -350,6 +368,7 @@ export async function applySentryCliRcEnvShim(cwd: string): Promise<void> {
`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;
Expand Down Expand Up @@ -400,4 +419,5 @@ export function clearSentryCliRcCache(): void {
cache.clear();
// Reset global paths — tests change SENTRY_CONFIG_DIR between runs
globalPaths = null;
rcInjectedTokenSource = undefined;
}
35 changes: 34 additions & 1 deletion packages/cli/test/lib/auth-hint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@
* 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,
resetAuthHintState,
} 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;
Expand All @@ -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();
});

Expand All @@ -51,6 +60,7 @@ afterEach(() => {
} else {
delete process.env.SENTRY_FORCE_ENV_TOKEN;
}
clearSentryCliRcCache();
});

/**
Expand Down Expand Up @@ -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";
Expand Down
18 changes: 18 additions & 0 deletions packages/cli/test/lib/sentryclirc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
assertRcUrlTrusted,
CONFIG_FILENAME,
clearSentryCliRcCache,
getRcInjectedTokenSource,
loadSentryCliRc,
} from "../../src/lib/sentryclirc.js";
import { cleanupTestDir, createTestConfigDir } from "../helpers.js";
Expand Down Expand Up @@ -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");
Expand Down
Loading