Skip to content

test(api): insert limits test requests with insertRequest - #2388

Closed
thesyncim wants to merge 1 commit into
mainfrom
fix/limits-test-insert-request
Closed

thesyncim wants to merge 1 commit into
mainfrom
fix/limits-test-insert-request

Conversation

@thesyncim

Copy link
Copy Markdown
Member

Problem

Fix

  • spent in limits_test.go uses insertRequest, as the package's other tests do.

Tests

  • go vet -tags integration ./internal/... fails on main and passes here. The suites run in CI.

@thesyncim thesyncim added the data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review label Nov 24, 2026
@thesyncim thesyncim removed the data-layer-review: ready Data layer review fix: passed automated review, merges cleanly, ready for a human review label Nov 24, 2026
Loschcode added a commit that referenced this pull request Nov 24, 2026
… writes Chat as (#2393)

## Why
`POST /v1/agents/sessions` and the `/v1/agents/socket` start frame are
client-accessible. Both copied a device's `user_id`, `user_name` and
`agent_id` into the session spec (`specOf`), and
`refuseServerSideFields` didn't check any of them. The router then acts
as that identity with its own server credentials:
- `user_id`: a Stream user token is minted for it, the agent joins the
call as it, and the voice transcript is written as it
(`cmd/router/sessions.go:38,47,75`).
- `agent_id`: on a kept conversation, the Chat user is created if
missing, it becomes the channel's `created_by` and a member, and every
agent message is sent as it
(`internal/conversation/conversation.go:768-775`, `sendWith` at
`:2354`).
- `user_name`: stored as the name of the Chat user the agent writes as.

So a user's or guest's device could make the agent write Chat messages
as any other user of the app, with content the device steers.

## Changes
- `refuseSessionRequest` calls a new `refuseDeviceAgentIdentity`.
`createSession` and the socket's start frame both go through it, so they
answer the same. For a caller that isn't server-side:
  - `agent_id`: refused (403) for any non-empty value.
- `user_id`: refused unless it is the default `vision-agent`, or the
caller's own user id on a verified (signed-in or guest) caller. An
anonymous caller's id is a claim nobody checked, so it gets only the
default, as `memory.user_id` already does.
  - `user_name`: refused unless it is the default `Vision Agent`.
- A backend sends all three as before.
- The defaults stay allowed because generated clients fill them in. The
Swift `CreateSessionRequest` init defaults `userId: "vision-agent"` and
`userName: "Vision Agent"`.
- `session.DefaultUserID` / `DefaultUserName` export the existing
defaults so the check uses the same values.
- **The indirect route through `id`.** A voice session with no agent id
is keyed under its call id (`Normalize`, `KeyedAgentID`), and that call
id is the session `id`, which a device may choose. So `{"id": "bob",
"start_voice": true}` from a device produced agent id `bob`, the same
impersonation as sending `agent_id`. `keyDeviceAgent` now gives a
device's voice session, on `createSession` and on the socket, an agent
id nobody chose (`session.NewAgentID`). Text sessions already got a
fresh one. A session that carries on a conversation keeps that
conversation's agent id, because opening it refuses any other
(`conversation.go:982`).
- The thread-channel tests (`thread_access_test.go`) relied on devices
naming an `agent_id`. They now check that a device naming a thread
channel's agent id is refused exactly as one naming an unknown agent id.
The invariant they protect still holds: nothing a device is told reveals
that a thread exists. Two `MessageHooksSuite` tests drop the device's
`agent_id`, or expect the refusal.
-
`SessionToolsSuite.TestAHostedCallInASessionOpenedWithAUsersTokenIsForThem`
used a device `agent_id` to reach a worker's hosted tool. It now opens
by config, and the worker hosts under the config's name (`hostedTools`
checks `AgentID`, then `AgentName`).

## Validation
- Unit (`session_identity_unit_test.go`): another user's id (user,
guest, anonymous), an anonymous caller's own claimed id, any `agent_id`,
and a non-default `user_name` are refused. All four tests failed on
main. The defaults, a signed-in device's own id, and a backend naming
anything are let through.
- Integration
(`SocketSessionSuite.TestADeviceMayNotHandTheSocketAnotherAgentIdentity`):
user, guest and anonymous callers are refused by both `createSession`
and the socket, with the same message, code and type. All 9 cases failed
on main.
- Integration
(`SocketSessionSuite.TestADevicesVoiceSessionIsNotKeyedUnderTheIdItChose`):
a device's voice session, through the socket and through `createSession`
with `start_voice`, does not get its chosen `id` as agent id, while a
backend's does. Removing either `keyDeviceAgent` call makes it fail.
Unit tests cover the text and resumed cases.
- The full `internal/api` integration package (`-tags integration -p 1`)
ran locally on a private Postgres and Redis. Its one failure, the
hosted-call test above, is reworked here and passes. On main,
`SlackChannelSuite` and `MessageHooksSuite` pass too. Unit tests for
`internal/api` and `internal/session` pass.
- Local note: my local `-tags integration` build of `internal/api`
failed in `limits_test.go` (`Requests.Insert`). #2388 fixes that. I
applied it locally only, and it is not part of this PR. CI's
`integration (api)` job is green on this branch.

## Ticket
- [CHA-6998](https://linear.app/stream/issue/CHA-6998) (Security · Chat
API authorization & visibility audit)
- Same finding as [AI-3586](https://linear.app/stream/issue/AI-3586)
(Audit Scan 23 November)

## Notes for review
- **Merge order.** This PR conflicts textually with #2366 (in
`dispatch_channel_test.go` and `chat_channels_hosted_test.go`) and
against #2362 (in `session_create.go` and `socketws_test.go`). In each
case both sides add code at the same place. Whichever lands later needs
a rebase. After #2366, the device cases here must also drop `Llm`, which
#2366 refuses.
- **Overlap with #2366.** #2366 refuses device-sent `phone` and
`greeting`. AI-3586's vetting also asked for `greeting.text` and
`phone.voicemail.message`, and since `phone.voicemail` sits inside
`phone`, #2366 covers both. This PR adds only the identity fields, in
its own helper, so it stays clear of #2366's and #2362's hunks in
`refuseServerSideFields`. Expect a small rebase on
`thread_access_test.go` / `dispatch_channel_test.go` after #2366, which
also stops devices sending `llm` there.
- **Compatibility, not measured.** Router logs don't record these
request fields, and I have no read access to the router's shard DB
(`calls.agent_user_id`). None of the first-party device `SessionOptions`
(Kotlin, Swift, Dart) expose `agent_id`, `user_id` or `user_name`. The
Swift generated request sends the defaults, which stay allowed. The JS
SDK spreads the whole request, so a web app could send `agent_id` today
and would now get a 403 naming the field. That includes a web app that
named an `agent_id` to reach tools its worker hosts under that id. It
now has to open by `agent` or `config_id`, and the worker hosts under
the config's name.
- **Why refuse `agent_id` outright** rather than "must equal the
caller"? It names the Chat user that creates the channel and authors the
agent's messages, so the only safe device value is one nobody chose.
- **Allowing a device's own `user_id`** is deliberate: it is the least
breaking option when usage can't be measured. It is self-only, with no
cross-user effect. The agent then joins the call as the caller, which is
odd but not a privilege. Tightening it to the default only is a one-line
change if reviewers prefer that.
- **Follow-ups filed, not in this PR:**
- CHA-7261: `unsourcedTransfer` accepts a device tool's result as a
source.
- CHA-7262: document these fields as server-side and stop the Swift
client defaulting them. This waits for #2366's SDK regeneration.
- CHA-7274: a device's session `id` in the thread namespace can probe
whether a thread channel exists. This is already on main. The PR also
leaves `threadConversation`'s device branches unreachable through the
agent id.

🤖 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>
@thesyncim thesyncim closed this Nov 24, 2026
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