Repository navigation
feat(oauth): Enforce login_hint in the device authorization flow - #128458
Conversation
|
@evanpurkhiser friendly nudge — could you take a look when you get a chance? This adds an optional |
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]>
aee341d to
b44dd1c
Compare
decision-model-linterapi
decision-model-linter: 10 calls on 4 files, 20,294 input tokens, $0.00085, model typesafe/jev-1.13-20260917 |
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]>
|
This PR has a migration; here is the generated SQL for for --
-- 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() |
There was a problem hiding this comment.
@michelletran-sentry casefold covers the stuff we fixed recently yeah?
There was a problem hiding this comment.
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]>
…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]>
`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]>
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_hinttoPOST /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 onApiDeviceCodeand 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. Thedetailnames 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
user.emailfrom the token response. Matching only the primary email keeps the server and the CLI in agreement.Notes for review
1245adds a nullablevarchar(200)column tosentry_apidevicecode. The length matchesUser.email. A hint over 200 characters getsinvalid_request.set_emailduring relocation, and the exhaustive backup fixture now sets it.has_login_hintmetric tag is added.The CLI side is getsentry/toolkit#1580.
Fixes #127918
Fixes ENG-8835
via BYK.
--
View Junior Session in Sentry