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(apple-search-ads): name a pasted public key instead of crashing (#112384)

Tom Owersf062866fbc62
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/apple_search_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/apple_search_ads.py
index 858465ee4c65..437a5d101cb3 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/apple_search_ads.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/apple_search_ads.py
@@ -135,6 +135,13 @@ def build_client_secret(credentials: AppleSearchAdsCredentials, *, issued_at: Op
and presents that as `client_secret`. Unchanged between v5 and the Platform API.
"""
now = int(issued_at if issued_at is not None else time.time())
+ private_key = _normalize_private_key(credentials.private_key)
+ # PyJWT loads a public key without complaint and only fails once it tries to sign with it.
+ if "PUBLIC KEY-----" in private_key:
+ raise AppleSearchAdsAuthError(
+ "You entered the public key. Paste the private key you generated for your Apple Ads API "
+ "client, not the public key you uploaded to Apple."
+ )
try:
return jwt.encode(
{
@@ -144,11 +151,11 @@ def build_client_secret(credentials: AppleSearchAdsCredentials, *, issued_at: Op
"exp": now + CLIENT_SECRET_TTL_SECONDS,
"iss": credentials.team_id,
},
- _normalize_private_key(credentials.private_key),
+ private_key,
algorithm="ES256",
headers={"alg": "ES256", "kid": credentials.key_id},
)
- except (jwt.PyJWTError, ValueError, TypeError) as e:
+ except (jwt.PyJWTError, ValueError, TypeError, AttributeError) as e:
# The cryptography backend's own text names its PEM framing internals and links its FAQ,
# neither of which helps someone in the setup form — keep it on the chained cause only.
raise AppleSearchAdsAuthError(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads.py b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads.py
index d9bedb6bf480..c6d8e9b4a48c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads.py
@@ -275,6 +275,18 @@ class TestAppleSearchAdsTransport:
assert "PEM file" not in message
assert raised.value.__cause__ is not None
+ @parameterized.expand(
+ [
+ ("pem", PUBLIC_KEY_PEM),
+ ("pem_with_escaped_newlines", PUBLIC_KEY_PEM.replace("\n", "\\n")),
+ ]
+ )
+ def test_client_secret_names_a_pasted_public_key(self, _name: str, public_key: str) -> None:
+ with pytest.raises(AppleSearchAdsAuthError) as raised:
+ build_client_secret(_with_key(public_key))
+
+ assert "You entered the public key" in str(raised.value)
+
@parameterized.expand([(V5, "orgId=555"), (V1, "adAccountId=123456789")])
def test_requests_carry_the_bearer_token_and_the_versions_context_id(
self, api_version: str, expected_context: str