AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend): trim Ayrshare profile keys in request headers (#13356)
Lucas Madbd1841f8da7
diff --git a/autogpt_platform/backend/backend/integrations/ayrshare.py b/autogpt_platform/backend/backend/integrations/ayrshare.pyindex bd4be5a8f1..6ae2e99c45 100644--- a/autogpt_platform/backend/backend/integrations/ayrshare.py+++ b/autogpt_platform/backend/backend/integrations/ayrshare.py@@ -74,6 +74,10 @@ def _extract_error_message(response: Any) -> str:return "Unknown error"+def _normalize_profile_key(profile_key: str) -> str:+ return profile_key.strip()++class SocialPlatform(str, Enum):BLUESKY = "bluesky"FACEBOOK = "facebook"@@ -226,13 +230,14 @@ class AyrshareClient:Raises:AyrshareAPIException: If the API request fails or private key is invalid."""+ profile_key = _normalize_profile_key(profile_key)payload: dict[str, Any] = {"domain": "id-pojeg","privateKey": private_key,"profileKey": profile_key,}- headers = self.headers+ headers = dict(self.headers)headers["Profile-Key"] = profile_keyif logout is not None:payload["logout"] = logout@@ -512,8 +517,8 @@ class AyrshareClient:if notes:payload["notes"] = notes- headers = self.headers- if profile_key:+ headers = dict(self.headers)+ if profile_key := _normalize_profile_key(profile_key or ""):headers["Profile-Key"] = profile_keyresponse = await self._requests.post(diff --git a/autogpt_platform/backend/backend/integrations/ayrshare_test.py b/autogpt_platform/backend/backend/integrations/ayrshare_test.pynew file mode 100644index 0000000000..dcaa183c22--- /dev/null+++ b/autogpt_platform/backend/backend/integrations/ayrshare_test.py@@ -0,0 +1,91 @@+from __future__ import annotations++from unittest.mock import AsyncMock++import pytest++from backend.integrations.ayrshare import AyrshareClient, SocialPlatform+++class _Response:+ ok = True+ status = 200++ def __init__(self, data: dict):+ self._data = data++ def json(self) -> dict:+ return self._data+++class _FakeRequests:+ def __init__(self, response: _Response):+ self.post = AsyncMock(return_value=response)+++def _set_ayrshare_api_key(monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setattr(+ "backend.integrations.ayrshare.settings.secrets.ayrshare_api_key",+ "org-api-key",+ )+++async def test_generate_jwt_strips_profile_key_without_mutating_base_headers(+ monkeypatch,+):+ _set_ayrshare_api_key(monkeypatch)+ requests = _FakeRequests(+ _Response(+ {+ "status": "success",+ "title": "Profile",+ "token": "jwt",+ "url": "https://profile.test/sso",+ }+ )+ )+ client = AyrshareClient(custom_requests=requests)++ await client.generate_jwt(private_key="private-key", profile_key=" profile-key ")++ requests.post.assert_awaited_once()+ call = requests.post.await_args+ assert call.kwargs["json"]["profileKey"] == "profile-key"+ assert call.kwargs["headers"]["Profile-Key"] == "profile-key"+ assert "Profile-Key" not in client.headers+++async def test_create_post_strips_profile_key_without_mutating_base_headers(+ monkeypatch,+):+ _set_ayrshare_api_key(monkeypatch)+ requests = _FakeRequests(+ _Response(+ {+ "status": "success",+ "posts": [+ {+ "status": "success",+ "id": "post-1",+ "refId": "ref-1",+ "profileTitle": "Profile",+ "post": "hello",+ }+ ],+ }+ )+ )+ client = AyrshareClient(custom_requests=requests)++ await client.create_post(+ post="hello",+ platforms=[SocialPlatform.TWITTER],+ profile_key=" profile-key ",+ )++ requests.post.assert_awaited_once()+ call = requests.post.await_args+ assert call.kwargs["headers"]["Profile-Key"] == "profile-key"+ assert "Profile-Key" not in client.headers