Skip to content

ref(auth): Ask has_user() in access, org lookups and users/me - #129136

Merged
gricha merged 1 commit into
masterfrom
ref/request-actor-adoption
Oct 30, 2026
Merged

gricha merged 1 commit into
masterfrom
ref/request-actor-adoption

Conversation

@gricha

@gricha gricha commented Oct 29, 2026 •

Copy link
Copy Markdown
Member

Checks that meant "is there a user" asked "is logged in" (is_authenticated) or read request.user.id. They now ask has_user() or call acting_user_id(request.user). Every existing caller gets the same answer; only a ServiceAccountPrincipal, which is authenticated but has no user, changes.

Nothing here is the runtime fix for service accounts: that's determine_access in #129146. These change for consistency and defense:

  • Control-silo org lookups use acting_user_id().
  • users/me is a defensive 404 for a service account.
  • normalize_valid_user returns None unless has_user(), so from_request with no org gives a service account no access instead of an OrganizationlessAccess.
  • Rate limits ask has_user instead of isinstance(AnonymousUser).

The viewer-context middleware now builds its actor with resolve_actor instead of a copy of it. That's deduplication: the only difference is that a service-account credential gets SERVICE_ACCOUNT instead of UNKNOWN, and the signed header overrides that anyway.

Service accounts are gated by organizations:service-accounts.

Matrix: fixes no service-account rows (known gaps stay at 89).

Stack (each PR is based on the one above it):

  1. ref(auth): Add a shared request.user interface #128982 Shared request.user interface
  2. ref(auth): Set request.user and request.auth through one helper #129133 Set request.user / request.auth through one helper
  3. feat(auth): Put service accounts on request.user as a principal #129146 Service accounts on request.user
  4. ref(auth): Ask has_user() in access, org lookups and users/me #129136 has_user() in access, org lookups and users/me
  5. feat(api): Let endpoints and code require a user #129143 requires_user / require_user()
  6. ref(auth): Lint request.user patterns that break for callers with no user #129144 Lints for patterns that break for a service account
  7. ref(orgs): Ask has_user() for the acting user in org and team endpoints #129154 Orgs, teams, projects
  8. ref(issues): Ask has_user() for the acting user on issue actions #129155 Issues
  9. ref(explore): Ask has_user() for the acting user on saved queries and events #129149 Discover, Explore
  10. ref(dashboards): Use acting_user_id() for the dashboard creator #129156 Dashboards
  11. ref(workflow_engine): Ask has_user() for the detector and workflow creator #129150 Workflow engine
  12. ref(replays): Use acting_user_id() for the viewer #129151 Replays
  13. ref(preprod): Use acting_user_id() for the uploader #129152 Preprod
  14. ref(auth): Ask has_user() for the acting user in the remaining endpoints #129153 Remaining endpoints
  15. ref(audit): Attribute audit entries only to a user #129253 Audit entries
  16. ref(api): Skip per-user serializer lookups when no user is calling #129254 Per-user serializer lookups
  17. ref(users): Serialize no user when no user is behind the caller #129255 serialize_generic_user
  18. ref(features): Give flagpole no user context without a user #129256 Flagpole user context

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Oct 29, 2026
@gricha
gricha force-pushed the ref/request-actor-adoption branch from 621787d to 8cb1029 Compare October 29, 2026 06:38
@gricha gricha changed the title ref(auth): Read request.actor in access, permissions and org lookups ref(auth): Read request.user.user_id in access, permissions and org lookups Oct 29, 2026
@gricha
gricha added this pull request to stack #129145 October 29, 2026 06:46
@gricha
gricha force-pushed the ref/request-actor-adoption branch from 8cb1029 to e658b35 Compare October 29, 2026 16:25
@sentry

sentry Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Sentry Snapshot Testing

Name Added Removed Changed Renamed Unchanged Skipped Status
sentry-frontend
sentry-frontend
0 0 0 0 780 0 ✅ Unchanged

