Skip to content

feat(oauth): Enforce login_hint in the device authorization flow - #128458

Merged
BYK merged 2 commits into
masterfrom
feat/oauth-device-login-hint
Oct 28, 2026
Merged

BYK merged 2 commits into
masterfrom
feat/oauth-device-login-hint

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

The Sentry CLI's sentry auth login <email> (getsentry/toolkit multi-account mode) has no way to tell the device flow which account it expects. Whoever is signed in to the browser approves the request, and the CLI can only throw the token away afterwards. By then the server has already issued it.

This PR adds an optional login_hint to POST /oauth/device/code/. RFC 8628 §3.1 allows extension parameters, and the name comes from OpenID Connect. The hint is an email. It is stored on ApiDeviceCode and checked on the verification endpoint:

  • GET /api/0/oauth/device/ (consent preparation) returns 403 if the signed-in user's primary email doesn't match the hint, case-insensitively. Consent is not bound to the session. The detail names the expected account.
  • POST /api/0/oauth/device/ checks again before approving or denying, so a prepared session can't get around it.

Without a hint, everything works as before.

Why 403 and the primary email

  • The device page already treats a 403 on consent from an authenticated session as "this account can't authorize the request." It shows the error and keeps the switch-account control. So the user sees "This request is for [email protected]. Sign in as that account to continue." with no frontend change needed.
  • The CLI checks user.email from the token response. Matching only the primary email keeps the server and the CLI in agreement.

Notes for review

  • Migration 1245 adds a nullable varchar(200) column to sentry_apidevicecode. The length matches User.email. A hint over 200 characters gets invalid_request.
  • The hint is sanitized with set_email during relocation, and the exhaustive backup fixture now sets it.
  • The hint is not logged. Only a has_login_hint metric tag is added.

The CLI side is getsentry/toolkit#1580.

Fixes #127918
Fixes ENG-8835

via BYK.

--

View Junior Session in Sentry

@sentry-junior
sentry-junior Bot requested review from a team as code owners October 21, 2026 21:38
@linear-code

linear-code Bot commented Oct 21, 2026

Copy link
Copy Markdown

ENG-8835

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 21, 2026
@sentry-junior
sentry-junior Bot requested a review from evanpurkhiser October 21, 2026 21:38
@sentry-junior

sentry-junior Bot commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

@evanpurkhiser friendly nudge — could you take a look when you get a chance? This adds an optional login_hint to the OAuth device flow so sentry auth login <email> can enforce the expected account server-side. CI is green. The CLI side is getsentry/toolkit#1580.

@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.

Resolve merge conflicts please

Device clients can now send an optional login_hint (an email) with
POST /oauth/device/code/. The hint is stored on ApiDeviceCode, and the
verification endpoint refuses to prepare or complete consent for a
signed-in user whose primary email does not match it. The 403 response
names the expected account, so the browser keeps the switch-account
controls and shows what to sign in as.

Requests without a hint behave as before.

Fixes #127918
Fixes ENG-8835

Co-Authored-By: BYK <[email protected]>
@sentry-junior
sentry-junior Bot force-pushed the feat/oauth-device-login-hint branch from aee341d to b44dd1c Compare October 27, 2026 21:42
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

decision-model-linter

api

src/sentry/web/frontend/oauth_device_authorization.py

  • warn readsRequestBodyOutsideSerializer = TRUE (0.97): Request body values should be read from a serializer's validated_data. This file reads a value directly from request.data or request.POST.

decision-model-linter: 10 calls on 4 files, 20,294 input tokens, $0.00085, model typesafe/jev-1.13-20260917

sentry-junior Bot added a commit to getsentry/toolkit that referenced this pull request Oct 27, 2026
Pass the requested email as the device flow's login_hint so Sentry
refuses approval from a different signed-in user up front, instead of
the CLI discarding the token after the fact. The local email check
stays for servers that ignore the hint.

Server side: getsentry/sentry#128458

Co-Authored-By: BYK <[email protected]>
@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

This PR has a migration; here is the generated SQL for src/sentry/migrations/1245_add_login_hint_to_apidevicecode.py

for 1245_add_login_hint_to_apidevicecode in sentry

--
-- Add field login_hint to apidevicecode
--
ALTER TABLE "sentry_apidevicecode" ADD COLUMN "login_hint" varchar(200) NULL;


def accepts_user_email(self, email: str) -> bool:
"""Check whether a user with this primary email may approve the request."""
return self.login_hint is None or self.login_hint.casefold() == email.casefold()

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.

@michelletran-sentry casefold covers the stuff we fixed recently yeah?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not quite. casefold() is the broader version of the Unicode-aware .lower() that #127401 and #128720 removed from auth email comparisons. It still maps look-alikes onto ASCII (e.g. "\u212a".casefold() == "k"), and it adds more folds on top ("ß".casefold() == "ss"). So a login_hint of [email protected] would accept a user whose primary email is [email protected] written with a Kelvin sign.

The fix that matches the recent changes is sentry.auth.email.emails_match(self.login_hint, email), which folds only ASCII A–Z. Happy to push that plus a look-alike test.

…ogin-hint

# Conflicts:
#	migrations_lockfile.txt

Co-Authored-By: BYK <[email protected]>
@BYK
BYK merged commit abae1b3 into master Oct 28, 2026
91 of 92 checks passed
@BYK
BYK deleted the feat/oauth-device-login-hint branch October 28, 2026 22:33
BYK added a commit that referenced this pull request Oct 28, 2026
…9114)

Follow-up to #128458, from [this review
thread](#128458 (comment)).

The device-flow `login_hint` check compared the hint to the signed-in
user's primary email with `str.casefold()`. Unicode case folding maps
some non-ASCII characters onto ASCII. For example, U+212A KELVIN SIGN
folds to `k`. So a hint for one address could be approved by an account
whose email only looks like it.

This switches the check to `sentry.auth.email.emails_match`. That helper
folds ASCII A–Z only, and it's what the other auth email comparisons
moved to in #127401 and #128720.

- `ensure_expected_account` in `oauth_device.py` now calls
`emails_match` directly. It was the only caller of
`ApiDeviceCode.accepts_user_email`, so I removed that method. The model
module can't import `sentry.auth.email` without a cycle, because that
module imports `User`, `Organization` and org services.
- `test_login_hint_rejects_unicode_lookalike` covers the case. It fails
on the old code, which returns 200, and passes with this change, which
returns 403. The existing ASCII case-insensitivity test still passes.

**Verification:** `test_oauth_device.py` and
`test_oauth_device_authorization.py` pass (40 tests). prek and mypy pass
on the changed files.

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

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:local%3Aweb%3A7f8efa11623b9416987ceb44 -->

--

[View Junior Session in
Sentry](https://sentry.sentry.io/explore/conversations/local%3Aweb%3A7f8efa11623b9416987ceb44/?project=4510944073809921)

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

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: BYK <[email protected]>
BYK added a commit to getsentry/toolkit that referenced this pull request Nov 9, 2026
`sentry auth login <email>` now sends the email as `login_hint` in the
device authorization request. Sentry adds support for this in
getsentry/sentry#128458. With it, the verification page refuses approval
from a different signed-in user up front and asks them to switch
accounts. Today the user approves, and only then does the CLI throw the
token away.

- `deviceCodeRequestBody` (toolkit-core) takes an optional `loginHint`
and adds `login_hint` only when one is given. Callers that don't pass
it, such as the MCP stdio flow, send the same body as before.
- `performDeviceFlow` / `requestDeviceCode` pass the hint through.
`runInteractiveLogin` sends `expectedEmail`.
- The existing `assertExpectedLoginEmail` check stays. Servers that
predate the parameter ignore it, including self-hosted instances and
sentry.io until #128458 deploys. On those, the browser's user can still
approve, and the CLI-side check is what catches a mismatch.
- The `auth login` help text and the `auth` docs fragment are updated to
match.

Re-authorizing a stored account by `accountId` (scope recovery) doesn't
send a hint. That check matches by user id, and the stored email could
be stale.

**Checks run locally**
- `vitest` passes for `test/lib/interactive-login.test.ts`,
`test/lib/oauth.test.ts`, `test/commands/auth/*` and
`toolkit-core/src/oauth-device.test.ts`.
- `pnpm run lint` passes.
- `tsc --noEmit` is clean for toolkit-core and mcp-server. In the CLI,
the only errors are missing generated files.
- The CLI's full `typecheck` script couldn't finish here because
`generate:banner` crashes in this sandbox's Node 22.22.2 (the pin is
22.23.4). CI should cover it.

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

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:local%3Aweb%3A7f8efa11623b9416987ceb44 -->

--

[View Junior Session in
Sentry](https://sentry.sentry.io/explore/conversations/local%3Aweb%3A7f8efa11623b9416987ceb44/?project=4510944073809921)

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

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

This branch was successfully deployed

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

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OAuth device flow: accept a login hint so the expected account is enforced

2 participants