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
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