⚙️ sentry-frontend Snapshot Settings

@gricha
gricha force-pushed the ref/request-actor-adoption branch from e658b35 to 51ce362 Compare October 29, 2026 18:30
@gricha
gricha force-pushed the ref/request-actor-adoption branch from 51ce362 to 7cdc780 Compare October 29, 2026 18:49
@gricha
gricha removed this pull request from stack #129145 October 29, 2026 18:54
@gricha
gricha added this pull request to stack #129233 October 29, 2026 18:54
@gricha
gricha removed this pull request from stack #129233 October 29, 2026 20:47
@gricha
gricha changed the base branch from ref/request-user-from-actor to ref/request-principal-chokepoint October 29, 2026 20:48
@gricha
gricha force-pushed the ref/request-actor-adoption branch from 7cdc780 to 9c87ec3 Compare October 29, 2026 20:48
@gricha
gricha added this pull request to stack #129249 October 29, 2026 21:07
@gricha
gricha removed this pull request from stack #129249 October 29, 2026 21:46
@gricha
gricha changed the base branch from ref/request-principal-chokepoint to feat/service-account-principal-core October 29, 2026 21:51
@gricha
gricha force-pushed the ref/request-actor-adoption branch from 9c87ec3 to 4a2be36 Compare October 29, 2026 21:51
@gricha
gricha added this pull request to stack #129262 October 29, 2026 21:57
@gricha
gricha requested a review from azulus October 30, 2026 03:56
@gricha
gricha force-pushed the ref/request-actor-adoption branch from 5251360 to 3f4e360 Compare October 30, 2026 04:09
@gricha gricha changed the title ref(auth): Ask is_human() in access, org lookups and users/me ref(auth): Ask has_user() in access, org lookups and users/me Oct 30, 2026
@gricha
gricha force-pushed the ref/request-actor-adoption branch from 3f4e360 to 4f66d17 Compare October 30, 2026 05:06
@gricha
gricha force-pushed the ref/request-actor-adoption branch 2 times, most recently from 64eeb7a to 77e1ec1 Compare October 30, 2026 06:37

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

Stale Bugbot comment from a previous run.

Comment thread src/sentry/auth/access.py Resolved
@gricha
gricha force-pushed the ref/request-actor-adoption branch from 77e1ec1 to 3737fa7 Compare October 30, 2026 08:56

@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 2 potential issues.

There are 3 total unresolved issues (including 1 from previous review).

Fix All in Cursor

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

Reviewed by Cursor Bugbot for commit 3737fa7. Configure here.

Comment thread src/sentry/middleware/viewer_context.py Resolved
Comment thread src/sentry/ratelimits/utils.py Resolved
@gricha
gricha removed this pull request from stack #129262 October 30, 2026 17:56
@gricha
gricha added this pull request to stack #129336 October 30, 2026 17:57
@gricha
gricha force-pushed the ref/request-actor-adoption branch from 3737fa7 to d72028c Compare October 30, 2026 18:10
gricha added a commit that referenced this pull request Oct 30, 2026
Adds `acting_user_id(principal)` in `sentry.auth.principal`: the user
behind whatever sits on `request.user`, or None. A user gives its own
id, an agent the user it acts for, anyone else None. `RequestPrincipal`
is the interface every caller shares; Django's `AnonymousUser` is
patched at startup with the two members it lacks (`is_sentry_app`,
`get_display_name`), and the mypy plugin declares them.

The goal of the stack is that endpoints work for users, agents and
service accounts by default, without each one checking which it got. A
service account must never carry a user id, so `acting_user_id()` is the
one question every caller can answer.

It also starts a short "Request Principals" section in `src/AGENTS.md`;
each PR above extends it with what it adds.

It also adds the rest of the vocabulary, so every PR above can use it:
`has_user(principal)` narrows to `User | RpcUser` when a user is behind
the caller (the user, or an agent acting for one), and `UserRequired` is
the 403 `user-required` for code that needs one.

Nothing reads them yet.

**Stack** (each PR is based on the one above it):
1. #128982 Shared `request.user` interface
2. #129133 Set `request.user` / `request.auth` through one helper
3. #129146 Service accounts on `request.user`
4. #129136 `has_user()` in access, org lookups and `users/me`
5. #129143 `requires_user` / `require_user()`
6. #129144 Lints for patterns that break for a service account
7. #129154 Orgs, teams, projects
8. #129155 Issues
9. #129149 Discover, Explore
10. #129156 Dashboards
11. #129150 Workflow engine
12. #129151 Replays
13. #129152 Preprod
14. #129153 Remaining endpoints
15. #129253 Audit entries
16. #129254 Per-user serializer lookups
17. #129255 `serialize_generic_user`
18. #129256 Flagpole user context
@gricha
gricha force-pushed the ref/request-actor-adoption branch from d72028c to 59df701 Compare October 30, 2026 18:23
Base automatically changed from feat/service-account-principal-core to master October 30, 2026 18:40
@gricha
gricha requested review from a team as code owners October 30, 2026 18:40
Checks that meant "is there a person" asked "is logged in"
(is_authenticated) or read request.user.id. They now ask has_user() or read
acting_user_id(request.user). Every existing caller gets the same answer; only a
ServiceAccountPrincipal, authenticated with user_id=None, changes.

The runtime fix for service accounts, determine_access, is in the core PR.
These change for consistency and defense: the control-silo org lookups,
users/me (a 404 for a service account), normalize_valid_user (from_request
with no org gives a service account no access) and rate limits. The
viewer-context middleware builds its actor with resolve_actor instead of a
copy of it.
gricha added a commit that referenced this pull request Oct 30, 2026
)

Adds `set_request_principal(request, user, auth)` and routes every place
in `src` that sets `request.user` or `request.auth` through it. That
covers the auth middleware, DRF authentication (`Request._authenticate`
is wrapped), the login/logout re-syncs, the cross-silo auth swap, the
internal API client (the request it builds and the DRF request
`initialize_request` stitches back together) and the test helpers. A
test fails on new direct assignments. Django's own `login()`/`logout()`
still set `request.user` internally; they only ever set a real or
anonymous user.

When DRF authentication finds a caller, the viewer context's actor is
updated to match. Endpoints with their own `authentication_classes` run
them after the viewer-context middleware, so the context would otherwise
describe the middleware's result. A credential with no caller of its
own, such as a signed seer-rpc call, keeps the viewer from the signed
header. So does a user that `ViewerContextAuthentication` loaded from a
signed `X-Viewer-Context`: the header already set its actor, e.g. an
agent acting for that user. The actor now also carries superuser
elevation from the credential, as the middleware already did.
`ApiClient` runs the view in its own viewer-context scope, so the view's
caller doesn't replace ours.

**Stack** (each PR is based on the one above it):
1. #128982 Shared `request.user` interface
2. #129133 Set `request.user` / `request.auth` through one helper
3. #129146 Service accounts on `request.user`
4. #129136 `has_user()` in access, org lookups and `users/me`
5. #129143 `requires_user` / `require_user()`
6. #129144 Lints for patterns that break for a service account
7. #129154 Orgs, teams, projects
8. #129155 Issues
9. #129149 Discover, Explore
10. #129156 Dashboards
11. #129150 Workflow engine
12. #129151 Replays
13. #129152 Preprod
14. #129153 Remaining endpoints
15. #129253 Audit entries
16. #129254 Per-user serializer lookups
17. #129255 `serialize_generic_user`
18. #129256 Flagpole user context
gricha added a commit that referenced this pull request Oct 30, 2026
)

On a DRF request, a service account's `request.user` is a
`ServiceAccountPrincipal` instead of `AnonymousUser`. Service accounts
are gated by `organizations:service-accounts`, which is now checked at
authentication too (`authenticate_service_account`), so that flag is the
one switch. Before, only service-account creation and the token endpoint
checked it, and anything holding the shared secret could sign a
service-account viewer context.

The principal has no user, so `has_user()` is False and
`acting_user_id()` is None, and its actor is `SERVICE_ACCOUNT`. Its `id`
is a `ServiceAccountId` that never equals an int and raises
`UserRequired` (403 `user-required`) when converted to one, so no
`request.user.id` call site can reach the user with the same number.

**Temporary:** `ServiceAccountId` is a runtime guard, because about 490
`request.user.id` reads still assume a user. It's deleted, along with
the RPC pass-through for `UserRequired`, once mypy types `request.user`
to include `ServiceAccountPrincipal` (plugin, shrinking per-module
allowlist) and those reads are migrated.

This PR sits right above the chokepoint on purpose: every PR after it is
written and tested against the real principal. Endpoints aren't blocked
up front. What breaks for a service account shows up in the auth matrix,
and the PRs above fix it or mark the endpoint `requires_user`.

What else this changes:
- **Web views:** `set_request_principal` sets a service account only on
a DRF `Request` (its setter also copies it to Django's `request.user`).
Until DRF authenticates it, Django's `request.user` is anonymous, and
session auth never sees it, since `ViewerContextAuthentication` runs
before `SessionAuthentication`. The check reads the credential
(`is_service_account_auth(auth)`), not `isinstance(user, ...)`: the
middleware passes Django's lazy user, and loading it reads the session,
which adds `Vary: Cookie` to every response.
`test_leaves_lazy_user_unloaded` guards it.
- **Access:** `SentryPermission.determine_access` asks `has_user()` and
`acting_user_id()`, so a service account keeps the credential-based
access (`from_rpc_auth`) it had as `AnonymousUser`, instead of its
`ServiceAccountId` reaching the org lookup.
- **Sudo:** neither the decorator nor the sudo middleware treats a
service account as sudo; both ask `has_user()` before
`has_usable_password()`. Sudo re-authenticates a person, so
`require_sudo()` answers a caller with no user with a 403
`user-required` instead of a sudo prompt, from `@sudo_required` and from
project delete. Org, team and project delete and project transfer stay
user-only.
- **Rate limits:** service accounts get their own bucket.
- **RPC:** `UserRequired` passes through as an API error.

The auth matrix enables the flag for its service-account rows and
records what still fails at this point in the stack. Rows that don't
match the oracle are strict xfails, so the PR that fixes a row has to
remove it. At this point 89 service-account rows are known gaps: 28
audit actors, 21 per-user serializer lookups, 20 `request.user.id`
reads, 2 `serialize_generic_user`, 1 project creation that needs
`require_user`, 12 control-silo endpoints and 5 that check the caller's
own membership or org role. The PRs above remove them. At the top of the
stack, 17 remain: the control-silo and membership ones.

This replaces #128983, split per review. It builds on #128779, now
landed.

**Stack** (each PR is based on the one above it):
1. #128982 Shared `request.user` interface
2. #129133 Set `request.user` / `request.auth` through one helper
3. #129146 Service accounts on `request.user`
4. #129136 `has_user()` in access, org lookups and `users/me`
5. #129143 `requires_user` / `require_user()`
6. #129144 Lints for patterns that break for a service account
7. #129154 Orgs, teams, projects
8. #129155 Issues
9. #129149 Discover, Explore
10. #129156 Dashboards
11. #129150 Workflow engine
12. #129151 Replays
13. #129152 Preprod
14. #129153 Remaining endpoints
15. #129253 Audit entries
16. #129254 Per-user serializer lookups
17. #129255 `serialize_generic_user`
18. #129256 Flagpole user context
@gricha
gricha force-pushed the ref/request-actor-adoption branch from 59df701 to 6011970 Compare October 30, 2026 18:40
@gricha
gricha merged commit 11d861a into master Oct 30, 2026
97 checks passed
@gricha
gricha deleted the ref/request-actor-adoption branch October 30, 2026 19:18
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