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.

fix(llma): remove dead web-auth guard from prompt endpoints (#112331)

Juraj Majerik5a04b79c94fd
diff --git a/posthog/api/llm_prompt.py b/posthog/api/llm_prompt.py
index 07c98a68e642..85bab4a1d7d7 100644
--- a/posthog/api/llm_prompt.py
+++ b/posthog/api/llm_prompt.py
@@ -58,13 +58,7 @@ from posthog.api.services.llm_prompt import (
resolve_versions_page,
set_prompt_label,
)
-from posthog.auth import (
- DelegatedOAuthAccessTokenAuthentication,
- JwtAuthentication,
- OAuthAccessTokenAuthentication,
- PersonalAPIKeyAuthentication,
- SessionAuthentication,
-)
+from posthog.auth import DelegatedOAuthAccessTokenAuthentication, OAuthAccessTokenAuthentication, SessionAuthentication
from posthog.event_usage import report_team_action, report_user_action
from posthog.exceptions_capture import capture_exception
from posthog.models import User
@@ -143,17 +137,6 @@ class LLMPromptViewSet(
# a user, which reads prompts like any other API caller.
return isinstance(request.successful_authenticator, SessionAuthentication | OAuthAccessTokenAuthentication)
- def _ensure_web_authenticated(self, request: Request) -> Response | None:
- if not isinstance(
- request.successful_authenticator,
- SessionAuthentication | JwtAuthentication | PersonalAPIKeyAuthentication | OAuthAccessTokenAuthentication,
- ):
- return Response(
- {"detail": "This endpoint is only available to web-authenticated users."},
- status=status.HTTP_403_FORBIDDEN,
- )
- return None
-
def _prompt_not_found_response(self, prompt_name: str) -> Response:
return Response(
{
@@ -454,10 +437,6 @@ class LLMPromptViewSet(
@llma_track_latency("llma_prompts_publish_by_name")
@monitor(feature=None, endpoint="llma_prompts_publish_by_name", method="PATCH")
def update_by_name(self, request: Request, prompt_name: str = "", **kwargs) -> Response:
- auth_error = self._ensure_web_authenticated(request)
- if auth_error is not None:
- return auth_error
-
# PATCH shares the GET's route, so the segment has to mean the same thing on both verbs.
resolved_name = self._resolve_prompt_name(prompt_name)
if resolved_name is None:
@@ -533,10 +512,6 @@ class LLMPromptViewSet(
@llma_track_latency("llma_prompts_resolve_by_name")
@monitor(feature=None, endpoint="llma_prompts_resolve_by_name", method="GET")
def resolve_by_name(self, request: Request, prompt_name: str = "", **kwargs) -> Response:
- auth_error = self._ensure_web_authenticated(request)
- if auth_error is not None:
- return auth_error
-
query_params = self._get_resolve_query_params(request)
version = cast(int | None, query_params.get("version"))
version_id = query_params.get("version_id")
@@ -588,10 +563,6 @@ class LLMPromptViewSet(
@llma_track_latency("llma_prompts_archive")
@monitor(feature=None, endpoint="llma_prompts_archive", method="POST")
def archive(self, request: Request, prompt_name: str = "", **kwargs) -> Response:
- auth_error = self._ensure_web_authenticated(request)
- if auth_error is not None:
- return auth_error
-
try:
prompt_versions = archive_prompt(self.team, prompt_name, user=cast(User, request.user))
except LLMPromptNotFoundError:
@@ -630,10 +601,6 @@ class LLMPromptViewSet(
@llma_track_latency("llma_prompts_duplicate")
@monitor(feature=None, endpoint="llma_prompts_duplicate", method="POST")
def duplicate(self, request: Request, prompt_name: str = "", **kwargs) -> Response:
- auth_error = self._ensure_web_authenticated(request)
- if auth_error is not None:
- return auth_error
-
payload = LLMPromptDuplicateSerializer(data=request.data)
payload.is_valid(raise_exception=True)
new_name = payload.validated_data["new_name"]
@@ -684,10 +651,6 @@ class LLMPromptViewSet(
@llma_track_latency("llma_prompts_set_label")
@monitor(feature=None, endpoint="llma_prompts_set_label", method="PUT")
def set_label(self, request: Request, prompt_name: str = "", label_name: str = "", **kwargs) -> Response:
- auth_error = self._ensure_web_authenticated(request)
- if auth_error is not None:
- return auth_error
-
label_name = validate_prompt_label_name_value(label_name)
payload = LLMPromptSetLabelSerializer(data=request.data)
payload.is_valid(raise_exception=True)
@@ -763,10 +726,6 @@ class LLMPromptViewSet(
@llma_track_latency("llma_prompts_delete_label")
@monitor(feature=None, endpoint="llma_prompts_delete_label", method="DELETE")
def delete_label(self, request: Request, prompt_name: str = "", label_name: str = "", **kwargs) -> Response:
- auth_error = self._ensure_web_authenticated(request)
- if auth_error is not None:
- return auth_error
-
try:
remove_prompt_label(self.team, prompt_name=prompt_name, label_name=label_name)
except LLMPromptLabelNotFoundError:
diff --git a/posthog/api/test/test_llm_prompt.py b/posthog/api/test/test_llm_prompt.py
index 0ccb78ec3944..d39df5191146 100644
--- a/posthog/api/test/test_llm_prompt.py
+++ b/posthog/api/test/test_llm_prompt.py
@@ -595,20 +595,30 @@ class TestLLMPromptAPI(APIBaseTest):
latest = LLMPrompt.objects.get(team=self.team, name="json-edit", version=2, deleted=False)
assert latest.prompt == {"system": "You are an expert.", "temperature": 0.7}
- def test_update_prompt_by_name_forbidden_for_personal_api_key_auth(self):
+ def test_update_prompt_by_name_personal_api_key_scope_enforcement(self):
self.create_prompt_version(name="publish-prompt", version=1, is_latest=True, prompt="v1")
- api_key = self.create_personal_api_key_with_scopes(["llm_prompt:read"])
- self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {api_key}")
+ read_key = self.create_personal_api_key_with_scopes(["llm_prompt:read"])
+ self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {read_key}")
read_response = self.client.get(f"/api/environments/{self.team.id}/llm_prompts/name/publish-prompt/")
- write_response = self.client.patch(
+ read_scope_write_response = self.client.patch(
+ f"/api/environments/{self.team.id}/llm_prompts/name/publish-prompt/",
+ data={"prompt": "v2", "base_version": 1},
+ format="json",
+ )
+
+ write_key = self.create_personal_api_key_with_scopes(["llm_prompt:write"])
+ self.client.credentials(HTTP_AUTHORIZATION=f"Bearer {write_key}")
+ write_scope_write_response = self.client.patch(
f"/api/environments/{self.team.id}/llm_prompts/name/publish-prompt/",
data={"prompt": "v2", "base_version": 1},
format="json",
)
assert read_response.status_code == status.HTTP_200_OK
- assert write_response.status_code == status.HTTP_403_FORBIDDEN
+ assert read_scope_write_response.status_code == status.HTTP_403_FORBIDDEN
+ assert write_scope_write_response.status_code == status.HTTP_200_OK
+ assert write_scope_write_response.json()["version"] == 2
def test_resolve_prompt_by_name_returns_selected_prompt_and_versions(self):
historical = self.create_prompt_version(name="versions-prompt", version=1, is_latest=False, prompt="v1")