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(replay): store an integer id in session_recording_linked_flag (#77087)

Phil Haack5ae135663b70
diff --git a/posthog/api/team.py b/posthog/api/team.py
index 085dbacf163b..7b72e7804d43 100644
--- a/posthog/api/team.py
+++ b/posthog/api/team.py
@@ -920,7 +920,18 @@ class TeamSerializer(serializers.ModelSerializer, UserPermissionsSerializerMixin
"Must provide a dictionary with only 'id' and 'key' keys. _or_ only 'id', 'key', and 'variant' keys."
)
- return value
+ # jsonb containment (`__contains={"id": <int>}`) is type-sensitive, so a non-integer id
+ # makes the flag-delete guard miss this row and delete a flag the team still advertises.
+ # A numeric string is normalized instead of rejected, so a client sending "123" stores a
+ # usable row rather than a broken one. Bools and floats are excluded because
+ # isinstance(True, int) is True, and int(12.5) would silently link flag 12.
+ flag_id = value["id"]
+ if isinstance(flag_id, bool) or not isinstance(flag_id, int | str):
+ raise exceptions.ValidationError("Must provide an integer 'id'.")
+ try:
+ return {**value, "id": int(flag_id)}
+ except ValueError:
+ raise exceptions.ValidationError("Must provide an integer 'id'.")
@staticmethod
def validate_session_recording_trigger_match_type_config(value) -> Literal["all", "any"] | None:
diff --git a/posthog/api/test/test_team.py b/posthog/api/test/test_team.py
index a642e47e3c9f..dfba56138956 100644
--- a/posthog/api/test/test_team.py
+++ b/posthog/api/test/test_team.py
@@ -15,6 +15,7 @@ from django.utils import timezone
from parameterized import parameterized
from rest_framework import status, test
+from posthog.api.project import ProjectBackwardCompatSerializer
from posthog.api.team import (
TEAM_CONFIG_FIELDS_SET,
TEAM_CONFIG_MEMBER_FIELDS_SET,
@@ -3518,11 +3519,34 @@ class TestTeamSerializerValidationNoDB(SimpleTestCase):
{"wat": "wat"},
"Must provide a dictionary with only 'id' and 'key' keys. _or_ only 'id', 'key', and 'variant' keys.",
],
+ ["non-numeric string id", {"id": "abc", "key": "flag-key"}, "Must provide an integer 'id'."],
+ ["float id", {"id": 12.5, "key": "flag-key"}, "Must provide an integer 'id'."],
+ ["null id", {"id": None, "key": None}, "Must provide an integer 'id'."],
+ ["boolean id", {"id": True, "key": "flag-key"}, "Must provide an integer 'id'."],
]
)
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
+
@parameterized.expand(
[
["string", "Marple bridge", "Must provide a dictionary or None."],