Skip to content

fix(auth): Use ASCII-only case folding for invite and registration emails - #128720

Merged
nora-shap merged 1 commit into
masterfrom
nora/auth/ID-1864
Oct 27, 2026
Merged

nora-shap merged 1 commit into
masterfrom
nora/auth/ID-1864

Conversation

@nora-shap

Copy link
Copy Markdown
Member

Two email comparisons still used Python's Unicode-aware lowercasing. Unicode case mapping can turn some non-ASCII characters into ASCII ones (for example, U+212A lowercases to "k"), so a look-alike address could be treated as an existing one.

  • AuthIdentityHandler._email_verified_via_pending_invite compared the SSO identity email to the invited email with .lower(). It now uses emails_match, the ASCII-only helper from ref(auth): Apply consistent email matching in user service and SSO verification #127401.
  • RegistrationValidator.validate_email returned value.lower(), so a look-alike address was saved as the ASCII address it resembles. It now returns ascii_casefold(value), which lowercases only A-Z.

The duplicate-account check in validate_email intentionally keeps iexact. Postgres UPPER matches some look-alikes (for example, "ı" to "I"), and for a uniqueness check, over-matching is the safe direction: it refuses to create a look-alike twin of an existing account.

@linear-code

linear-code Bot commented Oct 23, 2026

Copy link
Copy Markdown

ID-1864

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 23, 2026
@nora-shap
nora-shap marked this pull request as ready for review October 26, 2026 19:57
@nora-shap
nora-shap requested review from a team as code owners October 26, 2026 19:57

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit d73cd3d. Configure here.

Comment thread src/sentry/api/validators/auth.py Resolved
@nora-shap
nora-shap added this pull request to stack #128852 October 26, 2026 23:19
@nora-shap
nora-shap merged commit 32b05ed into master Oct 27, 2026
71 of 72 checks passed
@nora-shap
nora-shap deleted the nora/auth/ID-1864 branch October 27, 2026 17:04
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]>
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.

2 participants