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(error-tracking): validate host and disable redirects in gitlab file link search (#121280)

posthog[bot]c54e32f07851
diff --git a/products/error_tracking/backend/presentation/views/git_provider_file_link_resolver.py b/products/error_tracking/backend/presentation/views/git_provider_file_link_resolver.py
index ee6afe2f23e6..41647a878804 100644
--- a/products/error_tracking/backend/presentation/views/git_provider_file_link_resolver.py
+++ b/products/error_tracking/backend/presentation/views/git_provider_file_link_resolver.py
@@ -15,7 +15,7 @@ from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.api.utils import action
from posthog.egress.github.transport import GitHubEgressBudgetExhausted, github_request
from posthog.egress.limiter.policies import Priority
-from posthog.models.integration import GitHubIntegration, GitLabIntegration, Integration
+from posthog.models.integration import GitHubIntegration, GitLabIntegration, GitLabIntegrationError, Integration
logger = structlog.get_logger(__name__)
@@ -228,7 +228,9 @@ def get_gitlab_file_url(
url = f"{gitlab_url}/api/v4/projects/{encoded_project_path}/search?scope={search_scope}&search={encoded_search}"
try:
- response = requests.get(url, headers=headers, timeout=10)
+ GitLabIntegration._validate_api_url(url)
+ # The token header survives cross-host redirects, so never follow one.
+ response = requests.get(url, headers=headers, allow_redirects=False, timeout=10)
if response.status_code == 200:
data = response.json()
@@ -239,6 +241,8 @@ def get_gitlab_file_url(
ref = item.get("ref", "")
if ref and item_path:
return f"{gitlab_url}/{owner}/{repository}/-/blob/{ref}/{item_path}"
+ except GitLabIntegrationError as error:
+ logger.warning("gitlab_code_search_url_rejected", error=str(error))
except Exception as error:
logger.exception("gitlab_code_search_request_failed", error=str(error))
diff --git a/products/error_tracking/backend/tests/api/test_git_provider_file_links.py b/products/error_tracking/backend/tests/api/test_git_provider_file_links.py
index a7346ea4aadd..b16355703849 100644
--- a/products/error_tracking/backend/tests/api/test_git_provider_file_links.py
+++ b/products/error_tracking/backend/tests/api/test_git_provider_file_links.py
@@ -4,9 +4,10 @@ from posthog.test.base import APIBaseTest
from unittest.mock import patch
from django.core.cache import cache
-from django.test import override_settings
+from django.test import SimpleTestCase, override_settings
import requests
+from parameterized import parameterized
from requests.structures import CaseInsensitiveDict
from posthog.egress.github.transport import GitHubEgressBudgetExhausted
@@ -15,6 +16,7 @@ from posthog.egress.limiter.policies import Priority
from products.error_tracking.backend.presentation.views.git_provider_file_link_resolver import (
_PUBLIC_TOKEN_CIRCUIT_OPEN_KEY,
_PUBLIC_TOKEN_UNAUTHORIZED_COUNT_KEY,
+ get_gitlab_file_url,
search_github_file,
)
@@ -102,3 +104,52 @@ class TestGitProviderFileLinksResolveGithub(APIBaseTest):
assert response.status_code == 200
assert response.json() == {"found": False}
+
+
+class TestGetGitlabFileUrl(SimpleTestCase):
+ @parameterized.expand(
+ [
+ ("http_scheme", "http://gitlab.example.com"),
+ ("internal_host", "https://localhost"),
+ ("metadata_ip", "https://169.254.169.254"),
+ ]
+ )
+ def test_rejects_disallowed_host_without_request(self, _name: str, gitlab_url: str) -> None:
+ with patch(
+ "products.error_tracking.backend.presentation.views.git_provider_file_link_resolver.requests.get"
+ ) as get:
+ url = get_gitlab_file_url(
+ code_sample="print(1)",
+ token="tok",
+ owner="o",
+ repository="r",
+ file_name="main.py",
+ gitlab_url=gitlab_url,
+ )
+
+ assert url is None
+ get.assert_not_called()
+
+ def test_does_not_follow_redirects(self) -> None:
+ redirect = _response(302)
+ redirect.headers = CaseInsensitiveDict({"Location": "https://other.example.com/"})
+ with (
+ patch("posthog.models.integration.gitlab.is_url_allowed", return_value=(True, None)),
+ patch(
+ "products.error_tracking.backend.presentation.views.git_provider_file_link_resolver.requests.get",
+ return_value=redirect,
+ ) as get,
+ ):
+ url = get_gitlab_file_url(
+ code_sample="print(1)",
+ token="tok",
+ owner="o",
+ repository="r",
+ file_name="main.py",
+ gitlab_url="https://gitlab.example.com",
+ )
+
+ assert url is None
+ assert get.call_count == 2
+ for call in get.call_args_list:
+ assert call.kwargs["allow_redirects"] is False