Skip to content

[CHA-6986] fix(api): refuse user ids Stream reserves (!) where a request names one - #2395

Merged
Loschcode merged 2 commits into
mainfrom
fix/cha-6986-reserved-user-ids
Nov 24, 2026
Merged

Loschcode merged 2 commits into
mainfrom
fix/cha-6986-reserved-user-ids

Conversation

@Loschcode

@Loschcode Loschcode commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • it signs call and chat tokens for them;
  • it adds them to channels;
  • it links numbers and guests to them;
  • it joins calls and writes Chat as a session's user_id / agent_id.

checkUserID already 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:

Where Fields
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} when linking a number path user_id
createSession and the socket start frame, via refuseSessionRequest user_id, agent_id, memory.user_id

/v1/policies/users/{user_id} and limits already used checkUserID.

GET /v1/agents/users/{user_id}, unlinking (PUT with phone: null), and the DELETE .../memories / .../connections paths 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

  • Unit (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 reserved user_id, agent_id or memory.user_id is refused, and ordinary ones are not.
  • Integration: unlinking a reserved id's number still answers 200. Plus one test per endpoint above, in CallsSuite, ChannelsSuite, GuestsSuite and UserPhonesSuite. Each failed with the check disabled and passes with it. Those suites, plus SocketSessionSuite, SessionToolsSuite and the session-owner tests, pass locally.
  • Merged with [CHA-6998] fix(api): a device can't name the user the agent joins and writes Chat as #2393 (CHA-6998), the change merges cleanly, and go vet plus the internal/api unit tests pass on the result.

Ticket

CHA-6986 (Security · Chat API authorization & visibility audit)

Notes for review

  • Opened after [CHA-6998] fix(api): a device can't name the user the agent joins and writes Chat as #2393 (CHA-6998) as a separate PR. The two touch different hunks of session_create.go, and either can land first.
  • Prod use was checked on the ticket: zero !-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.
  • The error message names the field, the same way the other input checks do.
  • Out of scope, filed as CHA-7277:
    • the router's own check on the acting-for id (X-Stream-User-Id / ?user_id=), which CHA-6973 already refuses at the Chat proxy;
    • the POST /v1/agents/guests body id;
    • a backend call.id that becomes the agent id by default.

🤖 Generated with Claude Code

Laurent Schaffner and others added 2 commits November 24, 2026 18:21
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
Loschcode merged commit 81908cc into main Nov 24, 2026
13 checks passed
@Loschcode
Loschcode deleted the fix/cha-6986-reserved-user-ids branch November 24, 2026 18:44
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant