: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(tasks): make signals-generated tasks team-visible (#58671)
Joshua Snyder8b265a5ad547
diff --git a/products/tasks/backend/api.py b/products/tasks/backend/api.pyindex ada38ee38211..3b9940b3ed0f 100644--- a/products/tasks/backend/api.py+++ b/products/tasks/backend/api.py@@ -200,17 +200,26 @@ TASK_RUN_ARTIFACT_UPLOAD_FORM_OVERHEAD_BYTES = 64 * 1024def task_visibility_q(user_id: int | None) -> Q:"""Filter for tasks visible to the given user.- A task is visible if its creator matches `user_id`, or if it has no- creator at all (legacy unowned tasks remain visible to any team member —- they cannot be executed in any case because oauth.py requires- `task.created_by` to mint OAuth tokens).+ A task is visible if:+ - its creator matches `user_id`, or+ - it has no creator at all (legacy unowned tasks remain visible to any+ team member — they cannot be executed in any case because oauth.py+ requires `task.created_by` to mint OAuth tokens), or+ - it was generated by the signals pipeline (`origin_product=SIGNAL_REPORT`).+ Signals tasks are team-scoped artifacts that the pipeline must attach to+ a system-picked `created_by` so the agent can mint an OAuth token, but+ they are not personal — any team member should be able to view them."""- return Q(created_by_id=user_id) | Q(created_by__isnull=True)+ return Q(created_by_id=user_id) | Q(created_by__isnull=True) | Q(origin_product=Task.OriginProduct.SIGNAL_REPORT)def task_run_visibility_q(user_id: int | None) -> Q:"""`task_visibility_q` traversed via the `task` FK on TaskRun / TaskAutomation."""- return Q(task__created_by_id=user_id) | Q(task__created_by__isnull=True)+ return (+ Q(task__created_by_id=user_id)+ | Q(task__created_by__isnull=True)+ | Q(task__origin_product=Task.OriginProduct.SIGNAL_REPORT)+ )class TasksAccessPermission(BasePermission):diff --git a/products/tasks/backend/serializers.py b/products/tasks/backend/serializers.pyindex b3ba73e81219..ae32f8184b46 100644--- a/products/tasks/backend/serializers.py+++ b/products/tasks/backend/serializers.py@@ -257,6 +257,13 @@ class TaskSerializer(serializers.ModelSerializer):return taskdef update(self, instance, validated_data):+ # These fields are immutable after creation. origin_product controls+ # team-wide visibility (SIGNAL_REPORT tasks are visible to all members),+ # so allowing updates would let a user escalate a private task's visibility.+ # signal_report and its relationship are set-once associations.+ validated_data.pop("signal_report", None)+ validated_data.pop("signal_report_task_relationship", None)+ validated_data.pop("origin_product", None)if "title" in validated_data and "title_manually_set" not in validated_data:validated_data["title_manually_set"] = Truereturn super().update(instance, validated_data)diff --git a/products/tasks/backend/tests/test_api.py b/products/tasks/backend/tests/test_api.pyindex d25c316730f0..02263057ac9c 100644--- a/products/tasks/backend/tests/test_api.py+++ b/products/tasks/backend/tests/test_api.py@@ -230,6 +230,135 @@ class TestTaskCreatorScoping(BaseTaskAPITest):response = self.client.get(f"/api/projects/@current/tasks/{task.id}/")self.assertEqual(response.status_code, status.HTTP_200_OK)+ def test_retrieve_signal_report_task_owned_by_another_user_is_visible(self):+ # Signals-generated tasks are team-scoped artifacts; the pipeline picks+ # a system user as `created_by` purely to mint an OAuth token. Any team+ # member must be able to retrieve them by ID.+ other_user = self.create_organization_user("signal-owner")+ task = Task.objects.create(+ team=self.team,+ created_by=other_user,+ title="Signal Report Task",+ description="Generated by signals",+ origin_product=Task.OriginProduct.SIGNAL_REPORT,+ internal=True,+ )++ response = self.client.get(f"/api/projects/@current/tasks/{task.id}/")+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(response.json()["id"], str(task.id))++ def test_list_signal_report_tasks_hidden_by_default(self):+ # Signal-report tasks are always created with internal=True, so the+ # default list still hides them. The retrieve-by-ID path (and the+ # staff/DEBUG `?internal=true` path) is how they're surfaced.+ other_user = self.create_organization_user("signal-owner")+ signal_task = Task.objects.create(+ team=self.team,+ created_by=other_user,+ title="Signal Report Task",+ description="Generated by signals",+ origin_product=Task.OriginProduct.SIGNAL_REPORT,+ internal=True,+ )+ mine = self.create_task("Mine", created_by=self.user)++ response = self.client.get("/api/projects/@current/tasks/")+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ ids = {t["id"] for t in response.json()["results"]}+ self.assertIn(str(mine.id), ids)+ self.assertNotIn(str(signal_task.id), ids)++ def test_retrieve_other_user_non_signal_internal_task_returns_404(self):+ # Non-signal internal tasks created by another user remain private.+ other_user = self.create_organization_user("victim")+ task = Task.objects.create(+ team=self.team,+ created_by=other_user,+ title="Their internal",+ description="",+ origin_product=Task.OriginProduct.USER_CREATED,+ internal=True,+ )++ response = self.client.get(f"/api/projects/@current/tasks/{task.id}/")+ self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)++ def test_list_runs_signal_report_task_owned_by_another_user_succeeds(self):+ other_user = self.create_organization_user("signal-owner")+ task = Task.objects.create(+ team=self.team,+ created_by=other_user,+ title="Signal Report Task",+ description="Generated by signals",+ origin_product=Task.OriginProduct.SIGNAL_REPORT,+ internal=True,+ )+ run = TaskRun.objects.create(task=task, team=self.team, status=TaskRun.Status.COMPLETED)++ response = self.client.get(f"/api/projects/@current/tasks/{task.id}/runs/")+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ ids = {r["id"] for r in response.json()["results"]}+ self.assertEqual(ids, {str(run.id)})++ def test_retrieve_run_signal_report_task_owned_by_another_user_succeeds(self):+ other_user = self.create_organization_user("signal-owner")+ task = Task.objects.create(+ team=self.team,+ created_by=other_user,+ title="Signal Report Task",+ description="Generated by signals",+ origin_product=Task.OriginProduct.SIGNAL_REPORT,+ internal=True,+ )+ run = TaskRun.objects.create(task=task, team=self.team, status=TaskRun.Status.IN_PROGRESS)++ response = self.client.get(f"/api/projects/@current/tasks/{task.id}/runs/{run.id}/")+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(response.json()["id"], str(run.id))++ def test_retrieve_signal_report_automation_owned_by_another_user_succeeds(self):+ # TaskAutomationViewSet filters with task_run_visibility_q (task FK traversal).+ other_user = self.create_organization_user("signal-owner")+ task = Task.objects.create(+ team=self.team,+ created_by=other_user,+ title="Signal Report Task",+ description="Generated by signals",+ origin_product=Task.OriginProduct.SIGNAL_REPORT,+ internal=True,+ )+ automation = TaskAutomation.objects.create(+ task=task,+ cron_expression="0 9 * * *",+ timezone="Europe/London",+ enabled=True,+ )++ response = self.client.get(f"/api/projects/@current/task_automations/{automation.id}/")+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ self.assertEqual(response.json()["id"], str(automation.id))++ def test_retrieve_other_user_internal_non_signal_automation_returns_404(self):+ other_user = self.create_organization_user("victim")+ task = Task.objects.create(+ team=self.team,+ created_by=other_user,+ title="Their internal",+ description="",+ origin_product=Task.OriginProduct.USER_CREATED,+ internal=True,+ )+ automation = TaskAutomation.objects.create(+ task=task,+ cron_expression="0 9 * * *",+ timezone="Europe/London",+ enabled=True,+ )++ response = self.client.get(f"/api/projects/@current/task_automations/{automation.id}/")+ self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)+def test_repositories_excludes_other_user_repos(self):other_user = self.create_organization_user("victim")mine = self.create_task("Mine", created_by=self.user)@@ -610,6 +739,32 @@ class TestTaskAPI(BaseTaskAPITest):)self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)+ def test_patch_cannot_change_origin_product(self):+ task = self.create_task("Mine")+ self.assertEqual(task.origin_product, Task.OriginProduct.USER_CREATED)+ response = self.client.patch(+ f"/api/projects/@current/tasks/{task.id}/",+ {"origin_product": "signal_report"},+ format="json",+ )+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ task.refresh_from_db()+ self.assertEqual(task.origin_product, Task.OriginProduct.USER_CREATED)++ def test_patch_cannot_change_signal_report(self):+ from products.signals.backend.models import SignalReport++ report = SignalReport.objects.create(team=self.team)+ task = self.create_task("Mine")+ response = self.client.patch(+ f"/api/projects/@current/tasks/{task.id}/",+ {"signal_report": str(report.id)},+ format="json",+ )+ self.assertEqual(response.status_code, status.HTTP_200_OK)+ task.refresh_from_db()+ self.assertIsNone(task.signal_report_id)+def test_update_task(self):task = self.create_task("Original Task")