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.

chore(flags): ban raw FeatureFlag.filters access outside the flag product (#70128)

Anders066f5fc6dc9e
diff --git a/.semgrep/rules/devex/feature-flags-no-raw-filters-access.py b/.semgrep/rules/devex/feature-flags-no-raw-filters-access.py
new file mode 100644
index 000000000000..7b9f93de5ac8
--- /dev/null
+++ b/.semgrep/rules/devex/feature-flags-no-raw-filters-access.py
@@ -0,0 +1,55 @@
+# Test cases for feature-flags-no-raw-filters-access.
+# ruff: noqa
+
+
+class FeatureFlag:
+ filters: dict = {}
+ variants: list = []
+
+
+feature_flag = FeatureFlag()
+flag = FeatureFlag()
+existing_targeting_flag = FeatureFlag()
+ff_record = FeatureFlag()
+some_ff = FeatureFlag()
+dashboard = object()
+
+
+class FlagContainer:
+ ff = FeatureFlag()
+
+
+instance = FlagContainer()
+
+# ruleid: feature-flags-no-raw-filters-access
+groups = feature_flag.filters["groups"]
+# ruleid: feature-flags-no-raw-filters-access
+multivariate = flag.filters.get("multivariate", {})
+# ruleid: feature-flags-no-raw-filters-access
+variants = feature_flag.filters["multivariate"]["variants"]
+# ruleid: feature-flags-no-raw-filters-access
+aggregation = existing_targeting_flag.filters.get("aggregation_group_type_index")
+# ruleid: feature-flags-no-raw-filters-access
+feature_flag.filters = {"groups": []}
+# ruleid: feature-flags-no-raw-filters-access
+flag.filters["groups"][0]["rollout_percentage"] = 100
+# ruleid: feature-flags-no-raw-filters-access
+flag.filters.update({"payloads": {}})
+# ruleid: feature-flags-no-raw-filters-access
+ff_record.filters.get("groups")
+# ruleid: feature-flags-no-raw-filters-access
+some_ff.filters["groups"]
+# ruleid: feature-flags-no-raw-filters-access
+instance.ff.filters["groups"]
+# ruleid: feature-flags-no-raw-filters-access
+flag.filters |= {"groups": []}
+
+# Public model accessors are fine
+# ok: feature-flags-no-raw-filters-access
+variants = feature_flag.variants
+# Unrelated .filters attributes on non-flag objects are fine
+# ok: feature-flags-no-raw-filters-access
+date_from = dashboard.filters["date_from"]
+# Passing the blob around without digging in is not flagged
+# ok: feature-flags-no-raw-filters-access
+blob = feature_flag.filters
diff --git a/.semgrep/rules/devex/feature-flags-no-raw-filters-access.yaml b/.semgrep/rules/devex/feature-flags-no-raw-filters-access.yaml
new file mode 100644
index 000000000000..75d9911e1aee
--- /dev/null
+++ b/.semgrep/rules/devex/feature-flags-no-raw-filters-access.yaml
@@ -0,0 +1,64 @@
+rules:
+ - id: feature-flags-no-raw-filters-access
+ message: |
+ Raw access to `FeatureFlag.filters` internals outside `products/feature_flags/`.
+ The `filters` JSON shape (groups, multivariate, payloads, super_groups, holdout_groups)
+ is owned by the feature flags product and is changing with rule-level flags (RFC #1097).
+
+ Read through the model's public accessors (`flag.variants`,
+ `flag.aggregation_group_type_index`, `flag.get_payload(...)`) or a facade capability in
+ `products/feature_flags/backend/facade/` instead of digging into the JSON, and write
+ through the facade's gated write path instead of mutating `flag.filters` directly.
+
+ Files listed under `paths.exclude` are the pre-existing offender baseline being migrated
+ behind the facade — do not add new ones. For a genuine exception, add
+ `# nosemgrep: feature-flags-no-raw-filters-access -- <reason>` on the line.
+ severity: ERROR
+ languages: [python]
+ paths:
+ include:
+ - 'common/**/*.py'
+ - 'ee/**/*.py'
+ - 'posthog/**/*.py'
+ - 'products/**/*.py'
+ exclude:
+ # The flag product owns the filters schema
+ - '**/products/feature_flags/**'
+ # Test fixtures may build filters directly
+ - '**/test/**'
+ - '**/tests/**'
+ - '**/test_*.py'
+ - '**/*_test.py'
+ - '**/conftest*.py'
+ - '**/migrations/**'
+ # Baseline: pre-existing offenders being migrated behind the flag
+ # facade. Shrink this list — never grow it.
+ - '/ee/hogai/context/experiment/context.py'
+ - '/posthog/api/web_experiment.py'
+ - '/posthog/management/commands/fix_invalid_flag_property_types.py'
+ - '/posthog/models/product_intent/product_intent.py'
+ - '/posthog/tasks/update_survey_adaptive_sampling.py'
+ - '/posthog/tasks/update_survey_iteration.py'
+ - '/products/early_access_features/backend/api.py'
+ - '/products/early_access_features/backend/apps.py'
+ - '/products/experiments/backend/experiment_service.py'
+ - '/products/experiments/backend/experiment_summary_data_service.py'
+ - '/products/experiments/backend/hogql_queries/experiment_query_runner.py'
+ - '/products/experiments/backend/max_tools.py'
+ - '/products/product_tours/backend/api/product_tour.py'
+ patterns:
+ - pattern-either:
+ - pattern: $FLAG.filters[$KEY]
+ - pattern: $FLAG.filters.get(...)
+ - pattern: $FLAG.filters.setdefault(...)
+ - pattern: $FLAG.filters.pop(...)
+ - pattern: $FLAG.filters.update(...)
+ - pattern: $FLAG.filters = $V
+ - pattern: $FLAG.filters |= $V
+ - metavariable-regex:
+ metavariable: $FLAG
+ regex: (?i)^(.*flag.*|(?:.*[._])?ff(?:_.*)?)$
+ metadata:
+ category: best-practice
+ subcategory:
+ - audit