: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
"""What a subject's matching rules mean for one surface."""
from datetime import datetime
from ..facade.enums import Surface
from .matching import RuleIndex, candidates, rule_matches
from .rules import SnapshotRule
from .subjects import Subject
SCOPE_SURFACES: dict[str, frozenset[Surface]] = {
"all_access": frozenset({Surface.SIGNUP, Surface.APP, Surface.AI_GATEWAY}),
"signup": frozenset({Surface.SIGNUP}),
"ai_gateway": frozenset({Surface.AI_GATEWAY}),
"email_code": frozenset({Surface.EMAIL_CODE}),
"signup_risk": frozenset({Surface.SIGNUP_RISK}),
}
# The effect a rule needs to decide each surface.
SURFACE_EFFECT: dict[Surface, str] = {
Surface.SIGNUP: "block",
Surface.APP: "block",
Surface.AI_GATEWAY: "block",
Surface.EMAIL_CODE: "exempt",
Surface.SIGNUP_RISK: "exempt",
}
# Target types a surface accepts, where accepting every type would be too broad. The hub
# refuses these combinations when a rule is written; refusing them again here means a
# widening there cannot switch off a protection without a change on this side too. A
# surface absent from this map accepts any target type.
SURFACE_TARGET_TYPES: dict[Surface, frozenset[str]] = {
Surface.SIGNUP_RISK: frozenset({"email", "email_domain"}),
}
PROTECTED_DOMAIN = "posthog.com"
def is_protected_domain(domain: str | None) -> bool:
return domain is not None and (domain == PROTECTED_DOMAIN or domain.endswith(f".{PROTECTED_DOMAIN}"))
def _is_active(rule: SnapshotRule, now: datetime) -> bool:
return rule.expires_at is None or rule.expires_at > now
def deciding_rule(index: RuleIndex, subject: Subject, surface: Surface, now: datetime) -> SnapshotRule | None:
"""The oldest active rule that decides this surface, or None when the subject is allowed."""
effect = SURFACE_EFFECT[surface]
# A rule written before the hub's save-time check still never blocks a posthog.com
# account. Exemptions still apply to one.
if effect == "block" and is_protected_domain(subject.domain):
return None
allowed_targets = SURFACE_TARGET_TYPES.get(surface)
# Only blocks read the look-alike form: an exemption for one mailbox must not cover a different spelling.
variants = [subject] if effect != "block" or subject.lookalike is None else [subject, subject.lookalike]
matched = [
rule
for variant in variants
for rule in candidates(index, variant)
if rule.effect == effect
and (allowed_targets is None or rule.target_type in allowed_targets)
and surface in SCOPE_SURFACES.get(rule.scope, frozenset())
and _is_active(rule, now)
and rule_matches(rule, variant)
]
return min(matched, key=lambda rule: rule.id, default=None)