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.

chore(workflows): count push identity tokens that need the shared-secret fallback (#103600)

Daniel Marchuk07e0a212bc62
diff --git a/products/messaging/backend/api/push_identity_tokens.py b/products/messaging/backend/api/push_identity_tokens.py
index 3fc4625e12ea..82e8df0ef79d 100644
--- a/products/messaging/backend/api/push_identity_tokens.py
+++ b/products/messaging/backend/api/push_identity_tokens.py
@@ -32,6 +32,7 @@ secret only with HS256) to close the classic JWT algorithm-confusion attack.
from datetime import UTC, datetime, timedelta
import jwt
+from prometheus_client import Counter
from posthog.models.team.team import Team
@@ -39,6 +40,14 @@ PUSH_IDENTITY_TOKEN_AUDIENCE = "posthog:push_identity"
_HMAC_ALGORITHM = "HS256"
_ASYMMETRIC_ALGORITHM = "ES256"
+# Counts the registrations that verified only through the HS256 fallback, which is what says whether
+# the scheme can be withdrawn. Channel config cannot answer that on its own, because a channel that
+# registers a public key still reaches the fallback when the customer signs with the shared secret.
+PUSH_IDENTITY_HMAC_FALLBACK_COUNTER = Counter(
+ "push_subscription_identity_hmac_fallback",
+ "Identity tokens accepted only by the deprecated shared-secret scheme.",
+)
+
# Short TTL: the token only needs to survive the round trip from the customer's backend, through the
# app, to the registration call. Keeping it small bounds the replay window (a replay can only re-assert
# the same (distinct_id, app_id) binding the legitimate user already holds, so the value is low anyway).
@@ -105,6 +114,7 @@ def verify_push_identity_token(
return True
for secret in (team.secret_api_token, team.secret_api_token_backup):
if secret and _decode_matches(token, secret, _HMAC_ALGORITHM, distinct_id, app_id):
+ PUSH_IDENTITY_HMAC_FALLBACK_COUNTER.inc()
return True
return False
diff --git a/products/messaging/backend/api/test/test_push_identity_tokens.py b/products/messaging/backend/api/test/test_push_identity_tokens.py
index dc943ab9a181..81b0fa194546 100644
--- a/products/messaging/backend/api/test/test_push_identity_tokens.py
+++ b/products/messaging/backend/api/test/test_push_identity_tokens.py
@@ -10,6 +10,7 @@ import jwt
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ec
from parameterized import parameterized
+from prometheus_client import REGISTRY
from posthog.models.team.team import Team
@@ -60,9 +61,14 @@ class TestPushIdentityTokens(SimpleTestCase):
def _team(self, secret: str | None = CURRENT_SECRET, backup: str | None = None) -> Team:
return Team(secret_api_token=secret, secret_api_token_backup=backup)
+ def _fallback_count(self) -> float:
+ return REGISTRY.get_sample_value("push_subscription_identity_hmac_fallback_total") or 0.0
+
def test_verifies_a_token_signed_with_the_current_secret(self):
token = sign_push_identity_token(CURRENT_SECRET, DISTINCT_ID, APP_ID)
+ before = self._fallback_count()
assert verify_push_identity_token(token, self._team(), DISTINCT_ID, APP_ID) is True
+ assert self._fallback_count() == before + 1
def test_verifies_a_token_signed_with_the_backup_secret_after_rotation(self):
token = sign_push_identity_token(BACKUP_SECRET, DISTINCT_ID, APP_ID)
@@ -113,6 +119,17 @@ class TestPushIdentityTokens(SimpleTestCase):
team = self._team(secret=None) # no shared secret: only the public-key path can accept it
assert verify_push_identity_token(token, team, DISTINCT_ID, APP_ID, public_keys=[public_pem]) is True
+ def test_an_es256_token_does_not_count_as_using_the_shared_secret(self):
+ # A channel can register a public key while its team still holds a shared secret. Counting
+ # that as a fallback use would keep the scheme alive for a customer who never signs with it.
+ private_pem, public_pem = _es256_keypair()
+ token = sign_push_identity_token_es256(private_pem, DISTINCT_ID, APP_ID)
+ team = self._team(secret=CURRENT_SECRET)
+ before = self._fallback_count()
+
+ assert verify_push_identity_token(token, team, DISTINCT_ID, APP_ID, public_keys=[public_pem]) is True
+ assert self._fallback_count() == before
+
def test_rejects_an_es256_token_when_a_different_public_key_is_registered(self):
signer_private, _ = _es256_keypair()
_, registered_public = _es256_keypair()