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(plugins): route github commit lookup through egress (#90500)

Arno Hillenbc1de3c1d52f
diff --git a/posthog/egress/github/README.md b/posthog/egress/github/README.md
index 9c7bf06d10c2..0bda972dada7 100644
--- a/posthog/egress/github/README.md
+++ b/posthog/egress/github/README.md
@@ -60,7 +60,7 @@ resp = github_request(
`raise_if_github_rate_limited` and `GitHubRateLimitError` (GitHub's own 429, the reactive twin of `GitHubEgressBudgetExhausted`) live in `transport.py` for callers that want to raise and retry.
The model-coupled `GitHubIntegrationBase.api_request` layers the installation-token lifecycle (proactive refresh, 401 refresh-retry, rate-limit raising, per-instance `source` attribution) on top. Hold an integration, call that. Hold a bare token, call `github_request`.
-The `github-api-calls-go-through-egress` semgrep rule reads the URL argument only, which is why `posthog/plugins/utils.py` still calls `requests.get` directly with a URL it builds into a variable first.
+The `github-api-calls-go-through-egress` semgrep rule reads the URL argument only, so a call that builds its URL into a variable first gets past it, and review is the only check on that shape.
## Identity-blind callers and the PAT scope decision
diff --git a/posthog/models/test/test_organization_model.py b/posthog/models/test/test_organization_model.py
index 8e208cf09b2e..17e85f5e343c 100644
--- a/posthog/models/test/test_organization_model.py
+++ b/posthog/models/test/test_organization_model.py
@@ -17,7 +17,7 @@ from posthog.organization_caching import (
get_cached_organization_membership,
get_cached_organization_memberships,
)
-from posthog.plugins.test.mock import mocked_plugin_requests_get
+from posthog.plugins.test.mock import mocked_plugin_github_request, mocked_plugin_requests_get
from posthog.plugins.test.plugin_archives import HELLO_WORLD_PLUGIN_GITHUB_ZIP
from posthog.redis import get_client
@@ -112,8 +112,9 @@ class TestOrganization(BaseTest):
self.assertEqual(self.organization.invites.count(), 2)
self.assertEqual(self.organization.active_invites.count(), 1)
+ @mock.patch("posthog.plugins.utils.github_request", side_effect=mocked_plugin_github_request)
@mock.patch("posthog.plugins.utils.requests.get", side_effect=mocked_plugin_requests_get)
- def test_plugins_are_preinstalled_on_self_hosted(self, mock_get):
+ def test_plugins_are_preinstalled_on_self_hosted(self, mock_get, mock_github_request):
with self.is_cloud(False):
with self.settings(PLUGINS_PREINSTALLED_URLS=["https://github.com/PostHog/helloworldplugin/"]):
new_org, _, _ = Organization.objects.bootstrap(
@@ -126,14 +127,16 @@ class TestOrganization(BaseTest):
Plugin.objects.filter(organization=new_org, is_preinstalled=True).get().name,
"helloworldplugin",
)
- self.assertEqual(mock_get.call_count, 2)
+ self.assertEqual(mock_github_request.call_count, 1)
+ self.assertEqual(mock_get.call_count, 1)
mock_get.assert_any_call(
f"https://github.com/PostHog/helloworldplugin/archive/{HELLO_WORLD_PLUGIN_GITHUB_ZIP[0]}.zip",
headers={},
)
+ @mock.patch("posthog.plugins.utils.github_request", side_effect=mocked_plugin_github_request)
@mock.patch("posthog.plugins.utils.requests.get", side_effect=mocked_plugin_requests_get)
- def test_plugins_are_not_preinstalled_on_cloud(self, mock_get):
+ def test_plugins_are_not_preinstalled_on_cloud(self, mock_get, mock_github_request):
with self.is_cloud(True):
with self.settings(PLUGINS_PREINSTALLED_URLS=["https://github.com/PostHog/helloworldplugin/"]):
new_org, _, _ = Organization.objects.bootstrap(
@@ -142,6 +145,7 @@ class TestOrganization(BaseTest):
)
self.assertEqual(Plugin.objects.filter(organization=new_org, is_preinstalled=True).count(), 0)
+ self.assertEqual(mock_github_request.call_count, 0)
self.assertEqual(mock_get.call_count, 0)
def test_plugins_access_level_is_determined_based_on_realm(self):
diff --git a/posthog/plugins/test/mock.py b/posthog/plugins/test/mock.py
index 0884843370fb..fd5028955cb7 100644
--- a/posthog/plugins/test/mock.py
+++ b/posthog/plugins/test/mock.py
@@ -204,3 +204,7 @@ def mocked_plugin_requests_get(*args, **kwargs):
)
return MockJSONResponse(None, 404)
+
+
+def mocked_plugin_github_request(method, url, **kwargs):
+ return mocked_plugin_requests_get(url, **kwargs)
diff --git a/posthog/plugins/test/test_utils.py b/posthog/plugins/test/test_utils.py
index 3457cbdaeec1..62b65e49108c 100644
--- a/posthog/plugins/test/test_utils.py
+++ b/posthog/plugins/test/test_utils.py
@@ -14,7 +14,7 @@ from posthog.plugins.utils import (
put_json_into_zip_archive,
)
-from .mock import mocked_plugin_requests_get
+from .mock import mocked_plugin_github_request, mocked_plugin_requests_get
from .plugin_archives import (
HELLO_WORLD_PLUGIN_GITHUB_SUBDIR_ZIP,
HELLO_WORLD_PLUGIN_GITHUB_ZIP,
@@ -23,17 +23,18 @@ from .plugin_archives import (
)
[email protected]("posthog.plugins.utils.github_request", side_effect=mocked_plugin_github_request)
@mock.patch("posthog.plugins.utils.requests.get", side_effect=mocked_plugin_requests_get)
class TestPluginsUtils(BaseTest):
- def test_parse_github_urls(self, mock_get):
+ def test_parse_github_urls(self, mock_get, mock_github_request):
parsed_url = parse_url("https://github.com/PostHog/posthog")
self.assertEqual(parsed_url["type"], "github")
self.assertEqual(parsed_url["user"], "PostHog")
self.assertEqual(parsed_url["repo"], "posthog")
self.assertEqual(parsed_url.get("tag", None), None)
self.assertEqual(parsed_url.get("path", None), None)
- self.assertEqual(mock_get.call_count, 0)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 0)
+ mock_github_request.reset_mock()
parsed_url = parse_url("https://github.com/PostHog/posthog", get_latest_if_none=True)
self.assertEqual(parsed_url["type"], "github")
@@ -41,12 +42,15 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["repo"], "posthog")
self.assertEqual(parsed_url["tag"], "MOCKLATESTCOMMIT")
self.assertEqual(parsed_url.get("path", None), None)
- self.assertEqual(mock_get.call_count, 1)
- mock_get.assert_called_with(
+ self.assertEqual(mock_github_request.call_count, 1)
+ mock_github_request.assert_called_with(
+ "GET",
"https://api.github.com/repos/PostHog/posthog/commits?sha=&path=",
+ source="plugins",
headers={},
+ installation_id=None,
)
- mock_get.reset_mock()
+ mock_github_request.reset_mock()
parsed_url = parse_url("https://github.com/PostHog/posthog/tree/82c9218ee40f561b7f37a22d6b6a0ca82887ee3e")
self.assertEqual(parsed_url["type"], "github")
@@ -54,8 +58,8 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["repo"], "posthog")
self.assertEqual(parsed_url["tag"], "82c9218ee40f561b7f37a22d6b6a0ca82887ee3e")
self.assertEqual(parsed_url.get("path", None), None)
- self.assertEqual(mock_get.call_count, 0)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 0)
+ mock_github_request.reset_mock()
parsed_url = parse_url(
"https://github.com/PostHog/posthog/tree/82c9218ee40f561b7f37a22d6b6a0ca82887ee3e",
@@ -66,8 +70,8 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["repo"], "posthog")
self.assertEqual(parsed_url["tag"], "82c9218ee40f561b7f37a22d6b6a0ca82887ee3e")
self.assertEqual(parsed_url.get("path", None), None)
- self.assertEqual(mock_get.call_count, 0)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 0)
+ mock_github_request.reset_mock()
parsed_url = parse_url(
"https://github.com/PostHog/posthog/tree/82c9218ee40f561b7f37a22d6b6a0ca82887ee3e/test/path/in/repo",
@@ -78,8 +82,8 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["repo"], "posthog")
self.assertEqual(parsed_url["tag"], "82c9218ee40f561b7f37a22d6b6a0ca82887ee3e")
self.assertEqual(parsed_url["path"], "test/path/in/repo")
- self.assertEqual(mock_get.call_count, 0)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 0)
+ mock_github_request.reset_mock()
parsed_url = parse_url("https://github.com/PostHog/posthog/tree/main", get_latest_if_none=True)
self.assertEqual(parsed_url["type"], "github")
@@ -87,12 +91,15 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["repo"], "posthog")
self.assertEqual(parsed_url["tag"], "MOCKLATESTCOMMIT")
self.assertEqual(parsed_url.get("path", None), None)
- self.assertEqual(mock_get.call_count, 1)
- mock_get.assert_called_with(
+ self.assertEqual(mock_github_request.call_count, 1)
+ mock_github_request.assert_called_with(
+ "GET",
"https://api.github.com/repos/PostHog/posthog/commits?sha=main&path=",
+ source="plugins",
headers={},
+ installation_id=None,
)
- mock_get.reset_mock()
+ mock_github_request.reset_mock()
parsed_url = parse_url(
"https://github.com/PostHog/posthog/tree/main/test/path/in/repo",
@@ -103,12 +110,15 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["repo"], "posthog")
self.assertEqual(parsed_url["tag"], "MOCKLATESTCOMMIT")
self.assertEqual(parsed_url["path"], "test/path/in/repo")
- self.assertEqual(mock_get.call_count, 1)
- mock_get.assert_called_with(
+ self.assertEqual(mock_github_request.call_count, 1)
+ mock_github_request.assert_called_with(
+ "GET",
"https://api.github.com/repos/PostHog/posthog/commits?sha=main&path=test/path/in/repo",
+ source="plugins",
headers={},
+ installation_id=None,
)
- mock_get.reset_mock()
+ mock_github_request.reset_mock()
parsed_url = parse_url("https://www.github.com/PostHog/posthog/commit/82c9218ee40f561b7f37a22d6b6a0ca82887ee3e")
self.assertEqual(parsed_url["type"], "github")
@@ -116,8 +126,8 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["repo"], "posthog")
self.assertEqual(parsed_url["tag"], "82c9218ee40f561b7f37a22d6b6a0ca82887ee3e")
self.assertEqual(parsed_url.get("path", None), None)
- self.assertEqual(mock_get.call_count, 0)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 0)
+ mock_github_request.reset_mock()
parsed_url = parse_url(
"https://www.github.com/PostHog/posthog/commit/82c9218ee40f561b7f37a22d6b6a0ca82887ee3e",
@@ -128,8 +138,8 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["repo"], "posthog")
self.assertEqual(parsed_url["tag"], "82c9218ee40f561b7f37a22d6b6a0ca82887ee3e")
self.assertEqual(parsed_url.get("path", None), None)
- self.assertEqual(mock_get.call_count, 0)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 0)
+ mock_github_request.reset_mock()
parsed_url = parse_url(
"https://github.com/PostHog/posthog/releases/tag/82c9218ee40f561b7f37a22d6b6a0ca82887ee3e"
@@ -139,8 +149,8 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["repo"], "posthog")
self.assertEqual(parsed_url["tag"], "82c9218ee40f561b7f37a22d6b6a0ca82887ee3e")
self.assertEqual(parsed_url.get("path", None), None)
- self.assertEqual(mock_get.call_count, 0)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 0)
+ mock_github_request.reset_mock()
parsed_url = parse_url(
"https://www.github.com/PostHog/posthog/archive/82c9218ee40f561b7f37a22d6b6a0ca82887ee3e.zip"
@@ -150,8 +160,8 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["repo"], "posthog")
self.assertEqual(parsed_url["tag"], "82c9218ee40f561b7f37a22d6b6a0ca82887ee3e")
self.assertEqual(parsed_url.get("path", None), None)
- self.assertEqual(mock_get.call_count, 0)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 0)
+ mock_github_request.reset_mock()
parsed_url = parse_url(
"https://github.com/PostHog/posthog/archive/82c9218ee40f561b7f37a22d6b6a0ca82887ee3e.tar.gz"
@@ -161,8 +171,8 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["repo"], "posthog")
self.assertEqual(parsed_url["tag"], "82c9218ee40f561b7f37a22d6b6a0ca82887ee3e")
self.assertEqual(parsed_url.get("path", None), None)
- self.assertEqual(mock_get.call_count, 0)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 0)
+ mock_github_request.reset_mock()
# private tokens
parsed_url = parse_url("https://github.com/PostHog/posthog?private_token=TOKEN")
@@ -172,8 +182,8 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url.get("tag", None), None)
self.assertEqual(parsed_url.get("path", None), None)
self.assertEqual(parsed_url["private_token"], "TOKEN")
- self.assertEqual(mock_get.call_count, 0)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 0)
+ mock_github_request.reset_mock()
parsed_url = parse_url(
"https://github.com/PostHog/posthog?private_token=TOKEN",
@@ -184,12 +194,15 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["repo"], "posthog")
self.assertEqual(parsed_url["tag"], "MOCKLATESTCOMMIT")
self.assertEqual(parsed_url.get("path", None), None)
- mock_get.assert_called_with(
+ mock_github_request.assert_called_with(
+ "GET",
"https://api.github.com/repos/PostHog/posthog/commits?sha=&path=",
+ source="plugins",
headers={"Authorization": "Bearer TOKEN"},
+ installation_id=None,
)
- self.assertEqual(mock_get.call_count, 1)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 1)
+ mock_github_request.reset_mock()
parsed_url = parse_url(
"https://github.com/PostHog/posthog/tree/82c9218ee40f561b7f37a22d6b6a0ca82887ee3e?private_token=TOKEN"
@@ -200,8 +213,8 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["tag"], "82c9218ee40f561b7f37a22d6b6a0ca82887ee3e")
self.assertEqual(parsed_url.get("path", None), None)
self.assertEqual(parsed_url["private_token"], "TOKEN")
- self.assertEqual(mock_get.call_count, 0)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 0)
+ mock_github_request.reset_mock()
# default global token
with self.settings(GITHUB_TOKEN="MY_GITHUB_TOKEN"):
@@ -212,12 +225,15 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["tag"], "MOCKLATESTCOMMIT")
self.assertEqual(parsed_url.get("path", None), None)
self.assertEqual(parsed_url.get("private_token", None), None)
- mock_get.assert_called_with(
+ mock_github_request.assert_called_with(
+ "GET",
"https://api.github.com/repos/PostHog/posthog/commits?sha=&path=",
+ source="plugins",
headers={"Authorization": "Bearer MY_GITHUB_TOKEN"},
+ installation_id=None,
)
- self.assertEqual(mock_get.call_count, 1)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 1)
+ mock_github_request.reset_mock()
parsed_url = parse_url(
"https://github.com/PostHog/posthog?private_token=TOKEN",
@@ -229,14 +245,17 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(parsed_url["tag"], "MOCKLATESTCOMMIT")
self.assertEqual(parsed_url.get("path", None), None)
self.assertEqual(parsed_url["private_token"], "TOKEN")
- mock_get.assert_called_with(
+ mock_github_request.assert_called_with(
+ "GET",
"https://api.github.com/repos/PostHog/posthog/commits?sha=&path=",
+ source="plugins",
headers={"Authorization": "Bearer TOKEN"},
+ installation_id=None,
)
- self.assertEqual(mock_get.call_count, 1)
- mock_get.reset_mock()
+ self.assertEqual(mock_github_request.call_count, 1)
+ mock_github_request.reset_mock()
- def test_parse_gitlab_urls(self, mock_get):
+ def test_parse_gitlab_urls(self, mock_get, mock_github_request):
parsed_url = parse_url("https://gitlab.com/mariusandra/helloworldplugin")
self.assertEqual(parsed_url["type"], "gitlab")
self.assertEqual(parsed_url["project"], "mariusandra/helloworldplugin")
@@ -357,7 +376,7 @@ class TestPluginsUtils(BaseTest):
headers={"Authorization": "Bearer MY_GITLAB_TOKEN"},
)
- def test_parse_npm_urls(self, mock_get):
+ def test_parse_npm_urls(self, mock_get, mock_github_request):
parsed_url = parse_url("https://www.npmjs.com/package/posthog-helloworld-plugin")
self.assertEqual(parsed_url["type"], "npm")
self.assertEqual(parsed_url["pkg"], "posthog-helloworld-plugin")
@@ -470,7 +489,7 @@ class TestPluginsUtils(BaseTest):
headers={"Authorization": "Bearer MY_NPM_TOKEN"},
)
- def test_download_plugin_archive_github(self, mock_get):
+ def test_download_plugin_archive_github(self, mock_get, mock_github_request):
plugin_github_zip_1 = download_plugin_archive(
"https://www.github.com/PostHog/helloworldplugin/commit/82c9218ee40f561b7f37a22d6b6a0ca82887ee3e",
HELLO_WORLD_PLUGIN_GITHUB_ZIP[0],
@@ -549,7 +568,7 @@ class TestPluginsUtils(BaseTest):
2713501883,
)
- def test_download_plugin_archive_gitlab(self, mock_get):
+ def test_download_plugin_archive_gitlab(self, mock_get, mock_github_request):
plugin_gitlab = download_plugin_archive(
"https://www.gitlab.com/mariusandra/helloworldplugin/-/commit/ff78cbe1d70316055c610a962a8355a4616d874b",
HELLO_WORLD_PLUGIN_GITLAB_ZIP[0],
@@ -595,7 +614,7 @@ class TestPluginsUtils(BaseTest):
headers={"Authorization": "Bearer MY_GITLAB_TOKEN"},
)
- def test_download_plugin_archive_npm(self, mock_get):
+ def test_download_plugin_archive_npm(self, mock_get, mock_github_request):
plugin_npm_tgz = download_plugin_archive("https://www.npmjs.com/package/posthog-helloworld-plugin/v/0.0.0")
self.assertEqual(plugin_npm_tgz, base64.b64decode(HELLO_WORLD_PLUGIN_NPM_TGZ[1]))
self.assertEqual(mock_get.call_count, 1)
@@ -633,7 +652,7 @@ class TestPluginsUtils(BaseTest):
headers={"Authorization": "Bearer MY_NPM_TOKEN"},
)
- def test_get_file_from_archive(self, mock_get):
+ def test_get_file_from_archive(self, mock_get, mock_github_request):
plugin_json_zip = cast(
dict,
get_file_from_archive(base64.b64decode(HELLO_WORLD_PLUGIN_GITHUB_ZIP[1]), "plugin.json"),
@@ -658,7 +677,7 @@ class TestPluginsUtils(BaseTest):
self.assertEqual(plugin_json_tgz["url"], "https://github.com/PostHog/helloworldplugin")
self.assertEqual(plugin_json_tgz["description"], "Greet the World and Foo a Bar, JS edition!")
- def test_put_json_into_zip_archive(self, mock_get):
+ def test_put_json_into_zip_archive(self, mock_get, mock_github_request):
archive = base64.b64decode(HELLO_WORLD_PLUGIN_GITHUB_ZIP[1])
plugin_json = cast(dict, get_file_from_archive(archive, "plugin.json"))
plugin_json["posthogVersion"] = "0.0.0"
diff --git a/posthog/plugins/utils.py b/posthog/plugins/utils.py
index 2b0125fa84ba..65e814a55d9a 100644
--- a/posthog/plugins/utils.py
+++ b/posthog/plugins/utils.py
@@ -13,6 +13,7 @@ from django.conf import settings
import requests
from posthog.dataclasses import frozen
+from posthog.egress.github.transport import github_request
def parse_github_url(url: str, get_latest_if_none=False) -> Optional[dict[str, Optional[str]]]:
@@ -55,7 +56,15 @@ def parse_github_url(url: str, get_latest_if_none=False) -> Optional[dict[str, O
parsed["tag"] or "",
parsed["path"] or "",
)
- commits = requests.get(commits_url, headers=headers).json()
+ # Identity-blind: a plugin URL carries no GitHub App installation to meter the
+ # call against, so this records request volume only.
+ commits = github_request(
+ "GET",
+ commits_url,
+ source="plugins",
+ headers=headers,
+ installation_id=None,
+ ).json()
if isinstance(commits, dict):
raise Exception(commits.get("message"))
diff --git a/products/cdp/backend/api/test/test_plugin.py b/products/cdp/backend/api/test/test_plugin.py
index 76daed665cc5..df99b5fa1380 100644
--- a/products/cdp/backend/api/test/test_plugin.py
+++ b/products/cdp/backend/api/test/test_plugin.py
@@ -22,7 +22,7 @@ from posthog.models.organization import Organization, OrganizationMembership
from posthog.models.team.team import Team
from posthog.models.user import User
from posthog.plugins.access import can_configure_plugins, can_globally_manage_plugins, can_install_plugins
-from posthog.plugins.test.mock import mocked_plugin_requests_get
+from posthog.plugins.test.mock import mocked_plugin_github_request, mocked_plugin_requests_get
from posthog.plugins.test.plugin_archives import HELLO_WORLD_PLUGIN_GITHUB_ATTACHMENT_ZIP, HELLO_WORLD_PLUGIN_GITHUB_ZIP
from products.cdp.backend.api.test.test_hog_function_templates import MOCK_NODE_TEMPLATES
@@ -36,6 +36,8 @@ def mocked_plugin_reload(*args, **kwargs):
@mock.patch("products.cdp.backend.models.plugin.reload_plugins_on_workers", side_effect=mocked_plugin_reload)
@mock.patch("products.cdp.backend.api.plugin.requests.get", side_effect=mocked_plugin_requests_get)
+# `new=` keeps this out of every test signature (nothing here asserts on the commit lookup).
[email protected]("posthog.plugins.utils.github_request", new=mocked_plugin_github_request)
@pytest.mark.usefixtures("unittest_snapshot")
class TestPluginAPI(APIBaseTest, QueryMatchingTest):
maxDiff = None