Repository navigation
[AI-3412] Refuse a userId on a device's connections calls in Python, Go, Ruby and Rust - #2156
Conversation
kanat
left a comment
There was a problem hiding this comment.
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:namedreturnsuser_id.to_s, andactingreturnsOkof the same client. All four SDKs' new control tests pass on2d236ed, 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 ./streampasses, as do vet and gofmt. Rubyrake: 259 runs, 0 failures. Rust:cargo test,clippy -D warningsandfmt --checkpass. - 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.
#2156 changes the Go behaviour this sentence described. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…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>
|
Round 1 fixes:
|
kanat
left a comment
There was a problem hiding this comment.
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 aToken(SetUser) and naming a user is still refused. ABackend{APIKey, APISecret, UserID}without aTokendrops its ownUserIDfor that call and sends{server:true}plusX-Stream-User-Id: <named>, the wire of Pythonacting_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/streamis untouched, so Dispatch andclient.Agentare unchanged. - Mutations: dropping the
UserIDreset, both Token guard directions, the outer guard without the user check,ActingFor = backend.UserID, the outer guard withoutAuthenticate, and a leftoverTeamclaim 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.
getSessionisx-client-accessible(openapi.yaml ~18795), so the Ruby generic verbs reach a call a device may make naming its own user; anamedrefusal would break it. No gap found, no ticket. - Suites on the head:
gofmt -l,go vet ./...,go test ./client ./streampass;-race -cpu 1 -count=3on the new and device tests pass. - CI: draft, so go/sdks/unit skip (#2130); not waited.
…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>
Why
The router answers every connections call made with a user token with
403 server_side_only: these operations carry nox-client-accessible, sorefuseClientSidestops them before the handler (acceleration/internal/api/server.go:1262). A device that names auserIdon 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
Backendbuilt for one user with the API secret, naming another user on a connections call, signed a token for its own user plusX-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
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 onAuthenticatewith aUserIDand aToken, becauseSetUserkeeps the API secret and sets the token (sdks/go/client/client.go:96-98).Left as they were, each with a control test:
as_userorSetUserwith a customer id and no API key): it sends no token, works against a dev router today, and is out of scopeChanges
Connections._clientValueError(asClient._acting)Connections.apiclient:error; a backend built for a user and naming another drops its ownUserIDfor that call and sends the app's token withX-Stream-User-IdConnections#namedConfigurationErrorConnections::actingError::Configuration;actingnow returnsResult<Client>, public signatures unchangedsdks/go/stream(backend.go, Dispatch) andclient.Agentare unchanged: the Go fix is a local copy of the backend insideConnections.api.Also: one CHANGELOG entry and one note at the bottom of
.claude/skills/sdk/SKILL.md. .NET follows in #2155 (AI-3410).Validation
plugins/stream:uv run --no-sync pytest plugins/stream -m "not integration": 683 passed.sdks/go:gofmt -lempty,go vet ./...,go test ./client ./streamok,-race -cpu 1 -count=3on the new and device tests ok.rakein Docker: 259 runs, 0 failures.cargo test,clippy --workspace --all-targets -D warnings,fmt --all --check: ok.acting_for.|| APISecret == ""on the Go token guard:Resolvealready refuses a backend with neither secret nor token,backend.go:229).71e72b8ef(CodeQL, go, sdks, unit Lint and Test "not integration", Analyze).Review rounds
a7e82f8adget,post,put,patch,delete,socket) do not refuse a device naming a user989bd9235. Fixer r1 disputed R1.1:getSessionis client-accessible and a device may name itself71e72b8efgetSessionisx-client-accessible, so a refusal would break a working call)Related
🤖 Generated with Claude Code