Skip to content

fix(cli): Trust a .sentryclirc url that matches its org token's claim - #2538

Merged
BYK merged 2 commits into
mainfrom
fix/cli-rc-sntrys-claim-host
Nov 26, 2026
Merged

BYK merged 2 commits into
mainfrom
fix/cli-rc-sntrys-claim-host

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Fixes #2537

A project .sentryclirc that holds an sntrys_ org auth token and the self-hosted url it was issued for (what sentry wizard writes) was refused by every command. The env-token host snapshot is taken at boot, before the rc shim injects the token, so the rc token fell back to SaaS and never matched its own server.

The shim now scopes an injected sntrys_ token to its embedded url claim (scopeRcTokenToClaim in env-token-host.ts). assertRcUrlTrusted and the fetch guard then compare against that claim with the existing isHostTrusted rules. An rc url for any other host is still refused. Opaque rc tokens, env tokens, and stored logins keep today's behavior. The rc token also doesn't become an env account (getTrustedEnvTokenHost is unchanged), and the rc shim still doesn't register a login trust anchor. sentry mcp now serves such a token too; the test that pinned the old refusal is updated.

Threat model. The attacker can write or append to a .sentryclirc, or add a project-level one under a global rc that holds the token, but can't read the token. The rc url is compared against the claim inside the token, never against anything else in the file, so changing only the url can't redirect an existing token. A "url and token come from the same file" rule would not be safe, because an append-only write puts the attacker's url in the token's file. SENTRY_CUSTOM_HEADERS come from the user's env, so for a claim-scoped rc token they are checked against the host the token had before this change (the boot SENTRY_HOST/SENTRY_URL, else SaaS). A host that only the rc file names never gets them.

Trade-off: a self-hosted IAP setup that relies on SENTRY_CUSTOM_HEADERS with only an rc url still gets no custom headers. It needs SENTRY_URL in the env or sentry auth login --url, as today.

via @BYK.

--

View Session [Sentry]

A `sntrys_` org auth token that the `.sentryclirc` shim injects is now
scoped to the server named in its own `url` claim, so a self-hosted rc
`url` that matches the claim passes the host check. A `url` for any other
host is still refused, as are opaque rc tokens with a self-hosted `url`.
`SENTRY_CUSTOM_HEADERS` keep the host they had before (the boot
`SENTRY_HOST`/`SENTRY_URL`, else SaaS), so they never reach a host that
only the rc file names.

Fixes #2537

Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
@vercel

vercel Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
cli Ready Ready Preview Nov 26, 2026 7:36pm UTC
1 Skipped Deployment
Project Deployment Actions Updated
sentry-local Skipped Skipped Nov 26, 2026 7:36pm UTC

Request Review

Comment thread packages/cli/src/lib/sentryclirc.ts Resolved
…t host

The authenticated fetch applied `SENTRY_CUSTOM_HEADERS` when the request
matched the credential's host. For a `.sentryclirc` `sntrys_` token that
host is now its claim, which the rc file chose, so a forged token with a
matching rc url would have received the headers. `prepareHeaders` now uses
the same rule as `isRequestOriginTrustedForCustomHeaders`: an rc
claim-scoped token is checked against its boot host.
@vercel
vercel Bot temporarily deployed to Preview – sentry-local November 26, 2026 19:36 Inactive
@sentry-junior
sentry-junior Bot requested a review from BYK November 26, 2026 19:47
@BYK
BYK marked this pull request as ready for review November 26, 2026 22:36
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 26, 2026

