: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
fix(oauth): delete refresh tokens orphaned by an access token revoke (#85861)
Matt Brookerde493f1be58d
diff --git a/products/growth/dags/oauth.py b/products/growth/dags/oauth.pyindex e31f2ba2157f..71dd960c9348 100644--- a/products/growth/dags/oauth.py+++ b/products/growth/dags/oauth.py@@ -76,6 +76,14 @@ def clear_expired_oauth_tokens(context: dagster.OpExecutionContext) -> None:{"revoked": Q(revoked__lt=retention_cutoff),"expired_via_access_token": Q(access_token__expires__lt=refresh_token_expiry_cutoff),+ # Revoking an access token deletes the row, and this side of the OneToOne is+ # SET_NULL, so a refresh token can lose the join the query above reaches it by+ # while staying valid: validate_refresh_token never compares against created.+ "expired_orphaned": Q(+ access_token__isnull=True,+ revoked__isnull=True,+ created__lt=refresh_token_expiry_cutoff,+ ),},),(diff --git a/products/growth/dags/tests/test_oauth.py b/products/growth/dags/tests/test_oauth.pyindex 41370dbf0bed..0a9c445b6d9c 100644--- a/products/growth/dags/tests/test_oauth.py+++ b/products/growth/dags/tests/test_oauth.py@@ -208,6 +208,46 @@ class TestBatchDeleteFunctionality(TestCase):self.assertTrue(OAuthAccessToken.objects.filter(id=recent_expired_token.id).exists())self.assertFalse(OAuthGrant.objects.filter(id=old_grant.id).exists())+ def test_clear_expired_oauth_tokens_deletes_orphaned_refresh_tokens(self):+ """A refresh token whose access token was revoked keeps working, so age has to reach it."""+ now = timezone.now()+ beyond_expiry = now - timedelta(days=95)++ access_token = OAuthAccessToken.objects.create(+ user=self.user,+ application=self.oauth_application,+ token="orphaning_access_token",+ expires=now + timedelta(hours=1),+ scope="read",+ )+ orphaned = OAuthRefreshToken.objects.create(+ user=self.user, application=self.oauth_application, token="orphaned_refresh", access_token=access_token+ )+ recent_orphan = OAuthRefreshToken.objects.create(+ user=self.user, application=self.oauth_application, token="recent_orphaned_refresh"+ )+ revoked_within_retention = OAuthRefreshToken.objects.create(+ user=self.user,+ application=self.oauth_application,+ token="revoked_recently",+ revoked=now - timedelta(minutes=5),+ )++ # revoke() nulls the FK without setting `revoked`, which is the state under test.+ access_token.revoke()+ OAuthRefreshToken.objects.filter(id=orphaned.id).update(created=beyond_expiry)+ OAuthRefreshToken.objects.filter(id=revoked_within_retention.id).update(created=beyond_expiry)++ orphaned.refresh_from_db()+ self.assertIsNone(orphaned.access_token)+ self.assertIsNone(orphaned.revoked)++ clear_expired_oauth_tokens(dagster.build_op_context())++ self.assertFalse(OAuthRefreshToken.objects.filter(id=orphaned.id).exists())+ self.assertTrue(OAuthRefreshToken.objects.filter(id=recent_orphan.id).exists())+ self.assertTrue(OAuthRefreshToken.objects.filter(id=revoked_within_retention.id).exists())+@unittest.mock.patch("products.growth.dags.oauth.batch_delete_model")def test_clear_expired_oauth_tokens_metadata_output(self, mock_batch_delete):mock_batch_delete.return_value = 5