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.

1.2 KB
Raw Blame
from collections.abc import Iterator
from contextlib import contextmanager
from itertools import count
from typing import Any

from unittest.mock import patch

from products.security.backend.logic.snapshot import reset_memo, write_snapshot

_ids = count(1)


def _rule(overrides: dict[str, Any], **defaults: Any) -> dict[str, Any]:
    return {"id": f"01920000-0000-7000-8000-{next(_ids):012d}", "expiresAt": None, **defaults, **overrides}


def block_rule(**overrides: Any) -> dict[str, Any]:
    return _rule(overrides, targetType="email", targetValue="[email protected]", effect="block", scope="all_access")


def exempt_rule(**overrides: Any) -> dict[str, Any]:
    return _rule(overrides, targetType="email", targetValue="[email protected]", effect="exempt", scope="email_code")


def seed_rules(*rules: dict[str, Any]) -> None:
    write_snapshot(f"test-{next(_ids)}", list(rules))
    reset_memo()


@contextmanager
def enforcing(*surfaces: str) -> Iterator[None]:
    """Turns refusals on for these surfaces, as the enforcement flag's payload would."""
    with patch(
        "products.security.backend.logic.enforcement._enforcement_payload",
        return_value=dict.fromkeys(surfaces, True),
    ):
        yield