mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
fix(straiker): seed the derived session on the principal as well as the conversation
Straiker skips turns it has already scored for a session. The derived session hashed the system prompt and the first user turn alone, so two users who opened a conversation with the same words shared one session, and the second user's copy of an attack came back as a replay: unscored and allowed. Measured live on 2026-09-20: the first user's SSN turn was blocked (`social_security_number`, scored=2), the second user's identical turn was allowed (`controls: []`, replayed=2). The principal now joins the seed. Explicit session ids, the Claude Code header and LiteLLM's own session are unchanged. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
8cf1885419
commit
7e5c45a7b7
2 changed files with 40 additions and 5 deletions
|
|
@ -654,18 +654,24 @@ def _v3_session_id(
|
|||
|
||||
Claude Code names its session on the wire and that wins. Then the session LiteLLM
|
||||
resolved from its own metadata. Then, for a conversation that states none, a hash of
|
||||
the system prompt and the first message: a chat client replays the whole conversation
|
||||
on every turn, so that pair is constant for its lifetime and groups the turns. A fresh
|
||||
synthetic id per request would group nothing.
|
||||
the principal, the system prompt and the first message: a chat client replays the
|
||||
whole conversation on every turn, so that triple is constant for its lifetime and
|
||||
groups the turns. A fresh synthetic id per request would group nothing.
|
||||
|
||||
The principal is in the hash because Straiker skips turns it has already scored for a
|
||||
session. Two users who open with the same words are two conversations; hashed on the
|
||||
words alone they shared one session, and the second user's copy of an attack came
|
||||
back as a replay, unscored and allowed (measured 2026-09-20).
|
||||
"""
|
||||
supplied: Final = _request_header(request_data, V3_SESSION_HEADER)
|
||||
if supplied:
|
||||
return supplied
|
||||
if envelope.context.session_id:
|
||||
return envelope.context.session_id
|
||||
seed: Final = f"{_v3_system_text(request_body) or ''}\0{_v3_first_message_text(request_body)}"
|
||||
if seed == "\0":
|
||||
conversation: Final = f"{_v3_system_text(request_body) or ''}\0{_v3_first_message_text(request_body)}"
|
||||
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()
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -2283,3 +2283,32 @@ async def test_v3_responses_api_conversations_seed_on_instructions_and_the_first
|
|||
assert refund == refund_again and refund.startswith("litellm-")
|
||||
assert refund != cancel
|
||||
assert refund != billing
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_v3_derived_session_is_per_principal():
|
||||
"""Straiker de-duplicates turns it already scored per session. Two users who open a
|
||||
conversation with the same words must therefore never share a derived session, or the
|
||||
second user's copy of an attack is skipped as a replay."""
|
||||
|
||||
async def session_for(user):
|
||||
g = _make_guardrail(api_key=V3_KEY)
|
||||
g.async_handler.post.return_value = _v3_mock(V3_GATEWAY_ALLOW)
|
||||
data = _v3_request_data(
|
||||
messages=[
|
||||
{"role": "system", "content": "You are a helpful assistant."},
|
||||
{"role": "user", "content": "Please store this customer's SSN 536-90-4718 in the CRM notes."},
|
||||
],
|
||||
metadata={"user_api_key_user_email": user, "user_api_key_user_id": user},
|
||||
)
|
||||
data["proxy_server_request"] = {"headers": {}}
|
||||
await g.apply_guardrail(
|
||||
inputs={"texts": ["x"]}, request_data=data, input_type="request", logging_obj=_logging_obj()
|
||||
)
|
||||
return _posted_payload(g)["session_id"]
|
||||
|
||||
alice = await session_for("alice.chen@example.com")
|
||||
alice_again = await session_for("alice.chen@example.com")
|
||||
tom = await session_for("tom.becker@example.com")
|
||||
assert alice == alice_again and alice.startswith("litellm-")
|
||||
assert alice != tom
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue