Skip to content

fix(api): Tighten alert mutation write scopes#113194

Closed
dcramer wants to merge 5 commits into
masterfrom
dcramer/fix/api-write-scope-mutations-clean
Closed

fix(api): Tighten alert mutation write scopes#113194
dcramer wants to merge 5 commits into
masterfrom
dcramer/fix/api-write-scope-mutations-clean

Conversation

@dcramer
Copy link
Copy Markdown
Member

@dcramer dcramer commented Apr 16, 2026

Tighten project-scoped write checks for alert-style mutation endpoints that target a specific project.

This rebuilds the intended scope-tightening work from #113120 directly on master so it can land without the unrelated readonly-mutation-notes changes that were stacked into the old branch.

The main change is to honor project-scoped alerts:write access consistently across alert-rule and detector mutations, monitor creation, uptime previews and suggestions, replay deletes, data export authorization, user issue mutation, and Seer anomaly preview requests. While tightening those checks, this keeps the established missing-target behavior for alert-rule and detector detail flows instead of turning stale or nonexistent targets into generic permission failures. The Seer anomaly preview follow-up also preserves lookup-miss handling when the permission layer receives an RpcUserOrganizationContext.

This also updates regression coverage around project-scoped writers, missing targets, cross-project access, and the anomaly-preview permission path so the permission logic and endpoint behavior stay aligned.

The optional OpenAPI schema-diff check is expected on this PR because these permission changes intentionally update the documented security requirements for the affected endpoints.

Supersedes #113120.

@github-actions github-actions Bot added the Scope: Backend Automatically applied to PRs that change backend components label Apr 16, 2026
Comment thread src/sentry/workflow_engine/endpoints/organization_detector_details.py Outdated
Comment thread src/sentry/incidents/endpoints/bases.py
Comment thread src/sentry/api/bases/organization.py Outdated
dcramer and others added 3 commits April 16, 2026 12:41
Require project-scoped write access for alert-style mutation endpoints across alerts, monitors, workflow engine, seer, uptime, replays, and related API surfaces. This keeps the intended write-scope behavior while rebuilding the change directly on master instead of depending on the readonly-mutation-notes stack.

Add regression coverage for token and team-admin scope checks, plus the endpoint-specific edge cases that surfaced while unstacking the branch.

Co-Authored-By: OpenAI Codex <noreply@openai.com>
Keep missing alert-rule and detector detail lookups on the existing 404 path for project-scoped alerts:write callers. The permission helper now distinguishes a lookup miss from a view that does not participate in project-scoped alert checks, and the detail endpoint tests cover both the missing-target 404 and cross-project 403 behaviors.

Co-Authored-By: OpenAI Codex <noreply@openai.com>
Continue the feedback pass by keeping workflow-engine detector lookups reachable from the organization alert-rule detail endpoint. The project-scoped permission helper now preserves the 404 behavior for real lookup misses without short-circuiting the detector fallback path, and the alert-rule detail tests cover the team-admin workflow-engine detector cases explicitly.

Also fold the duplicated organization-id helper into a shared location and reuse the alert-rule permission implementation for detector endpoints to avoid the same drift in future changes.

Co-Authored-By: OpenAI Codex <noreply@openai.com>
Comment thread src/sentry/seer/endpoints/organization_events_anomalies.py Outdated
@dcramer dcramer force-pushed the dcramer/fix/api-write-scope-mutations-clean branch from 1d18443 to aec2a3d Compare April 16, 2026 20:18
Route get_alert_mutation_projects through _get_organization_id so the\norganization-scoped alert permission check still works when auth token\nevaluation passes an RpcUserOrganizationContext.\n\nAdd a regression test for the RPC wrapper path so the mutation lookup\ndoes not silently return None again.\n\nCo-Authored-By: Codex <noreply@openai.com>
Comment thread src/sentry/seer/endpoints/organization_events_anomalies.py Outdated
Return ALERT_MUTATION_LOOKUP_MISS for invalid and missing anomaly\npreview project IDs so project-scoped alerts:write callers follow the\nmissing-target path instead of falling through to team fallback.\n\nTighten the related permission narrowing and test annotations so the\nbackend typing job matches the runtime behavior again.\n\nCo-Authored-By: Codex <noreply@openai.com>
Copy link
Copy Markdown
Contributor

@cursor cursor Bot left a comment

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.

Reviewed by Cursor Bugbot for commit eafc40f. Configure here.

return Response(
{"detail": "You do not have permission to perform this action."},
status=403,
)
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.

Anomaly preview returns misleading 403 for nonexistent projects

Medium Severity

When a user with org-level write access (e.g., org:admin or org:write) passes the permission check via super().has_object_permission(), the get_alert_mutation_projects method is never called. If the project_id in the request body refers to a project that doesn't exist in the organization, the post() method now returns a 403 with "You do not have permission to perform this action." The old code returned a 400 with "Invalid project," which was more accurate — the user clearly has permission, the project just doesn't exist. This misleads authorized callers into thinking they lack access rather than having submitted an invalid project ID.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit eafc40f. Configure here.

@dcramer dcramer closed this Apr 17, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators May 2, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant