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(sentry): reject non-latin-1 auth tokens at setup (#92069)

Tom Owers3d2f13a8bee2
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/sentry.py b/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/sentry.py
index 4ed5dd5daa00..238957f01741 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/sentry.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/sentry.py
@@ -1120,6 +1120,19 @@ def validate_credentials(
return False, str(exc)
url = f"{base_url}/api/0/organizations/{organization_slug}/projects/"
+
+ try:
+ auth_token.encode("latin-1")
+ except UnicodeEncodeError:
+ # The token rides in the Authorization header, which http.client encodes as latin-1. A
+ # character outside that range raises mid-request; reject it as invalid input rather than
+ # letting the UnicodeEncodeError surface as a 500.
+ return (
+ False,
+ "Invalid Sentry auth token. It contains characters that can't be sent to Sentry. "
+ "Copy the token again from Sentry, then reconnect.",
+ )
+
headers = _auth_headers(auth_token)
try:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/tests/test_sentry.py b/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/tests/test_sentry.py
index c915fe673fb1..f331401a8558 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/tests/test_sentry.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/sentry/tests/test_sentry.py
@@ -306,6 +306,17 @@ class TestSentryTransport:
"API base URL must be one of https://sentry.io, https://us.sentry.io, or https://de.sentry.io.",
)
+ @patch("products.warehouse_sources.backend.temporal.data_imports.sources.sentry.sentry.make_tracked_session")
+ def test_validate_credentials_rejects_non_latin1_auth_token(self, mock_session) -> None:
+ # A token with a character outside latin-1 can't be encoded into the Authorization header;
+ # the guard must reject it before any request is dispatched.
+ valid, error = validate_credentials(auth_token="secret-’token", organization_slug="acme")
+
+ assert not valid
+ assert error is not None
+ assert error.startswith("Invalid Sentry auth token")
+ mock_session.assert_not_called()
+
@patch("products.warehouse_sources.backend.temporal.data_imports.sources.sentry.sentry.make_tracked_session")
def test_validate_credentials_401_tells_user_to_reconnect(self, mock_session) -> None:
mock_session.return_value.get.return_value = _response(None, status_code=401)