Skip to content

fix(responses): encrypt the response id on every streamed event - #39534

Merged
mateo-berri merged 3 commits into
litellm_internal_stagingfrom
litellm_fix_responses_queued_id_encryption
Sep 3, 2026
Merged

mateo-berri merged 3 commits into
litellm_internal_stagingfrom
litellm_fix_responses_queued_id_encryption

Conversation

@mateo-berri

@mateo-berri mateo-berri commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Background streaming advertised two different response ids
  • The response.queued one skipped the gateway's ownership check
  • Any other key could then read that response
  • Cancelling someone else's response went through too

How it solves it:

  • Rewrite the id wherever an event carries it
  • Event types with no typed model are covered
  • Regression test asserts every event, not one

User Flow

Before: a developer opening a background streaming response gets back one id that any other key on the same gateway can read.

  1. The developer's app sends POST https://litellm-domain/v1/responses with "stream": true and "background": true
  2. The response.created, response.in_progress and response.completed frames each carry the same long scrambled id
  3. The response.queued frame carries a different, much shorter id in an obviously different format
  4. GET https://litellm-domain/v1/responses/{id} with the developer's own key returns 200 for either of those two ids
  5. A second developer with their own key on the same gateway sends GET https://litellm-domain/v1/responses/{the long scrambled id} and gets 403 Forbidden
  6. The second developer sends GET https://litellm-domain/v1/responses/{the short id from response.queued} and gets 200 with the first developer's whole response, prompt and output text included
  7. POST https://litellm-domain/v1/responses/{the short id}/cancel from the second developer is not refused either; it reaches the provider and acts on the first developer's response

Anyone who sees a response.queued frame can read and cancel the response it belongs to.

After: every frame carries the same protected id, so the second developer is refused on all of them.

  1. The developer's app sends POST https://litellm-domain/v1/responses with "stream": true and "background": true
  2. The response.created, response.in_progress and response.completed frames each carry the same long scrambled id
  3. The response.queued frame now carries that same long scrambled id
  4. GET https://litellm-domain/v1/responses/{id} with the developer's own key returns 200
  5. A second developer with their own key sends GET https://litellm-domain/v1/responses/{that id} and gets 403 Forbidden
  6. There is no second id to try; every frame in the stream advertised the same one
  7. POST https://litellm-domain/v1/responses/{that id}/cancel from the second developer gets 403 Forbidden

Another user can no longer read or cancel a response they do not own.

Relevant issues

Linear ticket

Resolves LIT-6832

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • I have received a Greptile Confidence Score of at least 4/5 before requesting a maintainer review (Greptile reviews automatically once the PR is opened; only comment @greptileai to re-request a review after pushing changes)

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Both legs ran against a live proxy booted with --num_workers 2 against the real OpenAI API, same commands in the same order, only the commit differing. Response ids are redacted; their length and format are the whole point.

Shared setup, two keys belonging to two different users:

KEY_A=$(curl -s -X POST "$PROXY/key/generate" -H "Authorization: Bearer sk-1234" \
  -H 'Content-Type: application/json' -d '{"user_id":"user_a"}' | jq -r .key)
KEY_B=$(curl -s -X POST "$PROXY/key/generate" -H "Authorization: Bearer sk-1234" \
  -H 'Content-Type: application/json' -d '{"user_id":"user_b"}' | jq -r .key)

Before (e6e5be0)

  1. User A opens a background stream, and we print the id length of every frame carrying one:
curl -sN -X POST "$PROXY/v1/responses" -H "Authorization: Bearer $KEY_A" \
  -H 'Content-Type: application/json' \
  -d '{"model":"gpt-5.5","input":"just respond with the word ping","stream":true,"background":true}' \
  | sed -n 's/^data: //p' \
  | jq -r 'select(.response.id) | "\(.type)  id_len=\(.response.id|length)"'
response.created  id_len=465
response.queued  id_len=241
response.in_progress  id_len=465
response.completed  id_len=465

Four frames, two distinct ids. response.queued hands the client the gateway's own internal id format instead of the protected one every other frame carries.

  1. User A retrieves with the id from response.created:
curl -s -o /dev/null -w "HTTP %{http_code}\n" "$PROXY/v1/responses/$CREATED_ID" -H "Authorization: Bearer $KEY_A"
HTTP 200
  1. User A retrieves with the id from response.queued:
