Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(search): refuse AgentCore credentials over plaintext HTTP

mateo-berri70a4f9a73aab
diff --git a/litellm/llms/bedrock/search/transformation.py b/litellm/llms/bedrock/search/transformation.py
index b53236347de..920e566c9dd 100644
--- a/litellm/llms/bedrock/search/transformation.py
+++ b/litellm/llms/bedrock/search/transformation.py
@@ -87,6 +87,14 @@ def _gateway_host_match(api_base: str) -> re.Match[str] | None:
return _GATEWAY_HOST_PATTERN.fullmatch(httpx.URL(api_base).host)
+_LOOPBACK_HOSTS: Final = frozenset({"localhost", "127.0.0.1", "::1"})
+
+
+def _credential_safe_transport(api_base: str) -> bool:
+ url: Final = httpx.URL(api_base)
+ return url.scheme == "https" or url.host in _LOOPBACK_HOSTS
+
+
def _string_field(item: Mapping[str, object], *keys: str) -> str | None:
return next(
(value for key in keys if isinstance(value := item.get(key), str) and value),
@@ -259,6 +267,13 @@ class AgentCoreSearchConfig(BaseSearchConfig, BaseAWSLLM):
if not isinstance(request_data, dict):
raise TypeError("AgentCore search expects a single dict request body")
+ if not _credential_safe_transport(api_base):
+ raise ValueError(
+ f"Refusing to send AgentCore credentials over plaintext HTTP to '{api_base}': a bearer "
+ "token or SigV4 signature would be readable in transit. Use an https gateway URL "
+ "(plain http is allowed only for localhost)."
+ )
+
# Server-managed credentials only go to a trusted host, otherwise an
# authenticated caller could point api_base at their own server (e.g. via
# /search_tools/test_connection) and collect AGENTCORE_GATEWAY_TOKEN or a
diff --git a/tests/test_litellm/llms/bedrock/search/test_agentcore_search_transformation.py b/tests/test_litellm/llms/bedrock/search/test_agentcore_search_transformation.py
index 46f98356279..950336c7ad0 100644
--- a/tests/test_litellm/llms/bedrock/search/test_agentcore_search_transformation.py
+++ b/tests/test_litellm/llms/bedrock/search/test_agentcore_search_transformation.py
@@ -403,6 +403,66 @@ class TestAgentCoreSearch:
finally:
os.environ.pop("AGENTCORE_GATEWAY_URL", None)
+ @pytest.mark.parametrize(
+ "plaintext_api_base",
+ [
+ "http://gw.gateway.bedrock-agentcore.us-east-1.amazonaws.com/mcp",
+ "http://internal-gateway.corp/mcp",
+ ],
+ )
+ def test_sign_request_refuses_server_token_over_plaintext_http(self, plaintext_api_base):
+ """A trusted hostname over plain http would expose the bearer token to
+ network observers, so credentials only ride https (or localhost)."""
+ config = AgentCoreSearchConfig()
+ os.environ["AGENTCORE_GATEWAY_TOKEN"] = "env-jwt-token"
+ os.environ["AGENTCORE_GATEWAY_URL"] = plaintext_api_base
+ try:
+ with pytest.raises(ValueError, match="plaintext"):
+ config.sign_request(
+ headers={},
+ optional_params={},
+ request_data={"jsonrpc": "2.0"},
+ api_base=plaintext_api_base,
+ )
+ finally:
+ os.environ.pop("AGENTCORE_GATEWAY_TOKEN", None)
+ os.environ.pop("AGENTCORE_GATEWAY_URL", None)
+
+ def test_sign_request_refuses_sigv4_over_plaintext_http(self):
+ """Same for SigV4: a signature over plain http is replayable by observers."""
+ config = AgentCoreSearchConfig()
+ os.environ.pop("AGENTCORE_GATEWAY_TOKEN", None)
+ with patch.object(
+ AgentCoreSearchConfig.__mro__[2], # BaseAWSLLM
+ "_sign_request",
+ return_value=({}, b"{}"),
+ ) as mock_base_sign:
+ with pytest.raises(ValueError, match="plaintext"):
+ config.sign_request(
+ headers={},
+ optional_params={"aws_region_name": "us-east-1"},
+ request_data={"jsonrpc": "2.0"},
+ api_base="http://gw.gateway.bedrock-agentcore.us-east-1.amazonaws.com/mcp",
+ )
+ mock_base_sign.assert_not_called()
+
+ def test_sign_request_allows_plain_http_for_localhost(self):
+ """Local development against an MCP stub on 127.0.0.1 keeps working."""
+ config = AgentCoreSearchConfig()
+ os.environ["AGENTCORE_GATEWAY_TOKEN"] = "env-jwt-token"
+ os.environ["AGENTCORE_GATEWAY_URL"] = "http://127.0.0.1:8931/mcp"
+ try:
+ headers, _ = config.sign_request(
+ headers={},
+ optional_params={},
+ request_data={"jsonrpc": "2.0"},
+ api_base="http://127.0.0.1:8931/mcp",
+ )
+ assert headers["Authorization"] == "Bearer env-jwt-token"
+ finally:
+ os.environ.pop("AGENTCORE_GATEWAY_TOKEN", None)
+ os.environ.pop("AGENTCORE_GATEWAY_URL", None)
+
def test_sign_request_does_not_leak_bedrock_bearer_token(self):
"""AWS_BEARER_TOKEN_BEDROCK is a Bedrock Runtime credential — it must not
replace SigV4 on requests to an AgentCore gateway."""