: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(flags): harden team api writes to the replay gate columns (#117391)
Phil Haackddc2ecb63c90
diff --git a/posthog/api/project.py b/posthog/api/project.pyindex 310c2f866fec..3aa1899b690e 100644--- a/posthog/api/project.py+++ b/posthog/api/project.py@@ -147,6 +147,7 @@ from products.feature_flags.backend.models.evaluation_context import (TeamDefaultEvaluationContext,normalize_context_name,)+from products.feature_flags.backend.session_recording_links import locked_replay_gate_writefrom products.notifications.backend.facade.api import (NotificationData,NotificationType,@@ -1118,9 +1119,13 @@ class ProjectBackwardCompatSerializer(def validate_access_control(self, value) -> None:return TeamSerializer.validate_access_control(cast(TeamSerializer, self), value)- @staticmethod- def validate_session_recording_linked_flag(value) -> dict | None:- return session_recording_settings.validate_session_recording_linked_flag(value)+ def validate_session_recording_linked_flag(self, value: Any) -> dict | None:+ return session_recording_settings.validate_session_recording_linked_flag(+ value,+ stored=self.instance.passthrough_team.session_recording_linked_flag if self.instance is not None else None,+ project_id=self.instance.id if self.instance is not None else None,+ user_access_control=self.user_access_control,+ )@staticmethoddef validate_session_recording_network_payload_capture_config(value) -> dict | None:@@ -1395,39 +1400,48 @@ class ProjectBackwardCompatSerializer(# each other — e.g. an `onboarding_tasks` PATCH racing the onboarding-completion PATCH# erased `has_completed_onboarding_for` and reverted `completed_snippet_onboarding`,# bouncing freshly onboarded users back into onboarding.- updated_team_fields = []- updated_project_fields = []- for attr, value in validated_data.items():- if attr in LOCKED_CONVERSATIONS_COLUMNS:- continue- if attr not in self.Meta.team_passthrough_fields:- # This attr is a Project field- setattr(instance, attr, value)- updated_project_fields.append(attr)- else:- # This attr is actually on the Project's passthrough Team- setattr(team, attr, value)- updated_team_fields.append(attr)-- if "name" in validated_data:- # Keep Team.name mirroring Project.name: surfaces like the organization's teams- # list and the app context still read the name off the Team row- team.name = validated_data["name"]- updated_team_fields.append("name")-+ with locked_replay_gate_write(+ team,+ validated_data,+ self.user_access_control,+ can_read_flags=session_recording_settings.can_read_feature_flags(self.context.get("request")),+ ):+ updated_team_fields = []+ updated_project_fields = []+ for attr, value in validated_data.items():+ if attr in LOCKED_CONVERSATIONS_COLUMNS:+ continue+ if attr not in self.Meta.team_passthrough_fields:+ # This attr is a Project field+ setattr(instance, attr, value)+ updated_project_fields.append(attr)+ else:+ # This attr is actually on the Project's passthrough Team+ setattr(team, attr, value)+ updated_team_fields.append(attr)++ if "name" in validated_data:+ # Keep Team.name mirroring Project.name: surfaces like the organization's teams+ # list and the app context still read the name off the Team row+ team.name = validated_data["name"]+ updated_team_fields.append("name")++ # Merge conversations_settings under a lock; a null clear still keeps the managed keys.+ if conversations_lock_applied:+ locked_conversations = merge_conversations_settings_locked(+ team, validated_data, patch_conversations_settings, updated_team_fields+ )+ # The locked re-read is newer than the snapshot, so a concurrent write is not logged as this user's.+ team_before_update["conversations_settings"] = locked_conversations["conversations_settings"]+ team_before_update["conversations_enabled"] = locked_conversations["conversations_enabled"]+ elif updated_team_fields:+ # auto_now fields only refresh when included in update_fields+ team.save(update_fields=[*updated_team_fields, "updated_at"])+ # Saved outside the replay gate lock. The tagged path in `save()` locks the Project row+ # and then the Team row. A Project write under the Team lock would take the two locks in+ # the opposite order, so two concurrent requests could deadlock.if updated_project_fields:instance.save(update_fields=updated_project_fields)- # Merge conversations_settings under a lock; a null clear still keeps the managed keys.- if conversations_lock_applied:- locked_conversations = merge_conversations_settings_locked(- team, validated_data, patch_conversations_settings, updated_team_fields- )- # The locked re-read is newer than the snapshot, so a concurrent write is not logged as this user's.- team_before_update["conversations_settings"] = locked_conversations["conversations_settings"]- team_before_update["conversations_enabled"] = locked_conversations["conversations_enabled"]- elif updated_team_fields:- # auto_now fields only refresh when included in update_fields- team.save(update_fields=[*updated_team_fields, "updated_at"])# Snapshot before the cache refresh below so the audit diff only reflects this# request's writes, not fields a concurrent request changed.team_after_update = team.__dict__.copy()diff --git a/posthog/api/team/session_recording_settings.py b/posthog/api/team/session_recording_settings.pyindex 0888765b7327..75a7dfe196f2 100644--- a/posthog/api/team/session_recording_settings.py+++ b/posthog/api/team/session_recording_settings.py@@ -1,16 +1,37 @@"""Validators for session recording and replay settings."""import re-from typing import Any, Literal+from typing import TYPE_CHECKING, Any, Literalfrom rest_framework import exceptions+from rest_framework.request import Request+from posthog.permissions import get_authenticator_scopes+from posthog.scopes import scopes_not_coveredfrom posthog.session_recordings.data_retention import VALID_RETENTION_PERIODS, validate_retention_period+from products.feature_flags.backend.session_recording_links import flag_exists_in_project+from . import settings_validation+if TYPE_CHECKING:+ from products.access_control.backend.facade.user_access_control import UserAccessControl+++def can_read_feature_flags(request: Request | None) -> bool:+ """True for session auth, which carries no scopes. Access control alone limits which flags it can view."""+ scopes = get_authenticator_scopes(getattr(request, "successful_authenticator", None))+ return scopes is None or "*" in scopes or not scopes_not_covered(scopes, ["feature_flag:read"])+++def validate_session_recording_linked_flag(+ value: Any, *, stored: Any, project_id: int | None, user_access_control: "UserAccessControl | None"+) -> dict | None:+ """Shared by the team and project serializers, because both endpoints can PATCH the link.-def validate_session_recording_linked_flag(value) -> dict | None:+ `stored` is the link the team holds before this request.+ `project_id` is None when the request creates the project, so it has no flags to link yet.+ """if value is None:return None@@ -29,7 +50,27 @@ def validate_session_recording_linked_flag(value) -> dict | None:flag_id = settings_validation._replay_gate_flag_id(value["id"])if flag_id is None:raise exceptions.ValidationError("Must provide an integer 'id'.")- return {**value, "id": flag_id}++ # A client that sends the whole team back also sends the link it did not change. A 400 on that+ # link would reject the other edits in the same request. Accepting it reveals nothing, because a+ # GET of the team returns the stored link. The key rewrite leaves a flag that the user cannot view+ # as sent, so a match on the id alone would let a request store any key for that flag.+ # A stored link can hold its id as a numeric string, so the stored id is normalized too.+ normalized = {**value, "id": flag_id}+ if isinstance(stored, dict) and normalized == {+ **stored,+ "id": settings_validation._replay_gate_flag_id(stored.get("id")),+ }:+ return normalized++ # The SDKs treat a flag they cannot resolve as "do not record", so a link to a missing,+ # deleted, or other-project flag turns replay off for the team without an error.+ # A flag that the user cannot view gets the same error, so the error does not reveal that it exists.+ if project_id is None or not flag_exists_in_project(flag_id, project_id, user_access_control):+ raise exceptions.ValidationError(+ f"Feature flag {flag_id} doesn't exist in this project or has been deleted. Choose a different flag."+ )+ return normalizeddef validate_session_recording_trigger_match_type_config(value) -> Literal["all", "any"] | None:diff --git a/posthog/api/team/team_serializer.py b/posthog/api/team/team_serializer.pyindex 4bdf4d6919de..6d344448146c 100644--- a/posthog/api/team/team_serializer.py+++ b/posthog/api/team/team_serializer.py@@ -45,6 +45,7 @@ from products.access_control.backend.presentation.access_control import UserAccefrom products.dashboards.backend.models import Dashboardfrom products.feature_flags.backend.facade.enums import FlagEvaluationsModefrom products.feature_flags.backend.facade.flags import get_flag_evaluations_read_mode+from products.feature_flags.backend.session_recording_links import locked_replay_gate_writefrom . import (conversations_settings,@@ -290,9 +291,14 @@ class TeamSerializer(serializers.ModelSerializer, UserPermissionsSerializerMixinraise exceptions.ValidationError(settings_validation._format_serializer_errors(serializer.errors))return serializer.validated_data- @staticmethod- def validate_session_recording_linked_flag(value) -> dict | None:- return session_recording_settings.validate_session_recording_linked_flag(value)+ def validate_session_recording_linked_flag(self, value: Any) -> dict | None:+ project_id = self.instance.project_id if self.instance is not None else self.context.get("project_id")+ return session_recording_settings.validate_session_recording_linked_flag(+ value,+ stored=self.instance.session_recording_linked_flag if self.instance is not None else None,+ project_id=project_id,+ user_access_control=self.user_access_control,+ )@staticmethoddef validate_session_recording_trigger_match_type_config(value) -> Literal["all", "any"] | None:@@ -621,23 +627,29 @@ class TeamSerializer(serializers.ModelSerializer, UserPermissionsSerializerMixin# each other — e.g. an `onboarding_tasks` PATCH racing the onboarding-completion PATCH# erased `has_completed_onboarding_for` and reverted `completed_snippet_onboarding`,# bouncing freshly onboarded users back into onboarding.- other_team_fields = [- attr for attr in validated_data if attr not in conversations_settings.LOCKED_CONVERSATIONS_COLUMNS- ]- for attr in other_team_fields:- setattr(instance, attr, validated_data[attr])-- # Merge conversations_settings under a lock; a null clear still keeps the managed keys.- if conversations_lock_applied:- locked_conversations = conversations_settings.merge_conversations_settings_locked(- instance, validated_data, patch_conversations_settings, other_team_fields- )- # The locked re-read is newer than the snapshot, so a concurrent write is not logged as this user's.- before_update["conversations_settings"] = locked_conversations["conversations_settings"]- before_update["conversations_enabled"] = locked_conversations["conversations_enabled"]- elif other_team_fields:- # auto_now fields only refresh when included in update_fields- instance.save(update_fields=[*other_team_fields, "updated_at"])+ with locked_replay_gate_write(+ instance,+ validated_data,+ self.user_access_control,+ can_read_flags=session_recording_settings.can_read_feature_flags(self.context.get("request")),+ ):+ other_team_fields = [+ attr for attr in validated_data if attr not in conversations_settings.LOCKED_CONVERSATIONS_COLUMNS+ ]+ for attr in other_team_fields:+ setattr(instance, attr, validated_data[attr])++ # Merge conversations_settings under a lock; a null clear still keeps the managed keys.+ if conversations_lock_applied:+ locked_conversations = conversations_settings.merge_conversations_settings_locked(+ instance, validated_data, patch_conversations_settings, other_team_fields+ )+ # The locked re-read is newer than the snapshot, so a concurrent write is not logged as this user's.+ before_update["conversations_settings"] = locked_conversations["conversations_settings"]+ before_update["conversations_enabled"] = locked_conversations["conversations_enabled"]+ elif other_team_fields:+ # auto_now fields only refresh when included in update_fields+ instance.save(update_fields=[*other_team_fields, "updated_at"])# Snapshot before the cache refresh below so the audit diff only reflects this# request's writes, not fields a concurrent request changed.after_update = instance.__dict__.copy()diff --git a/posthog/api/test/test_team.py b/posthog/api/test/test_team.pyindex 59791f02cf55..bb894d13aa55 100644--- a/posthog/api/test/test_team.py+++ b/posthog/api/test/test_team.py@@ -1,5 +1,6 @@import timeimport threading+from collections.abc import Callablefrom datetime import UTC, datetime, timedeltafrom typing import Any@@ -58,6 +59,7 @@ from products.conversations.backend.playbook import compose_support_playbookfrom products.dashboards.backend.models.dashboard import Dashboardfrom products.dashboards.backend.models.team_home_tab_dashboard_config import TeamHomeTabDashboardConfigfrom products.feature_flags.backend.facade.enums import FlagEvaluationsMode+from products.feature_flags.backend.models.feature_flag import FeatureFlagfrom products.feature_flags.backend.models.organization_feature_flags_config import OrganizationFeatureFlagsConfigfrom products.feature_flags.backend.test import replay_gate_fixturesfrom products.workflows.backend.facade.team_extension import TeamWorkflowsConfig@@ -1301,20 +1303,138 @@ def team_api_test_factory():"type": "validation_error",}- def test_can_set_and_unset_session_recording_linked_flag(self) -> None:- self._patch_linked_flag_config({"id": 1, "key": "provided_value"})- self._assert_linked_flag_config({"id": 1, "key": "provided_value"})+ @parameterized.expand(+ [+ ["integer id", int, {}],+ ["numeric string id", str, {}],+ ["variant", int, {"variant": "test"}],+ ]+ )+ def test_can_set_and_unset_session_recording_linked_flag(+ self, _name: str, send_id: Callable[[int], Any], variant: dict[str, str]+ ) -> None:+ flag = FeatureFlag.objects.create(team=self.team, created_by=self.user, key="current-key")++ self._patch_linked_flag_config({"id": send_id(flag.id), "key": "stale-key", **variant})+ self._assert_linked_flag_config({"id": flag.id, "key": "current-key", **variant})self._patch_linked_flag_config(None)self._assert_linked_flag_config(None)- def test_can_set_and_unset_session_recording_linked_flag_variant(self) -> None:- self._patch_linked_flag_config({"id": 1, "key": "provided_value", "variant": "test"})- self._assert_linked_flag_config({"id": 1, "key": "provided_value", "variant": "test"})+ @parameterized.expand(+ [+ ["viewable", "current-key", None],+ ["access_denied", "stale-key", None],+ ["unchanged_column", "stale-key", None],+ ["token_with_flag_read", "current-key", ["project:write", "feature_flag:read"]],+ ["token_with_all_scopes", "current-key", ["*"]],+ ["token_without_flag_read", "stale-key", ["project:write"]],+ ]+ )+ def test_moves_a_trigger_group_flag_sent_with_an_id_to_the_flags_current_key(+ self, case: str, expected_key: str, token_scopes: list[str] | None+ ) -> None:+ flag = FeatureFlag.objects.create(team=self.team, created_by=self.user, key="current-key")+ if case == "access_denied":+ self._deny_flag_access(flag)+ trigger_groups: dict[str, Any] = {+ "version": 2,+ "groups": [+ {+ "id": "group-1",+ "sampleRate": 1.0,+ "conditions": {"matchType": "any", "flag": {"id": flag.id, "key": "stale-key"}},+ }+ ],+ }+ if case == "unchanged_column":+ Team.objects.filter(pk=self.team.pk).update(session_recording_trigger_groups=trigger_groups)+ trigger_groups = self.client.get("/api/environments/@current/").json()[+ "session_recording_trigger_groups"+ ]- self._patch_linked_flag_config(None)+ headers = (+ {"authorization": f"Bearer {self.create_personal_api_key_with_scopes(token_scopes)}"}+ if token_scopes+ else {}+ )++ response = self.client.patch(+ "/api/environments/@current/", {"session_recording_trigger_groups": trigger_groups}, headers=headers+ )+ assert response.status_code == status.HTTP_200_OK, response.json()++ response = self.client.get("/api/environments/@current/")+ assert response.json()["session_recording_trigger_groups"]["groups"][0]["conditions"]["flag"] == {+ "id": flag.id,+ "key": expected_key,+ }++ @parameterized.expand(["missing", "soft_deleted", "other_project", "access_denied"])+ def test_rejects_a_linked_flag_that_cannot_gate_recording(self, case: str) -> None:+ flag_team = Team.objects.create(organization=self.organization) if case == "other_project" else self.team+ flag = FeatureFlag.objects.create(+ team=flag_team, created_by=self.user, key="gate", deleted=case == "soft_deleted"+ )+ flag_id = flag.id+ if case == "missing":+ flag.delete()+ if case == "access_denied":+ self._deny_flag_access(flag)++ response = self.client.patch(+ "/api/environments/@current/", {"session_recording_linked_flag": {"id": flag_id, "key": "gate"}}+ )++ assert response.status_code == status.HTTP_400_BAD_REQUEST, response.json()+ assert response.json()["detail"] == (+ f"Feature flag {flag_id} doesn't exist in this project or has been deleted. Choose a different flag."+ )self._assert_linked_flag_config(None)+ @parameterized.expand(+ [+ ["unchanged_soft_deleted", "soft_deleted", {}, status.HTTP_200_OK],+ ["unchanged_access_denied", "access_denied", {}, status.HTTP_200_OK],+ ["unchanged_string_id_access_denied", "string_id_access_denied", {}, status.HTTP_200_OK],+ ["unchanged_renamed", "renamed", {}, status.HTTP_200_OK],+ ["changed_key_access_denied", "access_denied", {"key": "other-key"}, status.HTTP_400_BAD_REQUEST],+ ["added_variant_access_denied", "access_denied", {"variant": "x"}, status.HTTP_400_BAD_REQUEST],+ ]+ )+ def test_keeps_an_unchanged_link_as_stored(+ self, _name: str, case: str, sent_changes: dict[str, str], expected_status: int+ ) -> None:+ flag = FeatureFlag.objects.create(team=self.team, created_by=self.user, key="gate")+ stored_link = {"id": flag.id, "key": "gate"}+ self._patch_linked_flag_config(stored_link)+ if case == "soft_deleted":+ flag.deleted = True+ flag.save()+ elif case == "renamed":+ # update() sends no post_save signal, so no relink moves the team to the new key.+ FeatureFlag.objects.filter(pk=flag.pk).update(key="renamed")+ else:+ self._deny_flag_access(flag)+ sent_link = stored_link+ if case == "string_id_access_denied":+ sent_link = {**stored_link, "id": str(flag.id)}+ Team.objects.filter(pk=self.team.pk).update(session_recording_linked_flag=sent_link)++ response = self.client.patch(+ "/api/environments/@current/",+ {+ "session_recording_linked_flag": {**sent_link, **sent_changes},+ "session_recording_minimum_duration_milliseconds": 1000,+ },+ )++ assert response.status_code == expected_status, response.json()+ accepted = expected_status == status.HTTP_200_OK+ team = self.client.get("/api/environments/@current/").json()+ assert team["session_recording_linked_flag"] == stored_link+ assert team["session_recording_minimum_duration_milliseconds"] == (1000 if accepted else None)+def test_can_set_and_unset_session_recording_network_payload_capture_config(self) -> None:# can set just onefirst_patch_response = self.client.patch(@@ -2039,6 +2159,20 @@ def team_api_test_factory():) -> HttpResponse:return self._patch_config("session_replay_config", config, expected_status)+ def _deny_flag_access(self, flag: FeatureFlag) -> None:+ self.organization.available_product_features = [+ {"name": AvailableFeature.ACCESS_CONTROL, "key": AvailableFeature.ACCESS_CONTROL}+ ]+ self.organization.save()+ self.organization_membership.level = OrganizationMembership.Level.MEMBER+ self.organization_membership.save()+ # A flag's creator can always view it, so the flag needs a different creator.+ flag.created_by = self._create_user("[email protected]")+ flag.save()+ AccessControl.objects.create(+ team=self.team, resource="feature_flag", resource_id=str(flag.id), access_level="none"+ )+def _assert_linked_flag_config(self, expected_config: dict | None) -> HttpResponse:response = self.client.get("/api/environments/@current/")assert response.status_code == status.HTTP_200_OK@@ -4816,25 +4950,6 @@ class TestTeamSerializerValidationNoDB(SimpleTestCase):def test_invalid_session_recording_linked_flag(self, _name: str, value: Any, expected_detail: str) -> None:self._assert_field_error("session_recording_linked_flag", value, "invalid", expected_detail)- # Object-level validate() needs self.context["view"], so a value that passes field validation- # can't go through is_valid() here. The static validator is the shared entry point anyway:- # ProjectBackwardCompatSerializer.validate_session_recording_linked_flag delegates straight to it.- @parameterized.expand(- [- ["none", None, None],- ["integer id", {"id": 123, "key": "flag-key"}, {"id": 123, "key": "flag-key"}],- ["numeric string id", {"id": "123", "key": "flag-key"}, {"id": 123, "key": "flag-key"}],- [- "variant survives normalization",- {"id": "123", "key": "flag-key", "variant": "test"},- {"id": 123, "key": "flag-key", "variant": "test"},- ],- ]- )- def test_valid_session_recording_linked_flag(self, _name: str, value: dict | None, expected: dict | None) -> None:- assert TeamSerializer.validate_session_recording_linked_flag(value) == expected- assert ProjectBackwardCompatSerializer.validate_session_recording_linked_flag(value) == expected-@staticmethoddef _trigger_groups_gated_on(flag: Any) -> dict:return {diff --git a/posthog/models/test/test_hog_function.py b/posthog/models/test/test_hog_function.pyindex f0a7dacdcda7..2b998f9ca1f2 100644--- a/posthog/models/test/test_hog_function.py+++ b/posthog/models/test/test_hog_function.py@@ -434,9 +434,8 @@ class TestHogFunctionsBackgroundReloading(TestCase, QueryMatchingTest):{"key": "$host", "operator": "regex", "value": "^(localhost|127\\.0\\.0\\.1)($|:)"},{"key": "$pageview", "operator": "regex", "value": "test"},]- # 1 read old secret tokens (pre_save), 1 update team, 1 load hog flows, 1 load hog functions, 1 update hog functions- # Note: RemoteConfig refresh queries are now deferred via async signals- with self.assertNumQueries(5):+ # 1 read old secret tokens (pre_save), 1 update team. The hog flow and hog function refreshes run on commit.+ with self.captureOnCommitCallbacks(execute=True), self.assertNumQueries(2):self.team.save()hog_function_1.refresh_from_db()hog_function_2.refresh_from_db()diff --git a/products/cdp/backend/models/hog_functions/hog_function.py b/products/cdp/backend/models/hog_functions/hog_function.pyindex f7726ce9f05e..7df33e2267a5 100644--- a/products/cdp/backend/models/hog_functions/hog_function.py+++ b/products/cdp/backend/models/hog_functions/hog_function.py@@ -4,7 +4,7 @@ from types import SimpleNamespacefrom typing import TYPE_CHECKING, Optionalfrom django.conf import settings-from django.db import models+from django.db import models, transactionfrom django.db.models import QuerySetfrom django.db.models.signals import post_delete, post_savefrom django.dispatch.dispatcher import receiver@@ -383,7 +383,8 @@ def action_saved(sender, instance: Action, created, **kwargs):def team_saved(sender, instance: Team, created, **kwargs):from products.cdp.backend.tasks.hog_functions import refresh_affected_hog_functions- refresh_affected_hog_functions.delay(team_id=instance.id)+ team_id = instance.id+ transaction.on_commit(lambda: refresh_affected_hog_functions.delay(team_id=team_id))@receiver(post_save, sender="cohorts.Cohort")diff --git a/products/feature_flags/backend/session_recording_links.py b/products/feature_flags/backend/session_recording_links.pyindex e9ca40e4b7ef..a8fa5c7f55aa 100644--- a/products/feature_flags/backend/session_recording_links.py+++ b/products/feature_flags/backend/session_recording_links.py@@ -14,8 +14,9 @@ matcher here is project-scoped."""from collections import defaultdict-from collections.abc import Callable, Collection, Iterable, Mapping-from typing import Any+from collections.abc import Callable, Collection, Iterable, Iterator, Mapping+from contextlib import contextmanager+from typing import TYPE_CHECKING, Anyfrom django.db import transactionfrom django.db.models import Q, QuerySet@@ -31,6 +32,9 @@ from posthog.models import Teamfrom products.feature_flags.backend.field_snapshots import capture_fields_before_save, snapshot_if_changedfrom products.feature_flags.backend.models.feature_flag import FeatureFlag+if TYPE_CHECKING:+ from products.access_control.backend.facade.user_access_control import UserAccessControl+logger = structlog.get_logger(__name__)REPLAY_GATE_DELETE_ERROR = (@@ -62,6 +66,13 @@ class ReplayGateRewrite:linked_flag: dict[str, Any] | None = Nonetrigger_groups: dict[str, Any] | None = None+ def changed_columns(self) -> dict[str, Any]:+ columns = {+ "session_recording_linked_flag": self.linked_flag,+ "session_recording_trigger_groups": self.trigger_groups,+ }+ return {column: value for column, value in columns.items() if value is not None}+@frozenclass ReplayFlagGates:@@ -227,15 +238,140 @@ def rewritten_linked_flag(linked_flag: Any, *, flag_id: int, new_key: str) -> direturn {**linked_flag, "key": new_key}-def current_flag_keys(flag_ids: Iterable[int]) -> dict[int, str]:+def current_flag_keys(+ flag_ids: Iterable[int],+ *,+ project_id: int | None = None,+ user_access_control: "UserAccessControl | None" = None,+) -> dict[int, str]:"""Each flag's key as it stands right now. A flag absent from the result has been hard-deleted.The query includes soft-deleted flags, so a team follows a soft delete onto the tombstone keythat `_free_key_held_by_soft_deleted_flags` gives the flag. That rename frees the original keyfor a new flag to claim. A team left on the original key would gate recording on a flag itnever linked.++ With `project_id`, a flag in another project is absent from the result too. With+ `user_access_control`, a flag that the user cannot view is absent from the result too.+ """+ flags = FeatureFlag.objects_including_soft_deleted.filter(pk__in=set(flag_ids))+ if project_id is not None:+ flags = flags.filter(team__project_id=project_id)+ return dict(_visible_flags(flags, user_access_control).values_list("pk", "key"))+++def _visible_flags(+ flags: QuerySet[FeatureFlag], user_access_control: "UserAccessControl | None"+) -> QuerySet[FeatureFlag]:+ if user_access_control is None:+ return flags+ return user_access_control.filter_queryset_by_access_level(flags, include_all_if_admin=True)+++def rewrite_to_current_flag_keys(+ linked_flag: Any,+ trigger_groups: Any,+ *,+ project_id: int,+ user_access_control: "UserAccessControl | None" = None,+) -> ReplayGateRewrite:+ """Point every reference that stores a flag id at that flag's current key.++ A Team API write replaces the gate columns with values a client sent. A client that loaded+ its settings before a rename still sends the old key. Storing that key would undo the relink.+ A bare trigger-group key carries no id, so it stays as sent.++ Scoped to the project because flag ids are global. Without the scope, a reference holding+ another project's flag id would copy that flag's key into this team's settings. A reference+ whose id names no flag in the project stays as sent.++ A reference to a flag that the user cannot view also stays as sent. Otherwise a client could+ send any id and read that flag's key back from the response.+ """+ keys = current_flag_keys(+ referenced_flag_ids(linked_flag, trigger_groups),+ project_id=project_id,+ user_access_control=user_access_control,+ )+ linked_flag_id = stored_flag_id(linked_flag)+ refs = trigger_group_flag_refs(trigger_groups)++ new_linked_flag = None+ if linked_flag_id is not None and (new_key := keys.get(linked_flag_id)) is not None:+ new_linked_flag = rewritten_linked_flag(linked_flag, flag_id=linked_flag_id, new_key=new_key)+ return ReplayGateRewrite(+ linked_flag=new_linked_flag,+ trigger_groups=rewritten_trigger_groups(+ trigger_groups,+ {ref.group_index: keys[ref.flag_id] for ref in refs if ref.flag_id is not None and ref.flag_id in keys},+ ),+ )+++def referenced_flag_ids(linked_flag: Any, trigger_groups: Any) -> set[int]:+ """Every flag id the gate columns store. A bare trigger-group key adds no id."""+ flag_ids = {ref.flag_id for ref in trigger_group_flag_refs(trigger_groups) if ref.flag_id is not None}+ if (linked_flag_id := stored_flag_id(linked_flag)) is not None:+ flag_ids.add(linked_flag_id)+ return flag_ids+++@contextmanager+def locked_replay_gate_write(+ team: Team,+ validated_data: dict[str, Any],+ user_access_control: "UserAccessControl | None",+ *,+ can_read_flags: bool,+) -> Iterator[None]:+ """Hold a lock on the team row while a Team API write saves the replay gate columns.++ On entry, this rewrites `validated_data` in place: every reference that stores a flag id+ moves to the flag's current key. The caller saves the team inside the block.++ A column equal to the stored one stays as sent. A save that does not change a reference then+ never changes which flag gates recording, including a stale reference that a repair left alone.++ `relink_teams` reads a renamed flag's key under the same lock. For a team that referenced the+ flag before the relink selected its teams, whichever write commits second stores the key the+ flag has at that time. A write that adds a new reference during a rename can still store the+ old key, because the relink selects its teams before it takes any lock.++ With `can_read_flags` false, every reference stays as sent. The response returns the stored+ references, so a rewrite would let a token without `feature_flag:read` send any flag id and+ read that flag's key.++ That write and a write that stores no flag id get no lock and no transaction, because the lock+ cannot change what they store. Their `post_save` receivers then run after the save commits."""- return dict(FeatureFlag.objects_including_soft_deleted.filter(pk__in=set(flag_ids)).values_list("pk", "key"))+ linked_flag = validated_data.get("session_recording_linked_flag")+ trigger_groups = validated_data.get("session_recording_trigger_groups")+ if not can_read_flags or not referenced_flag_ids(linked_flag, trigger_groups):+ yield+ return++ with transaction.atomic():+ locked = (+ Team.objects.select_for_update(no_key=True)+ .only("session_recording_linked_flag", "session_recording_trigger_groups")+ .get(pk=team.pk)+ )+ # Compared with the locked row, not the caller's copy of the team. A client that loaded the+ # team before a relink sends the old key, which differs from the key the relink stored.+ rewrite = rewrite_to_current_flag_keys(+ None if linked_flag == locked.session_recording_linked_flag else linked_flag,+ None if trigger_groups == locked.session_recording_trigger_groups else trigger_groups,+ project_id=team.project_id,+ user_access_control=user_access_control,+ )+ validated_data.update(rewrite.changed_columns())+ yield+++def flag_exists_in_project(flag_id: int, project_id: int, user_access_control: "UserAccessControl | None") -> bool:+ """Whether the project has a flag with this id that the user can view. A soft-deleted flag does not count."""+ flags = FeatureFlag.objects.filter(pk=flag_id, team__project_id=project_id)+ return _visible_flags(flags, user_access_control).exists()def rewritten_trigger_groups(trigger_groups: Any, renames: Mapping[int, str]) -> dict[str, Any] | None:@@ -286,31 +422,24 @@ def save_replay_gate_rewrites(team_id: int, compute: Callable[[Team], ReplayGate# `no_key=True` because this writes no key column, so the lock does not block the# `KEY SHARE` that a foreign key check on this Team row takes. Two `FOR NO KEY UPDATE`# locks still conflict, so two calls to this function for one team stay serialized.- # The lock does not serialize this against the Team API. That serializer saves the- # column the client sent on a row it read without a lock, so a settings edit racing a- # rename can still land the pre-rename key. `repair_replay_linked_flag_keys` reports- # such a row on its next run.+ # A Team API write takes the same lock through `locked_replay_gate_write`. A write that+ # its docstring exempts from the lock can still store a key from before a rename.team = Team.objects.select_for_update(no_key=True).filter(pk=team_id).first()if team is None:return- rewrite = compute(team)- update_fields = []- if rewrite.linked_flag is not None:- team.session_recording_linked_flag = rewrite.linked_flag- update_fields.append("session_recording_linked_flag")- if rewrite.trigger_groups is not None:- team.session_recording_trigger_groups = rewrite.trigger_groups- update_fields.append("session_recording_trigger_groups")- if not update_fields:+ columns = compute(team).changed_columns()+ if not columns:# A no-op save would still spend a write, a Celery task, and a RemoteConfig rebuild.return+ for column, value in columns.items():+ setattr(team, column, value)# Saving the instance rather than issuing a queryset `update()` is what fires the# `post_save` receiver that refreshes the team's RemoteConfig; a bulk update would leave# the cached SDK payload holding the old key. Both columns go in one save because that# refresh is queued per save, not per changed field.- team.save(update_fields=update_fields)+ team.save(update_fields=list(columns))def relink_teams(feature_flag: FeatureFlag, *, old_key: str) -> None:@@ -338,7 +467,8 @@ def relink_teams(feature_flag: FeatureFlag, *, old_key: str) -> None:# no ordering guarantee between them, and a rename can also land partway through this# loop. Every relink holding this team's row lock reads the key at this point, so they# converge on the stored key instead of leaving later teams on the key this callback- # started with. A Team API write takes no such lock and is not ordered against them.+ # started with. A Team API write that `locked_replay_gate_write` locks reads the key under+ # the same lock, so it converges too for every reference that stores a flag id.new_key = current_flag_keys([feature_flag.pk]).get(feature_flag.pk)if new_key is None:# The row is gone entirely, not just soft-deleted, so there is no key to point thisdiff --git a/products/feature_flags/backend/test/test_session_recording_links.py b/products/feature_flags/backend/test/test_session_recording_links.pyindex 6753fa1aa335..7ddfbaf30f87 100644--- a/products/feature_flags/backend/test/test_session_recording_links.py+++ b/products/feature_flags/backend/test/test_session_recording_links.py@@ -13,6 +13,7 @@ from products.feature_flags.backend.session_recording_links import (relink_teams,replay_gated_flags,replay_gated_flags_for_projects,+ rewrite_to_current_flag_keys,rewritten_linked_flag,rewritten_trigger_groups,save_replay_gate_rewrites,@@ -104,6 +105,49 @@ class TestReplayGateMatchersAgree(BaseTest):assert replay_gated_flags(self.team.project_id).gates(mine) is False+class TestRewriteToCurrentFlagKeys(BaseTest):+ @parameterized.expand(+ [+ (+ "linked_flag_keeps_its_variant",+ lambda mine, theirs: ({"id": mine.id, "key": "stale", "variant": "test"}, None),+ lambda mine: ReplayGateRewrite(linked_flag={"id": mine.id, "key": "current", "variant": "test"}),+ ),+ (+ "trigger_group_object_form",+ lambda mine, theirs: (None, trigger_groups({"flag": {"id": mine.id, "key": "stale"}})),+ lambda mine: ReplayGateRewrite(+ trigger_groups=trigger_groups({"flag": {"id": mine.id, "key": "current"}})+ ),+ ),+ (+ "trigger_group_bare_key",+ lambda mine, theirs: (None, trigger_groups({"flag": "stale"})),+ lambda mine: ReplayGateRewrite(),+ ),+ (+ "another_projects_flag",+ lambda mine, theirs: (+ {"id": theirs.id, "key": "stale"},+ trigger_groups({"flag": {"id": theirs.id, "key": "stale"}}),+ ),+ lambda mine: ReplayGateRewrite(),+ ),+ ]+ )+ def test_points_references_holding_an_id_at_the_flags_current_key(+ self, _name: str, build_columns: Any, build_expected: Any+ ) -> None:+ mine = FeatureFlag.objects.create(team=self.team, created_by=self.user, key="current")+ other_team = Team.objects.create(organization=self.organization)+ theirs = FeatureFlag.objects.create(team=other_team, created_by=self.user, key="theirs")+ linked_flag, groups = build_columns(mine, theirs)++ rewrite = rewrite_to_current_flag_keys(linked_flag, groups, project_id=self.team.project_id)++ assert rewrite == build_expected(mine)++class TestReplayGateWritesUseTheLockedRow(BaseTest):def test_an_edit_to_the_linked_flag_since_the_caller_looked_survives(self) -> None:flag = FeatureFlag.objects.create(team=self.team, created_by=self.user, key="gate-new")diff --git a/products/workflows/backend/models/hog_flow/hog_flow.py b/products/workflows/backend/models/hog_flow/hog_flow.pyindex 553c2c98d042..b0d1bc1a6f89 100644--- a/products/workflows/backend/models/hog_flow/hog_flow.py+++ b/products/workflows/backend/models/hog_flow/hog_flow.py@@ -247,4 +247,5 @@ def action_saved_for_hog_flows(sender, instance: Action, created, **kwargs):def team_saved_for_hog_flows(sender, instance: Team, created, **kwargs):from products.workflows.backend.tasks.hog_flows import refresh_affected_hog_flows # noqa: PLC0415- refresh_affected_hog_flows.delay(team_id=instance.id)+ team_id = instance.id+ transaction.on_commit(lambda: refresh_affected_hog_flows.delay(team_id=team_id))diff --git a/products/workflows/backend/test/test_hog_flow.py b/products/workflows/backend/test/test_hog_flow.pyindex dcecd349385a..a3f6e1066da7 100644--- a/products/workflows/backend/test/test_hog_flow.py+++ b/products/workflows/backend/test/test_hog_flow.py@@ -55,7 +55,9 @@ class TestHogFlow(TestCase):@patch("products.workflows.backend.tasks.hog_flows.refresh_affected_hog_flows.delay")def test_team_saved_receiver(self, mock_refresh):- self.team.save()+ with self.captureOnCommitCallbacks(execute=True):+ self.team.save()+ mock_refresh.assert_not_called()mock_refresh.assert_called_once_with(team_id=self.team.id)@patch("products.workflows.backend.models.hog_flow.hog_flow.reload_hog_flows_on_workers")