curl -s -o /dev/null -w "HTTP %{http_code}\n" "$PROXY/v1/responses/$QUEUED_ID" -H "Authorization: Bearer $KEY_A"
HTTP 200
  1. User B retrieves user A's response with the response.created id, and is correctly refused:
curl -s -w "\nHTTP %{http_code}\n" "$PROXY/v1/responses/$CREATED_ID" -H "Authorization: Bearer $KEY_B"
{"error": {"message": "Forbidden. The response id is not associated with the user, who this key belongs to. ..."}}
HTTP 403
  1. User B retrieves user A's response with the response.queued id, and is NOT refused:
curl -s -w "\nHTTP %{http_code}\n" "$PROXY/v1/responses/$QUEUED_ID" -H "Authorization: Bearer $KEY_B"
{"id": "resp_...", "object": "response", "status": "completed", "output": [ ... user A's full response, "ping" ... ]}
HTTP 200
  1. User B cancels user A's response with the response.queued id. The gateway does not refuse it, so the request goes upstream and only the provider answers:
curl -s -w "\nHTTP %{http_code}\n" -X POST "$PROXY/v1/responses/$QUEUED_ID/cancel" -H "Authorization: Bearer $KEY_B"
{"error": {"message": "litellm.BadRequestError: OpenAIException - {\"error\": {\"message\": \"Cannot cancel a completed response.\", \"type\": \"invalid_request_error\"}}"}}
HTTP 400

After (2df5f4a)

  1. Same background stream, same command:
response.created  id_len=465
response.queued  id_len=465
response.in_progress  id_len=465
response.completed  id_len=465

Four frames, one distinct id.

  1. User A retrieves with the id from response.created:
HTTP 200
  1. User A retrieves with the id from response.queued, which is now the same id:
HTTP 200
  1. User B retrieves user A's response with the response.created id:
{"error": {"message": "Forbidden. The response id is not associated with the user, who this key belongs to. ..."}}
HTTP 403
  1. User B retrieves user A's response with the response.queued id:
{"error": {"message": "Forbidden. The response id is not associated with the user, who this key belongs to. ..."}}
HTTP 403
  1. User B cancels user A's response with the response.queued id:
{"error": {"message": "Forbidden. The response id is not associated with the user, who this key belongs to. ..."}}
HTTP 403

Observations from the run:

  • Non-streaming background: true was never affected
  • /v1/chat/completions and /v1/messages carry no response ids
  • Retrieve and cancel still resolve the internal id format
  • Nothing hands that format to a client now
  • OpenAI stalled one background stream after response.queued, unrelated

Live PR risk: base vs head vs merge-ref

Same scenarios on three proxies, each booted with --num_workers 2 on its own Postgres database, with keys for two users (lit6832_user_a owns the response, lit6832_user_b holds only its response.queued id): base is the merge-base e6e5be0, head is 2df5f4a, and merge-ref is 2df5f4a merged with staging ab44e8d (8a777f30, clean merge, no drift on the responses surface)

Path, user B with A's response.queued id base e6e5be0 head 2df5f4a merge-ref 8a777f30
queued id on POST /v1/responses stream+background router b64, 241 chars encrypted, 477 chars, equals created id same as head
queued id on POST /openai/v1/responses router b64 encrypted same as head
GET /v1/responses/{id} 200, A's output 403 403
GET /v1/responses/{id}/input_items 200, 1 item 403 403
POST /v1/responses with previous_response_id 200, billed to B 403 403
POST /v1/responses/{id}/cancel 400 from OpenAI 403 403
DELETE /v1/responses/{id} 200, A's response deleted 403 403

Identical on every side: A's own retrieve, input_items, chaining, and delete with the queued id (200); the non-streaming background: true create (encrypted id, B gets 403 on retrieve and cancel, LiteLLM_ManagedObjectTable.unified_object_id holds the 477-char encrypted id); claude-sonnet-5 streaming through /v1/responses (typed events, encrypted everywhere); /health/readiness 200; and the same successful call types and spend per side. One difference outside the PR: OpenAI stalled the head side's first background stream after response.queued, sending only its own keepalive frames for 240 s until the client gave up (the proxy log shows no upstream response.in_progress chunk ever arrived), so that side has 15 spend rows to the others' 16; three reruns on head streamed to completion in about 6 s, as did base and merge-ref. Not observed live: the enterprise check_responses_cost poller, which reads that stored id and is untouched by this PR

CircleCI at the tip (pipeline 89061, workflow 78646cb8, failed jobs rerun as 75d33c81): 40 of 43 jobs green, the same two red on both runs, both staging-side and outside this diff. litellm_router_unit_testing fails test_no_linear_scans_in_router on the two Router methods #39468 added (red on staging's own pipeline 89058, tracked in LIT-6911). e2e_ui_testing fails tests/e2e/ui/tests/users/searchUsers.spec.ts, which still looks for the Search by email… placeholder that #39604 renamed to Search by email or ID… (merged 21:14Z, after staging's last scheduled pipeline at 21:09Z, tracked in LIT-6919). upload-coverage did not run because of those two

Type

🐛 Bug Fix

Caveats (if any)

Low

  • Ownership checks only run on ids in the protected format
    • Unchanged here; that check behaved the same before
    • Nothing advertises an id in another format now
    • Tightening it would reject ids clients already hold
  • Nested ids get rewritten whatever their prefix
    • Matches what the typed events already did

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR
  • 2df5f4a passes /live-pr-risk
    • Live base, head, and merge-ref legs are clean; the box stays unchecked because CircleCI at that tip is red on the two staging-side jobs named above (LIT-6911, LIT-6919), neither reached by this diff

Note

High Risk
Security fix for proxy response-id isolation; incorrect rewriting could still leak provider ids or break legitimate retrieve/cancel flows.

Overview
Fixes a cross-tenant authorization bypass on background /v1/responses streams: frames like response.queued arrived as GenericEvent with a plain response dict, so their provider resp_* id was never rewritten while typed events got the encrypted id—clients could read or cancel another user’s response with that shorter id.

ResponsesIDSecurity now routes all outbound id rewriting through _rewrite_advertised_id, which updates top-level id, nested ResponsesAPIResponse, or dict payloads validated via Pydantic. _encrypt_response_id delegates to that helper and keeps the per-request cache so every frame advertises the same encrypted id.

Regression tests simulate a mixed background stream (typed + generic events) and assert a single encrypted id, no raw provider id in serialized output, and unchanged sibling fields. Lint/type-discipline budget files are nudged down slightly.

Reviewed by Cursor Bugbot for commit d3b6ce9. Bugbot is set up for automated code reviews on this repo. Configure here.

Background streaming emits event types with no typed model, which arrive
as GenericEvent holding a plain dict. Only typed events had their nested
response id rewritten, so those frames advertised the raw internal id
while their siblings advertised the encrypted one. The raw shape skips
the ownership check, so any other key could retrieve or cancel that
response.

Rewrite the advertised id wherever an event carries one, whichever shape
it arrives in, so a future event type cannot reopen this.
@greptile-apps

greptile-apps Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR applies consistent security hardening to response identifiers across typed and generic streamed events.

  • Adds a shared helper for rewriting identifiers in top-level, typed nested, and mapping-valued response payloads.
  • Preserves request-scoped identifier caching so every event in a stream advertises the same value.
  • Adds regression coverage for generic events, future event types, sibling-field preservation, and removal of raw identifiers from serialized frames.
  • Updates lint and type-discipline budgets to reflect the implementation.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
litellm/proxy/hooks/responses_id_security.py Centralizes streamed response-identifier rewriting and extends it to generic mapping payloads without an identified blocking regression.
tests/test_litellm/test_responses_id_security.py Adds focused mocked regression tests covering identifier consistency, serialization, and preservation of sibling event fields.
ruff-strict-budget.json Reduces the B010 lint budget to match the source cleanup.
type-discipline-budget.json Reduces the relevant type-discipline budgets to reflect improved typing.

Reviews (2): Last reviewed commit: "fix(responses): encrypt the response id ..." | Re-trigger Greptile

@codspeed

codspeed Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_fix_responses_queued_id_encryption (2df5f4a) with litellm_internal_staging (ab44e8d)

Open in CodSpeed

@codecov

codecov Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.89189% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/proxy/hooks/responses_id_security.py 91.89% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor Author

@greptileai

@mateo-berri

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit d3b6ce9. Configure here.

…itellm_fix_responses_queued_id_encryption

# Conflicts:
#	type-discipline-budget.json
…itellm_fix_responses_queued_id_encryption

# Conflicts:
#	type-discipline-budget.json
@mateo-berri
mateo-berri merged commit 3244a03 into litellm_internal_staging Sep 3, 2026
82 of 83 checks passed
@mateo-berri
mateo-berri deleted the litellm_fix_responses_queued_id_encryption branch September 3, 2026 21:54
@mateo-berri mateo-berri added run-ci and removed run-ci labels Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants