mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(straiker): derive the session id with sha256 and drop comments that restated constants
The derived session now hashes the principal, and CodeQL flags MD5 over an identity as a weak hash on sensitive data. SHA-256 truncated to the same 32 hex characters keeps the id shape. Comments that only labelled the allowlist groups or restated a constant are removed; the two that explain a non-obvious choice stay. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
7e5c45a7b7
commit
033c868303
2 changed files with 5 additions and 25 deletions
|
|
@ -72,14 +72,10 @@ V3_DERIVED_SESSION_PREFIX: Final = "litellm-"
|
|||
V3_AGENT_HEADER: Final = "x-s6r-agent"
|
||||
V3_RESPONSE_PHASE: Final = "response-sync"
|
||||
V3_BLOCK_DECISIONS: Final = frozenset({"block", "deny"})
|
||||
# Provider body fields, for every surface the proxy fronts. An allowlist rather than a
|
||||
# denylist: the hook sees the client body merged with proxy state (`deployment` carries
|
||||
# the resolved credential, `proxy_server_request` the client's Authorization header), and
|
||||
# a field this list does not know is not relayed. Detection reads messages, system, tools,
|
||||
# input and instructions; the rest travels so Straiker records the turn as the client sent it.
|
||||
# An allowlist: the hook's request dict merges the client body with proxy state (`deployment`
|
||||
# carries the resolved credential), so only fields named here are relayed.
|
||||
_V3_PROVIDER_BODY_KEYS: Final = frozenset(
|
||||
{
|
||||
# OpenAI chat completions
|
||||
"model",
|
||||
"messages",
|
||||
"tools",
|
||||
|
|
@ -110,12 +106,10 @@ _V3_PROVIDER_BODY_KEYS: Final = frozenset(
|
|||
"store",
|
||||
"service_tier",
|
||||
"web_search_options",
|
||||
# OpenAI text completions
|
||||
"prompt",
|
||||
"suffix",
|
||||
"echo",
|
||||
"best_of",
|
||||
# Anthropic messages
|
||||
"system",
|
||||
"stop_sequences",
|
||||
"top_k",
|
||||
|
|
@ -124,7 +118,6 @@ _V3_PROVIDER_BODY_KEYS: Final = frozenset(
|
|||
"mcp_servers",
|
||||
"context_management",
|
||||
"output_format",
|
||||
# OpenAI responses
|
||||
"input",
|
||||
"instructions",
|
||||
"previous_response_id",
|
||||
|
|
@ -135,19 +128,14 @@ _V3_PROVIDER_BODY_KEYS: Final = frozenset(
|
|||
"max_output_tokens",
|
||||
"background",
|
||||
"conversation",
|
||||
# Conversation grouping a client may state itself
|
||||
"session_id",
|
||||
}
|
||||
)
|
||||
# Fields on a `tools` or `mcp_servers` entry that carry a credential for the model's own remote
|
||||
# calls (an OpenAI `mcp` tool's `headers` and `authorization`, Anthropic's `authorization_token`).
|
||||
# Detection reads tool names, descriptions and schemas, never these. They sit directly on the
|
||||
# entry, so the scrub is one level deep on purpose: a function schema that defines a `token` or
|
||||
# `headers` property lives under `function.parameters` and is relayed exactly as sent.
|
||||
# The scrub of these is one level deep on purpose: a function schema that defines a `token` or
|
||||
# `headers` property lives under `function.parameters` and must be relayed as sent.
|
||||
_V3_CREDENTIAL_FIELDS: Final = frozenset({"authorization_token", "authorization", "headers"})
|
||||
_V3_REDACTED_VALUE: Final = "[redacted]"
|
||||
_V3_REDACTED_KEYS: Final = frozenset({"tools", "mcp_servers"})
|
||||
# The identity fields Straiker's LiteLLM adapter reads from `metadata`, most specific first.
|
||||
_V3_IDENTITY_METADATA_KEYS: Final = (
|
||||
"user_api_key_end_user_id",
|
||||
"user_api_key_user_email",
|
||||
|
|
@ -672,7 +660,7 @@ def _v3_session_id(
|
|||
if conversation == "\0":
|
||||
return None
|
||||
seed: Final = f"{_v3_user(envelope) or ''}\0{conversation}"
|
||||
return V3_DERIVED_SESSION_PREFIX + hashlib.md5(seed.encode("utf-8"), usedforsecurity=False).hexdigest()
|
||||
return V3_DERIVED_SESSION_PREFIX + hashlib.sha256(seed.encode("utf-8")).hexdigest()[:32]
|
||||
|
||||
|
||||
_V3_PREAMBLE_ROLES: Final = frozenset({"system", "developer"})
|
||||
|
|
|
|||
|
|
@ -1252,19 +1252,15 @@ async def test_v3_request_phase_relays_the_provider_body_and_nothing_else():
|
|||
|
||||
assert g.async_handler.post.call_args.args[0] == "https://test.straiker.ai/api/v3/detect"
|
||||
payload = _posted_payload(g)
|
||||
# provider body, relayed
|
||||
assert payload["messages"] == data["messages"]
|
||||
assert payload["tools"] == data["tools"]
|
||||
assert payload["model"] == "claude-haiku-4-5-20251001"
|
||||
# the body is the provider body and nothing pre-digested: Straiker parses it itself
|
||||
for flat in ("prompt", "app_response", "source", "user_name", "straiker_phase"):
|
||||
assert flat not in payload, flat
|
||||
# identity and session, the way the unified Kong plugin sends them
|
||||
assert payload["original"] == {"processed": {"Meta": {"user": "alice.chen@example.com"}}}
|
||||
assert payload["metadata"] == {"user_api_key_end_user_id": "alice.chen@example.com"}
|
||||
# the client's Claude Code session header outranks LiteLLM's own session id (Kong precedence)
|
||||
assert payload["session_id"] == "cc-sess-9"
|
||||
# nothing the proxy added
|
||||
serialized = json.dumps(payload)
|
||||
for leaked in (
|
||||
"deployment",
|
||||
|
|
@ -1519,12 +1515,9 @@ def test_v3_session_follows_kong_precedence():
|
|||
)
|
||||
|
||||
data = _v3_request_data()
|
||||
# 1. the client's own Claude Code session header wins
|
||||
assert _v3_session_id(envelope_with("meta-sess"), data, _v3_request_body(data)) == "cc-sess-9"
|
||||
# 2. then LiteLLM's resolved session
|
||||
data["proxy_server_request"] = {"headers": {}}
|
||||
assert _v3_session_id(envelope_with("meta-sess"), data, _v3_request_body(data)) == "meta-sess"
|
||||
# 3. then a hash of system + first message, stable across the conversation's replays
|
||||
a = _v3_session_id(envelope_with(None), data, _v3_request_body(data))
|
||||
data2 = _v3_request_data()
|
||||
data2["proxy_server_request"] = {"headers": {}}
|
||||
|
|
@ -1534,7 +1527,6 @@ def test_v3_session_follows_kong_precedence():
|
|||
]
|
||||
b = _v3_session_id(envelope_with(None), data2, _v3_request_body(data2))
|
||||
assert a == b and a.startswith("litellm-") and len(a) == len("litellm-") + 32
|
||||
# 4. nothing to hash: no session
|
||||
assert _v3_session_id(envelope_with(None), {"proxy_server_request": {"headers": {}}}, {}) is None
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue