From 128ced5aab4365bfff2193e937518f7104fa6fd9 Mon Sep 17 00:00:00 2001 From: Alessandro Pignati Date: Tue, 3 Nov 2026 13:53:19 +0100 Subject: [PATCH] fix(guardrails): take the agent from the authenticated key, not the request Pre-call processing keeps the request's own agent_id when the key is bound to none, so the processed metadata already carried a caller-chosen agent and reserving the field did not stop it reaching a vendor as the application source. Read it from the auth object, and omit it when the key is bound to no agent. --- .../proxy/guardrails/guardrail_endpoints.py | 15 ++++++++---- .../guardrails/test_guardrail_endpoints.py | 23 +++++++++++++++---- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/litellm/proxy/guardrails/guardrail_endpoints.py b/litellm/proxy/guardrails/guardrail_endpoints.py index f41561a656f..f14e24d3383 100644 --- a/litellm/proxy/guardrails/guardrail_endpoints.py +++ b/litellm/proxy/guardrails/guardrail_endpoints.py @@ -2349,7 +2349,7 @@ def _metadata_fields(value: object) -> Mapping[str, object]: _CALLER_OWNED_KEY_FIELDS: Final[frozenset[str]] = frozenset({"user_api_key_end_user_id"}) -_WITHHELD_FIELDS: Final[frozenset[str]] = frozenset({"user_api_key_request_route"}) +_WITHHELD_FIELDS: Final[frozenset[str]] = frozenset({"user_api_key_request_route", "agent_id"}) _RESERVED_IDENTITY_FIELDS: Final[frozenset[str]] = frozenset( {"requester_ip_address", "user_agent", "agent_id", "actor_agent_id"} @@ -2371,7 +2371,7 @@ def _is_key_identity_field(key: str) -> bool: def _guardrail_request_metadata( - caller: object, proxy: object + caller: object, proxy: object, agent_id: str | None = None ) -> dict[str, object]: # mutable-ok: guardrails annotate the metadata they are handed """Caller metadata with the identity fields taken from the authenticated key. @@ -2382,6 +2382,10 @@ def _guardrail_request_metadata( itself when a route is present, instead of the tools replayed to it, so a route here turns its checks off: the caller must not be able to set one, and this endpoint's own route describes the evaluation rather than the call being evaluated. + + The agent is taken from ``agent_id``, the one the key authenticated with, and omitted when the + key is bound to none. Pre-call processing keeps the request's own agent when the key has none, + so the processed metadata is not a trustworthy source for it either. """ caller_fields: Final = tuple( (key, value) for key, value in _metadata_fields(caller).items() if not _is_key_identity_field(key) @@ -2391,7 +2395,8 @@ def _guardrail_request_metadata( for key, value in _metadata_fields(proxy).items() if _is_key_identity_field(key) and key not in _WITHHELD_FIELDS ) - return dict((*caller_fields, *key_identity)) + bound_agent: Final = (("agent_id", agent_id),) if agent_id is not None else () + return dict((*caller_fields, *key_identity, *bound_agent)) def _execution_timeout_response(timeout: float) -> TestCustomCodeGuardrailResponse: @@ -2562,7 +2567,9 @@ async def apply_guardrail( request_data: Final = dict(response_handler.prepare_request_data(request)) caller_metadata: Final = request_data.get("metadata") metadata: Final = _guardrail_request_metadata( - caller_metadata, _GUARDRAIL_METADATA_ADAPTER.validate_python(data).get("metadata") + caller_metadata, + _GUARDRAIL_METADATA_ADAPTER.validate_python(data).get("metadata"), + agent_id=user_api_key_dict.invoked_agent_id or user_api_key_dict.agent_id, ) if caller_metadata is not None or metadata: request_data["metadata"] = metadata diff --git a/tests/unit/proxy/guardrails/test_guardrail_endpoints.py b/tests/unit/proxy/guardrails/test_guardrail_endpoints.py index 8c6c7f183f3..4c09f9afb4e 100644 --- a/tests/unit/proxy/guardrails/test_guardrail_endpoints.py +++ b/tests/unit/proxy/guardrails/test_guardrail_endpoints.py @@ -2517,6 +2517,22 @@ def test_no_request_route_reaches_the_guardrail() -> None: assert metadata == {"topic": "tax"} +def test_an_unbound_agent_is_not_taken_from_the_request() -> None: + """Pre-call processing keeps the request's own agent_id when the key has none, so the proxy's + metadata already carries a caller-chosen value and reserving the field is not enough.""" + metadata: Final = _guardrail_request_metadata( + {"agent_id": "someone-elses-agent"}, {"agent_id": "someone-elses-agent"} + ) + assert metadata == {} + + +def test_a_bound_agent_comes_from_the_authenticated_key() -> None: + metadata: Final = _guardrail_request_metadata( + {"agent_id": "someone-elses-agent"}, {"agent_id": "someone-elses-agent"}, agent_id="billing-agent" + ) + assert metadata == {"agent_id": "billing-agent"} + + def test_identity_fields_outside_the_key_namespace_are_reserved_too() -> None: metadata: Final = _guardrail_request_metadata( { @@ -2525,11 +2541,8 @@ def test_identity_fields_outside_the_key_namespace_are_reserved_too() -> None: "agent_id": "someone-elses-agent", "topic": "tax", }, - { - "requester_ip_address": "198.51.100.4", - "user_agent": "litellm-cli/2.3", - "agent_id": "billing-agent", - }, + {"requester_ip_address": "198.51.100.4", "user_agent": "litellm-cli/2.3"}, + agent_id="billing-agent", ) assert metadata == { "topic": "tax",