: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): fail closed for negative property filters when person properties never loaded (#75929)
posthog[bot]49407ecaa1ee
diff --git a/rust/feature-flags/src/flags/flag_matching.rs b/rust/feature-flags/src/flags/flag_matching.rsindex 13127b43eaf5..bfd0eb7db393 100644--- a/rust/feature-flags/src/flags/flag_matching.rs+++ b/rust/feature-flags/src/flags/flag_matching.rs@@ -183,6 +183,13 @@ impl FlagEvaluationState {}}+ /// True when person-property DB prep never ran (neither fetched nor deliberately+ /// skipped in favor of overrides). Distinct from `Skipped`, where overrides are+ /// known to cover every property the batch needs — see `PersonPropertyState`.+ pub(crate) fn person_properties_pending(&self) -> bool {+ matches!(self.person_property_state, PersonPropertyState::Pending)+ }+pub fn get_group_properties(&self) -> &HashMap<GroupTypeIndex, HashMap<String, Value>> {&self.group_properties}@@ -1661,7 +1668,23 @@ impl FeatureFlagMatcher {cohort_filters.push(filter);} else {let props = property_context.resolve_for_filter(filter);- if !match_property(filter, props, false, self.timezone).unwrap_or(false) {+ // Person properties that were never fetched (DB prep didn't run for this+ // evaluation) must not be treated as "person has no properties", because+ // an absent key would then make negative operators (is_not, not_icontains,+ // ...) and is_not_set match by accident. partial_props makes match_property+ // error on a missing key instead of matching it; the unwrap_or(false) below+ // turns that error into no-match, so the condition fails closed. Only+ // Pending gets this: Skipped and Fetched property maps are authoritative,+ // so an absent key there genuinely means the person lacks the property.+ // This is defense in depth: in the batch flow a prep failure errors those+ // flags out before evaluation, so the guard protects any path that reaches+ // evaluation with the state still Pending. Cohort filters (evaluated+ // separately below) don't get this guard; under Pending they're currently+ // safe only because cohorts are never loaded when person prep hasn't run.+ let partial_props = filter.prop_type != PropertyType::Group+ && self.flag_evaluation_state.person_properties_pending();+ if !match_property(filter, props, partial_props, self.timezone).unwrap_or(false)+ {return Ok((false, FeatureFlagMatchReason::NoConditionMatch));}}diff --git a/rust/feature-flags/src/flags/test_flag_matching.rs b/rust/feature-flags/src/flags/test_flag_matching.rsindex 64182145da7e..1184dd670428 100644--- a/rust/feature-flags/src/flags/test_flag_matching.rs+++ b/rust/feature-flags/src/flags/test_flag_matching.rs@@ -1658,6 +1658,126 @@ mod tests {assert_eq!(reason, FeatureFlagMatchReason::NoConditionMatch);}+ /// Regression test: when person-property DB prep never ran (`PersonPropertyState::Pending`,+ /// the matcher's default), a negative operator like `is_not` must not treat the missing+ /// key as a match. Fetch misses are transient, so failing open here would let a flag+ /// intermittently grant access to exactly the users a negative condition should exclude.+ #[tokio::test]+ async fn test_is_condition_match_is_not_fails_closed_when_person_properties_pending() {+ let context = TestContext::new(None).await;+ let cohort_cache = Arc::new(CohortCacheManager::new(+ context.non_persons_reader.clone(),+ None,+ None,+ ));+ let flag = mock!(FeatureFlag);++ let condition = FlagPropertyGroup {+ variant: None,+ properties: Some(vec![PropertyFilter {+ key: "tenant".to_string(),+ value: Some(json!("mecklenburgische")),+ operator: Some(OperatorType::IsNot),+ prop_type: PropertyType::Person,+ group_type_index: None,+ negation: None,+ compiled_regex: None,+ extra: Default::default(),+ }]),+ rollout_percentage: Some(100.0),+ ..Default::default()+ };++ let matcher = FeatureFlagMatcher::new(+ "test_user".to_string(),+ None,+ 1,+ context.create_postgres_router(),+ cohort_cache,+ empty_group_type_cache(),+ None,+ );+ // Left at its default `Pending` state: DB prep never ran for this matcher.+ assert!(matcher.flag_evaluation_state.person_properties_pending());++ // Mirrors what `get_person_properties` returns for a Pending state with no+ // overrides: an empty map, since the fetch miss is swallowed into a default.+ let empty_person = HashMap::new();+ let empty_groups = HashMap::new();+ let ctx = PropertyContext {+ person_properties: Some(&empty_person),+ group_properties: &empty_groups,+ aggregation: None,+ };+ let (is_match, reason) = matcher+ .is_condition_match(&flag, &condition, &ctx, None, &None)+ .unwrap();+ assert!(!is_match, "a missing property must not satisfy is_not");+ assert_eq!(reason, FeatureFlagMatchReason::NoConditionMatch);+ }++ /// Regression test: when DB prep is deliberately skipped because request overrides+ /// cover every property the batch needs (`PersonPropertyState::Skipped`), a negative+ /// operator must still evaluate normally against the override-supplied value — the+ /// fail-closed handling for `Pending` must not make this legitimate path inconclusive.+ #[tokio::test]+ async fn test_is_condition_match_is_not_evaluates_normally_when_person_properties_skipped() {+ let context = TestContext::new(None).await;+ let cohort_cache = Arc::new(CohortCacheManager::new(+ context.non_persons_reader.clone(),+ None,+ None,+ ));+ let flag = mock!(FeatureFlag);++ let condition = FlagPropertyGroup {+ variant: None,+ properties: Some(vec![PropertyFilter {+ key: "tenant".to_string(),+ value: Some(json!("mecklenburgische")),+ operator: Some(OperatorType::IsNot),+ prop_type: PropertyType::Person,+ group_type_index: None,+ negation: None,+ compiled_regex: None,+ extra: Default::default(),+ }]),+ rollout_percentage: Some(100.0),+ ..Default::default()+ };++ let mut matcher = FeatureFlagMatcher::new(+ "test_user".to_string(),+ None,+ 1,+ context.create_postgres_router(),+ cohort_cache,+ empty_group_type_cache(),+ None,+ );+ matcher.flag_evaluation_state.skip_person_properties();+ assert!(!matcher.flag_evaluation_state.person_properties_pending());++ // Mirrors what `get_person_properties` returns for a Skipped state: the+ // override value is merged in and present under the filter's key.+ let mut overridden_person = HashMap::new();+ overridden_person.insert("tenant".to_string(), json!("acme"));+ let empty_groups = HashMap::new();+ let ctx = PropertyContext {+ person_properties: Some(&overridden_person),+ group_properties: &empty_groups,+ aggregation: None,+ };+ let (is_match, reason) = matcher+ .is_condition_match(&flag, &condition, &ctx, None, &None)+ .unwrap();+ assert!(+ is_match,+ "acme is not mecklenburgische, so is_not should match"+ );+ assert_eq!(reason, FeatureFlagMatchReason::ConditionMatch);+ }+fn create_test_flag_with_variants(team_id: TeamId) -> FeatureFlag {mock!(FeatureFlag,team_id: team_id,diff --git a/rust/feature-flags/src/properties/property_matching.rs b/rust/feature-flags/src/properties/property_matching.rsindex f352c380291f..6deb35a98360 100644--- a/rust/feature-flags/src/properties/property_matching.rs+++ b/rust/feature-flags/src/properties/property_matching.rs@@ -170,7 +170,10 @@ pub fn match_property(// only looks for matches where key exists in override_property_values// doesn't support operator is_not_set with partial_propsif partial_props && !matching_property_values.contains_key(key) {- tracing::warn!("Missing property for matching: {}", property.key);+ // debug, not warn: this fires per filter per flag while person properties are+ // degraded, and the request-level miss is already recorded at error level with+ // a counter in get_person_properties_from_evaluation_state.+ tracing::debug!("Missing property for matching: {}", property.key);return Err(FlagMatchingError::MissingProperty(format!("can't match properties without a value. Missing property: {}",property.key