: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
chore(authorization): report direct legacy permission checks (#129384)
Reece Jonesfa21772c7023
diff --git a/.github/workflows/ci-security.yaml b/.github/workflows/ci-security.yamlindex f0b17f6ccb45..3d7235a35fe9 100644--- a/.github/workflows/ci-security.yaml+++ b/.github/workflows/ci-security.yaml@@ -518,6 +518,21 @@ jobs:--timeout 300 \bin/ common/ docs/onboarding/ ee/ frontend/ hog/ packages/ posthog/ products/ services/ tools/+ - name: Authorization migration (informational)+ run: |+ if [ ! -f .semgrep/rules/devex/authorization-through-sdk.yaml ]; then+ echo "authorization-through-sdk not present on this branch yet, skipping (rebase to pick it up)"+ exit 0+ fi+ docker run --rm -v "${{ github.workspace }}:/src" -w /src \+ -e SEMGREP_ENABLE_VERSION_CHECK=false \+ ${{ env.SEMGREP_IMAGE }} \+ semgrep \+ --config .semgrep/rules/devex/authorization-through-sdk.yaml \+ --metrics=off \+ --timeout 300 \+ common/ ee/ posthog/ products/+# Pass 2: enforce ERROR-severity rules. Any finding fails the# job — regression guards live here.- name: Errors (blocking)diff --git a/.semgrep/rules/devex/authorization-through-sdk.py b/.semgrep/rules/devex/authorization-through-sdk.pynew file mode 100644index 000000000000..a5d1b50fbbba--- /dev/null+++ b/.semgrep/rules/devex/authorization-through-sdk.py@@ -0,0 +1,81 @@+# ruff: noqa+from posthog.models import OrganizationMembership+from posthog.models import OrganizationMembership as Membership+from posthog.user_permissions import UserPermissions as Permissions++from products.access_control.backend.facade.user_access_control import UserAccessControl as Access+from products.authorization.backend.facade import api as authorization+from products.authorization.backend.facade.enums import Action+++def direct_authorization(user, team, obj, view, membership):+ # ruleid: authorization-through-sdk+ access = Access(user=user, team=team)+ # ruleid: authorization-through-sdk+ permissions = Permissions(user)+ # ruleid: authorization-through-sdk+ access.check_access_level_for_object(obj, "editor")+ # ruleid: authorization-through-sdk+ view.user_access_control.check_access_level_for_resource("insight", "viewer")+ # ruleid: authorization-through-sdk+ view.user_access_control.assert_access_level_for_resource("insight", "viewer")+ # ruleid: authorization-through-sdk+ access.check_access_level_for_ref(ref, "viewer")+ # ruleid: authorization-through-sdk+ access.check_can_modify_access_levels_for_object(obj)+ # ruleid: authorization-through-sdk+ access.check_can_modify_access_levels_for_ref(ref)+ # ruleid: authorization-through-sdk+ access.get_user_access_level(obj)+ # ruleid: authorization-through-sdk+ access.specific_access_level_for_object(obj)+ # ruleid: authorization-through-sdk+ access.access_level_for_resource("insight")+ # ruleid: authorization-through-sdk+ access.effective_access_level_for_resource("insight")+ # ruleid: authorization-through-sdk+ access.filter_queryset_by_access_level(queryset)+ # ruleid: authorization-through-sdk+ access.filter_and_annotate_file_system_queryset(queryset)+ # ruleid: authorization-through-sdk+ access.allowed_object_ids("insight")+ # ruleid: authorization-through-sdk+ access.denied_object_ids("insight")+ # ruleid: authorization-through-sdk+ access.bulk_object_access_levels(objects)+ # ruleid: authorization-through-sdk+ access.is_organization_admin+ # ruleid: authorization-through-sdk+ view.user_permissions.team(team).effective_membership_level+ # ruleid: authorization-through-sdk+ access.blocked_resource_ids_by_scope+ # ruleid: authorization-through-sdk+ access.allowlisted_resource_ids_by_scope+ # ruleid: authorization-through-sdk+ access.blocked_resources+ # ruleid: authorization-through-sdk+ membership.level >= OrganizationMembership.Level.ADMIN+ # ruleid: authorization-through-sdk+ OrganizationMembership.Level.MEMBER <= membership.level+ # ruleid: authorization-through-sdk+ membership.level in [OrganizationMembership.Level.ADMIN, OrganizationMembership.Level.OWNER]+ # ruleid: authorization-through-sdk+ membership.level >= Membership.Level.ADMIN+++def sdk_authorization(resource, subject):+ # ok: authorization-through-sdk+ authorization.check(resource=resource, action=Action.EDIT, subject=subject)+ # ok: authorization-through-sdk+ authorization.permissions(resource=resource, subject=subject)+ # ok: authorization-through-sdk+ authorization.list_objects(resource=resource, action=Action.VIEW, subject=subject)+ # ok: authorization-through-sdk+ membership = OrganizationMembership(level=OrganizationMembership.Level.MEMBER)+ # ok: authorization-through-sdk+ level = OrganizationMembership.Level.ADMIN+ # ok: authorization-through-sdk+ resource.id == other.id+ # ok: authorization-through-sdk+ access.has_project_access()+ return membership, leveldiff --git a/.semgrep/rules/devex/authorization-through-sdk.yaml b/.semgrep/rules/devex/authorization-through-sdk.yamlnew file mode 100644index 000000000000..f8b3638d798c--- /dev/null+++ b/.semgrep/rules/devex/authorization-through-sdk.yaml@@ -0,0 +1,73 @@+rules:+ # INFO keeps this migration report out of the blocking WARNING and ERROR passes.+ - id: authorization-through-sdk+ message: |+ Make authorization decisions through products.authorization.backend.facade.api.+ Use check for a decision, permissions for allowed actions, and the authorization+ presentation adapter for QuerySet filters. Build object resources with+ products.authorization.backend.presentation.resources.resource_for_object so+ creator and fallback-parent access are preserved. Background work must pass+ an explicit subject. This migration report is informational until the direct+ v1 callers have moved to the SDK.+ languages: [python]+ severity: INFO+ paths:+ include:+ - /common/**/*.py+ - /ee/**/*.py+ - /posthog/**/*.py+ - /products/**/*.py+ exclude:+ - /products/authorization/**+ - /products/access_control/**+ - /posthog/user_permissions.py+ - '**/test/**'+ - '**/tests/**'+ - '**/test_*.py'+ - '**/*_test.py'+ - '**/conftest*.py'+ - '**/migrations/**'+ pattern-either:+ - pattern: products.access_control.backend.facade.user_access_control.UserAccessControl(...)+ - pattern: posthog.user_permissions.UserPermissions(...)+ - patterns:+ - pattern: $ACCESS.$METHOD(...)+ - metavariable-regex:+ metavariable: $METHOD+ regex: ^(check_access_level_for_(object|resource|ref)|assert_access_level_for_resource|check_can_modify_access_levels_for_(object|ref)|access_level_for_(object|resource|ref)|specific_access_level_for_object|effective_access_level_for_resource|get_user_access_level|filter_queryset_by_access_level|filter_and_annotate_file_system_queryset|allowed_object_ids|denied_object_ids|object_ids_matching|none_denied_object_ids|bulk_object_access_levels|has_resource_access|has_any_specific_access_for_resource|has_object_access_limit|has_access_levels_for_resource|resolve_most_specific_(object|resource)_access)$+ - pattern: $ACCESS.is_organization_admin+ - pattern: $ACCESS.effective_membership_level+ - pattern: $ACCESS.blocked_resource_ids_by_scope+ - pattern: $ACCESS.allowlisted_resource_ids_by_scope+ - pattern: $ACCESS.blocked_resources+ - pattern: $VALUE == posthog.models.OrganizationMembership.Level.$LEVEL+ - pattern: $VALUE != posthog.models.OrganizationMembership.Level.$LEVEL+ - pattern: $VALUE < posthog.models.OrganizationMembership.Level.$LEVEL+ - pattern: posthog.models.OrganizationMembership.Level.$LEVEL < $VALUE+ - pattern: $VALUE <= posthog.models.OrganizationMembership.Level.$LEVEL+ - pattern: posthog.models.OrganizationMembership.Level.$LEVEL <= $VALUE+ - pattern: $VALUE > posthog.models.OrganizationMembership.Level.$LEVEL+ - pattern: posthog.models.OrganizationMembership.Level.$LEVEL > $VALUE+ - pattern: $VALUE >= posthog.models.OrganizationMembership.Level.$LEVEL+ - pattern: posthog.models.OrganizationMembership.Level.$LEVEL >= $VALUE+ - pattern: $VALUE in [..., posthog.models.OrganizationMembership.Level.$LEVEL, ...]+ - pattern: $VALUE in (..., posthog.models.OrganizationMembership.Level.$LEVEL, ...)+ - pattern: $VALUE not in [..., posthog.models.OrganizationMembership.Level.$LEVEL, ...]+ - pattern: $VALUE not in (..., posthog.models.OrganizationMembership.Level.$LEVEL, ...)+ - pattern: $VALUE == posthog.models.organization.OrganizationMembership.Level.$LEVEL+ - pattern: $VALUE != posthog.models.organization.OrganizationMembership.Level.$LEVEL+ - pattern: $VALUE < posthog.models.organization.OrganizationMembership.Level.$LEVEL+ - pattern: posthog.models.organization.OrganizationMembership.Level.$LEVEL < $VALUE+ - pattern: $VALUE <= posthog.models.organization.OrganizationMembership.Level.$LEVEL+ - pattern: posthog.models.organization.OrganizationMembership.Level.$LEVEL <= $VALUE+ - pattern: $VALUE > posthog.models.organization.OrganizationMembership.Level.$LEVEL+ - pattern: posthog.models.organization.OrganizationMembership.Level.$LEVEL > $VALUE+ - pattern: $VALUE >= posthog.models.organization.OrganizationMembership.Level.$LEVEL+ - pattern: posthog.models.organization.OrganizationMembership.Level.$LEVEL >= $VALUE+ - pattern: $VALUE in [..., posthog.models.organization.OrganizationMembership.Level.$LEVEL, ...]+ - pattern: $VALUE in (..., posthog.models.organization.OrganizationMembership.Level.$LEVEL, ...)+ - pattern: $VALUE not in [..., posthog.models.organization.OrganizationMembership.Level.$LEVEL, ...]+ - pattern: $VALUE not in (..., posthog.models.organization.OrganizationMembership.Level.$LEVEL, ...)+ metadata:+ category: best-practice+ confidence: MEDIUM