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(signals): check parent project access on the scout source config (#106822)

posthog[bot]3a7ec5e6459c
diff --git a/products/signals/backend/test/test_signal_source_config_api.py b/products/signals/backend/test/test_signal_source_config_api.py
index 3cb4b6c9786b..bed4e3a74e97 100644
--- a/products/signals/backend/test/test_signal_source_config_api.py
+++ b/products/signals/backend/test/test_signal_source_config_api.py
@@ -5,7 +5,9 @@ from django.test import SimpleTestCase
from parameterized import parameterized
from rest_framework import status
+from posthog.models.personal_api_key import PersonalAPIKey
from posthog.models.team.team import Team
+from posthog.models.utils import generate_random_token_personal, hash_key_value
from products.signals.backend.models import SignalSourceConfig
from products.signals.backend.serializers import SignalSourceConfigSerializer
@@ -536,6 +538,54 @@ class TestScoutSourceCanonicalization(APIBaseTest):
)
assert self.client.get(self._child_url()).json()["results"] == []
+ def _child_scoped_api_key_auth(self) -> str:
+ raw = generate_random_token_personal()
+ PersonalAPIKey.objects.create(
+ label="child-scoped",
+ user=self.user,
+ secure_value=hash_key_value(raw),
+ scopes=["task:read", "task:write"],
+ scoped_teams=[self.child_team.id],
+ )
+ self.client.logout()
+ return f"Bearer {raw}"
+
+ def test_child_scoped_api_key_lists_only_child_rows(self):
+ SignalSourceConfig.objects.create(
+ team=self.team, source_product="signals_scout", source_type="cross_source_issue"
+ )
+ child_row = SignalSourceConfig.objects.create(
+ team=self.child_team, source_product="session_replay", source_type="session_analysis_cluster"
+ )
+ response = self.client.get(self._child_url(), HTTP_AUTHORIZATION=self._child_scoped_api_key_auth())
+ assert response.status_code == status.HTTP_200_OK
+ assert [r["id"] for r in response.json()["results"]] == [str(child_row.id)]
+
+ @parameterized.expand([("get",), ("patch",), ("delete",)])
+ def test_child_scoped_api_key_cannot_touch_canonical_scout_row(self, method: str):
+ scout_row = SignalSourceConfig.objects.create(
+ team=self.team, source_product="signals_scout", source_type="cross_source_issue", enabled=True
+ )
+ response = getattr(self.client, method)(
+ self._child_url(str(scout_row.id)),
+ {"enabled": False},
+ format="json",
+ HTTP_AUTHORIZATION=self._child_scoped_api_key_auth(),
+ )
+ assert response.status_code == status.HTTP_404_NOT_FOUND
+ scout_row.refresh_from_db()
+ assert scout_row.enabled is True
+
+ def test_child_scoped_api_key_cannot_create_canonical_scout_row(self):
+ response = self.client.post(
+ self._child_url(),
+ {"source_product": "signals_scout", "source_type": "cross_source_issue", "enabled": False},
+ format="json",
+ HTTP_AUTHORIZATION=self._child_scoped_api_key_auth(),
+ )
+ assert response.status_code == status.HTTP_403_FORBIDDEN
+ assert not SignalSourceConfig.objects.filter(source_product="signals_scout").exists()
+
class TestIsSourceEnabledGating(APIBaseTest):
"""Source-level gating quirks: session_problem routes through the session_analysis_cluster
diff --git a/products/signals/backend/views.py b/products/signals/backend/views.py
index 0b4593b79586..78314dce819f 100644
--- a/products/signals/backend/views.py
+++ b/products/signals/backend/views.py
@@ -155,6 +155,7 @@ from products.signals.backend.report_metric_access import ReportMetricAccessPoli
from products.signals.backend.report_metric_refresh import CURRENT_REPORT_STATUSES, refresh_report_metric_snapshots
from products.signals.backend.reviewer_correction_notes import ReviewerCorrection, forward_reviewer_correction_note
from products.signals.backend.reviewer_pr_assignment import schedule_reviewer_pr_assignment
+from products.signals.backend.scout_harness.views import ScoutCanonicalTeamAccessPermission
from products.signals.backend.serializers import (
CommitDiffResponseSerializer,
PullRequestChecksPermissionErrorSerializer,
@@ -416,9 +417,16 @@ class SignalSourceConfigViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
# it to the canonical team keeps the inbox toggle and the emit gate on the same row from
# any environment. All other sources stay environment-scoped.
if self._is_scout_source(source_product, source_type):
+ if not self._can_reach_canonical_team():
+ raise exceptions.PermissionDenied(ScoutCanonicalTeamAccessPermission.message)
return self.team.parent_team_id or self.team_id
return self.team_id
+ def _can_reach_canonical_team(self) -> bool:
+ # The default team check authorizes only the URL environment. A caller must also reach the
+ # parent project before it reads or writes that project's scout row.
+ return ScoutCanonicalTeamAccessPermission().has_permission(self.request, self)
+
def _filter_queryset_by_parents_lookups(self, queryset):
# Mirror of `_config_team_id` on the read side: surface the scout row from the canonical
# (parent) team while every other source stays scoped to the URL environment, so the
@@ -428,6 +436,8 @@ class SignalSourceConfigViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
source_product=SignalSourceConfig.SourceProduct.SIGNALS_SCOUT,
source_type=SignalSourceConfig.SourceType.CROSS_SOURCE_ISSUE,
)
+ if not self._can_reach_canonical_team():
+ return queryset.filter(Q(team_id=self.team_id) & ~scout_source)
return queryset.filter(
(Q(team_id=self.team_id) & ~scout_source) | (Q(team_id=canonical_team_id) & scout_source)
)