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(flags): reject and explain unsupported cohort filter operators (#123709)

Phill1588833d0cde
diff --git a/products/feature_flags/backend/api/test/test_feature_flag.py b/products/feature_flags/backend/api/test/test_feature_flag.py
index 68647e50f86e..90b327de0819 100644
--- a/products/feature_flags/backend/api/test/test_feature_flag.py
+++ b/products/feature_flags/backend/api/test/test_feature_flag.py
@@ -531,6 +531,44 @@ class TestFeatureFlag(APIBaseTest, ClickhouseTestMixin):
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.json()["key"], f"cohort-feature-{operator}")
+ def test_cant_create_flag_with_unsupported_operator_for_cohort_properties(self) -> None:
+ cohort = Cohort.objects.create(team=self.team, name="test cohort", created_by=self.user)
+ count = FeatureFlag.objects.count()
+
+ response = self.client.post(
+ f"/api/projects/{self.team.id}/feature_flags",
+ {
+ "name": "Cohort feature exact",
+ "key": "cohort-feature-exact",
+ "filters": {
+ "groups": [
+ {
+ "rollout_percentage": 100,
+ "properties": [
+ {
+ "key": "id",
+ "type": "cohort",
+ "value": cohort.pk,
+ "operator": "exact",
+ }
+ ],
+ }
+ ]
+ },
+ },
+ )
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertEqual(
+ response.json(),
+ {
+ "type": "validation_error",
+ "code": "cross_field.cohort_requires_in_or_not_in",
+ "detail": "groups[0].properties[0].operator: Cohort properties must use the 'in' or 'not_in' operator, not 'exact'.",
+ "attr": "filters",
+ },
+ )
+ self.assertEqual(FeatureFlag.objects.count(), count)
+
def test_saving_flag_strips_legacy_holdout_groups(self):
flag = FeatureFlag.objects.create(
team=self.team,
diff --git a/products/feature_flags/backend/filters_validation.py b/products/feature_flags/backend/filters_validation.py
index 66d20ed9c6c0..60006143d4ad 100644
--- a/products/feature_flags/backend/filters_validation.py
+++ b/products/feature_flags/backend/filters_validation.py
@@ -58,6 +58,9 @@ def _is_comparable_number(value: Any) -> bool:
LIST_VALUE_OPERATORS: frozenset[str] = frozenset({"icontains_multi", "not_icontains_multi"})
+# The matcher honors these two on a cohort property and scores every other operator as a
+# non-match, so a condition that stores one of the others never matches.
+COHORT_OPERATORS: frozenset[str] = frozenset({"in", "not_in"})
PERSON_AGGREGATED_PROPERTY_TYPES: tuple[str, ...] = ("person", "cohort", "flag")
@@ -226,7 +229,7 @@ def check_operator_value_compatibility(filters: Mapping[str, Any]) -> list[Viola
message="Flag properties must use the 'flag_evaluates_to' operator.",
)
)
- if operator in ("in", "not_in") and prop.get("type") != "cohort":
+ if operator in COHORT_OPERATORS and prop.get("type") != "cohort":
violations.append(
Violation(
rule_id="cross_field.in_not_in_requires_cohort",
@@ -234,6 +237,15 @@ def check_operator_value_compatibility(filters: Mapping[str, Any]) -> list[Viola
message=f"Operator {operator} is only supported on cohort properties.",
)
)
+ # An omitted operator is valid, because the matcher reads it as `in`.
+ if prop.get("type") == "cohort" and operator is not None and operator not in COHORT_OPERATORS:
+ violations.append(
+ Violation(
+ rule_id="cross_field.cohort_requires_in_or_not_in",
+ path=f"groups[{group_index}].properties[{prop_index}].operator",
+ message=f"Cohort properties must use the 'in' or 'not_in' operator, not '{operator}'.",
+ )
+ )
if operator in DATE_OPERATORS and determine_parsed_date_for_property_matching(value) is None:
violations.append(
Violation(
diff --git a/products/feature_flags/backend/test/test_filters_validation.py b/products/feature_flags/backend/test/test_filters_validation.py
index bbbd304f4a48..e23c5a5ebc89 100644
--- a/products/feature_flags/backend/test/test_filters_validation.py
+++ b/products/feature_flags/backend/test/test_filters_validation.py
@@ -144,6 +144,21 @@ class TestFiltersValidation(SimpleTestCase):
{"groups": [{"properties": [{"key": "id", "type": "cohort", "operator": "in", "value": 5}]}]},
[],
),
+ (
+ "not_in_operator_on_cohort_property",
+ {"groups": [{"properties": [{"key": "id", "type": "cohort", "operator": "not_in", "value": 5}]}]},
+ [],
+ ),
+ (
+ "exact_operator_on_cohort_property",
+ {"groups": [{"properties": [{"key": "id", "type": "cohort", "operator": "exact", "value": 5}]}]},
+ ["cross_field.cohort_requires_in_or_not_in"],
+ ),
+ (
+ "cohort_property_without_operator",
+ {"groups": [{"properties": [{"key": "id", "type": "cohort", "value": 5}]}]},
+ [],
+ ),
(
"date_operator_unparseable_value",
{"groups": [{"properties": [_person_prop(operator="is_date_after", value="not a date")]}]},
diff --git a/products/surveys/backend/api/test/test_survey.py b/products/surveys/backend/api/test/test_survey.py
index 194186be08df..0d3243370007 100644
--- a/products/surveys/backend/api/test/test_survey.py
+++ b/products/surveys/backend/api/test/test_survey.py
@@ -1678,7 +1678,7 @@ class TestSurvey(APIBaseTest):
{
"key": "id",
"value": cohort_not_valid_for_ff.pk,
- "operator": "exact",
+ "operator": "in",
"type": "cohort",
}
],
diff --git a/rust/feature-flags/src/api/types.rs b/rust/feature-flags/src/api/types.rs
index 97c9d56f0758..a32885e7cd92 100644
--- a/rust/feature-flags/src/api/types.rs
+++ b/rust/feature-flags/src/api/types.rs
@@ -946,6 +946,26 @@ impl FlagDetails {
format!("Could not check if person is in {cohort_label}"),
),
};
+ // A membership line sends the reader to check a cohort that is not what
+ // rejected them. An absent operator keeps its line, because the matcher
+ // reads it as `in`.
+ let explanation = if property.cohort_operator_is_supported() {
+ explanation
+ } else {
+ // Name the operator the way the filter stores it. The `Debug`
+ // spelling in `operator_str` drops the underscore, so `is_not`
+ // would read as `isnot`, a spelling that exists nowhere.
+ let stored_operator = property
+ .operator
+ .and_then(|op| serde_json::to_value(op).ok())
+ .and_then(|value| value.as_str().map(str::to_owned))
+ .unwrap_or_else(|| operator_str.clone());
+ format!(
+ "A cohort filter needs the 'in' or 'not in' operator. This one \
+ uses '{stored_operator}', so the condition never matches. Remove \
+ this cohort filter and add the cohort again."
+ )
+ };
property_analyses.push(PropertyAnalysis {
key: property.key.clone(),
operator: operator_str,
@@ -2114,10 +2134,17 @@ mod tests {
}
#[rstest]
- #[case::is_in("in")]
- #[case::is_not_in("not_in")]
+ #[case::is_in("in", "Could not check if person is in cohort 12345")]
+ #[case::is_not_in("not_in", "Could not check if person is in cohort 12345")]
+ // An unsupported operator rejects the person whether or not membership resolves, so the
+ // operator line replaces the unresolved-membership line here too.
+ #[case::unsupported_operator(
+ "exact",
+ "A cohort filter needs the 'in' or 'not in' operator. This one uses 'exact', so the condition never matches. Remove this cohort filter and add the cohort again."
+ )]
fn test_condition_analysis_fails_cohort_filters_closed_when_membership_is_unresolved(
#[case] operator: &str,
+ #[case] expected_explanation: &str,
) {
use std::collections::HashMap;
@@ -2148,14 +2175,14 @@ mod tests {
assert_eq!(analysis.len(), 1);
assert!(
!analysis[0].properties[0].matched,
- "Unresolved cohort membership must report matched=false for both operators"
+ "Unresolved cohort membership must report matched=false for every operator"
);
assert!(
!analysis[0].properties_matched,
"Condition must agree with the matcher, which fails closed without cohorts"
);
assert_eq!(
- analysis[0].properties[0].explanation, "Could not check if person is in cohort 12345",
+ analysis[0].properties[0].explanation, expected_explanation,
"The line must not assert a membership that was never resolved"
);
assert_eq!(
@@ -2193,6 +2220,21 @@ mod tests {
true,
"Could not fully check if person is in cohort 12345, because it targets behavior over time"
)]
+ // An operator the matcher does not honor rejects the person whatever the membership is, so
+ // the line must name the operator rather than the cohort.
+ #[case::unsupported_operator_member(
+ "exact",
+ CohortMembership::Member,
+ false,
+ "A cohort filter needs the 'in' or 'not in' operator. This one uses 'exact', so the condition never matches. Remove this cohort filter and add the cohort again."
+ )]
+ // A two-word operator must be named the way the filter stores it, with the underscore.
+ #[case::unsupported_two_word_operator_non_member(
+ "is_not",
+ CohortMembership::NonMember,
+ false,
+ "A cohort filter needs the 'in' or 'not in' operator. This one uses 'is_not', so the condition never matches. Remove this cohort filter and add the cohort again."
+ )]
fn test_condition_analysis_applies_cohort_operator_to_known_membership(
#[case] operator: &str,
#[case] membership: CohortMembership,
diff --git a/rust/feature-flags/src/cohorts/cohort_operations.rs b/rust/feature-flags/src/cohorts/cohort_operations.rs
index a0f55735d75c..07a4fa88088f 100644
--- a/rust/feature-flags/src/cohorts/cohort_operations.rs
+++ b/rust/feature-flags/src/cohorts/cohort_operations.rs
@@ -559,16 +559,14 @@ pub fn apply_cohort_membership_logic<F: Borrow<PropertyFilter>>(
.get_cohort_id()
.ok_or(FlagError::CohortFiltersParsingError)?;
let matches = cohort_matches.get(&cohort_id).copied().unwrap_or(false);
- let operator = filter.operator.unwrap_or(OperatorType::In);
-
- // Combine the operator logic directly within this method
- let membership_match = match operator {
- OperatorType::In => matches,
- OperatorType::NotIn => !matches,
- // Currently supported operators are IN and NOT IN
- // Any other operator defaults to false
- _ => false,
- };
+
+ // `cohort_operator_is_supported` holds the supported set, so an explanation built from
+ // that helper cannot disagree with the verdict here. An absent operator reads as `in`.
+ let membership_match = filter.cohort_operator_is_supported()
+ && match filter.operator {
+ Some(OperatorType::NotIn) => !matches,
+ _ => matches,
+ };
// If any filter does not match, return false early
if !membership_match {
diff --git a/rust/feature-flags/src/flags/flag_matching.rs b/rust/feature-flags/src/flags/flag_matching.rs
index 13304474595b..5c805077709b 100644
--- a/rust/feature-flags/src/flags/flag_matching.rs
+++ b/rust/feature-flags/src/flags/flag_matching.rs
@@ -2317,13 +2317,18 @@ impl FeatureFlagMatcher {
if !self.evaluate_cohort_filters(&cohort_filters, cohort_props, cohorts.clone())? {
// A non-match isn't trustworthy when a targeted cohort has a behavioral or
// lifecycle leaf the dynamic path can't resolve, so flag it with a distinct reason.
- let cohort_membership_unresolved = cohort_filters.iter().any(|filter| {
- filter.get_cohort_id().is_some_and(|cohort_id| {
- cohorts
- .iter()
- .any(|c| c.id == cohort_id && c.has_behavioral_condition())
- })
- });
+ // An operator the matcher does not honor fails the condition whatever the
+ // membership is, so the caveat does not hold for one of those.
+ let cohort_membership_unresolved = cohort_filters
+ .iter()
+ .all(|filter| filter.cohort_operator_is_supported())
+ && cohort_filters.iter().any(|filter| {
+ filter.get_cohort_id().is_some_and(|cohort_id| {
+ cohorts
+ .iter()
+ .any(|c| c.id == cohort_id && c.has_behavioral_condition())
+ })
+ });
let reason = if cohort_membership_unresolved {
FeatureFlagMatchReason::NoConditionMatchCohortNotEvaluated
} else {
diff --git a/rust/feature-flags/src/flags/property_filter.rs b/rust/feature-flags/src/flags/property_filter.rs
index 1393d1b7fc0c..0ac661ead4c4 100644
--- a/rust/feature-flags/src/flags/property_filter.rs
+++ b/rust/feature-flags/src/flags/property_filter.rs
@@ -15,6 +15,17 @@ impl PropertyFilter {
self.prop_type == PropertyType::Cohort
}
+ /// Whether the cohort matcher honors this filter's operator.
+ /// `apply_cohort_membership_logic` applies `in` and `not_in`, reads an absent operator as
+ /// `in`, and scores every other operator as a non-match. That matcher and every path that
+ /// reports on a cohort filter's operator resolve through here, so they cannot drift apart.
+ pub fn cohort_operator_is_supported(&self) -> bool {
+ matches!(
+ self.operator.unwrap_or(OperatorType::In),
+ OperatorType::In | OperatorType::NotIn
+ )
+ }
+
/// Returns the cohort id if the filter is a cohort filter, or None if it's not a cohort filter
/// or if the value cannot be parsed as a cohort id.
/// Handles both JSON number and string representations (Python serializes both).
@@ -200,6 +211,29 @@ mod tests {
assert!(!filter.is_cohort());
}
+ fn cohort_filter_with(operator: Option<OperatorType>) -> PropertyFilter {
+ PropertyFilter {
+ key: "id".to_string(),
+ value: Some(Value::Number(serde_json::Number::from(12345))),
+ operator,
+ prop_type: PropertyType::Cohort,
+ group_type_index: None,
+ negation: None,
+ compiled_regex: None,
+ extra: Default::default(),
+ }
+ }
+
+ #[test]
+ fn test_cohort_operator_is_supported() {
+ assert!(cohort_filter_with(Some(OperatorType::In)).cohort_operator_is_supported());
+ assert!(cohort_filter_with(Some(OperatorType::NotIn)).cohort_operator_is_supported());
+ // The matcher reads an absent operator as `in`, so it still honors the filter.
+ assert!(cohort_filter_with(None).cohort_operator_is_supported());
+ assert!(!cohort_filter_with(Some(OperatorType::Exact)).cohort_operator_is_supported());
+ assert!(!cohort_filter_with(Some(OperatorType::IsNot)).cohort_operator_is_supported());
+ }
+
#[test]
fn test_get_cohort_id() {
let mut filter = mock!(crate::properties::property_models::PropertyFilter, key: "cohort".mock_into(), prop_type: PropertyType::Cohort, operator: Some(OperatorType::Exact));
diff --git a/rust/feature-flags/src/flags/test_flag_matching.rs b/rust/feature-flags/src/flags/test_flag_matching.rs
index 1890d0331538..d6030ca26c1d 100644
--- a/rust/feature-flags/src/flags/test_flag_matching.rs
+++ b/rust/feature-flags/src/flags/test_flag_matching.rs
@@ -5124,10 +5124,21 @@ mod tests {
);
}
+ // A behavioral-cohort non-match must report NoConditionMatchCohortNotEvaluated, not a bare
+ // NoConditionMatch: the evaluator can't resolve behavioral membership, so the negative is unreliable.
+ // An operator the matcher does not honor rejects the person whatever the membership is, so that
+ // negative is reliable and keeps the bare reason.
+ #[rstest::rstest]
+ #[case::supported_operator(None, FeatureFlagMatchReason::NoConditionMatchCohortNotEvaluated)]
+ #[case::unsupported_operator(
+ Some(OperatorType::Exact),
+ FeatureFlagMatchReason::NoConditionMatch
+ )]
#[tokio::test]
- async fn test_behavioral_cohort_non_match_surfaces_cohort_not_evaluated_reason() {
- // A behavioral-cohort non-match must report NoConditionMatchCohortNotEvaluated, not a bare
- // NoConditionMatch: the evaluator can't resolve behavioral membership, so the negative is unreliable.
+ async fn test_behavioral_cohort_non_match_reason_follows_the_cohort_operator(
+ #[case] operator_override: Option<OperatorType>,
+ #[case] expected_reason: FeatureFlagMatchReason,
+ ) {
let context = TestContext::new(None).await;
let cohort_cache = Arc::new(CohortCacheManager::new(
context.non_persons_reader.clone(),
@@ -5158,7 +5169,10 @@ mod tests {
.await
.unwrap();
- let flag = flag_targeting_cohort(team.id, cohort.id);
+ let mut flag = flag_targeting_cohort(team.id, cohort.id);
+ if let Some(operator) = operator_override {
+ flag.filters.groups[0].properties.as_mut().unwrap()[0].operator = Some(operator);
+ }
let mut matcher = FeatureFlagMatcher::new(
distinct_id.clone(),
@@ -5179,10 +5193,7 @@ mod tests {
let result = matcher.get_match(&flag, None, None, None, &None).unwrap();
assert!(!result.matches);
- assert_eq!(
- result.reason,
- FeatureFlagMatchReason::NoConditionMatchCohortNotEvaluated
- );
+ assert_eq!(result.reason, expected_reason);
// Serializes as the backward-compatible code; the enriched signal rides the description.
assert_eq!(result.reason.to_string(), "no_condition_match");
}