litellm/tests/unit/passthrough/test_utils.py
mateo-berri e47fcca489 fix(passthrough): add the helper that selects forwarded x-pass- headers
Prerequisite for the backport of #51237 to lts/1.108.x. Hand-ported _is_forwarded_pass_through_header, BasePassthroughUtils.pass_through_prefixed_headers, and their two tests only from 845f109524 (#50728, by norkelbyte). The Bedrock route forwarding that commit adds stays off the line. The OpenAI Agents turn billing reads the caller's forwarded OpenAI project headers through this helper.
2026-11-17 16:27:08 -08:00

54 lines
2 KiB
Python

from typing import Final
import pytest
from litellm.passthrough.utils import _PASS_THROUGH_PROTECTED_HEADERS, BasePassthroughUtils
def test_pass_through_prefixed_headers_keeps_only_the_x_pass_headers_forwarded_upstream() -> None:
inbound: Final = {
"authorization": "Bearer sk-litellm-key",
"x-litellm-api-key": "sk-litellm-key",
"content-type": "application/json",
"x-amzn-bedrock-guardrailidentifier": "native-guardrail",
"x-passthrough-id": "not-a-pass-header",
"x-pass-authorization": "Bearer upstream-secret",
"X-Pass-X-Api-Key": "upstream-api-key",
"x-pass-X-Amz-Security-Token": "forged-session-token",
"x-pass-x-amzn-bedrock-trace": "DISABLED",
"X-Pass-X-Amzn-Bedrock-GuardrailIdentifier": "caller-guardrail",
"X-PASS-ANTHROPIC-BETA": "beta-1",
}
selected: Final = BasePassthroughUtils.pass_through_prefixed_headers(inbound)
assert dict(selected) == {
"x-pass-x-amzn-bedrock-trace": "DISABLED",
"X-Pass-X-Amzn-Bedrock-GuardrailIdentifier": "caller-guardrail",
"X-PASS-ANTHROPIC-BETA": "beta-1",
}
@pytest.mark.parametrize(
"header_name",
[
*(f"x-pass-{name}" for name in sorted(_PASS_THROUGH_PROTECTED_HEADERS)),
"X-Pass-Authorization",
"x-pass-X-Amz-Date",
"x-pass-x-amz-security-token",
"x-pass-X-Amzn-Bedrock-Trace",
"x-pass-anthropic-beta",
"x-pass-cookie",
"x-passthrough-id",
"authorization",
],
)
def test_pass_through_prefixed_headers_select_exactly_the_headers_forwarded_upstream(header_name: str) -> None:
inbound: Final = {header_name: "caller-value"}
selected: Final = BasePassthroughUtils.pass_through_prefixed_headers(inbound)
forwarded: Final = BasePassthroughUtils.forward_headers_from_request(
request_headers=dict(inbound), headers={}, forward_headers=False
)
assert {name[len("x-pass-") :].lower(): value for name, value in selected.items()} == forwarded