mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(openai_like): ignore hop-by-hop extra_headers and trim line breaks around values
TE, Upgrade, Connection, Keep-Alive, Proxy-Connection and Trailer in extra_headers are now ignored like Host, Content-Length and Transfer-Encoding, since litellm builds the request and TE: gzip fails HTTP/2 while Upgrade makes some upstreams hang. Line breaks at the ends of a value are trimmed along with spaces and tabs, so a secret read from a file with a trailing newline is sent instead of dropped. The drop warning now names the model.
This commit is contained in:
parent
c66275528d
commit
2cd57c6442
2 changed files with 34 additions and 6 deletions
|
|
@ -24,7 +24,19 @@ from ..common_utils import OpenAILikeBase, OpenAILikeError
|
|||
|
||||
_KEY_HEADERS_ADAPTER: Final = TypeAdapter(Mapping[str, str])
|
||||
_EXTRA_HEADERS_ADAPTER: Final = TypeAdapter(Mapping[str, object])
|
||||
_TRANSPORT_HEADER_NAMES: Final = frozenset({"host", "content-length", "transfer-encoding"})
|
||||
_TRANSPORT_HEADER_NAMES: Final = frozenset(
|
||||
{
|
||||
"host",
|
||||
"content-length",
|
||||
"transfer-encoding",
|
||||
"connection",
|
||||
"keep-alive",
|
||||
"proxy-connection",
|
||||
"te",
|
||||
"trailer",
|
||||
"upgrade",
|
||||
}
|
||||
)
|
||||
_HEADER_NAME: Final = re.compile(r"[!#$%&'*+\-.^_`|~0-9A-Za-z]+")
|
||||
_HEADER_VALUE: Final = re.compile(r"[\t\x20-\x7e]*")
|
||||
|
||||
|
|
@ -34,7 +46,7 @@ def _header_text(name: str, value: object) -> str | None:
|
|||
return None
|
||||
if _HEADER_NAME.fullmatch(name) is None:
|
||||
return None
|
||||
return str(value).strip(" \t")
|
||||
return str(value).strip(" \t\r\n")
|
||||
|
||||
|
||||
def _extra_headers_as_text(extra_headers: Mapping[str, object], model: str, drop_params: bool) -> Mapping[str, str]:
|
||||
|
|
@ -54,7 +66,9 @@ def _extra_headers_as_text(extra_headers: Mapping[str, object], model: str, drop
|
|||
unsendable: Final = sorted(name for name, text in named.items() if _HEADER_VALUE.fullmatch(text) is None)
|
||||
if unsendable:
|
||||
verbose_logger.warning(
|
||||
"extra_headers %s dropped, since a header value must be printable ASCII on one line", unsendable
|
||||
"extra_headers %s dropped for model %s, since a header value must be printable ASCII on one line",
|
||||
unsendable,
|
||||
model,
|
||||
)
|
||||
return MappingProxyType({name: text for name, text in named.items() if name not in unsendable})
|
||||
|
||||
|
|
|
|||
|
|
@ -300,7 +300,17 @@ def test_hostinger_router_embeddings_drop_non_text_extra_headers_under_drop_para
|
|||
assert set(json.loads(request.content)) == {"model", "input"}
|
||||
|
||||
|
||||
TRANSPORT_EXTRA_HEADERS: Final = [("Host", "other.example"), ("content-length", "1"), ("Transfer-Encoding", "chunked")]
|
||||
TRANSPORT_EXTRA_HEADERS: Final = [
|
||||
("Host", "other.example"),
|
||||
("content-length", "1"),
|
||||
("Transfer-Encoding", "chunked"),
|
||||
("Connection", "close"),
|
||||
("Keep-Alive", "timeout=5"),
|
||||
("Proxy-Connection", "close"),
|
||||
("TE", "gzip"),
|
||||
("Trailer", "X-Checksum"),
|
||||
("Upgrade", "websocket"),
|
||||
]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("name", "configured"), TRANSPORT_EXTRA_HEADERS)
|
||||
|
|
@ -318,6 +328,7 @@ def test_hostinger_router_embeddings_ignore_transport_extra_headers(name: str, c
|
|||
assert request.headers.get_list("host") == ["router.hostinger.com"]
|
||||
assert request.headers.get_list("content-length") == [str(len(request.content))]
|
||||
assert "transfer-encoding" not in request.headers
|
||||
assert configured not in request.headers.get_list(name)
|
||||
assert request.headers.get_list("x-trace-id") == ["trace-1"]
|
||||
|
||||
|
||||
|
|
@ -376,8 +387,11 @@ def test_hostinger_router_embeddings_drop_extra_header_values_http_cannot_carry(
|
|||
assert "x-litellm-user_api_key_alias" not in request.headers
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("configured", "sent"), [(" x ", "x"), ("\t", ""), (" a\tb ", "a\tb")])
|
||||
def test_hostinger_router_embeddings_trim_spaces_around_extra_header_values(configured: str, sent: str):
|
||||
@pytest.mark.parametrize(
|
||||
("configured", "sent"),
|
||||
[(" x ", "x"), ("\t", ""), (" a\tb ", "a\tb"), ("env-secret\n", "env-secret"), ("\r\n x \r\n", "x")],
|
||||
)
|
||||
def test_hostinger_router_embeddings_trim_whitespace_around_extra_header_values(configured: str, sent: str):
|
||||
with respx.mock() as upstream:
|
||||
route: Final = upstream.post("https://router.hostinger.com/v1/embeddings").respond(200, json=EMBEDDING_RESPONSE)
|
||||
litellm.embedding(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue