Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(canvas): separate task read and write access (#81941)

Shy Alter51710a4031e0
diff --git a/products/canvas/backend/presentation/views.py b/products/canvas/backend/presentation/views.py
index 88682190ae14..9aa1bf5d147e 100644
--- a/products/canvas/backend/presentation/views.py
+++ b/products/canvas/backend/presentation/views.py
@@ -139,21 +139,32 @@ class CanvasViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
def safely_get_queryset(self, queryset: QuerySet) -> QuerySet:
queryset = queryset.filter(team_id=self.team_id, deleted=False)
- # Channels are per-user for the personal kind: the facade's visibility
- # rule makes a canvas filed into someone else's personal channel
- # invisible (and unwritable) to everyone but its owner, for list and
- # every detail action alike. The create() check alone is not enough —
- # DRF resolves all detail actions off this queryset.
user = self._request_user()
- queryset = queryset.filter(tasks_facade.visible_channels_q(user.id if user else None, relation="channel"))
- if self._is_sandbox_authenticated(self.request):
+ is_sandbox_authenticated = self._is_sandbox_authenticated(self.request)
+ if is_sandbox_authenticated:
sandbox_task_id = self._sandbox_task_id(self.request)
if sandbox_task_id is None:
return queryset.none()
- queryset = queryset.filter(
- Q(generation_task_id=sandbox_task_id) | Q(source_versions__task_id=sandbox_task_id)
- ).distinct()
- elif self.action in self._CREATOR_ONLY_ACTIONS:
+ public_canvas_q = tasks_facade.visible_channels_q(None, relation="channel")
+ if user is None:
+ queryset = (
+ queryset.filter(public_canvas_q)
+ if self.action in self.scope_object_read_actions
+ else queryset.none()
+ )
+ else:
+ actor_canvas_q = Q(created_by_id=user.id) & tasks_facade.visible_channels_q(user.id, relation="channel")
+ queryset = queryset.filter(
+ public_canvas_q | actor_canvas_q
+ if self.action in self.scope_object_read_actions
+ else actor_canvas_q
+ )
+ else:
+ # Channels are per-user for the personal kind: the facade's visibility
+ # rule makes a canvas filed into someone else's personal channel
+ # invisible (and unwritable) to everyone but its owner.
+ queryset = queryset.filter(tasks_facade.visible_channels_q(user.id if user else None, relation="channel"))
+ if not is_sandbox_authenticated and self.action in self._CREATOR_ONLY_ACTIONS:
if user is None:
return queryset.none()
queryset = queryset.filter(created_by_id=user.id)
diff --git a/products/canvas/backend/tests/test_canvas_api.py b/products/canvas/backend/tests/test_canvas_api.py
index 43b9acd2d634..f6dddae7051d 100644
--- a/products/canvas/backend/tests/test_canvas_api.py
+++ b/products/canvas/backend/tests/test_canvas_api.py
@@ -1,6 +1,6 @@
from datetime import timedelta
from typing import Any, cast
-from uuid import uuid4
+from uuid import UUID, uuid4
from posthog.test.base import APIBaseTest
from unittest.mock import patch
@@ -13,6 +13,7 @@ from rest_framework.test import APIClient
from posthog.models.activity_logging.activity_log import ActivityLog
from posthog.models.oauth import OAuthAccessToken, OAuthApplication
from posthog.models.scoping import team_scope
+from posthog.models.user import User
from posthog.temporal.oauth import ARRAY_APP_CLIENT_ID_DEV
from products.canvas.backend import activity_visibility, build_service
@@ -79,7 +80,8 @@ class CanvasAPIBaseTest(APIBaseTest):
assert entry.detail is not None
return entry.detail["changes"]
- def _sandbox_client(self, task_id) -> APIClient:
+ def _sandbox_client(self, task_id: UUID, *, user: User | None = None) -> APIClient:
+ actor = user or self.user
application = OAuthApplication.objects.create(
name="Canvas sandbox",
client_id=ARRAY_APP_CLIENT_ID_DEV,
@@ -88,10 +90,10 @@ class CanvasAPIBaseTest(APIBaseTest):
algorithm="RS256",
redirect_uris="https://example.com/callback",
organization=self.organization,
- user=self.user,
+ user=actor,
)
access_token = OAuthAccessToken.objects.create(
- user=self.user,
+ user=actor,
application=application,
token=f"pha_canvas_{uuid4().hex}",
expires=timezone.now() + timedelta(hours=1),
@@ -223,7 +225,7 @@ class TestCanvasCrud(CanvasAPIBaseTest):
assert Canvas.objects.unscoped().get(id=allowed.json()["id"]).generation_task_id == bound_task.id
assert denied.status_code == status.HTTP_403_FORBIDDEN
- def test_task_bound_sandbox_can_read_only_its_canvases(self):
+ def test_task_bound_sandbox_can_read_canvases_created_by_the_authenticated_user(self):
bound_task = Task.objects.create(
team=self.team,
channel=self.channel,
@@ -232,11 +234,11 @@ class TestCanvasCrud(CanvasAPIBaseTest):
description="d",
origin_product=Task.OriginProduct.USER_CREATED,
)
- other_task = Task.objects.create(
+ earlier_task = Task.objects.create(
team=self.team,
channel=self.channel,
created_by=self.user,
- title="Other",
+ title="Earlier",
description="d",
origin_product=Task.OriginProduct.USER_CREATED,
)
@@ -246,11 +248,12 @@ class TestCanvasCrud(CanvasAPIBaseTest):
name="Own",
generation_task_id=bound_task.id,
)
- other_canvas = Canvas.objects.unscoped().create(
+ earlier_canvas = Canvas.objects.unscoped().create(
team=self.team,
channel=self.channel,
- name="Other",
- generation_task_id=other_task.id,
+ name="Earlier",
+ generation_task_id=earlier_task.id,
+ created_by=self.user,
)
client = self._sandbox_client(bound_task.id)
@@ -258,13 +261,225 @@ class TestCanvasCrud(CanvasAPIBaseTest):
f"/api/projects/{self.team.id}/canvases/",
HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
)
- other_detail = client.get(
+ earlier_detail = client.get(
+ f"/api/projects/{self.team.id}/canvases/{earlier_canvas.id}/",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+ update = client.patch(
+ f"/api/projects/{self.team.id}/canvases/{earlier_canvas.id}/",
+ {"name": "Updated by later task"},
+ format="json",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+ linked_update = client.patch(
+ f"/api/projects/{self.team.id}/canvases/{own_canvas.id}/",
+ {"name": "Linked but unowned"},
+ format="json",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+
+ assert [row["id"] for row in listing.json()["results"]] == [str(earlier_canvas.id), str(own_canvas.id)]
+ assert earlier_detail.status_code == status.HTTP_200_OK
+ assert update.status_code == status.HTTP_200_OK
+ assert update.json()["name"] == "Updated by later task"
+ assert linked_update.status_code == status.HTTP_404_NOT_FOUND
+
+ def test_rebound_sandbox_does_not_inherit_task_creator_canvas_access(self) -> None:
+ actor = self._create_user("[email protected]")
+ with team_scope(self.team.id):
+ personal_channel = Channel.objects.create(
+ team=self.team,
+ name=Channel.PERSONAL_CHANNEL_NAME,
+ channel_type=Channel.ChannelType.PERSONAL,
+ created_by=self.user,
+ )
+ bound_task = Task.objects.create(
+ team=self.team,
+ channel=self.channel,
+ created_by=self.user,
+ title="Bound",
+ description="d",
+ origin_product=Task.OriginProduct.USER_CREATED,
+ )
+ creator_public_canvas = Canvas.objects.unscoped().create(
+ team=self.team,
+ channel=self.channel,
+ name="Creator public canvas",
+ created_by=self.user,
+ )
+ creator_personal_canvas = Canvas.objects.unscoped().create(
+ team=self.team,
+ channel=personal_channel,
+ name="Creator personal canvas",
+ created_by=self.user,
+ )
+ client = self._sandbox_client(bound_task.id, user=actor)
+
+ public_read = client.get(
+ f"/api/projects/{self.team.id}/canvases/{creator_public_canvas.id}/",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+ public_write = client.patch(
+ f"/api/projects/{self.team.id}/canvases/{creator_public_canvas.id}/",
+ {"name": "Not allowed"},
+ format="json",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+ personal_read = client.get(
+ f"/api/projects/{self.team.id}/canvases/{creator_personal_canvas.id}/",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+
+ assert public_read.status_code == status.HTTP_200_OK
+ assert public_write.status_code == status.HTTP_404_NOT_FOUND
+ assert personal_read.status_code == status.HTTP_404_NOT_FOUND
+
+ def test_rebound_sandbox_can_write_canvas_created_by_actor(self) -> None:
+ actor = self._create_user("[email protected]")
+ bound_task = Task.objects.create(
+ team=self.team,
+ channel=self.channel,
+ created_by=self.user,
+ title="Bound",
+ description="d",
+ origin_product=Task.OriginProduct.USER_CREATED,
+ )
+ client = self._sandbox_client(bound_task.id, user=actor)
+
+ create_response = client.post(
+ f"/api/projects/{self.team.id}/canvases/",
+ {"name": "Actor canvas", "channel_id": str(self.channel.id)},
+ format="json",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+ assert create_response.status_code == status.HTTP_201_CREATED
+ canvas_id = create_response.json()["id"]
+
+ update_response = client.patch(
+ f"/api/projects/{self.team.id}/canvases/{canvas_id}/",
+ {"name": "Updated by actor"},
+ format="json",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+ publish_response = client.post(
+ f"/api/projects/{self.team.id}/canvases/{canvas_id}/publish/",
+ {"project": self._project()},
+ format="json",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+
+ assert update_response.status_code == status.HTTP_200_OK
+ assert update_response.json()["name"] == "Updated by actor"
+ assert publish_response.status_code == status.HTTP_200_OK
+
+ def test_task_bound_sandbox_can_read_but_not_write_another_creators_public_canvas(self):
+ other_user = self._create_user("[email protected]")
+ bound_task = Task.objects.create(
+ team=self.team,
+ channel=self.channel,
+ created_by=self.user,
+ title="Bound",
+ description="d",
+ origin_product=Task.OriginProduct.USER_CREATED,
+ )
+ other_canvas = Canvas.objects.unscoped().create(
+ team=self.team,
+ channel=self.channel,
+ name="Other creator",
+ created_by=other_user,
+ )
+ client = self._sandbox_client(bound_task.id)
+
+ read_response = client.get(
+ f"/api/projects/{self.team.id}/canvases/{other_canvas.id}/",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+ write_response = client.patch(
f"/api/projects/{self.team.id}/canvases/{other_canvas.id}/",
+ {"name": "Not allowed"},
+ format="json",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+
+ assert read_response.status_code == status.HTTP_200_OK
+ assert write_response.status_code == status.HTTP_404_NOT_FOUND
+
+ def test_personal_space_sandbox_can_read_authenticated_users_canvas(self):
+ with team_scope(self.team.id):
+ personal_channel = Channel.objects.create(
+ team=self.team,
+ name=Channel.PERSONAL_CHANNEL_NAME,
+ channel_type=Channel.ChannelType.PERSONAL,
+ created_by=self.user,
+ )
+ bound_task = Task.objects.create(
+ team=self.team,
+ channel=personal_channel,
+ created_by=self.user,
+ title="Continue canvas",
+ description="d",
+ origin_product=Task.OriginProduct.USER_CREATED,
+ )
+ canvas = Canvas.objects.unscoped().create(
+ team=self.team,
+ channel=personal_channel,
+ name="Project tracker",
+ created_by=self.user,
+ )
+ client = self._sandbox_client(bound_task.id)
+
+ read_response = client.get(
+ f"/api/projects/{self.team.id}/canvases/{canvas.id}/",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+ write_response = client.patch(
+ f"/api/projects/{self.team.id}/canvases/{canvas.id}/",
+ {"name": "Updated project tracker"},
+ format="json",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+
+ assert read_response.status_code == status.HTTP_200_OK
+ assert write_response.status_code == status.HTTP_200_OK
+
+ def test_task_bound_sandbox_cannot_access_another_creators_personal_canvas(self):
+ other_user = self._create_user("[email protected]")
+ with team_scope(self.team.id):
+ other_personal_channel = Channel.objects.create(
+ team=self.team,
+ name=Channel.PERSONAL_CHANNEL_NAME,
+ channel_type=Channel.ChannelType.PERSONAL,
+ created_by=other_user,
+ )
+ bound_task = Task.objects.create(
+ team=self.team,
+ channel=self.channel,
+ created_by=self.user,
+ title="Bound",
+ description="d",
+ origin_product=Task.OriginProduct.USER_CREATED,
+ )
+ other_canvas = Canvas.objects.unscoped().create(
+ team=self.team,
+ channel=other_personal_channel,
+ name="Private project tracker",
+ created_by=other_user,
+ )
+ client = self._sandbox_client(bound_task.id)
+
+ read_response = client.get(
+ f"/api/projects/{self.team.id}/canvases/{other_canvas.id}/",
+ HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
+ )
+ write_response = client.patch(
+ f"/api/projects/{self.team.id}/canvases/{other_canvas.id}/",
+ {"name": "Not allowed"},
+ format="json",
HTTP_X_POSTHOG_TASK_ID=str(bound_task.id),
)
- assert [row["id"] for row in listing.json()["results"]] == [str(own_canvas.id)]
- assert other_detail.status_code == status.HTTP_404_NOT_FOUND
+ assert read_response.status_code == status.HTTP_404_NOT_FOUND
+ assert write_response.status_code == status.HTTP_404_NOT_FOUND
def test_create_rejects_unknown_channel(self):
response = self.client.post(
diff --git a/products/tasks/docs/CHANNELS.md b/products/tasks/docs/CHANNELS.md
index 3b678a78782c..f95a4d96abf0 100644
--- a/products/tasks/docs/CHANNELS.md
+++ b/products/tasks/docs/CHANNELS.md
@@ -111,6 +111,17 @@ membership without adding permissions to Task.
- Only a Canvas creator can change Canvas metadata or source.
- Any project member can queue a build for the current source version through `publish-current-version`.
+Task sandboxes use the authenticated OAuth token user for the creator boundary across public and personal spaces:
+
+| Canvas space | Token user | Read | Write |
+| ------------ | ----------------------------- | ---- | ----- |
+| Public | Canvas creator | Yes | Yes |
+| Public | Another user or no token user | Yes | No |
+| Personal | Canvas creator | Yes | Yes |
+| Personal | Another user or no token user | No | No |
+
+An exact task-to-Canvas link does not grant additional write access. Canvas creation remains limited to the bound task's space.
+
### `/api/projects/{id}/tasks/{task_id}/thread_messages/`
- `GET /` — thread messages, ascending `created_at` (paginated).