@@ -1012,6 +1012,7 @@ class TestRouteSelectionSpendLogs:
10121012 """
10131013
10141014 async def test_outbound_header_carries_route_selection (self ):
1015+ """The outbound header records the full selection for the chosen endpoint."""
10151016 config = LatencyServiceBackendConfig (
10161017 latency_service_url = LATENCY_SERVICE_URL ,
10171018 endpoints = [
@@ -1040,6 +1041,7 @@ async def test_outbound_header_carries_route_selection(self):
10401041 uuid .UUID (str (payload ["router_correlation_id" ]))
10411042
10421043 async def test_ctx_records_selection_matching_outbound_header (self ):
1044+ """ctx records exactly the payload the wire header carried."""
10431045 backend = _make_backend (_config ("model-A" ))
10441046 backend ._clients ["model-A" ].acompletion = AsyncMock (
10451047 return_value = _make_completion ()
@@ -1055,6 +1057,7 @@ async def test_ctx_records_selection_matching_outbound_header(self):
10551057 assert payload ["router_selected_provider" ] == "model-A"
10561058
10571059 async def test_failover_restamps_selection_and_keeps_correlation_id (self ):
1060+ """Each attempt is stamped with its own endpoint under one correlation id."""
10581061 backend = _make_backend (_config ("model-A" , "model-B" ))
10591062 _set_health (
10601063 backend ,
@@ -1082,6 +1085,7 @@ async def test_failover_restamps_selection_and_keeps_correlation_id(self):
10821085 assert ctx .metadata [CTX_ROUTE_SELECTION ] == second
10831086
10841087 async def test_correlation_id_is_fresh_per_request (self ):
1088+ """Two client requests never share a correlation id."""
10851089 backend = _make_backend (_config ("model-A" ))
10861090 mock = AsyncMock (return_value = _make_completion ())
10871091 backend ._clients ["model-A" ].acompletion = mock
@@ -1094,6 +1098,7 @@ async def test_correlation_id_is_fresh_per_request(self):
10941098 assert first ["router_correlation_id" ] != second ["router_correlation_id" ]
10951099
10961100 async def test_router_model_falls_back_to_configured_route_model (self ):
1101+ """A model-less client body attributes to the configured route id."""
10971102 backend = _make_backend (
10981103 _config ("model-A" , route_model = "nvidia/switchyard/gpt-5.5" )
10991104 )
@@ -1108,6 +1113,7 @@ async def test_router_model_falls_back_to_configured_route_model(self):
11081113 assert payload ["router_model" ] == "nvidia/switchyard/gpt-5.5"
11091114
11101115 async def test_no_selection_recorded_when_all_attempts_fail (self ):
1116+ """No billed success → no selection recorded on ctx."""
11111117 backend = _make_backend (_config ("model-A" ))
11121118 backend ._clients ["model-A" ].acompletion = AsyncMock (
11131119 side_effect = _api_status_error (401 ),
@@ -1120,6 +1126,7 @@ async def test_no_selection_recorded_when_all_attempts_fail(self):
11201126 assert CTX_ROUTE_SELECTION not in ctx .metadata
11211127
11221128 async def test_responses_surface_is_stamped_too (self ):
1129+ """The Responses-API surface is stamped like the Chat surface."""
11231130 backend = _make_backend (_config ("model-A" , request_type = "openai_responses" ))
11241131 backend ._clients ["model-A" ].aresponses = AsyncMock (
11251132 return_value = {"id" : "resp-test" , "object" : "response" , "output" : []}
@@ -1134,6 +1141,34 @@ async def test_responses_surface_is_stamped_too(self):
11341141 assert payload ["router_selected_endpoint" ] == "model-A"
11351142 assert payload ["router_strategy" ] == "latency"
11361143
1144+ async def test_cased_spoof_of_spend_logs_header_is_stripped (self ):
1145+ """A differently-cased spoof key cannot ride client extra_headers to the wire.
1146+
1147+ Header names are case-insensitive on the wire while dict merges are
1148+ not: exactly one instance of the protected header — ours — may reach
1149+ the SDK, while benign client headers still pass through.
1150+ """
1151+ backend = _make_backend (_config ("model-A" ))
1152+ mock = AsyncMock (return_value = _make_completion ())
1153+ backend ._clients ["model-A" ].acompletion = mock
1154+
1155+ await backend .call (
1156+ ProxyContext (),
1157+ _openai_request (
1158+ extra_headers = {
1159+ "X-LiteLLM-Spend-Logs-Metadata" : "spoofed" ,
1160+ "x-client-tag" : "42" ,
1161+ },
1162+ ),
1163+ )
1164+
1165+ headers = mock .call_args .kwargs ["extra_headers" ]
1166+ spoof_keys = [k for k in headers if k .lower () == SPEND_LOGS_METADATA_HEADER ]
1167+ assert spoof_keys == [SPEND_LOGS_METADATA_HEADER ]
1168+ payload = _spend_logs_payload (mock )
1169+ assert payload ["router_selected_endpoint" ] == "model-A"
1170+ assert headers ["x-client-tag" ] == "42"
1171+
11371172
11381173# ---------------------------------------------------------------------------
11391174# Credential policy
0 commit comments