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.

feat(security): count every access decision, not only would-blocks (#110545)

Yasenf13248e3215f
diff --git a/products/security/backend/facade/api.py b/products/security/backend/facade/api.py
index b00ff5cd9903..b0c0d1f15a34 100644
--- a/products/security/backend/facade/api.py
+++ b/products/security/backend/facade/api.py
@@ -16,7 +16,7 @@ from ..logic.accounts import email_for_user
from ..logic.decisions import deciding_rule
from ..logic.snapshot import current_snapshot
from ..logic.subjects import normalize_subject
-from ..metrics import DECISION_ERRORS_COUNTER, WOULD_BLOCK_COUNTER
+from ..metrics import DECISION_ERRORS_COUNTER, DECISIONS_COUNTER, WOULD_BLOCK_COUNTER
from . import contracts
from .enums import Outcome, Surface
@@ -45,10 +45,19 @@ def decide(subject: contracts.SubjectInput, surface: Surface) -> contracts.Decis
)
+def _counted(decision: contracts.Decision, call_site: str) -> contracts.Decision:
+ """Records that a call site reached a decision, before anything branches on its outcome."""
+ DECISIONS_COUNTER.labels(surface=decision.surface.value, call_site=call_site, outcome=decision.outcome.value).inc()
+ return decision
+
+
def is_email_code_exempt(email: str) -> bool:
"""Whether a rule lets this address skip the emailed login code. Any failure keeps the code."""
try:
- return decide(contracts.SubjectInput(email=email), Surface.EMAIL_CODE).outcome == Outcome.EXEMPT
+ return (
+ _counted(decide(contracts.SubjectInput(email=email), Surface.EMAIL_CODE), "email_code").outcome
+ == Outcome.EXEMPT
+ )
except Exception:
logger.exception("security_email_code_exemption_check_failed")
DECISION_ERRORS_COUNTER.labels(call_site="email_code").inc()
@@ -58,7 +67,10 @@ def is_email_code_exempt(email: str) -> bool:
def is_signup_risk_exempt(email: str) -> bool:
"""Whether a rule lets this address skip the Radar verdict. Any failure keeps the verdict."""
try:
- return decide(contracts.SubjectInput(email=email), Surface.SIGNUP_RISK).outcome == Outcome.EXEMPT
+ return (
+ _counted(decide(contracts.SubjectInput(email=email), Surface.SIGNUP_RISK), "signup_risk").outcome
+ == Outcome.EXEMPT
+ )
except Exception:
logger.exception("security_signup_risk_exemption_check_failed")
DECISION_ERRORS_COUNTER.labels(call_site="signup_risk").inc()
@@ -68,7 +80,7 @@ def is_signup_risk_exempt(email: str) -> bool:
def shadow_check(subject: contracts.SubjectInput, surface: Surface, *, call_site: str) -> None:
"""Records what a block rule would do here, and changes nothing. Never raises."""
try:
- decision = decide(subject, surface)
+ decision = _counted(decide(subject, surface), call_site)
if decision.outcome != Outcome.BLOCK:
return
WOULD_BLOCK_COUNTER.labels(
diff --git a/products/security/backend/metrics.py b/products/security/backend/metrics.py
index 1ff3c33a0f02..f4af515c9097 100644
--- a/products/security/backend/metrics.py
+++ b/products/security/backend/metrics.py
@@ -5,6 +5,14 @@ WOULD_BLOCK_COUNTER = Counter(
"Requests an access rule would block, while blocks are logged only",
labelnames=["surface", "call_site", "target_type"],
)
+# Counts every completed decision, whatever its outcome. WOULD_BLOCK_COUNTER only moves on a
+# block, so an empty would-block series cannot tell "the check ran and nothing matched" from
+# "the check never ran". This counter is the denominator that tells them apart.
+DECISIONS_COUNTER = Counter(
+ "posthog_security_access_decisions_total",
+ "Access decisions evaluated, by surface, call site and outcome",
+ labelnames=["surface", "call_site", "outcome"],
+)
DECISION_ERRORS_COUNTER = Counter(
"posthog_security_access_decision_errors_total",
"Access decisions that raised and were treated as allow",
diff --git a/products/security/backend/tests/test_facade.py b/products/security/backend/tests/test_facade.py
index 625090ac084a..24aef57bbdb6 100644
--- a/products/security/backend/tests/test_facade.py
+++ b/products/security/backend/tests/test_facade.py
@@ -1,6 +1,7 @@
from posthog.test.base import BaseTest
from unittest.mock import patch
+from parameterized import parameterized
from prometheus_client import REGISTRY
from posthog.models import User
@@ -21,6 +22,16 @@ def _would_block(call_site: str) -> float:
)
+def _decisions(surface: str, call_site: str, outcome: str) -> float:
+ return (
+ REGISTRY.get_sample_value(
+ "posthog_security_access_decisions_total",
+ {"surface": surface, "call_site": call_site, "outcome": outcome},
+ )
+ or 0.0
+ )
+
+
class TestFacade(BaseTest):
def test_a_uuid_only_subject_is_resolved_before_the_posthog_check(self) -> None:
staff = User.objects.create_and_join(self.organization, "[email protected]", "password1234")
@@ -57,8 +68,39 @@ class TestFacade(BaseTest):
user = User.objects.create_and_join(self.organization, "[email protected]", "password1234")
seed_rules(block_rule(targetType="user_uuid", targetValue=str(user.uuid)))
before = _would_block("test_site")
+ blocked_before = _decisions("app", "test_site", "block")
shadow_check(SubjectInput(email=user.email, user_uuid=str(user.uuid)), Surface.APP, call_site="test_site")
assert _would_block("test_site") == before + 1
+ assert _decisions("app", "test_site", "block") == blocked_before + 1
+
+ # An allow must be counted too. When no block rule matches, every decision is an
+ # allow, so a counter that moved only on a block would have no series exactly when
+ # there is nothing to block, which is the case it exists to distinguish.
+ allowed_before = _decisions("app", "test_site", "allow")
+ shadow_check(SubjectInput(email="[email protected]"), Surface.APP, call_site="test_site")
+ assert _decisions("app", "test_site", "allow") == allowed_before + 1
+ # A decision that raised is an error, not an evaluation, and must not be counted twice.
+ allowed_before = _decisions("app", "test_site", "allow")
with patch("products.security.backend.facade.api.current_snapshot", side_effect=RuntimeError("boom")):
shadow_check(SubjectInput(email=user.email), Surface.APP, call_site="test_site")
+ assert _decisions("app", "test_site", "allow") == allowed_before
+
+ @parameterized.expand(
+ [
+ ("email_code exempt", "email_code", "email_code", "[email protected]", "exempt"),
+ ("email_code allow", "email_code", "email_code", "[email protected]", "allow"),
+ ("signup_risk exempt", "signup_risk", "signup_risk", "[email protected]", "exempt"),
+ ("signup_risk allow", "signup_risk", "signup_risk", "[email protected]", "allow"),
+ ]
+ )
+ def test_exemption_checks_count_every_decision(
+ self, _name: str, scope: str, surface: str, address: str, outcome: str
+ ) -> None:
+ # The exemption checks do not go through shadow_check, so they need their own count
+ # to show that they ran.
+ seed_rules(exempt_rule(targetValue="[email protected]", scope=scope))
+ check = is_email_code_exempt if surface == "email_code" else is_signup_risk_exempt
+ before = _decisions(surface, surface, outcome)
+ check(address)
+ assert _decisions(surface, surface, outcome) == before + 1