: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(elevenlabs): accept a scoped api key that lacks one read permission (#122453)
Tom Owers39bad64763cf
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/elevenlabs.py b/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/elevenlabs.pyindex 2729e7a953b8..a320c6040bae 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/elevenlabs.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/elevenlabs.py@@ -22,7 +22,6 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.resfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.resource import Resourcefrom products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.typing import ClientConfigfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManager-from products.warehouse_sources.backend.temporal.data_imports.sources.common.source_helpers import validate_via_probefrom products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceResponsefrom products.warehouse_sources.backend.temporal.data_imports.sources.elevenlabs.settings import (ELEVENLABS_ENDPOINTS,@@ -207,13 +206,24 @@ _INVALID_API_KEY_MESSAGE = ()+def _is_missing_permission(response: Response) -> bool:+ try:+ detail = response.json().get("detail")+ except (ValueError, AttributeError):+ return False+ return isinstance(detail, dict) and detail.get("status") == "missing_permissions"++def validate_credentials(api_key: str, schema_name: Optional[str] = None) -> tuple[bool, str | None]:"""Probe the API key. 200 => valid. 401 => invalid key. 403 => valid key missing a scope.- At source-create (`schema_name=None`) a 403 is accepted: users may grant only the scopes for the- endpoints they want, so a missing scope must not block connecting. When probing a specific schema- a 403 is a genuine per-table scope error and is surfaced. Sync-time 403s are handled separately by- `get_non_retryable_errors`.+ ElevenLabs also answers a valid key that lacks a scope with a 401 whose body carries+ `detail.status == "missing_permissions"`, so that 401 is a missing scope too, not an invalid key.++ At source-create (`schema_name=None`) a missing scope is accepted: users may grant only the scopes+ for the endpoints they want, so a missing scope must not block connecting. When probing a specific+ schema it is a genuine per-table scope error and is surfaced. Sync-time 403s are handled separately+ by `get_non_retryable_errors`.Any other status (429, 5xx, or an unexpected code) means the key was never actually verified, sovalidation fails rather than saving an unverified key as valid — the user can retry a transient blip.@@ -223,27 +233,30 @@ def validate_credentials(api_key: str, schema_name: Optional[str] = None) -> tupif config is not None and config.fanout is not None:config = ELEVENLABS_ENDPOINTS[config.fanout.parent_name]probe_path = config.path if config else "/v1/user"- # Bake the probe's page_size into the URL: validate_via_probe issues a bare GET with no params.+ # Bake the probe's page_size into the URL: the probe issues a bare GET with no params.url = f"{ELEVENLABS_BASE_URL}{probe_path}{f'?{config.page_size_param}=1' if config and config.page_size else ''}"# Don't follow redirects: requests preserves the custom `xi-api-key` header across a cross-origin# 3xx, so a redirect off the fixed API host could replay the key to another origin.- _ok, status = validate_via_probe(- lambda: make_tracked_session(redact_values=(api_key,), allow_redirects=False),- url,- headers=_get_headers(api_key),- )-- if status is None:+ try:+ response = make_tracked_session(redact_values=(api_key,), allow_redirects=False).get(+ url, headers=_get_headers(api_key), timeout=10.0, allow_redirects=False+ )+ except Exception: # noqa: BLE001 — a credential probe must never raise; any failure means "not validated"return False, "Could not reach the ElevenLabs API. Please try again."++ status = response.status_codeif status == 200:return True, None- if status == 401:- return False, _INVALID_API_KEY_MESSAGE- if status == 403:+ if status == 403 or (status == 401 and _is_missing_permission(response)):if schema_name is None:return True, None- return False, f"Your ElevenLabs API key is missing the permission required to sync `{schema_name}`."+ return False, (+ f"Your ElevenLabs API key is missing the permission required to sync `{schema_name}`. "+ "Grant it on the key in your ElevenLabs account settings, then try again."+ )+ if status == 401:+ return False, _INVALID_API_KEY_MESSAGE# A 4xx other than the 401/403 above (a 400 is what users actually hit) is a deterministic client# error: the key was rejected and retrying sends the same request, so treat it as an invalid key# rather than a transient "try again". 429 is a rate limit and stays retryable below.diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/tests/test_elevenlabs.py b/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/tests/test_elevenlabs.pyindex 8b4fb4d18f91..e7c4be5c2290 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/tests/test_elevenlabs.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/elevenlabs/tests/test_elevenlabs.py@@ -343,22 +343,32 @@ class TestUnpaginatedEndpoints:assert result.params("page_size") == [None]+_INVALID_KEY_BODY = {"detail": {"status": "invalid_api_key", "message": "Invalid API key"}}+_MISSING_PERMISSION_BODY = {+ "detail": {"status": "missing_permissions", "message": "The API key you used is missing the permission user_read."}+}++class TestValidateCredentials:@parameterized.expand([- ("valid", 200, None, True),- ("bad_key", 401, None, False),+ ("valid", 200, None, None, True),+ ("bad_key", 401, None, _INVALID_KEY_BODY, False),# A 400 (the status users actually hit) is a deterministic client error, not transient.- ("bad_request_at_create", 400, None, False),- ("missing_scope_at_create", 403, None, True),- ("missing_scope_at_schema", 403, "history", False),+ ("bad_request_at_create", 400, None, None, False),+ ("missing_scope_at_create", 403, None, None, True),+ ("missing_scope_at_schema", 403, "history", None, False),+ ("missing_permission_401_at_create", 401, None, _MISSING_PERMISSION_BODY, True),+ ("missing_permission_401_at_schema", 401, "history", _MISSING_PERMISSION_BODY, False),# An unverified key (transient 429/5xx) must not be saved as valid at create time.- ("rate_limited_at_create", 429, None, False),- ("server_error_at_create", 500, None, False),+ ("rate_limited_at_create", 429, None, None, False),+ ("server_error_at_create", 500, None, None, False),])- def test_status_mapping(self, _name: str, status: int, schema_name: str | None, expected_ok: bool) -> None:- answer = ScriptedResponse(status=status)+ def test_status_mapping(+ self, _name: str, status: int, schema_name: str | None, body: Any, expected_ok: bool+ ) -> None:+ answer = ScriptedResponse(status=status, json=body)with scripted_network(always(answer) if status in (429, 500) else [answer]) as network:ok, _msg = validate_credentials("k", schema_name)assert ok is expected_ok