Skip to content

[AI-3412] Refuse a userId on a device's connections calls in Python, Go, Ruby and Rust - #2156

Merged
kanat merged 9 commits into
mainfrom
kanat/ai-3412-device-user-refusal
Nov 20, 2026
Merged

kanat merged 9 commits into
mainfrom
kanat/ai-3412-device-user-refusal

Conversation

@kanat

@kanat kanat commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Why

The router answers every connections call made with a user token with 403 server_side_only: these operations carry no x-client-accessible, so refuseClientSide stops them before the handler (acceleration/internal/api/server.go:1262). A device that names a userId on a connections call therefore always failed, after a request, with a generic router error. The JavaScript and PHP SDKs already refuse it before sending. Python, Go, Ruby and Rust did not (AI-3412).

Go had a second bug. A Backend built for one user with the API secret, naming another user on a connections call, signed a token for its own user plus X-Stream-User-Id: <named>. Stream's proxy refuses that pair with 403 (chat projects/stream-accelerate/proxy.go:425-431). The other SDKs act for the named user with the app's token.

How it works

device (user token, no secret)           backend (API key + secret)
   connections.get(id, "a")                 connections.get(id, "a")
        |                                        |
   was: request -> router 403               request as "a" (app token)
   now: SDK error, nothing sent             (unchanged in Python, Ruby, Rust)

Go backend built for "ada", naming "a":
   was: token for "ada" + X-Stream-User-Id: a  -> proxy 403
   now: server token + X-Stream-User-Id: a      (the wire of Python's acting_for)

A device is a client holding a user's token. Python, Ruby and Rust key it on the SDK's own server_side. Go keys it on Authenticate with a UserID and a Token, because SetUser keeps the API secret and sets the token (sdks/go/client/client.go:96-98).

Left as they were, each with a control test:

  • a call naming no user, or an empty one, on any client
  • a backend naming a user in Python, Ruby and Rust
  • a client on a router that trusts the customer id (as_user or SetUser with a customer id and no API key): it sends no token, works against a dev router today, and is out of scope

Changes

SDK Where Result
Python Connections._client a device naming a user raises ValueError (as Client._acting)
Go Connections.api a device naming a user returns a client: error; a backend built for a user and naming another drops its own UserID for that call and sends the app's token with X-Stream-User-Id
Ruby Connections#named a device naming a user raises ConfigurationError
Rust Connections::acting a device naming a user returns Error::Configuration; acting now returns Result<Client>, public signatures unchanged

sdks/go/stream (backend.go, Dispatch) and client.Agent are unchanged: the Go fix is a local copy of the backend inside Connections.api.

Also: one CHANGELOG entry and one note at the bottom of .claude/skills/sdk/SKILL.md. .NET follows in #2155 (AI-3410).

Validation

  • Python plugins/stream: uv run --no-sync pytest plugins/stream -m "not integration": 683 passed.
  • Go sdks/go: gofmt -l empty, go vet ./..., go test ./client ./stream ok, -race -cpu 1 -count=3 on the new and device tests ok.
  • Ruby: rake in Docker: 259 runs, 0 failures.
  • Rust: cargo test, clippy --workspace --all-targets -D warnings, fmt --all --check: ok.
  • Tests per SDK: a device naming a user is refused on every connections call, with nothing sent; a device naming no user sends the same request as before; a customer-id client still names a user. Go adds a backend built for a user that names another user and acts for the named one, with the wire checked against Python's acting_for.
  • Each new rule has a mutation check. Round 1: 13 mutants, 0 survived. Round 2: 8 mutants, 1 survivor that is equivalent (|| APISecret == "" on the Go token guard: Resolve already refuses a backend with neither secret nor token, backend.go:229).
  • CI: green on 71e72b8ef (CodeQL, go, sdks, unit Lint and Test "not integration", Analyze).

Review rounds

Round Head Verdict Findings Outcome
R1 a7e82f8ad GO, with an orchestrator Should fix X0.1 (Should fix): a Go backend built for a user, naming another user, is refused locally. Confirmed by #2155's review. R1.1 (Nit): the Ruby generic verbs (get, post, put, patch, delete, socket) do not refuse a device naming a user Fixer r1 fixed X0.1 in 989bd9235. Fixer r1 disputed R1.1: getSession is client-accessible and a device may name itself
R2 71e72b8ef GO X0.1 fixed; R1.1 withdrawn (no gap: getSession is x-client-accessible, so a refusal would break a working call) 8 mutants, 1 equivalent survivor

Related

  • AI-3412 (this PR)
  • #2155: the .NET part of the device refusal, AI-3410

🤖 Generated with Claude Code

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Reviewed #2156 at a7e82f8 merged onto main (2d236ed). CI: the draft runs no SDK jobs (#2130), so CI runs after gh pr ready.

[Nit] R1.1 sdks/ruby/lib/getstream/vision_agents/client.rb:52-82 (not in this diff): Client#get, post, put, patch, delete and socket take user_id: (#2102) and a device naming a user there is not refused. This does no harm: the router ignores X-Stream-User-Id from a verified user token (acceleration/internal/auth/auth.go:441-446) and answers a server-only operation with 403. JS has no per-call user on its generic request. Better handled as a ticket than in this PR.

Verdict: approve (no Blocker, no Should fix).

Checked and sound:

  • Hard rules, base against head. The full request was compared: headers, body and query. In Python (8 client shapes) and Go (6 shapes), each call was made with no user, an empty one, and a named one. A device naming a user now gets the configuration error on all five calls, and nothing is sent. Every other case sends exactly what the base sent: a device naming no user or an empty one, a backend naming a user, and the customer-id client with or without as_user/SetUser. In Ruby and Rust the path that is not refused sends what the base sent by construction: named returns user_id.to_s, and acting returns Ok of the same client. All four SDKs' new control tests pass on 2d236ed, and their refusal tests fail there.
  • Call sites. All five calls in each SDK go through the guard. 13 mutants were run and all were killed: guard off, guard inverted, guard refusing an empty or absent user, a predicate keyed on the secret, token or ServerSide(), and Create, List and reach each bypassing the guard (Go and Rust).
  • Suites. Python plugins/stream: 683 passed. ruff and mypy are clean. Go ./client ./stream passes, as do vet and gofmt. Ruby rake: 259 runs, 0 failures. Rust: cargo test, clippy -D warnings and fmt --check pass.
  • Go Backend{APIKey, APISecret, UserID}. This client is now refused when it names a user. On the base it sent a token whose claim is that user, which the router reads as a device, so the call always got a 403.
  • CHANGELOG and skill note. The CHANGELOG has one entry and no stray blank lines. The skill note is the last line of .claude/skills/sdk/SKILL.md, and the lines it cites are correct.

kanat pushed a commit that referenced this pull request Nov 20, 2026
#2156 changes the Go behaviour this sentence described.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kanat and others added 2 commits November 20, 2026 16:36
…call names

A Backend with the API secret and a UserID naming another user on a
connections call signed a token for its own user, which the proxy refuses
(403). Like the other SDKs, the call now drops the client's own user and
sends the app's token with X-Stream-User-Id. Only a client holding a
user's Token (SetUser) is refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kanat

kanat commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator Author

Round 1 fixes:

  • X0.1: fixed in 989bd92 (docs in 71e72b8). A Go Backend with the API secret and a UserID that names another user on a connections call now drops its own UserID for that call. It sends the app's token (server: true) with X-Stream-User-Id: <named>, as Python's acting_for does. Before, it signed a token for its own user, and the proxy refuses that token when the header names another user. A client that holds a user's Token (SetUser) is still refused. Tests:
    • TestABackendBuiltForAUserActsForTheUserACallNames fails on 2d236ed (claims user_id: ada) and on a7e82f8 (refused).
    • Control: TestABackendBuiltForAUserNamingNoUserSendsWhatItAlwaysDid passes on 2d236ed.
    • Mutations: 4 run, 4 killed.
  • R1.1: disputed, not changed. Client#get/post/put/patch/delete/socket also reach client-accessible operations, for example GET /v1/agents/sessions/{id}. A device that names its own user there gets through the proxy today: the proxy lets a user token's header repeat its own user. The router then reads the user from the token. So the named refusal would break calls that work now. Details are in the fixer log.

@kanat kanat left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Delta round 2: GO (71e72b8, contains origin/main 8e68a6c via 07f5382, no conflicts)

  • X0.1 fixed. Connections.api (connections.go:189-201): a client holding a Token (SetUser) and naming a user is still refused. A Backend{APIKey, APISecret, UserID} without a Token drops its own UserID for that call and sends {server:true} plus X-Stream-User-Id: <named>, the wire of Python acting_for (_backend.py:457-463). A call naming no user, and the customer-id path, are unchanged (control test, claims {user_id: ada}, no acting header). sdks/go/stream is untouched, so Dispatch and client.Agent are unchanged.
  • Mutations: dropping the UserID reset, both Token guard directions, the outer guard without the user check, ActingFor = backend.UserID, the outer guard without Authenticate, and a leftover Team claim each fail a test. One survivor, || APISecret == "" on the Token guard, is equivalent: Resolve already refuses a backend with no secret and no token.
  • R1.1 withdrawn. getSession is x-client-accessible (openapi.yaml ~18795), so the Ruby generic verbs reach a call a device may make naming its own user; a named refusal would break it. No gap found, no ticket.
  • Suites on the head: gofmt -l, go vet ./..., go test ./client ./stream pass; -race -cpu 1 -count=3 on the new and device tests pass.
  • CI: draft, so go/sdks/unit skip (#2130); not waited.

@kanat
kanat marked this pull request as ready for review November 20, 2026 15:41
@kanat
kanat merged commit 6069418 into main Nov 20, 2026
13 checks passed
@kanat
kanat deleted the kanat/ai-3412-device-user-refusal branch November 20, 2026 16:04
kanat added a commit that referenced this pull request Nov 23, 2026
…ser wire pinned (#2155)

## Why

AI-3410: a .NET client made for one user (secret + `UserId`, behind the
proxy) naming another user on a connections call: does it lose its user
token, and does Go do the same? AI-3412 (.NET part): a device client
naming a user on a connections call got the router's generic 403; it
should be refused before sending, as JavaScript does
(`sdks/js/src/connections.ts:137-146`).

## How it works

```
Connections call naming a user
  client holds the secret (or customer-id)  ->  ActingFor(user): server credential + X-Stream-User-Id   (unchanged)
  client holds a user's token (device)      ->  ConfigurationException, no request                       (new, was a 403 from the server)
Connections call naming no user             ->  sent as the client is                                    (unchanged)
```

- A server token with `X-Stream-User-Id` makes the router's principal a
backend acting for that user (`acceleration/internal/auth/auth.go:468`).
A token carrying a `user_id` is a user, never server side
(`auth.go:435-444`).
- Stream's proxy refuses a user's token that names another user with a
403, before the router sees it
(`chat/projects/stream-accelerate/proxy.go:430`,
`chat/lib/core/errors/errors.go:223-225`, both on chat master).

## Changes

- `Connections.SendAsync`
(`sdks/dotnet/src/GetStream.VisionAgents/Connections.cs`): a call naming
a user on a client that is not `ServerSide` throws
`ConfigurationException` before anything is sent. The customer-id path
(`ServerSide` true, header-trusting dev router) is unchanged. No change
to `Backend` or `ActingFor`.
- Doc comments in `Connections.cs` (the class remarks and `SendAsync`).
- `sdks/dotnet/README.md`: a paragraph on naming a user on a connections
call.
- `.claude/skills/sdk/SKILL.md`: one note at the bottom (Go differs; Go,
Python, Ruby and Rust to follow).
- `CHANGELOG.md`: one sentence in the .NET entry, with `(#2155)`.
- `ConnectionsTests.cs`: four new tests (see Validation).
- The branch also carries main's merge `4626f6ae4`, which brings
`8e68a6c5c` (#2150, acceleration tests only). It changes no .NET code.
- Not in this PR: Go keeping `UserID` on a call that names a user
(#2156); Python, Ruby and Rust refusing a device's user (AI-3412, other
PRs).

## Validation

- `ConnectionsTests`: 20/20 on head (reviewer-1.md:11).
- Full .NET suite, without LiveTests: 328/328 on each of three runs by
the reviewer (reviewer-1.md:8).
- New tests:
- A client with a secret and `UserId=ada` behind the proxy: a call
naming alice sends `server: true`, no `user_id` claim and
`X-Stream-User-Id: alice`; a call naming none sends `user_id: ada` and
no header.
- A device (`ApiKey` + `Token` + `UserId`, and `AsUser` of a secret
client), direct and proxied: all five calls throw
`ConfigurationException`, and the router sees 0 requests.
- A device with a null or empty user on a call: sends `Bearer <its
token>`, `Stream-Auth-Type: jwt`, no user header.
- A customer-id client made by `AsUser`, naming alice: still sends
`X-Customer-Id` and `X-Stream-User-Id`.
- Mutations: six, one script, each run against `ConnectionsTests`:

| Mutation | Tests failing |
|---|---|
| guard removed | 2 |
| guard keyed on `Token != ""` | 1 |
| guard above the no-user branch | 2 |
| `Acting` keeps `UserId` (the Go behaviour) | 2 |
| `ServerSide` ignores customer-id | 5 |
| `ServerSide` true for a token | 2 |

- `dotnet format` of `ConnectionsTests.cs`: applied in 41a0dea; CI had
caught the format difference.
- CI: green on 41a0dea (ledger `ci.txt`: SDK checks, CodeQL,
GitGuardian, labeler, Analyze). Commit 2999041 changes only a README
sentence; its CI was pending when this body was written.
- Base flake:
`DispatchTests.AWorkerReconnectingSaysWhatItIsStillHandling` timed out
once in three runs on base `2d236ed89` (`DispatchTests.cs:298`).
Ticketed separately; not in this PR.

## Review rounds

| Round | Head | Verdict | Finding or change |
|---|---|---|---|
| R1 | 124b857 | NO-GO | Five findings, below |
| R1.1 | | Should fix, fixed | The proxy, not the router, refuses a
device naming another user: a 403 from `proxy.go:425-431`, not the
router's `server_side_only`. Wording fixed in 6cf1e07; cite in
ead1ee5. |
| R1.2 | | Nit, larger, ticketed | A client given both `ApiSecret` and
`Token` counts as `ServerSide`, so it gets past the guard and sends the
user's token with `X-Stream-User-Id`. The base sends the same request.
Ticket: refuse a `Token` beside an `ApiSecret`, or drop the token when a
secret is held. |
| R1.3 | | Nit, fixed | The docs said `AsUser` throws; a customer-id
client's `AsUser` is not refused. Now "a client with an API key holding
a user's token". 7db125d. |
| R1.4 | | Nit, fixed | The CHANGELOG entry had no PR number. `(#2155)`
added in ce1d826. |
| R1.5 | | Question, routed | Go behaviour confirmed by a throwaway
test: it sends `user_id: ada` with `X-Stream-User-Id: alice`, which the
proxy refuses. Go fix in #2156. |
| R2 | ead1ee5 | GO | Orchestrator check, no reviewer rerun: the delta
was prose, one doc comment and the base merge `8e68a6c5c` (acceleration
tests only). |
| after R2 | 41a0dea | trivial | `dotnet format` of
`ConnectionsTests.cs`; CI had caught it. |
| after R2 | 2999041 | trivial | README sentence: the .NET README no
longer mentions Go. |

## Answer to AI-3410

.NET already sends what the router needs: the server token plus
`X-Stream-User-Id`, as Python, JavaScript, PHP, Ruby and Rust do. Go was
the odd one on base: it signs the client's own user (`user_id: ada`)
with the header, and Stream's proxy refuses that user's token with a 403
when it names another user. Go is fixed in #2156. The .NET README no
longer mentions Go (commit 2999041).

Wire for a client built with secret + `UserId=ada`, proxy on, one call
naming `alice` (all at `2d236ed89`; Go measured with a throwaway test on
`stream.Backend.Credentials()`):

| SDK | Token claims sent | `X-Stream-User-Id` | Router sees | Source |
|---|---|---|---|---|
| Go | `user_id: ada` | `alice` | nothing: the proxy refuses it (403) |
`sdks/go/stream/backend.go:250-253,273`; `client/connections.go:187-190`
keeps `UserID` |
| .NET | `server: true` | `alice` | backend acting for alice |
`Backend.cs:254` (`UserId = ""`), `Backend.cs:273-275` |
| Python | `server: true` | `alice` | same | `_backend.py:463` |
| JavaScript | `server: true` | `alice` | same | `backend.ts:573-581` |
| PHP | `server: true` | `alice` | same | `Backend.php:198` |
| Ruby | `server: true` | `alice` | same | `backend.rb:301` |
| Rust | `server: true` | `alice` | same | `client.rs:172-176` |

Without the secret (a device: `ApiKey` + `Token`), .NET keeps the token,
as Go does. That call is now refused before sending (see How it works).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-authored-by: Kanat <kanat.kiialbaev@getstream.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant