: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): skip NUL-byte flag keys in last-called sync (#71635)
Phil Haackdc1426f5b9e6
diff --git a/posthog/tasks/tasks.py b/posthog/tasks/tasks.pyindex 47a3e893c151..1811d97f11e8 100644--- a/posthog/tasks/tasks.py+++ b/posthog/tasks/tasks.py@@ -1388,11 +1388,13 @@ def sync_feature_flag_last_called(self: PushGatewayTask) -> None:)return- # Build lookup map from merged results+ # Build lookup map from merged results. Skip flag keys containing NUL bytes:+ # Postgres can't store them, so they can never match a real FeatureFlag.key,+ # and passing one into the key__in query below raises psycopg.DataError.flag_updates: dict[tuple[int, str], datetime] = {}- for key, (ts, _count) in merged_results.items():- if ts is not None:- flag_updates[key] = ts+ for (team_id, flag_key), (ts, _count) in merged_results.items():+ if ts is not None and "\x00" not in flag_key:+ flag_updates[(team_id, flag_key)] = tsif not flag_updates:redis_client.set(FEATURE_FLAG_LAST_CALLED_SYNC_KEY, current_sync_timestamp.isoformat())diff --git a/products/feature_flags/backend/test/test_feature_flag_sync.py b/products/feature_flags/backend/test/test_feature_flag_sync.pyindex c667dc89e6b1..4f1bbf255922 100644--- a/products/feature_flags/backend/test/test_feature_flag_sync.py+++ b/products/feature_flags/backend/test/test_feature_flag_sync.py@@ -165,6 +165,25 @@ class TestSyncFeatureFlagLastCalled(BaseTest):self.flag1.refresh_from_db()assert self.flag1.last_called_at is not None+ @freeze_time("2024-06-15 12:00:00")+ @patch("posthog.clickhouse.client.sync_execute")+ @patch("posthog.tasks.tasks.get_client")+ def test_skips_flag_keys_with_nul_bytes(self, mock_get_client: MagicMock, mock_sync_execute: MagicMock) -> None:+ """A ClickHouse-sourced flag key containing a NUL byte must not reach the Postgres query,+ since psycopg rejects NUL bytes in query parameters."""+ redis_mock = mock_redis_client()+ mock_get_client.return_value = redis_mock++ mock_sync_execute.return_value = [+ (self.team.pk, self.flag1.key, tz.make_aware(datetime(2024, 6, 15, 11, 0, 0)), 100),+ (self.team.pk, "bad-flag-\x00-key", tz.make_aware(datetime(2024, 6, 15, 11, 0, 0)), 50),+ ]++ sync_feature_flag_last_called() # must not raise++ self.flag1.refresh_from_db()+ assert self.flag1.last_called_at is not None+@freeze_time("2024-06-15 12:00:00")@patch("posthog.clickhouse.client.sync_execute")@patch("posthog.tasks.tasks.get_client")