Meta Ads (Facebook/Instagram) MCP server for Claude, ChatGPT, Perplexity & Cursor — the Meta node of Pipeboard’s 5-platform family (+ Google, TikTok, Snap, Reddit). Hosted remote MCP, badged Meta Business Partner, free plan — no self-hosting required.
Python advertisingai-agentschatgptclaudefacebook-adsinstagram-adsmcpmeta-adsmodel-context-protocolpipeboard
fix(security): stop make_api_request mutating the caller's params dict (token leak) (#145)
George Phelipe Azevedo555ae487f4a8
diff --git a/meta_ads_mcp/core/api.py b/meta_ads_mcp/core/api.pyindex f6bbe43..5a9c00b 100644--- a/meta_ads_mcp/core/api.py+++ b/meta_ads_mcp/core/api.py@@ -198,7 +198,12 @@ async def make_api_request("User-Agent": USER_AGENT,}- request_params = params or {}+ # Shallow-copy the caller's params: this function injects credentials+ # (access_token, appsecret_proof) and JSON-stringifies dict/list values+ # below. Mutating the caller's dict leaked the access token into tool+ # responses that echo their params back (e.g. update_ad_creative's+ # attempted_updates on error 1815573).+ request_params = dict(params) if params else {}request_params["access_token"] = access_token# Add appsecret_proof when META_APP_SECRET is configured.diff --git a/tests/test_make_api_request_param_mutation.py b/tests/test_make_api_request_param_mutation.pynew file mode 100644index 0000000..a88ff18--- /dev/null+++ b/tests/test_make_api_request_param_mutation.py@@ -0,0 +1,90 @@+#!/usr/bin/env python3+"""+Regression tests: make_api_request must NOT mutate the caller's params dict.++make_api_request injects credentials (access_token, appsecret_proof) and+JSON-stringifies dict/list values into the outgoing request. Before the fix it+did this on the caller's own dict (request_params = params or {} shares the+reference for non-empty dicts), so any tool that echoes its params back to the+user AFTER the call leaked the access token — real case: update_ad_creative+returns `attempted_updates` in its error_subcode-1815573 response, which came+back carrying `access_token` plus json.dumps-stringified specs.+"""++import httpx+import pytest+from unittest.mock import AsyncMock, MagicMock, patch++from meta_ads_mcp.core.api import make_api_request+++def _mock_response():+ """A minimal successful httpx response."""+ response = MagicMock()+ response.status_code = 200+ response.headers = {}+ response.json.return_value = {"success": True}+ response.raise_for_status.return_value = None+ return response+++def mock_httpx_client():+ """Patch httpx.AsyncClient so no network I/O happens."""+ with patch("meta_ads_mcp.core.api.httpx.AsyncClient") as mock_client_cls:+ client = MagicMock()+ client.get = AsyncMock(return_value=_mock_response())+ client.post = AsyncMock(return_value=_mock_response())+ mock_client_cls.return_value.__aenter__ = AsyncMock(return_value=client)+ mock_client_cls.return_value.__aexit__ = AsyncMock(return_value=False)+ yield client+++async def test_post_does_not_mutate_caller_params(mock_httpx_client):+ """POST path: credentials and json.dumps'd values must stay out of the caller's dict."""+ caller_params = {+ "name": "My creative",+ "object_story_spec": {"link_data": {"message": "New text"}},+ "headlines": [{"text": "H1"}],+ }+ snapshot = {+ "name": "My creative",+ "object_story_spec": {"link_data": {"message": "New text"}},+ "headlines": [{"text": "H1"}],+ }++ await make_api_request("123/endpoint", "SECRET_TOKEN", caller_params, method="POST")++ assert caller_params == snapshot, (+ "make_api_request mutated the caller's params dict — tools that echo "+ "their params back (e.g. update_ad_creative attempted_updates) would "+ "leak the access token and stringified specs"+ )+ assert "access_token" not in caller_params+ assert "appsecret_proof" not in caller_params+ assert isinstance(caller_params["object_story_spec"], dict)+++async def test_post_still_sends_credentials_and_stringified_values(mock_httpx_client):+ """The outgoing request keeps the old wire format (token + JSON strings)."""+ caller_params = {"object_story_spec": {"link_data": {"message": "x"}}}++ await make_api_request("123/endpoint", "SECRET_TOKEN", caller_params, method="POST")++ sent = mock_httpx_client.post.call_args.kwargs["data"]+ assert sent["access_token"] == "SECRET_TOKEN"+ assert isinstance(sent["object_story_spec"], str)+++async def test_get_does_not_mutate_caller_params(mock_httpx_client):+ """GET path: same guarantee for query-style calls."""+ caller_params = {"fields": "id,name", "targeting_spec": {"geo_locations": {"countries": ["US"]}}}+ snapshot = {"fields": "id,name", "targeting_spec": {"geo_locations": {"countries": ["US"]}}}++ await make_api_request("123/endpoint", "SECRET_TOKEN", caller_params, method="GET")++ assert caller_params == snapshot+ assert "access_token" not in caller_params