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.

3.3 KB
Raw Blame
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.
          `flag.get_filters()` and `(flag.filters or {})` return the same raw JSON.
          `posthog/test/repo_invariants/test_feature_flag_raw_reads.py` also covers the flag
          product, aliases, serialized flags and ORM and SQL reads of the column.

          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/entity_search/context.py'
              - '/posthog/management/commands/reencrypt_flag_payloads.py'
              - '/products/experiments/backend/experiment_service.py'
              - '/products/experiments/backend/hogql_queries/experiment_exposures_query_runner.py'
              - '/products/experiments/backend/replay_linkage.py'
      pattern-either:
          - 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
                      - pattern: ($FLAG.filters or $DEFAULT)[$KEY]
                      - pattern: ($FLAG.filters or $DEFAULT).get(...)
                - metavariable-regex:
                      metavariable: $FLAG
                      regex: (?i)^(.*flag.*|(?:.*[._])?ff(?:_.*)?)$
          # Only FeatureFlag defines a get_filters() that takes no arguments.
          - pattern: $F.get_filters()[$KEY]
          - pattern: $F.get_filters().get(...)
      metadata:
          category: best-practice
          subcategory:
              - audit