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(scoping): set default_manager_name = "all_teams" on ProductTeamModel (#58052)

Julian Bez8e2bba6b3654
diff --git a/posthog/models/scoping/README.md b/posthog/models/scoping/README.md
index aa9218ed3312..bf9a0b6b7fa0 100644
--- a/posthog/models/scoping/README.md
+++ b/posthog/models/scoping/README.md
@@ -105,6 +105,9 @@ The two are deliberately not the same name. A future contributor autocompleting
## Known limitations
-- **`_base_manager`**: Django uses `_base_manager` (not `objects`) for related-object access like `repo.runs.all()`. This bypasses the scoped manager. Related-object traversal is still safe because the FK constrains the result set — but the `team_id` filter is not applied.
+- **Django framework managers (`_default_manager` and `_base_manager`)**: admin queryset, `ForeignKeyRawIdWidget` label rendering, related-object access (e.g. `repo.runs.all()`), generic relations, `prefetch_related`, and DRF default querysets all go through these.
+ Per Django's contract they expect an unfiltered manager — `TeamScopedManager` would raise on missing context.
+ `ProductTeamModel.Meta.default_manager_name = "all_teams"` redirects `_default_manager` (and `_base_manager`, which inherits from `_default_manager` when `base_manager_name` is unset) to the unscoped `all_teams` manager so the framework just works.
+ `Model.objects` (the explicit attribute) stays bound to `TeamScopedManager` — user code that types it explicitly stays fail-closed.
+ The `team_id` filter is not applied on these framework paths; related-object traversal stays correct because the FK already constrains the result set, but anything reading through `_default_manager` is genuinely cross-team.
- **Raw SQL**: `cursor.execute()` and `QuerySet.raw()` bypass managers entirely.
-- **Django admin**: Uses `_default_manager`. Since `objects` is declared first on `ProductTeamModel`, admin goes through the scoped manager and would raise without context — use `Model.all_teams` in admin classes.
diff --git a/posthog/models/scoping/manager.py b/posthog/models/scoping/manager.py
index 9a60f6f3a70b..8ed77f847c2f 100644
--- a/posthog/models/scoping/manager.py
+++ b/posthog/models/scoping/manager.py
@@ -6,9 +6,14 @@ explicitly opted out via .unscoped(). This forces every code path to
declare its team context — there is no silent "return everything" mode.
This is a defense-in-depth convenience layer, not a complete security
-boundary. Django's _base_manager bypasses custom managers for related-
-object access, and raw SQL bypasses the ORM entirely. Use this alongside
-explicit team checks at the API layer.
+boundary. Django's `_default_manager` / `_base_manager` (used for admin
+querysets, related-object access like `repo.runs.all()`, generic
+relations, `prefetch_related`, DRF default querysets) intentionally
+route through the unscoped sibling manager via
+`ProductTeamModel.Meta.default_manager_name = "all_teams"` — the
+framework expects an unfiltered manager and a fail-closed default would
+raise on framework code that has no team context. Raw SQL bypasses the
+ORM entirely. Use this alongside explicit team checks at the API layer.
"""
from typing import TypeVar, cast
diff --git a/posthog/models/scoping/product_mixin.py b/posthog/models/scoping/product_mixin.py
index 3545e47ff596..9327a9e3b939 100644
--- a/posthog/models/scoping/product_mixin.py
+++ b/posthog/models/scoping/product_mixin.py
@@ -45,15 +45,23 @@ class ProductTeamModel(models.Model):
and reads agree on where data lives — same convention as
RootTeamMixin for main-DB models.
- Two managers:
+ Two managers, with `default_manager_name = "all_teams"` set in `Meta`
+ so Django's framework reads through the unscoped sibling:
- `objects` (TeamScopedManager): fail-closed, auto-scopes by context.
- Raises TeamScopeError when no context is set. This is the manager
- Django admin / Model.objects / `_default_manager` resolves to.
- - `all_teams` (plain Manager): bypass for admin / migrations / contexts
- that genuinely need cross-team access. Named distinctly from the
- queryset method `Model.objects.unscoped()` to avoid the autocomplete
- footgun where `Model.unscoped.filter(...)` looks like "I'm being
- explicit about scoping" but actually returns every team's rows.
+ Raises TeamScopeError when no context is set. This is what user
+ code reaches for explicitly — `Model.objects.filter(...)` stays
+ fail-closed.
+ - `all_teams` (plain Manager): bypass for admin, migrations, and any
+ Django framework internal that uses `_default_manager` /
+ `_base_manager` (admin widgets, related-object access, generic
+ relations, `prefetch_related`, DRF default querysets). Per Django's
+ contract those framework managers shouldn't filter, so they're
+ pointed at `all_teams` rather than the scoped manager.
+
+ Named distinctly from the queryset method `Model.objects.unscoped()`
+ to avoid the autocomplete footgun where `Model.unscoped.filter(...)`
+ looks like "I'm being explicit about scoping" but actually returns
+ every team's rows.
"""
team_id = models.BigIntegerField(db_index=True)
@@ -64,6 +72,17 @@ class ProductTeamModel(models.Model):
class Meta:
abstract = True
+ # Django framework internals (admin widgets, related-object access,
+ # generic relations, prefetch_related, DRF default querysets, ...)
+ # use `_default_manager`. Per Django's contract those internals
+ # expect an unfiltered manager. The `objects = TeamScopedManager()`
+ # above is what user code reaches for explicitly (`Repo.objects.all()`)
+ # and stays fail-closed; the framework reads through `all_teams` so
+ # an admin change-page or a `repo.runs.all()` traversal doesn't have
+ # to know about scoping. `Model._base_manager` defaults to
+ # `_default_manager` so it picks up the same setting.
+ default_manager_name = "all_teams"
+
def save(self, *args: Any, **kwargs: Any) -> None:
# Rewrite child team_ids to canonical on insert / when team_id is
# being written. Updates that don't touch team_id skip the lookup —
diff --git a/products/visual_review/backend/migrations/0011_alter_artifact_managers_and_more.py b/products/visual_review/backend/migrations/0011_alter_artifact_managers_and_more.py
new file mode 100644
index 000000000000..463b7971fc1a
--- /dev/null
+++ b/products/visual_review/backend/migrations/0011_alter_artifact_managers_and_more.py
@@ -0,0 +1,53 @@
+# State-only — records the new `all_teams` manager on each
+# `ProductTeamModel`-backed model. No SQL emitted; just keeps Django's
+# migration state in sync with the model definitions after
+# `posthog/models/scoping/product_mixin.py` started exposing a sibling
+# unscoped manager alongside `objects`.
+
+import django.db.models.manager
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("visual_review", "0010_backfill_change_kind_and_ssim"),
+ ]
+
+ operations = [
+ migrations.AlterModelManagers(
+ name="artifact",
+ managers=[
+ ("all_teams", django.db.models.manager.Manager()),
+ ],
+ ),
+ migrations.AlterModelManagers(
+ name="quarantinedidentifier",
+ managers=[
+ ("all_teams", django.db.models.manager.Manager()),
+ ],
+ ),
+ migrations.AlterModelManagers(
+ name="repo",
+ managers=[
+ ("all_teams", django.db.models.manager.Manager()),
+ ],
+ ),
+ migrations.AlterModelManagers(
+ name="run",
+ managers=[
+ ("all_teams", django.db.models.manager.Manager()),
+ ],
+ ),
+ migrations.AlterModelManagers(
+ name="runsnapshot",
+ managers=[
+ ("all_teams", django.db.models.manager.Manager()),
+ ],
+ ),
+ migrations.AlterModelManagers(
+ name="toleratedhash",
+ managers=[
+ ("all_teams", django.db.models.manager.Manager()),
+ ],
+ ),
+ ]
diff --git a/products/visual_review/backend/migrations/max_migration.txt b/products/visual_review/backend/migrations/max_migration.txt
index 5e23994e0365..57987126c9d7 100644
--- a/products/visual_review/backend/migrations/max_migration.txt
+++ b/products/visual_review/backend/migrations/max_migration.txt
@@ -1 +1 @@
-0010_backfill_change_kind_and_ssim
+0011_alter_artifact_managers_and_more