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(plugins): restrict local plugin installs to development (#93836)

Tom Piccirelloc48928755a22
diff --git a/products/cdp/backend/api/test/test_plugin.py b/products/cdp/backend/api/test/test_plugin.py
index 11fd832edcc3..1ee2c2fa9a79 100644
--- a/products/cdp/backend/api/test/test_plugin.py
+++ b/products/cdp/backend/api/test/test_plugin.py
@@ -1,4 +1,6 @@
+import os
import json
+import tempfile
from datetime import datetime
from typing import Any, Optional, cast
from zoneinfo import ZoneInfo
@@ -9,6 +11,8 @@ from posthog.test.base import APIBaseTest, QueryMatchingTest, snapshot_postgres_
from unittest import mock
from unittest.mock import patch
+from django.test import override_settings
+
from rest_framework import status
from posthog.cdp.templates.helpers import mock_transpile
@@ -606,6 +610,18 @@ class TestPluginAPI(APIBaseTest, QueryMatchingTest):
self.assertEqual(Plugin.objects.count(), 1)
self.assertEqual(mock_reload.call_count, 1)
+ @override_settings(DEBUG=False)
+ def test_create_plugin_rejects_local_file_url(self, mock_get, mock_reload):
+ with tempfile.TemporaryDirectory() as plugin_dir:
+ with open(os.path.join(plugin_dir, "plugin.json"), "w") as plugin_json:
+ json.dump({"name": "on-disk", "description": "contents of a file on the server"}, plugin_json)
+
+ response = self.client.post("/api/organizations/@current/plugins/", {"url": f"file:{plugin_dir}"})
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.content)
+ self.assertEqual(Plugin.objects.count(), 0)
+ self.assertEqual(PluginSourceFile.objects.count(), 0)
+
def test_create_plugin_version_range_eq_current(self, mock_get, mock_reload):
with self.is_cloud(False):
response = self.client.post(
diff --git a/products/cdp/backend/models/plugin.py b/products/cdp/backend/models/plugin.py
index 8cc4aa200a9f..62cd2e943f74 100644
--- a/products/cdp/backend/models/plugin.py
+++ b/products/cdp/backend/models/plugin.py
@@ -50,6 +50,10 @@ def update_validated_data_from_url(validated_data: dict[str, Any], url: str) ->
"""If remote plugin, download the archive and get up-to-date validated_data from there. Returns plugin.json."""
plugin_json: Optional[dict[str, Any]]
if url.startswith("file:"):
+ # A local plugin reads its plugin.json from the app server's filesystem, and the caller chooses the path.
+ # Only local development installs plugins this way, so refuse the scheme everywhere else.
+ if not settings.DEBUG:
+ raise ValidationError("Must be a GitHub/GitLab repository or a npm package URL!")
plugin_path = url[5:]
plugin_json_path = os.path.join(plugin_path, "plugin.json")
plugin_json = cast(Optional[dict[str, Any]], load_json_file(plugin_json_path))