Skip to content

Commit

Permalink
fix: workspace slug validation (#5023)
Browse files Browse the repository at this point in the history
  • Loading branch information
pablohashescobar authored Jul 3, 2024
1 parent db722d5 commit 095639b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
8 changes: 5 additions & 3 deletions apiserver/plane/app/serializers/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ class WorkSpaceSerializer(DynamicBaseSerializer):
total_members = serializers.IntegerField(read_only=True)
total_issues = serializers.IntegerField(read_only=True)

def validated(self, data):
if data.get("slug") in RESTRICTED_WORKSPACE_SLUGS:
raise serializers.ValidationError({"slug": "Slug is not valid"})
def validate_slug(self, value):
# Check if the slug is restricted
if value in RESTRICTED_WORKSPACE_SLUGS:
raise serializers.ValidationError("Slug is not valid")
return value

class Meta:
model = Workspace
Expand Down
8 changes: 6 additions & 2 deletions apiserver/plane/app/views/workspace/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
WorkspaceTheme,
)
from plane.utils.cache import cache_response, invalidate_cache
from plane.utils.constants import RESTRICTED_WORKSPACE_SLUGS


class WorkSpaceViewSet(BaseViewSet):
Expand Down Expand Up @@ -118,7 +119,7 @@ def create(self, request):
status=status.HTTP_400_BAD_REQUEST,
)

if serializer.is_valid():
if serializer.is_valid(raise_exception=True):
serializer.save(owner=request.user)
# Create Workspace member
_ = WorkspaceMember.objects.create(
Expand Down Expand Up @@ -231,7 +232,10 @@ def get(self, request):
status=status.HTTP_400_BAD_REQUEST,
)

workspace = Workspace.objects.filter(slug=slug).exists()
workspace = (
Workspace.objects.filter(slug=slug).exists()
or slug in RESTRICTED_WORKSPACE_SLUGS
)
return Response({"status": not workspace}, status=status.HTTP_200_OK)


Expand Down
15 changes: 2 additions & 13 deletions apiserver/plane/db/models/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# Module imports
from .base import BaseModel
from plane.utils.constants import RESTRICTED_WORKSPACE_SLUGS

ROLE_CHOICES = (
(20, "Owner"),
Expand Down Expand Up @@ -112,19 +113,7 @@ def get_issue_props():


def slug_validator(value):
if value in [
"404",
"accounts",
"api",
"create-workspace",
"god-mode",
"installations",
"invitations",
"onboarding",
"profile",
"spaces",
"workspace-invitations",
]:
if value in RESTRICTED_WORKSPACE_SLUGS:
raise ValidationError("Slug is not valid")


Expand Down

0 comments on commit 095639b

Please sign in to comment.