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(js-snippet): let scoped API credentials use the version endpoints (#103426)

posthog[bot]13a5550d6c2a
diff --git a/posthog/api/js_snippet.py b/posthog/api/js_snippet.py
index c73772cacb7e..8c5908d7bfeb 100644
--- a/posthog/api/js_snippet.py
+++ b/posthog/api/js_snippet.py
@@ -24,6 +24,8 @@ class JsSnippetVersionSerializer(serializers.Serializer):
class JsSnippetViewSet(TeamAndOrgViewSetMixin, GenericViewSet):
scope_object = "project"
+ scope_object_read_actions = ["resolve", "get_version"]
+ scope_object_write_actions = ["update_version"]
@extend_schema(responses={200: OpenApiTypes.OBJECT})
@action(methods=["GET"], detail=False, url_path="resolve")
diff --git a/posthog/api/test/test_js_snippet.py b/posthog/api/test/test_js_snippet.py
index 95383952972a..0bc69c347ba6 100644
--- a/posthog/api/test/test_js_snippet.py
+++ b/posthog/api/test/test_js_snippet.py
@@ -5,6 +5,7 @@ from posthog.test.base import APIBaseTest
from django.core.cache import cache
from django.test import override_settings
+from parameterized import parameterized
from rest_framework import status
import posthog.models.js_snippet_versioning as sv
@@ -145,3 +146,25 @@ class TestJsSnippetVersionAPI(APIBaseTest):
self.client.logout()
response = self.client.get(f"/api/projects/{self.team.id}/js-snippet/version/")
assert response.status_code in (status.HTTP_401_UNAUTHORIZED, status.HTTP_403_FORBIDDEN)
+
+ @parameterized.expand(
+ [
+ ("resolve_with_read", "get", "resolve/?pin=1.358.0", ["project:read"], status.HTTP_200_OK),
+ ("version_with_read", "get", "version/", ["project:read"], status.HTTP_200_OK),
+ ("update_with_write", "patch", "version/", ["project:write"], status.HTTP_200_OK),
+ ("update_with_read_only", "patch", "version/", ["project:read"], status.HTTP_403_FORBIDDEN),
+ ("resolve_with_other_scope", "get", "resolve/?pin=1.358.0", ["insight:read"], status.HTTP_403_FORBIDDEN),
+ ]
+ )
+ @override_settings(POSTHOG_JS_S3_BUCKET="test-bucket")
+ def test_scoped_credentials_reach_the_handlers(
+ self, _name: str, method: str, path: str, scopes: list[str], expected_status: int
+ ):
+ self.client.logout()
+ response = getattr(self.client, method)(
+ f"/api/projects/{self.team.id}/js-snippet/{path}",
+ data=json.dumps({"js_snippet_version": "1.358.0"}) if method == "patch" else None,
+ content_type="application/json",
+ headers={"authorization": f"Bearer {self.create_personal_api_key_with_scopes(scopes)}"},
+ )
+ assert response.status_code == expected_status