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(eng-analytics): fail closed on none-level source access (#70825)

Raúl Negrón-Oteroe539ca1d9121
diff --git a/products/engineering_analytics/backend/logic/sources.py b/products/engineering_analytics/backend/logic/sources.py
index ea9200b1576e..fc394ddb15fd 100644
--- a/products/engineering_analytics/backend/logic/sources.py
+++ b/products/engineering_analytics/backend/logic/sources.py
@@ -21,9 +21,10 @@ from dataclasses import dataclass
from typing import TYPE_CHECKING
from uuid import UUID
-from django.db.models import QuerySet
+from django.db.models import Q, QuerySet
from posthog.models.team import Team
+from posthog.rbac.user_access_control import NO_ACCESS_LEVEL
from products.engineering_analytics.backend.facade.contracts import GitHubSource, GitHubSourceNotConnectedError
from products.warehouse_sources.backend.facade.models import ExternalDataSchema, ExternalDataSource
@@ -187,6 +188,16 @@ def _github_sources(team: Team, user_access_control: "UserAccessControl | None"
)
if user_access_control is not None:
sources = user_access_control.filter_queryset_by_access_level(sources)
+ if not user_access_control.has_resource_access("external_data_source"):
+ # "none" resource-level access: the platform filter drops nothing when the user holds no
+ # object grants, so fail closed here to self-created or explicitly granted sources.
+ granted_ids = [
+ source.id
+ for source in sources
+ if (level := user_access_control.access_level_for_object(source, explicit=True))
+ and level != NO_ACCESS_LEVEL
+ ]
+ sources = sources.filter(Q(created_by=user_access_control.user) | Q(id__in=granted_ids))
return sources
diff --git a/products/engineering_analytics/backend/tests/test_views.py b/products/engineering_analytics/backend/tests/test_views.py
index a99375a6d573..07106aa930b4 100644
--- a/products/engineering_analytics/backend/tests/test_views.py
+++ b/products/engineering_analytics/backend/tests/test_views.py
@@ -8,12 +8,15 @@ import pandas as pd
from posthog.hogql.query import execute_hogql_query
+from posthog.constants import AvailableFeature
from posthog.models.team import Team
+from posthog.rbac.user_access_control import UserAccessControl
from products.engineering_analytics.backend.logic.sources import (
PULL_REQUESTS_SCHEMA,
WORKFLOW_RUNS_SCHEMA,
GitHubTables,
+ list_github_sources,
)
from products.engineering_analytics.backend.logic.views import pull_requests, workflow_runs
from products.engineering_analytics.backend.logic.views.source_schema import (
@@ -24,6 +27,8 @@ from products.warehouse_sources.backend.facade.models import DataWarehouseTable,
from products.warehouse_sources.backend.facade.types import ExternalDataSourceType
from products.warehouse_sources.backend.test.utils import create_data_warehouse_table_from_csv
+from ee.models.rbac.access_control import AccessControl
+
TEST_BUCKET = "test_storage_bucket-posthog.products.engineering_analytics.views"
# Non-default prefix on purpose: every fixture below lands tables named
@@ -162,6 +167,42 @@ def _run_row(
}
+class TestListGithubSourcesAccessControl(BaseTest):
+ def setUp(self) -> None:
+ super().setUp()
+ self.organization.available_product_features = [
+ {"key": AvailableFeature.ACCESS_CONTROL, "name": AvailableFeature.ACCESS_CONTROL}
+ ]
+ self.organization.save()
+
+ def test_none_resource_access_fails_closed_to_self_created_sources(self) -> None:
+ # filter_queryset_by_access_level returns the queryset UNFILTERED for a user with "none"
+ # resource access and no object grants — without the guard, such a user enumerates every
+ # GitHub source on the team.
+ mine = create_github_source(self.team, prefix="mine_", source_id="gh-mine")
+ mine.created_by = self.user
+ mine.save()
+ theirs = create_github_source(self.team, prefix="theirs_", source_id="gh-theirs")
+ access_control = UserAccessControl(user=self.user, team=self.team)
+
+ assert len(list_github_sources(team=self.team, user_access_control=access_control)) == 2
+
+ AccessControl.objects.create(team=self.team, resource="external_data_source", access_level="none")
+ visible = list_github_sources(
+ team=self.team, user_access_control=UserAccessControl(user=self.user, team=self.team)
+ )
+ assert [source.id for source in visible] == [str(mine.id)]
+
+ # An explicit object grant survives the fail-closed guard.
+ AccessControl.objects.create(
+ team=self.team, resource="external_data_source", resource_id=str(theirs.id), access_level="editor"
+ )
+ visible = list_github_sources(
+ team=self.team, user_access_control=UserAccessControl(user=self.user, team=self.team)
+ )
+ assert {source.id for source in visible} == {str(mine.id), str(theirs.id)}
+
+
class TestEngineeringAnalyticsViews(ClickhouseTestMixin, BaseTest):
"""The curated query builders, exercised as inline subqueries over real
warehouse tables. Skips when object storage is unreachable so the suite still