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(middleware): normalize path before impersonation block check (#58948)

Tom Piccirello0a4a05784d3b
diff --git a/posthog/middleware.py b/posthog/middleware.py
index 2f42390df401..2cf70d29512b 100644
--- a/posthog/middleware.py
+++ b/posthog/middleware.py
@@ -1265,7 +1265,11 @@ class ImpersonationBlockedPathsMiddleware:
)
def _is_path_blocked(self, path: str) -> bool:
- return any(path.startswith(blocked_path) for blocked_path in IMPERSONATION_BLOCKED_PATHS)
+ # DefaultRouterPlusPlus accepts an optional trailing slash, so /api/personal_api_keys
+ # and /api/personal_api_keys/ both reach the same viewset. Normalize before matching
+ # so the slashless form cannot bypass the block list.
+ normalized_path = path if path.endswith("/") else f"{path}/"
+ return any(normalized_path.startswith(blocked_path) for blocked_path in IMPERSONATION_BLOCKED_PATHS)
def _is_allowed_users_request(self, request: HttpRequest) -> bool:
"""
diff --git a/posthog/test/test_middleware.py b/posthog/test/test_middleware.py
index 51e0d28dbcf2..d1fe02adb4a1 100644
--- a/posthog/test/test_middleware.py
+++ b/posthog/test/test_middleware.py
@@ -1250,6 +1250,46 @@ class TestImpersonationBlockedPathsMiddleware(APIBaseTest):
assert response_data["type"] == "authentication_error"
assert response_data["code"] == "impersonation_path_blocked"
+ def test_impersonation_blocks_post_to_personal_api_keys_without_trailing_slash(self):
+ """The DefaultRouterPlusPlus router accepts paths with or without a trailing slash
+ (trailing_slash = r"/?"), so /api/personal_api_keys reaches the same viewset as
+ /api/personal_api_keys/. The middleware must block both forms — a startswith
+ check against "/api/personal_api_keys/" alone is bypassed by the slashless variant."""
+ self.login_as_other_user()
+
+ assert self.client.get("/api/users/@me/").json()["email"] == "[email protected]"
+
+ response = self.client.post(
+ "/api/personal_api_keys",
+ data={"label": "Test Key"},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 403, (
+ f"expected 403 (impersonation block), got {response.status_code}: {response.content[:200]!r}"
+ )
+ response_data = response.json()
+ assert response_data["type"] == "authentication_error"
+ assert response_data["code"] == "impersonation_path_blocked"
+
+ def test_impersonation_blocks_patch_to_users_api_without_trailing_slash(self):
+ """Same bypass class as the personal_api_keys slashless test: /api/users/@me
+ (no trailing slash) must be blocked just like /api/users/@me/."""
+ self.login_as_other_user()
+
+ assert self.client.get("/api/users/@me/").json()["email"] == "[email protected]"
+
+ response = self.client.patch(
+ "/api/users/@me",
+ data={"first_name": "Changed"},
+ content_type="application/json",
+ )
+
+ assert response.status_code == 403, (
+ f"expected 403 (impersonation block), got {response.status_code}: {response.content[:200]!r}"
+ )
+ assert response.json()["code"] == "impersonation_path_blocked"
+
@override_settings(ADMIN_PORTAL_ENABLED=True)
@override_settings(ADMIN_AUTH_GOOGLE_OAUTH2_KEY=None)