Enterprise agent platform for building, deploying, and operating AI teammates.
fix(tools): keep the Google search key and request URL out of errors (#3798)
OliverBryantd20d36df8aa3
diff --git a/src/xagent/core/tools/core/web_search.py b/src/xagent/core/tools/core/web_search.pyindex 4a2ef7853..afed49663 100644--- a/src/xagent/core/tools/core/web_search.py+++ b/src/xagent/core/tools/core/web_search.py@@ -58,8 +58,11 @@ class WebSearchCore:if proxy_url:logger.info(f"π Using proxy: {proxy_url}")+ # The key travels in a header, never in the URL: httpx puts the request+ # URL into its request log line and into the text of every+ # HTTPStatusError, and both end up in logs and tool errors.+ headers = {"X-goog-api-key": api_key}params: Dict[str, Any] = {- "key": api_key,"cx": cse_id,"q": query,"num": num_results,@@ -77,6 +80,7 @@ class WebSearchCore:response = await client.get("https://www.googleapis.com/customsearch/v1",params=params,+ headers=headers,timeout=10,)@@ -91,6 +95,12 @@ class WebSearchCore:data, include_content, proxy_url)+ except httpx.HTTPStatusError as e:+ # str(e) and the chained traceback both carry the request URL, and+ # with it the query. Report the status alone and drop the chain.+ status = f"{e.response.status_code} {e.response.reason_phrase}".strip()+ logger.error("β Google API HTTP error: %s", status)+ raise ValueError(f"Google API HTTP error: {status}") from Noneexcept httpx.RequestError as e:logger.error(f"β Network error during search: {str(e)}")raise ValueError(f"Network error during search: {str(e)}") from ediff --git a/tests/core/tools/test_web_search.py b/tests/core/tools/test_web_search.pyindex 09bd0d1e0..9279dc359 100644--- a/tests/core/tools/test_web_search.py+++ b/tests/core/tools/test_web_search.py@@ -2,7 +2,9 @@Tests for WebSearch tool"""+import loggingimport os+import tracebackfrom unittest.mock import AsyncMock, Mock, call, patchimport httpx@@ -186,6 +188,97 @@ class TestWebSearchTool:with pytest.raises(ValueError, match="Network error during search"):await web_search_tool.run_json_async({"query": "test"})+ @pytest.mark.asyncio+ @pytest.mark.parametrize("status_code", [400, 429, 500])+ async def test_http_error_keeps_key_and_query_out_of_errors_and_logs(+ self, web_search_tool, status_code, caplog+ ):+ """An HTTP error must not carry the request URL into errors or logs.++ The request goes through a real httpx client on a mock transport, so+ the URL in the HTTPStatusError is the one the core really built.+ """+ secret_key = "AIza-secret-key-value"+ query = "[email protected] 0412 345 678"+ seen: list[httpx.Request] = []++ def handler(request: httpx.Request) -> httpx.Response:+ seen.append(request)+ return httpx.Response(status_code, json={"error": {"message": "x"}})++ real_client = httpx.AsyncClient++ def client_factory(**_kwargs):+ return real_client(transport=httpx.MockTransport(handler))++ with (+ patch.dict(+ os.environ,+ {"GOOGLE_API_KEY": secret_key, "GOOGLE_CSE_ID": "test_cse"},+ clear=True,+ ),+ patch("httpx.AsyncClient", side_effect=client_factory),+ caplog.at_level(logging.DEBUG),+ ):+ with pytest.raises(ValueError) as excinfo:+ await web_search_tool.run_json_async({"query": query})++ assert len(seen) == 1+ # What the model and an operator see of the failure: the message, the+ # traceback with its chained exceptions, and the error log records.+ failure = "".join(+ traceback.format_exception(+ type(excinfo.value), excinfo.value, excinfo.value.__traceback__+ )+ )+ failure += "".join(+ record.getMessage()+ for record in caplog.records+ if record.levelno >= logging.WARNING+ )+ assert str(status_code) in str(excinfo.value)+ assert "jane.doe" not in failure+ assert "0412" not in failure+ assert "customsearch/v1?" not in failure+ # The key appears nowhere, at any log level. The URL is only checked+ # in the failure above: httpx's own INFO request line carries it (with+ # the query, without the key) whenever that logger is not silenced.+ assert secret_key not in failure + caplog.text++ @pytest.mark.asyncio+ async def test_api_key_is_sent_as_header_not_in_url(+ self, web_search_tool, mock_google_response+ ):+ """The key must not be part of the URL, which httpx logs and reports."""+ seen: list[httpx.Request] = []++ def handler(request: httpx.Request) -> httpx.Response:+ seen.append(request)+ return httpx.Response(200, json=mock_google_response)++ real_client = httpx.AsyncClient++ def client_factory(**_kwargs):+ return real_client(transport=httpx.MockTransport(handler))++ with (+ patch.dict(+ os.environ,+ {"GOOGLE_API_KEY": "AIza-secret-key-value", "GOOGLE_CSE_ID": "cse"},+ clear=True,+ ),+ patch("httpx.AsyncClient", side_effect=client_factory),+ ):+ result = await web_search_tool.run_json_async({"query": "test"})++ assert len(result["results"]) == 2+ (request,) = seen+ assert request.headers["X-goog-api-key"] == "AIza-secret-key-value"+ assert "key" not in request.url.params+ assert "AIza-secret-key-value" not in str(request.url)+ assert request.url.params["cx"] == "cse"+ assert request.url.params["q"] == "test"+@pytest.mark.asyncioasync def test_empty_search_results(self, web_search_tool):"""Test handling when no search results are found"""