mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(guardrails): reserve the bare user_api_key metadata field
This commit is contained in:
parent
d61c6c4d0d
commit
ec2969f7fe
2 changed files with 35 additions and 3 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue