: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(canvas): stop treating desktop OAuth grants as sandbox tokens (#81573)
Adam Bowkerad2e6ffed561
diff --git a/products/canvas/backend/presentation/views.py b/products/canvas/backend/presentation/views.pyindex 9ccb1f6748ba..88682190ae14 100644--- a/products/canvas/backend/presentation/views.py+++ b/products/canvas/backend/presentation/views.py@@ -888,10 +888,20 @@ class CanvasViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):@staticmethoddef _is_sandbox_authenticated(request: Request) -> bool:- """True when the request bears an OAuth token minted under a sandbox app —- the credential a task sandbox (via the MCP server) calls this API with."""+ """True when the request bears an OAuth token minted for a task sandbox —+ the credential a task sandbox (via the MCP server) calls this API with.++ The sandbox apps also issue the desktop app's interactive grants, so the application+ alone does not prove sandbox origin. Server-minted tokens carry either a task binding+ or the internal provenance scope. An unbound server token must still fail closed rather+ than inherit its user's Canvas visibility.+ """authenticator = request.successful_authenticatorif not isinstance(authenticator, OAuthAccessTokenAuthentication):return False- application = authenticator.access_token.application+ access_token = authenticator.access_token+ scopes = set((access_token.scope or "").split())+ if access_token.sandbox_task_id is None and "internal_run:read" not in scopes:+ return False+ application = access_token.applicationreturn application is not None and application.client_id in SANDBOX_OAUTH_APP_CLIENT_IDSdiff --git a/products/canvas/backend/tests/test_canvas_oauth.py b/products/canvas/backend/tests/test_canvas_oauth.pyindex e568ecf257e6..cc98531b6fe8 100644--- a/products/canvas/backend/tests/test_canvas_oauth.py+++ b/products/canvas/backend/tests/test_canvas_oauth.py@@ -10,6 +10,7 @@ from posthog.models.oauth import OAuthAccessToken, OAuthApplicationfrom posthog.models.scoping import team_scopefrom posthog.temporal.oauth import ARRAY_APP_CLIENT_ID_DEV+from products.canvas.backend.models import Canvasfrom products.tasks.backend.models import Channel, Task@@ -43,29 +44,47 @@ class TestCanvasOAuthAccess(APIBaseTest):)return token.token- def _list_canvases(self, scope: str) -> int:+ def _list_canvases(self, scope: str, client_id: str | None = None):with team_scope(self.team.id):channel = Channel.objects.create(team=self.team, name="general")- token = self._bearer(scope)+ Canvas.objects.create(team=self.team, channel=channel, name="Signups", created_by=self.user)+ token = self._bearer(scope, client_id=client_id)self.client.logout()- res = self.client.get(+ return self.client.get(f"/api/projects/{self.team.id}/canvases/?channel={channel.id}&limit=200",HTTP_AUTHORIZATION=f"Bearer {token}",)- return res.status_codedef test_list_canvases_with_wildcard_oauth_token(self):- assert self._list_canvases("*") == 200+ assert self._list_canvases("*").status_code == 200def test_list_canvases_with_canvas_read_oauth_token(self):- assert self._list_canvases("canvas:read") == 200+ assert self._list_canvases("canvas:read").status_code == 200def test_list_canvases_denied_without_canvas_scope(self):# A token whose enumerated grant predates the canvas scope: other scopes# present, canvas absent. This is what a stale desktop session narrowed# to an app's scope ceiling looks like; the client's OAUTH_SCOPE_VERSION# bump exists to re-auth these.- assert self._list_canvases("task:read task:write dashboard:read") == 403+ assert self._list_canvases("task:read task:write dashboard:read").status_code == 403++ def test_list_canvases_with_an_interactive_desktop_grant(self):+ # The desktop app's own grants come from the same OAuth apps that mint+ # sandbox tokens, so treating a client-id match as sandbox origin empties+ # every canvas list in the app: no bound task means nothing to scope to.+ response = self._list_canvases("*", client_id=ARRAY_APP_CLIENT_ID_DEV)++ assert response.status_code == 200+ assert [row["name"] for row in response.json()["results"]] == ["Signups"]++ def test_list_canvases_with_an_unbound_server_minted_sandbox_token(self):+ response = self._list_canvases(+ "canvas:read internal_run:read",+ client_id=ARRAY_APP_CLIENT_ID_DEV,+ )++ assert response.status_code == 200+ assert response.json()["results"] == []def _create_canvas(self,