diff --git a/litellm/proxy/guardrails/guardrail_endpoints.py b/litellm/proxy/guardrails/guardrail_endpoints.py index c66fe78dbdb..085c4aff5c2 100644 --- a/litellm/proxy/guardrails/guardrail_endpoints.py +++ b/litellm/proxy/guardrails/guardrail_endpoints.py @@ -2328,14 +2328,18 @@ def _metadata_fields(value: object) -> Mapping[str, object]: return {} +def _is_key_identity_field(key: str) -> bool: + return key == "user_api_key" or key.startswith("user_api_key_") + + def _guardrail_request_metadata(caller: object, proxy: object) -> Mapping[str, object]: caller_fields: Final = tuple( - (key, value) for key, value in _metadata_fields(caller).items() if not key.startswith("user_api_key_") + (key, value) for key, value in _metadata_fields(caller).items() if not _is_key_identity_field(key) ) key_identity: Final = tuple( - (key, value) for key, value in _metadata_fields(proxy).items() if key.startswith("user_api_key_") + (key, value) for key, value in _metadata_fields(proxy).items() if _is_key_identity_field(key) ) - return dict((*caller_fields, *key_identity)) + return MappingProxyType(dict((*caller_fields, *key_identity))) def _execution_timeout_response(timeout: float) -> TestCustomCodeGuardrailResponse: diff --git a/tests/unit/proxy/guardrails/test_guardrail_endpoints.py b/tests/unit/proxy/guardrails/test_guardrail_endpoints.py index 3d5087d6d1e..17487b846f8 100644 --- a/tests/unit/proxy/guardrails/test_guardrail_endpoints.py +++ b/tests/unit/proxy/guardrails/test_guardrail_endpoints.py @@ -2251,6 +2251,34 @@ async def test_apply_guardrail_replaces_caller_identity_with_the_authenticated_k } +@pytest.mark.asyncio +async def test_apply_guardrail_ignores_a_caller_supplied_user_api_key(mocker): + """The bare key field carries attribution as much as the prefixed ones, so a caller + cannot set it either.""" + mock_guardrail = _patch_apply_guardrail_env( + mocker, + {"texts": ["ok"]}, + processed_data={ + "guardrail_name": "test-guardrail", + "metadata": {"route": "/apply_guardrail", "user_api_key": "hash-of-the-real-key"}, + }, + ) + + request = ApplyGuardrailRequest( + guardrail_name="test-guardrail", + text="hello", + metadata={"forbidden_topics": ["tax"], "user_api_key": "hash-of-someone-else"}, + ) + await apply_guardrail( + fastapi_request=mocker.Mock(), + request=request, + user_api_key_dict=UserAPIKeyAuth(key_alias="billing-app", user_id="u-1"), + ) + + forwarded = mock_guardrail.apply_guardrail.await_args.kwargs["request_data"]["metadata"] + assert forwarded == {"forbidden_topics": ["tax"], "user_api_key": "hash-of-the-real-key"} + + @pytest.mark.asyncio async def test_apply_guardrail_attaches_key_identity_when_caller_sends_no_metadata(mocker): """A caller that sends no metadata still gets the authenticated identity