: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): match mobile app versions in semver flag conditions (#69118)
posthog[bot]58628b519b3a
diff --git a/rust/feature-flags/src/properties/property_matching.rs b/rust/feature-flags/src/properties/property_matching.rsindex 46f5333661d3..1338422595c1 100644--- a/rust/feature-flags/src/properties/property_matching.rs+++ b/rust/feature-flags/src/properties/property_matching.rs@@ -73,13 +73,68 @@ fn normalize_version_string(version: &str) -> &str {version.strip_prefix('v').unwrap_or(version).trim()}+/// Strip leading zeros from an all-digit component ("08" -> "8", "000" -> "0").+/// Non-numeric components are returned unchanged so invalid versions still fail to parse.+fn strip_component_leading_zeros(part: &str) -> &str {+ if part.is_empty() || !part.bytes().all(|b| b.is_ascii_digit()) {+ return part;+ }+ let trimmed = part.trim_start_matches('0');+ if trimmed.is_empty() {+ "0"+ } else {+ trimmed+ }+}++/// Canonicalize a version string into strict `MAJOR.MINOR.PATCH` semver so the `semver`+/// crate can parse the shapes mobile SDKs actually emit. The crate rejects a missing patch+/// component and any leading zeros, so real versions like "3.10", "3.08", and "2.48" would+/// otherwise fail to parse and make version-gated flag conditions silently never match.+///+/// Two adjustments are made to the numeric core only, leaving any pre-release/build suffix+/// (e.g. "-alpha.1", "+build.7") untouched:+/// - pad a missing minor/patch component with 0 ("3" -> "3.0.0", "3.10" -> "3.10.0")+/// - strip leading zeros from numeric identifiers ("3.08" -> "3.8.0", "0" -> "0.0.0")+///+/// Non-numeric components are left as-is so genuinely invalid versions still fail to parse.+///+/// Note: this is intentionally more permissive than two other surfaces that evaluate the+/// same semver conditions: `STRICT_SEMVER_REGEX` in `posthog/hogql/property.py`, which+/// gates the ClickHouse `sortableSemver` path used for insights/cohort analytics, and the+/// posthog-python SDK's local evaluator (`parse_semver`), which rejects leading zeros+/// outright. `match_property` also backs local cohort evaluation, so a person on a version+/// like "3.08" can now satisfy a cohort-gated flag locally while being excluded from the+/// same cohort as computed by ClickHouse. Flag evaluation here cleanly returns `false` on a+/// parse failure (no ClickHouse array-ordering pitfall), so accepting these formats only+/// turns silent non-matches into correct matches for direct flag conditions; the+/// cross-engine divergence above is the tradeoff.+fn canonicalize_version_string(version: &str) -> String {+ // Split off any pre-release ("-") or build ("+") suffix; the numeric core precedes it.+ let (core, suffix) = match version.find(['-', '+']) {+ Some(idx) => version.split_at(idx),+ None => (version, ""),+ };++ let mut components: Vec<&str> = core.split('.').map(strip_component_leading_zeros).collect();++ // Pad two-component versions ("3.10" -> "3.10.0") so strict semver parsing succeeds.+ while components.len() < 3 {+ components.push("0");+ }++ format!("{}{}", components.join("."), suffix)+}+pub fn to_semver_representation(value: &Value) -> Option<Version> {let version_string = to_string_representation(value);let normalized = normalize_version_string(&version_string);- // TODO: Build metadata (e.g., "1.0.0+build.1") is not currently supported because- // our `sortableSemver` method in ClickHouse/HogQL doesn't support it yet.- // For semver equality checks, use regular string equality operators instead.- Version::parse(normalized).ok()+ let canonical = canonicalize_version_string(normalized);+ // Build metadata (e.g., "1.0.0+build.1") parses fine here and canonicalization above+ // preserves it, but our `sortableSemver` method in ClickHouse/HogQL ignores it, so+ // equality-gated flags can diverge from insights/cohort analytics for build-tagged+ // versions. Prefer regular string equality operators for exact build-tagged matches.+ Version::parse(&canonical).ok()}pub fn match_property(@@ -383,8 +438,12 @@ pub fn match_property(let normalized_version = normalize_version_string(&version_string);let requirement_string = match operator {- OperatorType::SemverTilde => format!("~{normalized_version}"),- OperatorType::SemverCaret => format!("^{normalized_version}"),+ OperatorType::SemverTilde => {+ format!("~{}", canonicalize_version_string(normalized_version))+ }+ OperatorType::SemverCaret => {+ format!("^{}", canonicalize_version_string(normalized_version))+ }OperatorType::SemverWildcard => {// For wildcard, replace * with x for semver compatibility.// Supported patterns: "1.*", "1.2.*", "1.*.*", "*"@@ -399,7 +458,16 @@ pub fn match_property(//// Invalid patterns like "1.*.3" will fail VersionReq parsing and// return a ValidationError, which is the expected behavior.- normalized_version.replace('*', "x")+ //+ // Leading zeros are stripped from numeric components (not padded,+ // since "*" supplies the trailing components) so a mobile-shaped+ // wildcard filter like "3.08.*" parses the same way "~3.08" does.+ normalized_version+ .split('.')+ .map(strip_component_leading_zeros)+ .collect::<Vec<_>>()+ .join(".")+ .replace('*', "x")}_ => normalized_version.to_string(),};@@ -2478,38 +2546,6 @@ mod test_match_properties {).expect("expected match to exist"));- // Leading zeros are not valid semver- assert!(!match_property(- &property,- &HashMap::from([("version".to_string(), json!("01.02.03"))]),- true- )- .expect("expected match to exist"));-- // Leading zero in a single component is also invalid (was the user-visible HogQL bug- // where "3.07" silently became [3, 7] and matched a "version >= 3.7" filter).- assert!(!match_property(- &property,- &HashMap::from([("version".to_string(), json!("3.07"))]),- true- )- .expect("expected match to exist"));-- // Two-part versions are not valid semver (must be X.Y.Z)- assert!(!match_property(- &property,- &HashMap::from([("version".to_string(), json!("3.7"))]),- true- )- .expect("expected match to exist"));-- assert!(!match_property(- &property,- &HashMap::from([("version".to_string(), json!("3.0"))]),- true- )- .expect("expected match to exist"));-// Too many version components (common in .NET)assert!(!match_property(&property,@@ -2607,6 +2643,146 @@ mod test_match_properties {.is_err());}+ #[test]+ fn test_semver_zero_padded_and_short_form_versions() {+ // These shapes used to be rejected as invalid semver; canonicalization now accepts+ // them, so they're covered separately from `test_semver_invalid_versions`.+ let property = PropertyFilter {+ key: "version".to_string(),+ value: Some(json!("1.0.0")),+ operator: Some(OperatorType::SemverGt),+ prop_type: PropertyType::Person,+ group_type_index: None,+ negation: None,+ compiled_regex: None,+ extra: Default::default(),+ };++ // Zero-padded components are canonicalized ("01.02.03" -> "1.2.3"), which many+ // mobile SDKs emit. 1.2.3 > 1.0.0, so this matches.+ assert!(match_property(+ &property,+ &HashMap::from([("version".to_string(), json!("01.02.03"))]),+ true+ )+ .expect("expected match to exist"));++ // A leading zero in a single component is canonicalized too ("3.07" -> "3.7.0").+ assert!(match_property(+ &property,+ &HashMap::from([("version".to_string(), json!("3.07"))]),+ true+ )+ .expect("expected match to exist"));++ // Two-part versions get a padded patch component ("3.7" -> "3.7.0"), the common+ // shape mobile SDKs emit. 3.7.0 > 1.0.0, so this matches.+ assert!(match_property(+ &property,+ &HashMap::from([("version".to_string(), json!("3.7"))]),+ true+ )+ .expect("expected match to exist"));++ assert!(match_property(+ &property,+ &HashMap::from([("version".to_string(), json!("3.0"))]),+ true+ )+ .expect("expected match to exist"));+ }++ #[test]+ fn test_semver_mobile_version_formats() {+ // Mobile SDKs commonly emit two-component versions ("3.10") and zero-padded+ // components ("3.08"), neither of which is strict semver. Before canonicalization+ // these silently failed to parse, so version-gated flag conditions never matched.++ // "3.08+" is the reported enterprise case: property "3.08" (-> 3.8.0) satisfies+ // a SemverGte "3.08" (-> 3.8.0) condition.+ let gte_308 = PropertyFilter {+ key: "version".to_string(),+ value: Some(json!("3.08")),+ operator: Some(OperatorType::SemverGte),+ prop_type: PropertyType::Person,+ group_type_index: None,+ negation: None,+ compiled_regex: None,+ extra: Default::default(),+ };++ for version in ["3.08", "3.8", "3.8.0", "3.9", "3.10", "4.0.0"] {+ assert!(+ match_property(+ >e_308,+ &HashMap::from([("version".to_string(), json!(version))]),+ true+ )+ .expect("expected match to exist"),+ "expected {version} to satisfy SemverGte 3.08"+ );+ }++ for version in ["3.07", "3.7", "2.48", "3.0"] {+ assert!(+ !match_property(+ >e_308,+ &HashMap::from([("version".to_string(), json!(version))]),+ true+ )+ .expect("expected match to exist"),+ "expected {version} to not satisfy SemverGte 3.08"+ );+ }++ // Two-component ordering: 3.10 (-> 3.10.0) is greater than 3.9 (-> 3.9.0), not a+ // string comparison where "3.10" < "3.9".+ let gt_39 = PropertyFilter {+ key: "version".to_string(),+ value: Some(json!("3.9")),+ operator: Some(OperatorType::SemverGt),+ prop_type: PropertyType::Person,+ group_type_index: None,+ negation: None,+ compiled_regex: None,+ extra: Default::default(),+ };++ assert!(match_property(+ >_39,+ &HashMap::from([("version".to_string(), json!("3.10"))]),+ true+ )+ .expect("expected match to exist"));++ // Canonicalization also applies to the filter value in tilde/caret ranges.+ let tilde_308 = PropertyFilter {+ key: "version".to_string(),+ value: Some(json!("3.08")),+ operator: Some(OperatorType::SemverTilde),+ prop_type: PropertyType::Person,+ group_type_index: None,+ negation: None,+ compiled_regex: None,+ extra: Default::default(),+ };++ // ~3.08 (-> ~3.8.0) means >=3.8.0 <3.9.0+ assert!(match_property(+ &tilde_308,+ &HashMap::from([("version".to_string(), json!("3.8.5"))]),+ true+ )+ .expect("expected match to exist"));++ assert!(!match_property(+ &tilde_308,+ &HashMap::from([("version".to_string(), json!("3.9.0"))]),+ true+ )+ .expect("expected match to exist"));+ }+#[test]fn test_semver_missing_property() {let property = PropertyFilter {@@ -2958,6 +3134,66 @@ mod test_match_properties {true).is_err());++ // Mobile SDKs emit zero-padded/two-part versions on the property side too.+ let property_mobile_wildcard = PropertyFilter {+ key: "version".to_string(),+ value: Some(json!("3.*")),+ operator: Some(OperatorType::SemverWildcard),+ prop_type: PropertyType::Person,+ group_type_index: None,+ negation: None,+ compiled_regex: None,+ extra: Default::default(),+ };++ assert!(match_property(+ &property_mobile_wildcard,+ &HashMap::from([("version".to_string(), json!("3.08"))]),+ true+ )+ .expect("expected match to exist"));++ assert!(match_property(+ &property_mobile_wildcard,+ &HashMap::from([("version".to_string(), json!("3.10"))]),+ true+ )+ .expect("expected match to exist"));++ assert!(!match_property(+ &property_mobile_wildcard,+ &HashMap::from([("version".to_string(), json!("2.9"))]),+ true+ )+ .expect("expected match to exist"));++ // The filter value itself can be zero-padded ("3.08.*"), which needs the same+ // canonicalization as "~3.08"/"^3.08" to parse as a VersionReq.+ let property_zero_padded_filter = PropertyFilter {+ key: "version".to_string(),+ value: Some(json!("3.08.*")),+ operator: Some(OperatorType::SemverWildcard),+ prop_type: PropertyType::Person,+ group_type_index: None,+ negation: None,+ compiled_regex: None,+ extra: Default::default(),+ };++ assert!(match_property(+ &property_zero_padded_filter,+ &HashMap::from([("version".to_string(), json!("3.8.5"))]),+ true+ )+ .expect("expected match to exist"));++ assert!(!match_property(+ &property_zero_padded_filter,+ &HashMap::from([("version".to_string(), json!("3.9.0"))]),+ true+ )+ .expect("expected match to exist"));}#[test]