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(signals): turn anomaly scoring off on a malformed team allowlist (#126801)

posthog[bot]1c8596a77f5e
diff --git a/products/signals/backend/anomaly_scores/config.py b/products/signals/backend/anomaly_scores/config.py
index f0f76e6beeed..538f7d6bc2ea 100644
--- a/products/signals/backend/anomaly_scores/config.py
+++ b/products/signals/backend/anomaly_scores/config.py
@@ -1,7 +1,8 @@
"""Scoring settings, and the levers of the scheduled tick, read from the `anomaly-scoring` flag payload.
The payload parse fails closed: a missing, malformed or unreadable payload turns the tick off. An invalid
-field falls back to its default, and no default is unbounded.
+field falls back to its default, and no default is unbounded. A malformed team allowlist also turns the tick
+off, because its default enrolls every team of the listed organizations.
"""
import uuid
@@ -193,6 +194,14 @@ def _team_ids(value: Any) -> tuple[int, ...]:
return tuple(item for item in value if isinstance(item, int) and not isinstance(item, bool))
+def _allow_team_ids(value: Any) -> tuple[int, ...] | None:
+ """`None` when the allowlist is malformed. An empty allowlist enrolls every team of the listed organizations."""
+ if not isinstance(value, list):
+ return None
+ team_ids = _team_ids(value)
+ return team_ids if team_ids or not value else None
+
+
def _canonical_uuid(value: str) -> str | None:
try:
return str(uuid.UUID(value))
@@ -247,11 +256,15 @@ def parse_limits(payload: Any) -> ScoringLimits:
def parse_config(payload: Any) -> AnomalyScoringConfig:
if not isinstance(payload, dict) or payload.get("enabled") is not True:
return DISABLED
+ allow_team_ids = _allow_team_ids(payload.get("allow_team_ids", []))
+ if allow_team_ids is None:
+ logger.warning("anomaly_scoring_config_malformed_allowlist")
+ return DISABLED
defaults = AnomalyScoringConfig()
return AnomalyScoringConfig(
enabled=True,
organization_ids=_organization_ids(payload.get("organization_ids")),
- allow_team_ids=_team_ids(payload.get("allow_team_ids")),
+ allow_team_ids=allow_team_ids,
self_driving_organization_percentage=_percentage(
payload.get("self_driving_organization_percentage"), defaults.self_driving_organization_percentage
),
diff --git a/products/signals/backend/test/test_anomaly_scoring_tick.py b/products/signals/backend/test/test_anomaly_scoring_tick.py
index 8e7fa61a6716..249e199a0acd 100644
--- a/products/signals/backend/test/test_anomaly_scoring_tick.py
+++ b/products/signals/backend/test/test_anomaly_scoring_tick.py
@@ -133,16 +133,23 @@ def _series(key: str, starts: list[datetime]) -> ScoredSeries:
)
+_MISSING_OR_MALFORMED_PAYLOADS: list[tuple[str, Any]] = [
+ ("missing", None),
+ ("not_an_object", "enabled"),
+ ("no_enabled", {"organization_ids": [ORGANIZATION_ID]}),
+ ("enabled_not_bool", {"enabled": "true", "organization_ids": [ORGANIZATION_ID]}),
+ ("disabled", {"enabled": False, "organization_ids": [ORGANIZATION_ID], "allow_team_ids": [2]}),
+ ("allowlist_not_a_list", {"enabled": True, "organization_ids": [ORGANIZATION_ID], "allow_team_ids": 2}),
+ ("allowlist_null", {"enabled": True, "organization_ids": [ORGANIZATION_ID], "allow_team_ids": None}),
+ (
+ "allowlist_only_invalid_ids",
+ {"enabled": True, "organization_ids": [ORGANIZATION_ID], "allow_team_ids": ["2", True]},
+ ),
+]
+
+
class TestTickConfig(SimpleTestCase):
- @parameterized.expand(
- [
- ("missing", None),
- ("not_an_object", "enabled"),
- ("no_enabled", {"organization_ids": [ORGANIZATION_ID]}),
- ("enabled_not_bool", {"enabled": "true", "organization_ids": [ORGANIZATION_ID]}),
- ("disabled", {"enabled": False, "organization_ids": [ORGANIZATION_ID], "allow_team_ids": [2]}),
- ]
- )
+ @parameterized.expand(_MISSING_OR_MALFORMED_PAYLOADS)
def test_a_missing_or_malformed_payload_does_no_work(self, _name: str, payload: Any) -> None:
with (
patch("products.signals.backend.anomaly_scores.config.read_flag_payload", return_value=payload),
@@ -187,6 +194,14 @@ class TestTickConfig(SimpleTestCase):
}
assert config.active_window_days == 90
+ @parameterized.expand([("missing", {}), ("empty", {"allow_team_ids": []})])
+ def test_an_absent_or_empty_allowlist_enrolls_every_team_of_the_listed_organizations(
+ self, _name: str, allowlist: dict[str, Any]
+ ) -> None:
+ config = parse_config({"enabled": True, "organization_ids": [ORGANIZATION_ID], **allowlist})
+
+ assert (config.enabled, config.allow_team_ids) == (True, ())
+
@parameterized.expand(
[
("max_insights_per_tick", 51, 50),