: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
feat(oauth): add hogli client metadata document (#79086)
Raúl Negrón-Otero3dbffe43b0b8
diff --git a/posthog/api/oauth/hogli_metadata.py b/posthog/api/oauth/hogli_metadata.pynew file mode 100644index 000000000000..5aa6e926a8b2--- /dev/null+++ b/posthog/api/oauth/hogli_metadata.py@@ -0,0 +1,48 @@+from django.conf import settings+from django.http import JsonResponse+from django.views import View++HOGLI_METADATA_PATH = "api/oauth/hogli/client-metadata"++# Scope ceiling: /authorize clamps to this, so keep it to what a hogli command actually reads.+HOGLI_SCOPES = [+ "engineering_analytics:read",+]++# Tracked on `main` rather than pinned to a commit: a moved file only hides the image, while a+# pin would keep serving stale art through a rebrand.+HOGLI_LOGO_URI = "https://raw.githubusercontent.com/PostHog/brand/main/assets/crests/full/png/dev-experience.png"+++class HogliClientMetadataView(View):+ """+ Serves a static CIMD (Client ID Metadata Document) for the hogli CLI.++ The client_id in the response is the canonical URL where this document is hosted,+ constructed from SITE_URL so it's correct on each region (US, EU, self-hosted).++ The redirect is registered without a port because RFC 8252 section 7.3 obliges the server+ to accept any port on a loopback address, which lets each login bind a free ephemeral port+ against this one entry.+ """++ http_method_names = ["get"]++ def get(self, request):+ client_id = f"{settings.SITE_URL}/{HOGLI_METADATA_PATH}"++ metadata = {+ "client_id": client_id,+ "client_name": "hogli CLI for PostHog",+ "logo_uri": HOGLI_LOGO_URI,+ "redirect_uris": ["http://127.0.0.1/callback"],+ "grant_types": ["authorization_code", "refresh_token"],+ "response_types": ["code"],+ "token_endpoint_auth_method": "none",+ "com.posthog": {"scopes": HOGLI_SCOPES},+ }++ response = JsonResponse(metadata)+ response["Cache-Control"] = "public, max-age=3600"+ response["Content-Type"] = "application/json"+ return responsediff --git a/posthog/api/oauth/test_hogli_metadata.py b/posthog/api/oauth/test_hogli_metadata.pynew file mode 100644index 000000000000..b7801bdcdc53--- /dev/null+++ b/posthog/api/oauth/test_hogli_metadata.py@@ -0,0 +1,124 @@+import json+from ipaddress import ip_address+from urllib.parse import urlparse++from posthog.test.base import APIBaseTest+from unittest.mock import MagicMock, patch++from django.test import SimpleTestCase, override_settings++from oauth2_provider.models import AbstractApplication+from parameterized import parameterized++from posthog.api.oauth.cimd import fetch_and_upsert_cimd_application, get_application_by_client_id+from posthog.api.oauth.hogli_metadata import HOGLI_LOGO_URI, HOGLI_SCOPES+from posthog.models.oauth import is_loopback_host+from posthog.scopes import UNPRIVILEGED_SCOPES+++def _mock_cimd_response(document: dict):+ body = json.dumps(document).encode()+ resp = MagicMock()+ resp.status_code = 200+ resp.headers = {}+ resp.is_redirect = False+ resp.is_permanent_redirect = False+ resp.close = MagicMock()+ resp.iter_content = MagicMock(return_value=iter([body]))+ return resp+++@override_settings(SITE_URL="https://us.posthog.com")+class TestHogliClientMetadataView(SimpleTestCase):+ def test_returns_valid_cimd_metadata(self):+ res = self.client.get("/api/oauth/hogli/client-metadata")+ assert res.status_code == 200+ assert res["Cache-Control"] == "public, max-age=3600"+ assert "application/json" in res["Content-Type"]++ data = res.json()+ assert data["client_name"] == "hogli CLI for PostHog"+ assert data["redirect_uris"] == ["http://127.0.0.1/callback"]+ assert data["grant_types"] == ["authorization_code", "refresh_token"]+ assert data["response_types"] == ["code"]+ assert data["token_endpoint_auth_method"] == "none"+ assert data["com.posthog"]["scopes"] == HOGLI_SCOPES++ @parameterized.expand(+ [+ ("https://us.posthog.com", "https://us.posthog.com/api/oauth/hogli/client-metadata"),+ ("https://eu.posthog.com", "https://eu.posthog.com/api/oauth/hogli/client-metadata"),+ ]+ )+ def test_client_id_is_built_from_site_url(self, site_url: str, expected_client_id: str):+ # hogli derives the same URL from whichever host it talks to, so a self-hosted+ # instance resolves its own document with no client registered for it.+ with override_settings(SITE_URL=site_url):+ res = self.client.get("/api/oauth/hogli/client-metadata")+ assert res.json()["client_id"] == expected_client_id++ def test_declared_scopes_are_unprivileged(self):+ # A scope outside this set makes CIMD reject the whole document (cimd.py, _resolve_scopes).+ assert set(HOGLI_SCOPES) <= set(UNPRIVILEGED_SCOPES)++ def test_redirect_is_a_portless_loopback_uri(self):+ # The registered *host* is the binding part: hogli must redirect to 127.0.0.1, because a+ # `localhost` request against this registration is rejected. The port is not load-bearing+ # either way (the port check is waived for a registered loopback literal, with or without+ # one), so portless is a convention that says so rather than a requirement.+ [redirect] = self.client.get("/api/oauth/hogli/client-metadata").json()["redirect_uris"]+ parsed = urlparse(redirect)+ assert parsed.scheme == "http"+ assert parsed.hostname == "127.0.0.1"+ assert parsed.port is None+ assert is_loopback_host(parsed.hostname)++ def test_logo_is_https_so_cimd_keeps_it(self):+ # cimd.py drops a non-HTTPS logo_uri instead of failing, shipping a logo-less screen.+ logo = self.client.get("/api/oauth/hogli/client-metadata").json()["logo_uri"]+ assert logo == HOGLI_LOGO_URI and logo.startswith("https://")++ def test_post_not_allowed(self):+ res = self.client.post("/api/oauth/hogli/client-metadata")+ assert res.status_code == 405+++@override_settings(SITE_URL="https://us.posthog.com")+class TestHogliClientMetadataRegistration(APIBaseTest):+ @patch("posthog.security.url_validation.resolve_host_ips", return_value={ip_address("93.184.216.34")})+ @patch("posthog.api.oauth.cimd.requests.Session.get")+ def test_document_registers_through_cimd(self, mock_get, _url_mock):+ # Serve the live document back through the CIMD fetch path, so the document and the+ # CIMD validation rules cannot drift apart. The fetch is patched because client_ids+ # must be HTTPS.+ document = self.client.get("/api/oauth/hogli/client-metadata").json()+ client_id = document["client_id"]+ mock_get.return_value = _mock_cimd_response(document)++ app = fetch_and_upsert_cimd_application(client_id)++ assert app is not None+ assert app.is_cimd_client+ assert app.cimd_metadata_url == client_id+ assert app.name == "hogli CLI for PostHog"+ assert app.client_type == AbstractApplication.CLIENT_PUBLIC+ assert app.authorization_grant_type == AbstractApplication.GRANT_AUTHORIZATION_CODE+ assert app.redirect_uris == " ".join(document["redirect_uris"])+ assert set(app.scopes) == set(HOGLI_SCOPES)+ assert app.logo_uri == HOGLI_LOGO_URI++ assert get_application_by_client_id(client_id).pk == app.pk++ @patch("posthog.security.url_validation.resolve_host_ips", return_value={ip_address("93.184.216.34")})+ @patch("posthog.api.oauth.cimd.requests.Session.get")+ def test_registration_leaves_verification_to_a_human(self, mock_get, _url_mock):+ # Registration must not self-certify: the unverified warning only means something+ # while `is_verified` stays staff-set.+ document = self.client.get("/api/oauth/hogli/client-metadata").json()+ mock_get.return_value = _mock_cimd_response(document)++ app = fetch_and_upsert_cimd_application(document["client_id"])++ assert app is not None+ assert app.is_verified is False+ assert app.skip_authorization is Falsediff --git a/posthog/urls.py b/posthog/urls.pyindex dff5cd7fbe45..c2cc0865e395 100644--- a/posthog/urls.py+++ b/posthog/urls.py@@ -34,6 +34,7 @@ from posthog.api import ()from posthog.api.github_callback.views import github_oauth_callback, github_setup_callbackfrom posthog.api.oauth.connected_apps import ConnectedAppsViewSet+from posthog.api.oauth.hogli_metadata import HOGLI_METADATA_PATH, HogliClientMetadataViewfrom posthog.api.oauth.raycast_metadata import RAYCAST_METADATA_PATH, RaycastClientMetadataViewfrom posthog.api.oauth.wizard_metadata import WIZARD_METADATA_PATH, WizardClientMetadataViewfrom posthog.api.sdk_health import sdk_health@@ -632,6 +633,11 @@ urlpatterns = [RaycastClientMetadataView.as_view(),name="raycast-client-metadata",),+ path(+ HOGLI_METADATA_PATH,+ HogliClientMetadataView.as_view(),+ name="hogli-client-metadata",+ ),re_path(r"^api.+", api_not_found),path("authorize_and_redirect/", login_required(authorize_and_redirect)),path("integrations/connect/<str:kind>/", login_required(integration_connect_redirect)),