diff --git a/litellm/proxy/guardrails/anthropic_sse.py b/litellm/proxy/guardrails/anthropic_sse.py index e544e6c661e..60a73c60167 100644 --- a/litellm/proxy/guardrails/anthropic_sse.py +++ b/litellm/proxy/guardrails/anthropic_sse.py @@ -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": { diff --git a/litellm/proxy/guardrails/guardrail_hooks/thirdlaw/thirdlaw.py b/litellm/proxy/guardrails/guardrail_hooks/thirdlaw/thirdlaw.py index 38f7d27e554..43df53d1103 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/thirdlaw/thirdlaw.py +++ b/litellm/proxy/guardrails/guardrail_hooks/thirdlaw/thirdlaw.py @@ -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", diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_thirdlaw.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_thirdlaw.py index 48317d89880..3b271ca180c 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_thirdlaw.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_thirdlaw.py @@ -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())