Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deploys): Fix permissions for deploy endpoint projects #78026

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/sentry/api/endpoints/release_deploys.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class DeploySerializer(serializers.Serializer):
dateStarted = serializers.DateTimeField(required=False, allow_null=True)
dateFinished = serializers.DateTimeField(required=False, allow_null=True)
projects = serializers.ListField(
child=ProjectField(scope="project:read", id_allowed=True), required=False, allow_empty=False
child=ProjectField(scope=("project:read", "project:releases"), id_allowed=True),
required=False,
allow_empty=False,
)

def validate_environment(self, value):
Expand Down
13 changes: 11 additions & 2 deletions src/sentry/api/serializers/rest_framework/project.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from collections.abc import Collection

from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import extend_schema_field
from rest_framework import serializers
Expand All @@ -9,7 +11,12 @@

@extend_schema_field(field=OpenApiTypes.STR)
class ProjectField(serializers.Field):
def __init__(self, scope="project:write", id_allowed=False):
def __init__(self, scope: str | Collection[str] = "project:write", id_allowed: bool = False):
"""
The scope parameter specifies which permissions are required to access the project field.
If multiple scopes are provided, the project can be accessed when the user is authenticated with
any of the scopes.
"""
self.scope = scope
self.id_allowed = id_allowed
super().__init__()
Expand All @@ -27,6 +34,8 @@ def to_internal_value(self, data):
project = Project.objects.get(organization=self.context["organization"], slug=data)
except Project.DoesNotExist:
raise ValidationError("Invalid project")
if not self.context["access"].has_project_scope(project, self.scope):

scopes = (self.scope,) if isinstance(self.scope, str) else self.scope
if not self.context["access"].has_any_project_scope(project, scopes):
raise ValidationError("Insufficient access to project")
return project
Loading