diff --git a/litellm/proxy/guardrails/guardrail_hooks/straiker/straiker.py b/litellm/proxy/guardrails/guardrail_hooks/straiker/straiker.py index fbef7181d62..9231525d9b8 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/straiker/straiker.py +++ b/litellm/proxy/guardrails/guardrail_hooks/straiker/straiker.py @@ -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() diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_straiker.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_straiker.py index 94485c860e7..4720de78839 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_straiker.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_straiker.py @@ -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