mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(guardrails): withhold every credential-bearing litellm param from ThirdLaw
The body denylist stripped api_key, but litellm accepts many other credentials in request data: aws_secret_access_key, aws_session_token, azure_ad_token, client_secret, vertex_credentials and more. A caller authenticating with any of those had the secret serialized into request_body and posted to the guardrail endpoint. Derive the strip set from CredentialLiteLLMParams.model_fields instead of listing keys here, so a provider credential added upstream is withheld without a matching edit in this module, plus an explicit set for the credential-bearing kwargs that model does not declare (azure_ad_token_provider, the oci_* signing fields, and extra_headers / default_headers, either of which can carry a provider Authorization header). _WRITE_BACK_DENY_KEYS builds on the same set, so a modify_request decision cannot inject a credential either. Also convert three collections in anthropic_sse to immutable forms to bring LIT002 back under its ceiling. Only the spots that are splatted or serialized as arrays are converted; the dict literals that json.dumps must see as real dicts are left alone, since default=str would silently stringify a MappingProxyType.
This commit is contained in:
parent
2e4dc1afd1
commit
0a1302d38d
3 changed files with 109 additions and 6 deletions
|
|
@ -252,7 +252,9 @@ class _ContentBlock:
|
|||
return _EMPTY_MAP
|
||||
|
||||
def rendered(self) -> dict[str, object]:
|
||||
cited: Final[Mapping[str, object]] = {"citations": list(self.citations)} if self.citations else _EMPTY_MAP
|
||||
cited: Final[Mapping[str, object]] = (
|
||||
MappingProxyType({"citations": tuple(self.citations)}) if self.citations else _EMPTY_MAP
|
||||
)
|
||||
return {**self.start, **self._accumulated_fields(), **cited}
|
||||
|
||||
|
||||
|
|
@ -328,7 +330,7 @@ def assemble_anthropic_sse_body(all_chunks: Sequence[object]) -> Mapping[str, ob
|
|||
started_usage: Final = envelope.get("usage")
|
||||
return {
|
||||
**envelope,
|
||||
"content": [block.rendered() for _, block in sorted(state.blocks.items())],
|
||||
"content": tuple(block.rendered() for _, block in sorted(state.blocks.items())),
|
||||
"stop_reason": state.stop_reason if state.stop_reason is not None else envelope.get("stop_reason"),
|
||||
"stop_sequence": state.stop_sequence if state.stop_sequence is not None else envelope.get("stop_sequence"),
|
||||
"usage": {
|
||||
|
|
|
|||
|
|
@ -75,11 +75,44 @@ _ENDPOINT_PATH: Final = "/guardrails/litellm/v2"
|
|||
|
||||
_UNREACHABLE_STATUS_CODES: Final = frozenset({502, 503, 504})
|
||||
|
||||
# Not part of the provider request body. ``secret_fields`` holds plaintext Authorization
|
||||
# values and ``api_key`` can carry a client-forwarded provider key, so neither may leave the proxy.
|
||||
_BODY_STRIP_KEYS: Final = frozenset(
|
||||
# Credential-bearing LiteLLM parameters that are not declared on ``CredentialLiteLLMParams``
|
||||
# because callers supply them through **kwargs. ``extra_headers`` / ``default_headers`` are
|
||||
# here because either can carry a provider Authorization header.
|
||||
_KWARGS_ONLY_CREDENTIAL_KEYS: Final = frozenset(
|
||||
{
|
||||
"azure_ad_token_provider",
|
||||
"default_headers",
|
||||
"extra_headers",
|
||||
"oci_fingerprint",
|
||||
"oci_key",
|
||||
"oci_key_file",
|
||||
"oci_signer",
|
||||
"oci_tenancy",
|
||||
"oci_user",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def _credential_keys() -> frozenset[str]:
|
||||
"""Every LiteLLM parameter that can carry a provider credential.
|
||||
|
||||
Derived from ``CredentialLiteLLMParams`` rather than enumerated here, so a provider
|
||||
credential field added upstream is withheld from ThirdLaw without a matching edit in
|
||||
this module. The model also carries non-secret routing fields (``api_base``,
|
||||
``vertex_location``, ``aws_region_name``); those are not provider body content either,
|
||||
so stripping the whole model costs nothing.
|
||||
"""
|
||||
from litellm.types.router import CredentialLiteLLMParams
|
||||
|
||||
return frozenset(CredentialLiteLLMParams.model_fields) | _KWARGS_ONLY_CREDENTIAL_KEYS
|
||||
|
||||
|
||||
# Not part of the provider request body. ``secret_fields`` holds plaintext Authorization
|
||||
# values, and every credential-bearing litellm parameter is withheld alongside it: a caller
|
||||
# authenticating with aws_secret_access_key, azure_ad_token, client_secret or
|
||||
# vertex_credentials must not have that value serialized out to the guardrail service.
|
||||
_BODY_STRIP_KEYS: Final = _credential_keys() | frozenset(
|
||||
{
|
||||
"api_key",
|
||||
"guardrail_config",
|
||||
"guardrails",
|
||||
"headers",
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ from litellm.proxy.guardrails.guardrail_hooks.thirdlaw import (
|
|||
initialize_guardrail,
|
||||
)
|
||||
from litellm.proxy.guardrails.guardrail_hooks.thirdlaw.thirdlaw import (
|
||||
_BODY_STRIP_KEYS,
|
||||
ThirdlawGuardrailMissingConfig,
|
||||
)
|
||||
from litellm.types.guardrails import (
|
||||
|
|
@ -313,6 +314,73 @@ async def test_pre_call_payload_shape():
|
|||
assert "sk-forwarded-provider-key" not in json.dumps(payload)
|
||||
|
||||
|
||||
_PROVIDER_CREDENTIALS: JsonDict = {
|
||||
"api_key": "sk-forwarded-provider-key",
|
||||
"aws_access_key_id": "AKIAIOSFODNN7EXAMPLE",
|
||||
"aws_secret_access_key": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
|
||||
"aws_session_token": "FwoGZXIvYXdzEJr//////////wEaDEXAMPLE",
|
||||
"azure_ad_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.azure-ad",
|
||||
"azure_password": "hunter2-azure",
|
||||
"client_secret": "oauth-client-secret-value",
|
||||
"vertex_credentials": {"type": "service_account", "private_key": "-----BEGIN PRIVATE KEY-----"},
|
||||
"watsonx_region_name": "eu-de",
|
||||
"extra_headers": {"authorization": "Bearer sk-provider-side"},
|
||||
}
|
||||
|
||||
|
||||
async def test_no_provider_credential_reaches_thirdlaw():
|
||||
g = _make_guardrail(decisions=[_decision_response({"action": "allow"})])
|
||||
data = {**_request_data(), **_PROVIDER_CREDENTIALS}
|
||||
await _run_pre_call(g, data)
|
||||
payload = _sent_payload(g)
|
||||
for key in _PROVIDER_CREDENTIALS:
|
||||
assert key not in payload["request_body"], key
|
||||
serialized = json.dumps(payload)
|
||||
for secret in (
|
||||
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
|
||||
"FwoGZXIvYXdzEJr//////////wEaDEXAMPLE",
|
||||
"azure-ad",
|
||||
"hunter2-azure",
|
||||
"oauth-client-secret-value",
|
||||
"BEGIN PRIVATE KEY",
|
||||
"sk-provider-side",
|
||||
"sk-forwarded-provider-key",
|
||||
):
|
||||
assert secret not in serialized, secret
|
||||
assert payload["request_body"]["messages"] == data["messages"]
|
||||
|
||||
|
||||
def test_every_credential_param_litellm_declares_is_withheld():
|
||||
"""The strip set is derived from CredentialLiteLLMParams, so a provider credential
|
||||
added upstream is withheld without an edit here. Guards against it being re-frozen
|
||||
into a hand-maintained literal that silently falls behind."""
|
||||
from litellm.types.router import CredentialLiteLLMParams
|
||||
|
||||
assert set(CredentialLiteLLMParams.model_fields) <= set(_BODY_STRIP_KEYS)
|
||||
|
||||
|
||||
async def test_modify_request_cannot_inject_a_provider_credential():
|
||||
g = _make_guardrail(
|
||||
decisions=[
|
||||
_decision_response(
|
||||
{
|
||||
"action": "modify_request",
|
||||
"request_body": {
|
||||
"messages": [{"role": "user", "content": "scrubbed"}],
|
||||
"aws_secret_access_key": "attacker-supplied",
|
||||
"azure_ad_token": "attacker-supplied",
|
||||
"vertex_credentials": {"private_key": "attacker-supplied"},
|
||||
},
|
||||
}
|
||||
)
|
||||
]
|
||||
)
|
||||
out = await _run_pre_call(g, _request_data())
|
||||
assert out["messages"] == [{"role": "user", "content": "scrubbed"}]
|
||||
for key in ("aws_secret_access_key", "azure_ad_token", "vertex_credentials"):
|
||||
assert key not in out, key
|
||||
|
||||
|
||||
async def test_pre_call_sends_live_body_not_snapshot():
|
||||
g = _make_guardrail(decisions=[_decision_response({"action": "allow"})])
|
||||
await _run_pre_call(g, _request_data())
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue