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(github): surface a clean error when credential check gets a non-JSON body (#71850)

Tom Owers8af85a004d59
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py b/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py
index d7c8a9b8e3fb..86d72532ae37 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/github/github.py
@@ -286,13 +286,21 @@ def validate_credentials(personal_access_token: str, repository: str) -> tuple[b
return False, f"Repository '{repository}' not found or not accessible"
try:
- error_data = response.json()
- message = error_data.get("message", response.text)
+ body = response.json()
+ message = body.get("message") if isinstance(body, dict) else None
+ except ValueError:
+ message = None
+ if message:
return False, message
- except Exception:
- pass
- return False, response.text
+ # A non-JSON body means GitHub returned an HTML error page (e.g. its 5xx "Unicorn!" page) —
+ # never surface that raw markup to the user.
+ if response.status_code >= 500:
+ return False, "GitHub is temporarily unavailable. Please try again in a few minutes."
+ return (
+ False,
+ f"GitHub rejected the request (status {response.status_code}). Please check your token and repository access.",
+ )
except requests.exceptions.RequestException as e:
return False, str(e)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_validate_credentials.py b/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_validate_credentials.py
new file mode 100644
index 000000000000..9ba07efdcc2f
--- /dev/null
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/github/tests/test_github_validate_credentials.py
@@ -0,0 +1,46 @@
+import pytest
+from unittest import mock
+
+import requests
+
+from products.warehouse_sources.backend.temporal.data_imports.sources.github import github
+
+
+def _response(status_code: int, *, json_body: object | None = None, text: str = "") -> mock.Mock:
+ response = mock.Mock(spec=requests.Response)
+ response.status_code = status_code
+ response.text = text
+ if json_body is None:
+ response.json.side_effect = requests.exceptions.JSONDecodeError("no json", "", 0)
+ else:
+ response.json.return_value = json_body
+ return response
+
+
+def _validate_with(response: mock.Mock) -> tuple[bool, str | None]:
+ session = mock.Mock()
+ session.get.return_value = response
+ with mock.patch.object(github, "make_tracked_session", return_value=session):
+ return github.validate_credentials("token", "owner/repo")
+
+
+ "status_code,expected_substring",
+ [
+ # GitHub's 5xx "Unicorn!" page is HTML, so response.json() raises — we must never echo the markup.
+ (503, "temporarily unavailable"),
+ (418, "status 418"),
+ ],
+)
+def test_non_json_body_is_not_leaked(status_code, expected_substring):
+ is_valid, message = _validate_with(_response(status_code, text="<!DOCTYPE html><html>...</html>"))
+ assert is_valid is False
+ assert message is not None
+ assert "<" not in message
+ assert expected_substring in message
+
+
+def test_json_error_message_is_surfaced():
+ is_valid, message = _validate_with(_response(422, json_body={"message": "Validation failed"}))
+ assert is_valid is False
+ assert message == "Validation failed"