@BYK BYK left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Based on the threat model, (let's assume GitHub Actions with a fork etc), is the env var safe? I think anyone can drop an env var in a subsequent stage by writing into $GITHUB_ENV or something?

@sentry-junior

sentry-junior Bot commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@BYK Short answer: the token stays safe. The custom headers have one narrow gap, and this PR doesn't widen it.

Token. An injected SENTRY_HOST/SENTRY_URL doesn't change where an rc sntrys_ token is scoped: its host is the claim. Every authenticated request goes through assertCredentialScope in prepareHeaders, which refuses any origin that doesn't match the claim. So SENTRY_URL=https://evil written to $GITHUB_ENV makes the commands fail with HostScopeError, and the token never reaches that host. With SENTRY_URL set, the shim also doesn't apply the rc url.

Custom headers on authenticated requests. These are safe for the same reason. assertCredentialScope throws before applyCustomHeaders runs, so nothing goes out.

Custom headers on unauthenticated requests. For a claim-scoped rc token, custom headers are checked against the boot env host. That's the same host the rc token was scoped to before this PR, so nothing new is exposed. The one path that sends them without the credential guard is the unauthenticated shared-issue lookup (getSharedIssue in api/issues.ts). If an attacker can write $GITHUB_ENV and get that host into the share URL a later step passes (sentry issue view https://evil/.../share/...), the headers go there. That gap already exists on main for any env token with a self-hosted SENTRY_URL.

Tracking. The getSharedIssue custom-header gap is tracked in #2545, which covers env and rc tokens together. This PR doesn't change for it.

@BYK
BYK merged commit 03396b8 into main Nov 26, 2026
29 checks passed
@BYK
BYK deleted the fix/cli-rc-sntrys-claim-host branch November 26, 2026 23:04
BYK added a commit that referenced this pull request Nov 27, 2026
…ged-in hosts (#2548)

Fixes #2545.

`getSharedIssue` sends no bearer token, so `assertCredentialScope` never
runs on it. Its custom-header check
(`isRequestOriginTrustedForCustomHeaders`) fell back to the active
credential's host. For an env token, and for an rc `sntrys_` token, that
host is the boot-time `SENTRY_URL`/`SENTRY_HOST`. A CI step that can
write `$GITHUB_ENV` can set that without reading the token, so a later
`sentry issue view <share-url>` for its host would send it
`SENTRY_CUSTOM_HEADERS`.

`isRequestOriginTrustedForCustomHeaders` is now the check for every
request that has no credential guard. It trusts only:
- this invocation's login trust anchor (`sentry auth login --url`), or
- a usable stored login on the request's server
(`hasStoredAccountForHost`).

It no longer looks at the active credential, so env and rc tokens are
both covered. `isRequestOriginTrusted` was only used by the old branch,
so this removes it.

**Paths that use this check** (all callers of `applyCustomHeaders` that
don't pass their own trust):
- `getSharedIssue` (`lib/api/issues.ts`): the reported path.
- OAuth device code and token polling, and the setup-code exchange
(`lib/oauth.ts`), when there's no `--url` anchor. These were already
behind the login host guard (`isLoginHostTrusted` /
`isAutoLoginHostTrusted`). With `auth login --url`, the anchor still
trusts the host.
- `sentry api` pagination (`commands/api.ts`) calls it only to build a
cache-key fingerprint. Nothing is sent there. The real request goes
through the authenticated transport.

**Not changed:** authenticated requests (`sentry-client.ts`
`prepareHeaders`) still pass trust from the credential that
`assertCredentialScope` matched, through #2538's
`isRequestOriginTrustedForCustomHeadersInContext`, so rc `sntrys_`
tokens keep its boot-host rule there. Token refresh
(`refreshAccessToken`) still trusts the stored credential host. Agent
handoff still uses its own host guard (`isAutoLoginHostTrusted`).

**Behavior change:** if you're behind IAP or a proxy and only set
`SENTRY_URL`/`SENTRY_HOST` with an env or `.sentryclirc` token, the
share-link lookup in `sentry issue view <share-url>` no longer gets
custom headers. To get them, run `sentry auth login --url <your-url>`
once. The self-hosted docs now say this. Authenticated commands against
the token's host still send the headers.

The docs change went into
`apps/cli-docs/src/content/docs/self-hosted.md`, the hand-written IAP
section, because neither file under `apps/cli-docs/src/fragments/`
covers custom headers.

<!-- junior-request-attribution:start -->
via @BYK.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A4dd9ae661b8a199ef3fb06c8525c3907 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A4dd9ae661b8a199ef3fb06c8525c3907)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A4dd9ae661b8a199ef3fb06c8525c3907/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <126780+BYK@users.noreply.github.com>

This branch was successfully deployed

1 active and 1 inactive deployments
Preview – cli — 133c116a Deployed Nov 26, 2026 by vercel[bot]
Preview – sentry-local — 133c116a Deployed Nov 26, 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.

Self-hosted url in .sentryclirc is refused with its own token

1 participant