Repository navigation
[CHA-6986] fix(api): refuse user ids Stream reserves (!) where a request names one - #2395
Merged
Merged
Conversation
The router acts as the user ids it is handed with the app's own credentials: it signs call
and chat tokens for them, adds them to channels, links numbers and guests to them, and
joins calls and writes Chat as a session's user_id and agent_id. checkUserID already
refused "!" for policies and limits, but these places took any id. Chat refuses storing a
reserved id on its own writes, not the token the router signs locally or the call it joins.
A user id starting with "!" is now a 400 in:
- POST /v1/agents/calls/{id}/token (user_id);
- POST /v1/agents/chat-token (user_id, agent_id);
- POST /v1/agents/channels/links (user_id);
- POST /v1/agents/guests/claim (user_id);
- PUT /v1/agents/users/{user_id};
- createSession and the socket start frame (user_id, agent_id, memory.user_id).
Reads and deletes by a path user id are left open, so anything kept under such an id
before can still be read and erased.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Loschcode
added a commit
that referenced
this pull request
Nov 25, 2026
…vice session ids (behind switches) (#2460) ## Why Two follow-ups from the reviews of #2395 (CHA-6986) and #2393 (CHA-6998). **CHA-7277: reserved (`!`) user ids are still accepted in three places.** Stream reserves user ids starting with `!`. #2395 refused them in request bodies, but three places still take them: - **The acting-for id.** A backend's `X-Stream-User-Id` or `?user_id=` becomes `Caller.UserID`. The router stores it as the owner of sessions, tasks and connections, and upserts it as a Chat user when a conversation opens. The Chat proxy refuses such an id (CHA-6973), but the router has no check of its own. - **The `POST /v1/agents/guests` body `id`.** - **A voice session's `call.id`.** When the session names no `agent_id`, the call id becomes the agent id (`Spec.KeyedAgentID`), which gets past #2395's `agent_id` check. **CHA-7274: a device can probe for thread channels.** A device's session `id` of the form `thread-<uuid>` answered 409 when a thread channel had that id, and opened a session when none did. So the answer said whether the thread exists, against the invariant `thread_access_test.go` protects. ## Changes - **`transitional` package (new).** `transitional.Switch` ships a security change that a legitimate caller could feel, switched OFF. Unset, it keeps the old behaviour exactly. The env var takes `true` for every app, or a comma-separated list of app ids. Every request the change applies to is counted in `router_security_would_change_total{switch,app_id,outcome}` and logged, whether the switch is on or off. It is the router's equivalent of chat's `chat.security.*` Release flags (CHA-7311). The same file is in #2458 (CHA-7261), so whichever lands second merges cleanly. - **CHA-7277**, behind `ROUTER_SECURITY_CHA7277_RESERVED_USER_IDS` (`security.cha7277.reserved_user_ids`): - `refuseReservedActingFor` answers 400 to a backend acting for a reserved id on these writes: session create, the socket start frame (both via `refuseSessionRequest`), fork, resume, run, scheduled-task create and connection create. Reads and deletes stay open, so what is already kept under such an id stays readable and erasable. - A `call.id` that would become the agent id is refused the same way. - **CHA-7277, guest `id`, no switch.** A reserved `id` on `POST /v1/agents/guests` is taken as naming no guest, and a new guest is minted. No guest ever has such an id, so nobody is refused anything new. - **CHA-7274**, behind `ROUTER_SECURITY_CHA7274_DEVICE_THREAD_SESSION_IDS` (`session.ThreadSessionIDsSwitch`). In `Manager.Create`, when an end user's device names a session id shaped like a thread channel's (`thread-` plus a UUID, case-folded), the id is answered as taken. The answer is the same for an existing thread and an unknown one, on REST and on the socket. Other `thread-…` ids still open. The backend is unchanged. ## Validation Every new test with the switch on fails on main and passes here: - **Unit:** `TestABackendActingForAReservedUserIdOpensNoSession` and `TestABackendsCallIdThatWouldBecomeAReservedAgentIdIsRefused`. `TestWithTheSwitchOff…IsLetThrough` checks the old answer is kept. - **`ReservedActingForSuite`:** create, fork, resume, scheduled task and connection are refused. Listing still works. An app the switch isn't on for is let through. - **`GuestsSuite`:** `TestAReservedIDIsTakenAsNamingNoGuest`. - **`SlackChannelSuite`:** a device's session id on an existing thread versus an unknown `thread-<uuid>` gets the same reply, on create (including upper case) and on the socket. A `thread-` id that isn't a UUID still opens. With the switch off, an unknown thread id opens as before. A backend's unknown thread id opens. - **`transitional`:** off by default, on via env, on for listed apps, and forced on for a test's app. - Also run locally: `ConnectionsSuite` and `ScheduledTasksSuite`. ## Ticket [CHA-7277](https://linear.app/stream/issue/CHA-7277), [CHA-7274](https://linear.app/stream/issue/CHA-7274) (Security · Chat API authorization & visibility audit) ## Notes for review - **Prod traffic** in router logs (GCP, every acceleration cluster, last 30 days): zero requests with `user_id=!…`, and zero requests on a `/v1/agents/sessions/thread-…` path. Bodies aren't logged, so the guest `id`, `call.id` and the session id on create can't be counted directly. The switch's counter covers that. - **Kept out of scope:** - `recordUser` / `recordEmail` still record a users row for a reserved acting-for id on reads. Filed as CHA-7323. - `threadConversation`'s device branches, on the agent-id route, are left as defence in depth. - **Fable review fixes (second round).** - The thread switch counts a device's thread-shaped id only when no thread has it, since that is the only case where the answer changes. - `endUserKind` leaves out a backend acting for a user (`Caller.ServerSide`). - The acting-for check on tasks and connections runs after the customer is known, so the counter never gets an empty `app_id`. - A reserved guest `id` from the backend now gets a new guest (200) instead of the old 400 "not a guest of this app". No guest can have such an id. - **Overlap with #2366.** It touches `session_create.go` and `sessions.go` too, but different hunks. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Laurent Schaffner <laurentschaffner@Laurents-MacBook-Pro-Stream.local> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Stream reserves user ids that start with
!, for anonymous users among others. The router acts as the user ids it is handed, with the app's own credentials:user_id/agent_id.checkUserIDalready refused!, because its pattern doesn't admit it, but only for policies and limits. Every other place took any id. Chat refuses storing a reserved id on its own writes (GetStream/chat#21135). That guard does not cover the token the router signs locally (streamapp/mint.go,streamedge) or the call it joins.Changes
refuseReservedUserID(user_policies.go) answers 400 for an id that starts with!, after trimming spaces. It is called where a request names a user id that reaches Stream or is stored against one:POST /v1/agents/calls/{id}/tokenuser_idPOST /v1/agents/chat-tokenuser_id,agent_idPOST /v1/agents/channels/linksuser_idPOST /v1/agents/guests/claimuser_idPUT /v1/agents/users/{user_id}when linking a numberuser_idcreateSessionand the socket start frame, viarefuseSessionRequestuser_id,agent_id,memory.user_id/v1/policies/users/{user_id}and limits already usedcheckUserID.GET /v1/agents/users/{user_id}, unlinking (PUTwithphone: null), and theDELETE .../memories/.../connectionspaths are left open on purpose. They don't write anything new, and anything already kept under such an id must stay readable and erasable.Validation
reserved_user_id_unit_test.go): the prefix is refused, including after leading spaces. Ordinary ids, including a!inside an id, and empty ids pass. A backend's session naming a reserveduser_id,agent_idormemory.user_idis refused, and ordinary ones are not.CallsSuite,ChannelsSuite,GuestsSuiteandUserPhonesSuite. Each failed with the check disabled and passes with it. Those suites, plusSocketSessionSuite,SessionToolsSuiteand the session-owner tests, pass locally.go vetplus theinternal/apiunit tests pass on the result.Ticket
CHA-6986 (Security · Chat API authorization & visibility audit)
Notes for review
session_create.go, and either can land first.!-prefixed values in router and gateway logs in us-east4, europe-west4 and us-west1 since 13-11. So nothing real should start getting 400s.X-Stream-User-Id/?user_id=), which CHA-6973 already refuses at the Chat proxy;POST /v1/agents/guestsbodyid;call.idthat becomes the agent id by default.🤖 Generated with Claude Code